Add argument handling and a README for mygrep
This commit is contained in:
parent
069953548c
commit
36420488a1
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
*.out
|
||||||
|
@ -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.**
|
@ -1,5 +1,68 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "mygrep.h"
|
||||||
|
|
||||||
void main() {
|
void main(int argc, char *argv[]) {
|
||||||
printf("Hello, World!");
|
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);
|
||||||
}
|
}
|
||||||
|
8
mygrep/mygrep.h
Normal file
8
mygrep/mygrep.h
Normal file
@ -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
|
Reference in New Issue
Block a user