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
4d98d13d
Commit
4d98d13d
authored
Oct 07, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement check for user input and file data, add file append
parent
80977a0d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
6 deletions
+62
-6
mygrep/README.md
mygrep/README.md
+1
-1
mygrep/mygrep.c
mygrep/mygrep.c
+60
-4
mygrep/mygrep.h
mygrep/mygrep.h
+1
-1
No files found.
mygrep/README.md
View file @
4d98d13d
...
@@ -10,7 +10,7 @@ The program `mygrep` reads files line by line and for each line checks whether i
...
@@ -10,7 +10,7 @@ The program `mygrep` reads files line by line and for each line checks whether i
term keyword. The line is printed if it contains the keyword, otherwise it is not printed.
term keyword. The line is printed if it contains the keyword, otherwise it is not printed.
The program accepts lines of any length.
The program accepts lines of any length.
If one or multiple input files are specified (given as positional arguments after keyword), then mygrep
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
reads each of them in the order they are given. If no input file is specified, the program reads from
`stdin`
.
`stdin`
.
...
...
mygrep/mygrep.c
View file @
4d98d13d
#define _GNU_SOURCE // in order for strcasestr to work
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <string.h>
#include "mygrep.h"
#include "mygrep.h"
void
main
(
int
argc
,
char
*
argv
[])
{
void
main
(
int
argc
,
char
*
argv
[])
{
...
@@ -12,6 +14,7 @@ void main(int argc, char *argv[]) {
...
@@ -12,6 +14,7 @@ void main(int argc, char *argv[]) {
char
*
filename
=
NULL
;
char
*
filename
=
NULL
;
int
c
;
int
c
;
// Get the options
while
((
c
=
getopt
(
argc
,
argv
,
"io:h"
))
!=
-
1
)
{
while
((
c
=
getopt
(
argc
,
argv
,
"io:h"
))
!=
-
1
)
{
switch
(
c
)
{
switch
(
c
)
{
case
'i'
:
case
'i'
:
...
@@ -21,7 +24,9 @@ void main(int argc, char *argv[]) {
...
@@ -21,7 +24,9 @@ void main(int argc, char *argv[]) {
ofile
=
optarg
;
ofile
=
optarg
;
break
;
break
;
case
'h'
:
case
'h'
:
printf
(
"Command usage:
\n
"
);
print_usage
();
print_usage
();
exit
(
EXIT_SUCCESS
);
break
;
break
;
case
'?'
:
case
'?'
:
print_usage
();
print_usage
();
...
@@ -31,6 +36,7 @@ void main(int argc, char *argv[]) {
...
@@ -31,6 +36,7 @@ void main(int argc, char *argv[]) {
}
}
}
}
// Check if the required argument is there
if
(
argv
[
optind
]
==
NULL
)
{
if
(
argv
[
optind
]
==
NULL
)
{
printf
(
"Mandatory argument 'keyword' missing.
\n
"
);
printf
(
"Mandatory argument 'keyword' missing.
\n
"
);
print_usage
();
print_usage
();
...
@@ -40,8 +46,31 @@ void main(int argc, char *argv[]) {
...
@@ -40,8 +46,31 @@ void main(int argc, char *argv[]) {
filename
=
argv
[
optind
+
1
];
filename
=
argv
[
optind
+
1
];
}
}
if
(
filename
==
NULL
)
{
// Get user input if there is no input file defined
check_for_string
(
"test"
,
keyword
);
while
(
filename
==
NULL
)
{
size_t
len
;
// Define an unsigned string length value
if
(
getline
(
&
filename
,
&
len
,
stdin
))
{
// Read until new line
check_for_string
(
filename
,
keyword
,
iflag
,
ofile
);
filename
=
NULL
;
// Reset the input
}
}
// Get data from input file
if
(
filename
!=
NULL
)
{
FILE
*
in
;
if
((
in
=
fopen
(
filename
,
"r"
))
==
NULL
)
{
printf
(
"Error opening the file %s
\n
"
,
filename
);
exit
(
EXIT_FAILURE
);
}
size_t
len
;
char
*
line
=
NULL
;
// Define a pointer to hold our value
// Read the file line by line and execute check_for_string for each line
while
(
getline
(
&
line
,
&
len
,
in
)
!=
-
1
)
{
check_for_string
(
line
,
keyword
,
iflag
,
ofile
);
}
}
}
exit
(
EXIT_SUCCESS
);
exit
(
EXIT_SUCCESS
);
...
@@ -51,6 +80,7 @@ void print_usage() {
...
@@ -51,6 +80,7 @@ void print_usage() {
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
[])
{
void
check_opts_number
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<=
1
)
{
if
(
argc
<=
1
)
{
fprintf
(
stderr
,
"At least one argument expected.
\n
"
);
fprintf
(
stderr
,
"At least one argument expected.
\n
"
);
...
@@ -63,6 +93,32 @@ void check_opts_number(int argc, char *argv[]) {
...
@@ -63,6 +93,32 @@ void check_opts_number(int argc, char *argv[]) {
}
}
}
}
void
check_for_string
(
char
*
to_check
,
char
*
to_find
)
{
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
)
{
printf
(
"Not implemented. This function will search for the substring %s in the string %s.
\n
"
,
to_find
,
to_check
);
if
(
ofile
==
NULL
)
{
/**
* Find the first occurance of needle in haystack and return it as a pointer.
* If found, print haystack
**/
if
(
iflag
==
0
&&
strstr
(
to_check
,
to_find
)
!=
NULL
)
printf
(
"%s"
,
to_check
);
else
if
(
iflag
==
1
&&
strcasestr
(
to_check
,
to_find
))
printf
(
"%s"
,
to_check
);
}
else
{
/**
* Open the specified file for appending
* Find the first occurance of needle in haystack and return it as a pointer.
* If found, append haystack to the end of the file
* Close the file
**/
FILE
*
out
;
// Check if the file was opened successfully
if
((
out
=
fopen
(
ofile
,
"a"
))
==
NULL
)
{
printf
(
"Error opening the file %s
\n
"
,
ofile
);
exit
(
EXIT_FAILURE
);
}
if
(
iflag
==
0
&&
strstr
(
to_check
,
to_find
))
fprintf
(
out
,
"%s"
,
to_check
);
else
if
(
iflag
==
1
&&
strcasestr
(
to_check
,
to_find
))
fprintf
(
out
,
"%s"
,
to_check
);
fclose
(
out
);
}
}
}
mygrep/mygrep.h
View file @
4d98d13d
...
@@ -3,6 +3,6 @@
...
@@ -3,6 +3,6 @@
void
print_usage
();
void
print_usage
();
void
check_opts_number
(
int
argc
,
char
*
argv
[]);
void
check_opts_number
(
int
argc
,
char
*
argv
[]);
void
check_for_string
(
char
*
to_check
,
char
*
to_find
);
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
);
#endif
#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