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
e94d16e7
Commit
e94d16e7
authored
Oct 08, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add coments in Doxygen style and clearify the code
parent
eec12ea2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
16 deletions
+16
-16
mygrep/mygrep.c
mygrep/mygrep.c
+16
-16
No files found.
mygrep/mygrep.c
View file @
e94d16e7
...
@@ -15,10 +15,10 @@ int main(int argc, char *argv[]) {
...
@@ -15,10 +15,10 @@ int main(int argc, char *argv[]) {
char
*
ofile
=
NULL
;
char
*
ofile
=
NULL
;
char
*
keyword
=
NULL
;
char
*
keyword
=
NULL
;
char
*
filename
=
NULL
;
char
*
filename
=
NULL
;
int
c
;
int
c
=
getopt
(
argc
,
argv
,
"io:h"
);
///< Get the options
//
Get
the options
//
/ Process
the options
while
(
(
c
=
getopt
(
argc
,
argv
,
"io:h"
))
!=
-
1
)
{
while
(
c
!=
-
1
)
{
switch
(
c
)
{
switch
(
c
)
{
case
'i'
:
case
'i'
:
iflag
=
1
;
iflag
=
1
;
...
@@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
...
@@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
}
}
}
}
// Check if the required argument is there
/
// Check if the required argument is there
if
(
argv
[
optind
]
==
NULL
)
{
if
(
argv
[
optind
]
==
NULL
)
{
fprintf
(
stderr
,
"Mandatory argument 'keyword' missing.
\n
"
);
fprintf
(
stderr
,
"Mandatory argument 'keyword' missing.
\n
"
);
print_usage
();
print_usage
();
...
@@ -49,28 +49,28 @@ int main(int argc, char *argv[]) {
...
@@ -49,28 +49,28 @@ int main(int argc, char *argv[]) {
filename
=
argv
[
optind
+
1
];
filename
=
argv
[
optind
+
1
];
}
}
// Get user input if there is no input file defined
//
/
Get user input if there is no input file defined
while
(
filename
==
NULL
)
{
while
(
filename
==
NULL
)
{
size_t
len
;
// Define an unsigned string length value
size_t
len
;
//
/<
Define an unsigned string length value
if
(
getline
(
&
filename
,
&
len
,
stdin
)
)
{
//
Read until new line
if
(
getline
(
&
filename
,
&
len
,
stdin
)
!=
-
1
)
{
///<
Read until new line
check_for_string
(
filename
,
keyword
,
iflag
,
ofile
);
check_for_string
(
filename
,
keyword
,
iflag
,
ofile
);
filename
=
NULL
;
// Reset the input
filename
=
NULL
;
//
/<
Reset the input
}
}
}
}
// Get data from input file
//
/
Get data from input file
if
(
filename
!=
NULL
)
{
if
(
filename
!=
NULL
)
{
FILE
*
in
;
FILE
*
in
=
fopen
(
filename
,
"r"
)
;
if
(
(
in
=
fopen
(
filename
,
"r"
))
==
NULL
)
{
if
(
in
==
NULL
)
{
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
filename
);
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
filename
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
size_t
len
;
size_t
len
;
char
*
line
=
NULL
;
// Define a pointer to hold our value
char
*
line
=
NULL
;
//
/<
Define a pointer to hold our value
// Read the file line by line and execute check_for_string for each line
//
/
Read the file line by line and execute check_for_string for each line
while
(
getline
(
&
line
,
&
len
,
in
)
!=
-
1
)
{
while
(
getline
(
&
line
,
&
len
,
in
)
!=
-
1
)
{
check_for_string
(
line
,
keyword
,
iflag
,
ofile
);
check_for_string
(
line
,
keyword
,
iflag
,
ofile
);
}
}
...
@@ -132,10 +132,10 @@ static void check_for_string(char *to_check, char *to_find, int iflag, char *ofi
...
@@ -132,10 +132,10 @@ static void check_for_string(char *to_check, char *to_find, int iflag, char *ofi
* If found, append haystack to the end of the file
* If found, append haystack to the end of the file
* Close the file
* Close the file
**/
**/
FILE
*
out
;
FILE
*
out
=
fopen
(
ofile
,
"a"
)
;
// Check if the file was opened successfully
//
/
Check if the file was opened successfully
if
(
(
out
=
fopen
(
ofile
,
"a"
))
==
NULL
)
{
if
(
out
==
NULL
)
{
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
ofile
);
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
ofile
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
...
...
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