Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Systems744
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
SHREYANSH JAIN
Systems744
Commits
9e2d48fd
Commit
9e2d48fd
authored
Aug 13, 2019
by
desiredeveloper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
part 4 IO
parent
a8193dca
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
25 deletions
+86
-25
assignment1/part1.c
assignment1/part1.c
+86
-25
No files found.
assignment1/part1.c
View file @
9e2d48fd
...
@@ -60,23 +60,29 @@ void funcCd(char *fpath){
...
@@ -60,23 +60,29 @@ void funcCd(char *fpath){
free
(
temp
);
free
(
temp
);
}
}
void
exBackground
(
char
**
tokens
,
int
n
){
void
ioRedirect
(
char
**
tokens
,
int
n
){
FILE
*
fp
;
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
int
j
=
0
,
forkcalls
=
0
;
int
j
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
for
(
int
i
=
0
;
strcmp
(
tokens
[
i
],
">>"
)
&&
strcmp
(
tokens
[
i
],
">"
)
&&
strcmp
(
tokens
[
i
],
"<"
);
i
++
){
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&"
)
!=
0
){
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
strcpy
(
arglist
[
j
++
],
tokens
[
i
]);
}
}
arglist
[
j
]
=
NULL
;
arglist
[
j
]
=
NULL
;
forkcalls
++
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
switch
(
fork
())
{
switch
(
fork
())
{
case
-
1
:
case
-
1
:
break
;
break
;
case
0
:
case
0
:
if
(
!
strcmp
(
tokens
[
n
-
2
],
">>"
))
fp
=
freopen
(
tokens
[
n
-
1
],
"a"
,
stdout
);
else
if
(
!
strcmp
(
tokens
[
n
-
2
],
">"
))
fp
=
freopen
(
tokens
[
n
-
1
],
"w"
,
stdout
);
else
fp
=
freopen
(
tokens
[
n
-
1
],
"r"
,
stdin
);
strcpy
(
temp
,
"/bin/"
);
strcpy
(
temp
,
"/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
strcat
(
temp
,
arglist
[
0
]);
if
(
execv
(
temp
,
arglist
)
==-
1
){
if
(
execv
(
temp
,
arglist
)
==-
1
){
...
@@ -86,8 +92,7 @@ void exBackground(char **tokens,int n){
...
@@ -86,8 +92,7 @@ void exBackground(char **tokens,int n){
}
}
free
(
temp
);
free
(
temp
);
}
}
j
=
0
;
wait
(
NULL
);
}
for
(
int
i
=
0
;
arglist
[
i
]
!=
NULL
;
i
++
)
for
(
int
i
=
0
;
arglist
[
i
]
!=
NULL
;
i
++
)
free
(
arglist
[
i
]);
free
(
arglist
[
i
]);
...
@@ -95,16 +100,17 @@ void exBackground(char **tokens,int n){
...
@@ -95,16 +100,17 @@ void exBackground(char **tokens,int n){
free
(
arglist
);
free
(
arglist
);
}
}
void
ex
Serial
(
char
**
tokens
,
int
n
){
void
ex
Background
(
char
**
tokens
,
int
n
){
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
int
j
=
0
;
int
j
=
0
,
forkcalls
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
for
(
int
i
=
0
;
i
<
n
;
i
++
){
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&
&
"
)
!=
0
){
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&"
)
!=
0
){
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
}
}
arglist
[
j
]
=
NULL
;
arglist
[
j
]
=
NULL
;
forkcalls
++
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
switch
(
fork
())
{
switch
(
fork
())
{
...
@@ -120,6 +126,48 @@ void exSerial(char **tokens,int n){
...
@@ -120,6 +126,48 @@ void exSerial(char **tokens,int n){
}
}
free
(
temp
);
free
(
temp
);
}
}
j
=
0
;
}
for
(
int
i
=
0
;
arglist
[
i
]
!=
NULL
;
i
++
)
free
(
arglist
[
i
]);
free
(
arglist
);
}
void
exSerial
(
char
**
tokens
,
int
n
){
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
int
j
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
int
ioFlag
=
0
;
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&&"
)
!=
0
){
if
(
!
ioFlag
)
ioFlag
=
strcmp
(
tokens
[
i
],
">>"
)
==
0
||
strcmp
(
tokens
[
i
],
">"
)
==
0
||
strcmp
(
tokens
[
i
],
"<"
)
==
0
;
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
}
arglist
[
j
]
=
NULL
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
if
(
!
ioFlag
){
switch
(
fork
())
{
case
-
1
:
break
;
case
0
:
strcpy
(
temp
,
"/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
if
(
execv
(
temp
,
arglist
)
==-
1
){
strcpy
(
temp
,
"/usr/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
execv
(
temp
,
arglist
);
}
free
(
temp
);
}
}
else
{
ioRedirect
(
arglist
,
j
);
}
wait
(
NULL
);
wait
(
NULL
);
j
=
0
;
j
=
0
;
}
}
...
@@ -135,27 +183,34 @@ void exParallel(char **tokens,int n){
...
@@ -135,27 +183,34 @@ void exParallel(char **tokens,int n){
int
j
=
0
,
forkcalls
=
0
;
int
j
=
0
,
forkcalls
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
for
(
int
i
=
0
;
i
<
n
;
i
++
){
int
ioFlag
=
0
;
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&&&"
)
!=
0
){
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&&&"
)
!=
0
){
if
(
!
ioFlag
)
ioFlag
=
strcmp
(
tokens
[
i
],
">>"
)
==
0
||
strcmp
(
tokens
[
i
],
">"
)
==
0
||
strcmp
(
tokens
[
i
],
"<"
)
==
0
;
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
}
}
forkcalls
++
;
arglist
[
j
]
=
NULL
;
arglist
[
j
]
=
NULL
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
switch
(
fork
())
{
if
(
!
ioFlag
){
case
-
1
:
forkcalls
++
;
break
;
switch
(
fork
())
{
case
0
:
case
-
1
:
strcpy
(
temp
,
"/bin/"
);
break
;
strcat
(
temp
,
arglist
[
0
]);
case
0
:
if
(
execv
(
temp
,
arglist
)
==-
1
){
strcpy
(
temp
,
"/bin/"
);
strcpy
(
temp
,
"/usr/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
strcat
(
temp
,
arglist
[
0
]);
execv
(
temp
,
arglist
);
if
(
execv
(
temp
,
arglist
)
==-
1
){
}
strcpy
(
temp
,
"/usr/bin/"
);
free
(
temp
);
strcat
(
temp
,
arglist
[
0
]);
execv
(
temp
,
arglist
);
}
free
(
temp
);
}
}
else
{
ioRedirect
(
arglist
,
j
);
}
}
j
=
0
;
j
=
0
;
}
}
...
@@ -169,6 +224,7 @@ void exParallel(char **tokens,int n){
...
@@ -169,6 +224,7 @@ void exParallel(char **tokens,int n){
free
(
arglist
);
free
(
arglist
);
}
}
/* Splits the string by space and returns the array of tokens
/* Splits the string by space and returns the array of tokens
*/
*/
...
@@ -216,7 +272,7 @@ int main(int argc, char* argv[]) {
...
@@ -216,7 +272,7 @@ int main(int argc, char* argv[]) {
}
}
while
(
1
)
{
while
(
1
)
{
int
parallel
=
0
,
serial
=
0
,
background
=
0
,
i
nternal
=
0
,
cdir
=
0
;
int
parallel
=
0
,
serial
=
0
,
background
=
0
,
i
o
=
0
,
cdir
=
0
;
/* BEGIN: TAKING INPUT */
/* BEGIN: TAKING INPUT */
bzero
(
line
,
sizeof
(
line
));
bzero
(
line
,
sizeof
(
line
));
...
@@ -250,6 +306,9 @@ int main(int argc, char* argv[]) {
...
@@ -250,6 +306,9 @@ int main(int argc, char* argv[]) {
if
(
!
background
)
if
(
!
background
)
background
=
strcmp
(
tokens
[
i
],
"&"
)
==
0
;
background
=
strcmp
(
tokens
[
i
],
"&"
)
==
0
;
if
(
!
io
)
io
=
strcmp
(
tokens
[
i
],
">>"
)
==
0
||
strcmp
(
tokens
[
i
],
">"
)
==
0
||
strcmp
(
tokens
[
i
],
"<"
)
==
0
;
// printf("found token %s\n", tokens[i]);
// printf("found token %s\n", tokens[i]);
}
}
t1
=
clock
();
t1
=
clock
();
...
@@ -259,6 +318,8 @@ int main(int argc, char* argv[]) {
...
@@ -259,6 +318,8 @@ int main(int argc, char* argv[]) {
exSerial
(
tokens
,
i
);
exSerial
(
tokens
,
i
);
else
if
(
background
)
else
if
(
background
)
exBackground
(
tokens
,
i
);
exBackground
(
tokens
,
i
);
else
if
(
io
)
ioRedirect
(
tokens
,
i
);
else
{
else
{
if
(
!
strcmp
(
tokens
[
0
],
"dir"
)){
if
(
!
strcmp
(
tokens
[
0
],
"dir"
)){
funcDir
(
tokens
);
funcDir
(
tokens
);
...
...
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