Optionally read from stdin
This commit is contained in:
parent
e514846c98
commit
087ac26c84
1 changed files with 18 additions and 8 deletions
|
@ -40,7 +40,7 @@ char* parse_ip(char* startptr, struct IP* ip) {
|
|||
return endptr;
|
||||
}
|
||||
|
||||
// parse_ip parses a port starting at startptr. Returns the ending position of the pointer in line.
|
||||
// parse_port parses a port starting at startptr. Returns the ending position of the pointer in line.
|
||||
// Returns NULL on failure.
|
||||
char* parse_port(char* startptr, Port* port) {
|
||||
char* end;
|
||||
|
@ -153,10 +153,18 @@ char* parse_rule(char line[], struct Rule* result) {
|
|||
// by the return value should be freed by the caller. If the return value is
|
||||
// NULL, an error was encoutered and echo'd.
|
||||
struct Rule** parse_file(char fname[], size_t* num_parsed_rules) {
|
||||
FILE *fp = fopen(fname, "r");
|
||||
if (fp == NULL) {
|
||||
perror("Read input file");
|
||||
return NULL;
|
||||
int is_stdin = strcmp(fname, "--") == 0;
|
||||
|
||||
FILE *fp = NULL;
|
||||
|
||||
if (is_stdin == 1) {
|
||||
fp = stdin;
|
||||
} else {
|
||||
fp = fopen(fname, "r");
|
||||
if (fp == NULL) {
|
||||
perror("Read input file");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct Rule** parsed_rules = malloc(sizeof(struct Rule*) * MAX_RULES);
|
||||
|
@ -185,9 +193,11 @@ struct Rule** parse_file(char fname[], size_t* num_parsed_rules) {
|
|||
}
|
||||
free(line);
|
||||
|
||||
if (fclose(fp) != 0) {
|
||||
perror("Close input file");
|
||||
return NULL;
|
||||
if (is_stdin != 1) {
|
||||
if (fclose(fp) != 0) {
|
||||
perror("Close input file");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return parsed_rules;
|
||||
|
|
Reference in a new issue