mygrep.c 1.59 KB
Newer Older
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
1
#include <stdio.h>
2 3 4
#include <stdlib.h>
#include <unistd.h>
#include "mygrep.h"
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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);
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
68
}