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
e18af3e9
Commit
e18af3e9
authored
Nov 03, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add comments to mygrep, begin implementing the http client
parent
7765c696
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
155 additions
and
7 deletions
+155
-7
.gitignore
.gitignore
+1
-0
http/Makefile
http/Makefile
+27
-0
http/client.c
http/client.c
+84
-0
http/shared/http.h
http/shared/http.h
+7
-0
mygrep/Makefile
mygrep/Makefile
+1
-1
mygrep/mygrep.c
mygrep/mygrep.c
+35
-6
No files found.
.gitignore
View file @
e18af3e9
.vscode/
.vscode/
*.out
*.out
*.o
http/Makefile
0 → 100644
View file @
e18af3e9
CC
=
gcc
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-D_DEFAULT_SOURCE
-D_BSD_SOURCE
-D_SVID_SOURCE
-D_POSIX_C_SOURCE
=
200809L
-g
-c
TARGET_1
=
client
TARGET_2
=
server
all
:
$(TARGET_1).c $(TARGET_2).c
$(CC)
$(CFLAGS)
$(TARGET_1)
.c
$(TARGET_2)
.c
$(CC)
$(TARGET_1)
.o
-o
$(TARGET_1)
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
client
:
$(TARGET_1).c
$(CC)
$(CFLAGS)
$(TARGET_1)
.c
$(CC)
$(TARGET_1)
.o
-o
$(TARGET_1)
server
:
$(TARGET_2).c
$(CC)
$(CFLAGS)
$(TARGET_2)
.c
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
install
:
cp
$(TARGET_1)
/usr/local/bin/myhttp-
$(TARGET_1)
cp
$(TARGET_2)
/usr/local/bin/myhttp-
$(TARGET_2)
clean
:
$(RM)
$(TARGET_1)
.o
$(RM)
$(TARGET_1)
$(RM)
$(TARGET_2)
.o
$(RM)
$(TARGET_2)
\ No newline at end of file
http/client.c
View file @
e18af3e9
#define _GNU_SOURCE ///< in order for optarg to be defined
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "shared/http.h"
int
main
(
int
argc
,
char
*
argv
[])
{
check_opts_number
(
argc
);
int
port
=
80
;
char
*
ofile
=
NULL
;
char
*
dir
=
NULL
;
char
*
url
=
NULL
;
int
c
;
/// Process the options
while
((
c
=
getopt
(
argc
,
argv
,
"p:o:d:h"
))
!=
-
1
)
{
switch
(
c
)
{
case
'p'
:
port
=
atoi
(
optarg
);
break
;
case
'o'
:
ofile
=
optarg
;
break
;
case
'd'
:
dir
=
optarg
;
break
;
case
'h'
:
printf
(
"Command usage:
\n
"
);
print_usage
();
exit
(
EXIT_SUCCESS
);
break
;
case
'?'
:
print_usage
();
exit
(
EXIT_FAILURE
);
default:
abort
();
}
}
/// Check for the case that both output file and directory are set
if
(
ofile
!=
NULL
&&
dir
!=
NULL
)
{
printf
(
"Either output file or directory is specified. You cannot use both.
\n
"
);
print_usage
();
exit
(
EXIT_FAILURE
);
}
/// Check if the required argument is there
if
(
argv
[
optind
]
==
NULL
)
{
fprintf
(
stderr
,
"Mandatory argument 'url' missing.
\n
"
);
print_usage
();
exit
(
EXIT_FAILURE
);
}
else
{
url
=
argv
[
optind
];
}
const
char
*
full_url
=
strstr
(
url
,
"http://"
)
+
7
;
//< the full url starts after http://
const
char
*
path
=
strstr
(
full_url
,
"/"
);
}
static
void
print_usage
(
void
)
{
printf
(
"client [-p PORT] [ -o FILE | -d DIR ] URL
\n
"
);
}
/**
* @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
)
{
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
);
}
}
http/shared/http.h
0 → 100644
View file @
e18af3e9
#ifndef HTTP_HEADER_FILE
#define HTTP_HEADER_FILE
static
void
print_usage
();
static
void
check_opts_number
(
int
argc
);
#endif
\ No newline at end of file
mygrep/Makefile
View file @
e18af3e9
...
@@ -2,7 +2,7 @@ CC = gcc
...
@@ -2,7 +2,7 @@ CC = gcc
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-D_DEFAULT_SOURCE
-D_BSD_SOURCE
-D_SVID_SOURCE
-D_POSIX_C_SOURCE
=
200809L
-g
-c
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-D_DEFAULT_SOURCE
-D_BSD_SOURCE
-D_SVID_SOURCE
-D_POSIX_C_SOURCE
=
200809L
-g
-c
TARGET
=
mygrep
TARGET
=
mygrep
all
:
$(TARGET).c
all
:
$(TARGET).c
$(CC)
$(CFLAGS)
$(TARGET)
.c
$(CC)
$(CFLAGS)
$(TARGET)
.c
$(CC)
$(TARGET)
.o
-o
$(TARGET)
$(CC)
$(TARGET)
.o
-o
$(TARGET)
...
...
mygrep/mygrep.c
View file @
e18af3e9
/**
* @file mygrep.c
* @author Ivaylo Ivanov 11777707
* @date 03.11.2018
*
* @brief Main program module.
*
* 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.
*
**/
#define _GNU_SOURCE ///< in order for strcasestr to work
#define _GNU_SOURCE ///< in order for strcasestr to work
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
...
@@ -85,11 +114,11 @@ static void print_usage(void) {
...
@@ -85,11 +114,11 @@ static void print_usage(void) {
/**
/**
* @brief Function to check the number of arguments
* @brief Function to check the number of arguments
* @details Checks the number of arguments supplied to the command line.
* @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
* If the arguments are less than the required or more than possible, it exits
* @param argc Number of arguments obtained from the command line
* @param argc Number of arguments obtained from the command line
* @return none
* @return none
*
*
**/
**/
static
void
check_opts_number
(
int
argc
)
{
static
void
check_opts_number
(
int
argc
)
{
if
(
argc
<=
1
)
{
if
(
argc
<=
1
)
{
...
@@ -105,17 +134,17 @@ static void check_opts_number(int argc) {
...
@@ -105,17 +134,17 @@ static void check_opts_number(int argc) {
/**
/**
* @brief Function to check whether or not a string is contained in another string
* @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.
* @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 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
* If it is there and an output file is passed, it will append the line to the end of the output file
*
*
* @param
* @param
* to_check: String to be checked against
* to_check: String to be checked against
* to_find: String to search for
* to_find: String to search for
* iflag: Whether or not to ignore the casing of the letters
* iflag: Whether or not to ignore the casing of the letters
* ofile: The file to append the output to
* ofile: The file to append the output to
* @return none
* @return none
*
*
**/
**/
static
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
)
{
static
void
check_for_string
(
char
*
to_check
,
char
*
to_find
,
int
iflag
,
char
*
ofile
)
{
if
(
ofile
==
NULL
)
{
if
(
ofile
==
NULL
)
{
...
...
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