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
eec12ea2
Commit
eec12ea2
authored
Oct 08, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove useless header file, add working Makefile and comments
parent
4d98d13d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
19 deletions
+49
-19
mygrep/Makefile
mygrep/Makefile
+14
-0
mygrep/mygrep.c
mygrep/mygrep.c
+35
-11
mygrep/mygrep.h
mygrep/mygrep.h
+0
-8
No files found.
mygrep/Makefile
View file @
eec12ea2
CC
=
gcc
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-D_DEFAULT_SOURCE
-D_BSD_SOURCE
-D_SVID_SOURCE
-D_POSIX_C_SOURCE
=
200809L
-g
-c
TARGET
=
mygrep
all
:
$(TARGET).c
$(CC)
$(CFLAGS)
$(TARGET)
.c
$(CC)
$(TARGET)
.o
-o
$(TARGET)
install
:
cp
$(TARGET)
/usr/local/bin/
$(TARGET)
clean
:
$(RM)
$(TARGET)
.o
$(RM)
$(TARGET)
\ No newline at end of file
mygrep/mygrep.c
View file @
eec12ea2
...
@@ -3,10 +3,13 @@
...
@@ -3,10 +3,13 @@
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <string.h>
#include <string.h>
#include "mygrep.h"
void
main
(
int
argc
,
char
*
argv
[])
{
static
void
print_usage
(
void
);
check_opts_number
(
argc
,
argv
);
static
void
check_opts_number
(
int
argc
);
static
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
);
int
main
(
int
argc
,
char
*
argv
[])
{
check_opts_number
(
argc
);
int
iflag
=
0
;
int
iflag
=
0
;
char
*
ofile
=
NULL
;
char
*
ofile
=
NULL
;
...
@@ -38,7 +41,7 @@ void main(int argc, char *argv[]) {
...
@@ -38,7 +41,7 @@ void 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
)
{
printf
(
"Mandatory argument 'keyword' missing.
\n
"
);
fprintf
(
stderr
,
"Mandatory argument 'keyword' missing.
\n
"
);
print_usage
();
print_usage
();
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
else
{
}
else
{
...
@@ -60,7 +63,7 @@ void main(int argc, char *argv[]) {
...
@@ -60,7 +63,7 @@ void main(int argc, char *argv[]) {
if
(
filename
!=
NULL
)
{
if
(
filename
!=
NULL
)
{
FILE
*
in
;
FILE
*
in
;
if
((
in
=
fopen
(
filename
,
"r"
))
==
NULL
)
{
if
((
in
=
fopen
(
filename
,
"r"
))
==
NULL
)
{
printf
(
"Error opening the file %s
\n
"
,
filename
);
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
filename
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
...
@@ -73,15 +76,22 @@ void main(int argc, char *argv[]) {
...
@@ -73,15 +76,22 @@ void main(int argc, char *argv[]) {
}
}
}
}
exit
(
EXIT_SUCCESS
)
;
return
EXIT_SUCCESS
;
}
}
void
print_usage
(
)
{
static
void
print_usage
(
void
)
{
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
[])
{
* @brief Function to check the number of arguments
* @details Checks the number of arguments supplied to the command line.
* If the arguments are less than the required or more than possible, it exits
* @param argc Number of arguments obtained from the command line
* @return none
*
**/
static
void
check_opts_number
(
int
argc
)
{
if
(
argc
<=
1
)
{
if
(
argc
<=
1
)
{
fprintf
(
stderr
,
"At least one argument expected.
\n
"
);
fprintf
(
stderr
,
"At least one argument expected.
\n
"
);
print_usage
();
print_usage
();
...
@@ -93,7 +103,21 @@ void check_opts_number(int argc, char *argv[]) {
...
@@ -93,7 +103,21 @@ void check_opts_number(int argc, char *argv[]) {
}
}
}
}
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
)
{
/**
* @brief Function to check whether or not a string is contained in another string
* @details Checks whether or not a substring is contained in a line.
* If it is there and no output file is passed, it will print the line to stdout
* If it is there and an output file is passed, it will append the line to the end of the output file
*
* @param
* to_check: String to be checked against
* to_find: String to search for
* iflag: Whether or not to ignore the casing of the letters
* ofile: The file to append the output to
* @return none
*
**/
static
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
)
{
if
(
ofile
==
NULL
)
{
if
(
ofile
==
NULL
)
{
/**
/**
* Find the first occurance of needle in haystack and return it as a pointer.
* Find the first occurance of needle in haystack and return it as a pointer.
...
@@ -112,7 +136,7 @@ void check_for_string(char *to_check, char *to_find, int iflag, char *ofile) {
...
@@ -112,7 +136,7 @@ void check_for_string(char *to_check, char *to_find, int iflag, char *ofile) {
// Check if the file was opened successfully
// Check if the file was opened successfully
if
((
out
=
fopen
(
ofile
,
"a"
))
==
NULL
)
{
if
((
out
=
fopen
(
ofile
,
"a"
))
==
NULL
)
{
printf
(
"Error opening the file %s
\n
"
,
ofile
);
fprintf
(
stderr
,
"Error opening the file %s
\n
"
,
ofile
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
...
...
mygrep/mygrep.h
deleted
100644 → 0
View file @
4d98d13d
#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
,
int
iflag
,
char
*
ofile
);
#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