69 lines
1.6 KiB
C
69 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include "mygrep.h"
|
|
|
|
void main(int argc, char *argv[]) {
|
|
check_opts_number(argc, argv);
|
|
|
|
int iflag = 0;
|
|
char *ofile = NULL;
|
|
char *keyword = NULL;
|
|
char *filename = NULL;
|
|
int c;
|
|
|
|
while((c = getopt(argc, argv, "io:h")) != -1) {
|
|
switch(c) {
|
|
case 'i':
|
|
iflag = 1;
|
|
break;
|
|
case 'o':
|
|
ofile = optarg;
|
|
break;
|
|
case 'h':
|
|
print_usage();
|
|
break;
|
|
case '?':
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
default:
|
|
abort();
|
|
}
|
|
}
|
|
|
|
if(argv[optind] == NULL) {
|
|
printf("Mandatory argument 'keyword' missing.\n");
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
} else {
|
|
keyword = argv[optind];
|
|
filename = argv[optind + 1];
|
|
}
|
|
|
|
if(filename == NULL) {
|
|
check_for_string("test", keyword);
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
void print_usage() {
|
|
printf("mygrep [-i] [-o outfile] keyword [file...]\n");
|
|
}
|
|
|
|
void check_opts_number(int argc, char *argv[]) {
|
|
if(argc <= 1) {
|
|
fprintf(stderr, "At least one argument expected.\n");
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
} else if(argc > 6) {
|
|
fprintf(stderr, "Too many arguments supplied.\n");
|
|
print_usage();
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
void check_for_string(char *to_check, char *to_find) {
|
|
printf("Not implemented. This function will search for the substring %s in the string %s.\n", to_find, to_check);
|
|
}
|