Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
unix
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
unix
Commits
36420488
"fb_arc_set/Makefile" did not exist on "e18af3e94e5375678de861d7396d5409b270b8f3"
Commit
36420488
authored
Oct 06, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add argument handling and a README for mygrep
parent
06995354
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
3 deletions
+98
-3
.gitignore
.gitignore
+2
-1
mygrep/README.md
mygrep/README.md
+23
-0
mygrep/mygrep.c
mygrep/mygrep.c
+65
-2
mygrep/mygrep.h
mygrep/mygrep.h
+8
-0
No files found.
.gitignore
View file @
36420488
.vscode/
.vscode/
*.out
mygrep/README.md
View file @
36420488
# 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
mygrep/mygrep.c
View file @
36420488
#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
);
}
}
mygrep/mygrep.h
0 → 100644
View file @
36420488
#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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment