diff --git a/mygrep/Makefile b/mygrep/Makefile index e69de29..c160c19 100644 --- a/mygrep/Makefile +++ b/mygrep/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L -g -c +TARGET = mygrep + +all: $(TARGET).c + $(CC) $(CFLAGS) $(TARGET).c + $(CC) $(TARGET).o -o $(TARGET) + +install: + cp $(TARGET) /usr/local/bin/$(TARGET) + +clean: + $(RM) $(TARGET).o + $(RM) $(TARGET) \ No newline at end of file diff --git a/mygrep/mygrep.c b/mygrep/mygrep.c index 64b5788..3b29892 100644 --- a/mygrep/mygrep.c +++ b/mygrep/mygrep.c @@ -3,10 +3,13 @@ #include #include #include -#include "mygrep.h" -void main(int argc, char *argv[]) { - check_opts_number(argc, argv); +static void print_usage(void); +static void check_opts_number(int argc); +static void check_for_string(char *to_check, char *to_find, int iflag, char *ofile); + +int main(int argc, char *argv[]) { + check_opts_number(argc); int iflag = 0; char *ofile = NULL; @@ -38,7 +41,7 @@ void main(int argc, char *argv[]) { // Check if the required argument is there if(argv[optind] == NULL) { - printf("Mandatory argument 'keyword' missing.\n"); + fprintf(stderr, "Mandatory argument 'keyword' missing.\n"); print_usage(); exit(EXIT_FAILURE); } else { @@ -60,7 +63,7 @@ void main(int argc, char *argv[]) { if(filename != NULL) { FILE *in; if((in = fopen(filename, "r")) == NULL) { - printf("Error opening the file %s\n", filename); + fprintf(stderr, "Error opening the file %s\n", filename); exit(EXIT_FAILURE); } @@ -73,15 +76,22 @@ void main(int argc, char *argv[]) { } } - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } -void print_usage() { +static void print_usage(void) { printf("mygrep [-i] [-o outfile] keyword [file...]\n"); } -// Check for coorect number of options -void check_opts_number(int argc, char *argv[]) { +/** + * @brief Function to check the number of arguments + * @details Checks the number of arguments supplied to the command line. + * If the arguments are less than the required or more than possible, it exits + * @param argc Number of arguments obtained from the command line + * @return none + * + **/ +static void check_opts_number(int argc) { if(argc <= 1) { fprintf(stderr, "At least one argument expected.\n"); print_usage(); @@ -93,7 +103,21 @@ void check_opts_number(int argc, char *argv[]) { } } -void check_for_string(char *to_check, char *to_find, int iflag, char *ofile) { +/** + * @brief Function to check whether or not a string is contained in another string + * @details Checks whether or not a substring is contained in a line. + * If it is there and no output file is passed, it will print the line to stdout + * If it is there and an output file is passed, it will append the line to the end of the output file + * + * @param + * to_check: String to be checked against + * to_find: String to search for + * iflag: Whether or not to ignore the casing of the letters + * ofile: The file to append the output to + * @return none + * + **/ +static void check_for_string(char *to_check, char *to_find, int iflag, char *ofile) { if(ofile == NULL) { /** * Find the first occurance of needle in haystack and return it as a pointer. @@ -112,7 +136,7 @@ void check_for_string(char *to_check, char *to_find, int iflag, char *ofile) { // Check if the file was opened successfully if((out = fopen(ofile, "a")) == NULL) { - printf("Error opening the file %s\n", ofile); + fprintf(stderr, "Error opening the file %s\n", ofile); exit(EXIT_FAILURE); } diff --git a/mygrep/mygrep.h b/mygrep/mygrep.h deleted file mode 100644 index d522ad3..0000000 --- a/mygrep/mygrep.h +++ /dev/null @@ -1,8 +0,0 @@ -#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, int iflag, char *ofile); - -#endif \ No newline at end of file