"fb_arc_set/Makefile" did not exist on "e18af3e94e5375678de861d7396d5409b270b8f3"
Commit 36420488 authored by Ivaylo Ivanov's avatar Ivaylo Ivanov

Add argument handling and a README for mygrep

parent 06995354
.vscode/ .vscode/
*.out
# 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
#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);
} }
#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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment