Remove useless header file, add working Makefile and comments

This commit is contained in:
Ivaylo Ivanov 2018-10-08 12:21:18 +02:00
parent 4d98d13dce
commit eec12ea2ff
3 changed files with 49 additions and 19 deletions

View File

@ -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)

View File

@ -3,10 +3,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "mygrep.h"
void main(int argc, char *argv[]) { static void print_usage(void);
check_opts_number(argc, argv); 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; int iflag = 0;
char *ofile = NULL; char *ofile = NULL;
@ -38,7 +41,7 @@ void main(int argc, char *argv[]) {
// Check if the required argument is there // Check if the required argument is there
if(argv[optind] == NULL) { if(argv[optind] == NULL) {
printf("Mandatory argument 'keyword' missing.\n"); fprintf(stderr, "Mandatory argument 'keyword' missing.\n");
print_usage(); print_usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else { } else {
@ -60,7 +63,7 @@ void main(int argc, char *argv[]) {
if(filename != NULL) { if(filename != NULL) {
FILE *in; FILE *in;
if((in = fopen(filename, "r")) == NULL) { 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); 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"); 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) { if(argc <= 1) {
fprintf(stderr, "At least one argument expected.\n"); fprintf(stderr, "At least one argument expected.\n");
print_usage(); 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) { if(ofile == NULL) {
/** /**
* Find the first occurance of needle in haystack and return it as a pointer. * 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 // Check if the file was opened successfully
if((out = fopen(ofile, "a")) == NULL) { 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); exit(EXIT_FAILURE);
} }

View File

@ -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