#include "header.h" void printerror(char *errmsg1, char *errmsg2) { strcat(errmsg1, errmsg2); perror(errmsg1); } void print_usage(char *arg) { fprintf(stderr, "USAGE: \n\t %s [input] [-o output]\n", arg); exit(1); } struct args parse_input(int argc, char *argv[]) { struct args these_args; int i; boolean input_init = FALSE; boolean output_init = FALSE; boolean num_of_lines_init = FALSE; for(i = 1; i < argc; i++) { char *cur_arg = *(argv+i); if(cur_arg[0] == '-') { if(cur_arg[1] == 'o') { /* open for writing the name following -o */ these_args.output_fd = creat(argv[i+1], 0644); if(these_args.output_fd < 0) { printerror("can't open ", argv[i+1]); } /* increment i to skip through the argument past -o */ i++; output_init = TRUE; } else { these_args.num_of_lines = cur_arg; num_of_lines_init = TRUE; } } else { /* must be the input file */ /* in the case that the input file was already specified */ if(input_init) { print_usage(argv[0]); } these_args.input_fd = open(cur_arg, O_RDONLY); if(these_args.input_fd < 0) { printerror("can't open ", cur_arg); } input_init = TRUE; } } if(!input_init) { these_args.input_fd = 0; } if(!output_init) { these_args.output_fd = 1; } if(!num_of_lines_init) { these_args.num_of_lines = ""; } return these_args; }