From 36420488a1172f2a976b0c65dcd325794f100615 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Sat, 6 Oct 2018 17:23:33 +0200 Subject: [PATCH] Add argument handling and a README for mygrep --- .gitignore | 3 ++- mygrep/README.md | 23 +++++++++++++++++ mygrep/mygrep.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++-- mygrep/mygrep.h | 8 ++++++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 mygrep/mygrep.h diff --git a/.gitignore b/.gitignore index dbe9c82..c047fbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode/ \ No newline at end of file +.vscode/ +*.out diff --git a/mygrep/README.md b/mygrep/README.md index e69de29..cead972 100644 --- a/mygrep/README.md +++ b/mygrep/README.md @@ -0,0 +1,23 @@ +# mygrep + +A reduced variation of the Unix-command `grep`. +It reads in several files and prints all lines containing a keyword. + +` SYNOPSIS + mygrep [-i] [-o outfile] keyword [file...] ` + +The program `mygrep` reads files line by line and for each line checks whether it contains the search +term keyword. The line is printed if it contains the keyword, otherwise it is not printed. +The program accepts lines of any length. + +If one or multiple input files are specified (given as positional arguments after keyword), then mygrep +reads each of them in the order they are given. If no input file is specified, the program reads from +`stdin`. + +If the option `-o` is given, the output is written to the specified file (outfile). Otherwise, the output is +written to `stdout`. + +If the option `-i` is given, the program does not differentiate between lower and upper case letters, i.e. +the search for the keyword in a line is case insensitive. + +**Note: The description is from the task I got from TU.** \ No newline at end of file diff --git a/mygrep/mygrep.c b/mygrep/mygrep.c index 5a7647a..672fa7d 100644 --- a/mygrep/mygrep.c +++ b/mygrep/mygrep.c @@ -1,5 +1,68 @@ #include +#include +#include +#include "mygrep.h" -void main() { - printf("Hello, World!"); +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); } diff --git a/mygrep/mygrep.h b/mygrep/mygrep.h new file mode 100644 index 0000000..cbdbe92 --- /dev/null +++ b/mygrep/mygrep.h @@ -0,0 +1,8 @@ +#ifndef MYGREP_HEADER +#define MYGREP_HEADER + +void print_usage(); +void check_opts_number(int argc, char *argv[]); +void check_for_string(char *to_check, char *to_find); + +#endif \ No newline at end of file