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
dda893a2
Commit
dda893a2
authored
Aug 13, 2019
by
desiredeveloper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
almost final
parent
9e2d48fd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
29 deletions
+41
-29
assignment1/a.out
assignment1/a.out
+0
-0
assignment1/myshell.c
assignment1/myshell.c
+41
-29
assignment1/part1
assignment1/part1
+0
-0
No files found.
assignment1/a.out
View file @
dda893a2
No preview for this file type
assignment1/
part1
.c
→
assignment1/
myshell
.c
100644 → 100755
View file @
dda893a2
...
...
@@ -60,7 +60,6 @@ void funcCd(char *fpath){
free
(
temp
);
}
void
ioRedirect
(
char
**
tokens
,
int
n
){
FILE
*
fp
;
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
...
...
@@ -69,29 +68,29 @@ void ioRedirect(char **tokens,int n){
for
(
int
i
=
0
;
strcmp
(
tokens
[
i
],
">>"
)
&&
strcmp
(
tokens
[
i
],
">"
)
&&
strcmp
(
tokens
[
i
],
"<"
);
i
++
){
arglist
[
j
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
arglist
[
j
++
],
tokens
[
i
]);
}
arglist
[
j
]
=
NULL
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
}
arglist
[
j
]
=
NULL
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
switch
(
fork
())
{
case
-
1
:
break
;
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/"
);
switch
(
fork
())
{
case
-
1
:
break
;
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/"
);
strcat
(
temp
,
arglist
[
0
]);
if
(
execv
(
temp
,
arglist
)
==-
1
){
strcpy
(
temp
,
"/usr/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
if
(
execv
(
temp
,
arglist
)
==-
1
){
strcpy
(
temp
,
"/usr/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
execv
(
temp
,
arglist
);
}
free
(
temp
);
}
execv
(
temp
,
arglist
);
}
free
(
temp
);
}
wait
(
NULL
);
for
(
int
i
=
0
;
arglist
[
i
]
!=
NULL
;
i
++
)
...
...
@@ -100,9 +99,26 @@ void ioRedirect(char **tokens,int n){
free
(
arglist
);
}
// http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal-sigchld-266290/
void
handler
(
int
sig
){
char
*
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
100
);
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
pid_t
pid
;
pid
=
wait
(
NULL
);
printf
(
"
\n
Background process [%d] finished.
\n
"
,
pid
);
signal
(
SIGCHLD
,
NULL
);
getcwd
(
temp
,
150
);
sprintf
(
buffer
,
"%s:$ "
,
temp
);
write
(
1
,
buffer
,
50
);
free
(
temp
);
free
(
buffer
);
}
void
exBackground
(
char
**
tokens
,
int
n
){
char
**
arglist
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
int
j
=
0
,
forkcalls
=
0
;
int
j
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
){
while
(
tokens
[
i
]
!=
NULL
&&
strcmp
(
tokens
[
i
],
"&"
)
!=
0
){
...
...
@@ -110,8 +126,8 @@ void exBackground(char **tokens,int n){
strcpy
(
arglist
[
j
++
],
tokens
[
i
++
]);
}
arglist
[
j
]
=
NULL
;
forkcalls
++
;
char
*
temp
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
150
);
signal
(
SIGCHLD
,
handler
);
switch
(
fork
())
{
case
-
1
:
...
...
@@ -283,19 +299,16 @@ int main(int argc, char* argv[]) {
line
[
strlen
(
line
)
-
1
]
=
'\0'
;
}
else
{
// interactive mode
funcPwd
();
// waitpid(-1,NULL,WNOHANG);
scanf
(
"%[^
\n
]"
,
line
);
getchar
();
}
if
(
!
strlen
(
line
))
continue
;
// printf("Command entered: %s\n", line);
/* END: TAKING INPUT */
line
[
strlen
(
line
)]
=
'\n'
;
//terminate with new line
tokens
=
tokenize
(
line
);
//do whatever you want with the commands, here we just print them
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
){
if
(
!
parallel
)
parallel
=
strcmp
(
tokens
[
i
],
"&&&"
)
==
0
;
...
...
@@ -309,7 +322,6 @@ int main(int argc, char* argv[]) {
if
(
!
io
)
io
=
strcmp
(
tokens
[
i
],
">>"
)
==
0
||
strcmp
(
tokens
[
i
],
">"
)
==
0
||
strcmp
(
tokens
[
i
],
"<"
)
==
0
;
// printf("found token %s\n", tokens[i]);
}
t1
=
clock
();
if
(
parallel
)
...
...
@@ -321,7 +333,7 @@ int main(int argc, char* argv[]) {
else
if
(
io
)
ioRedirect
(
tokens
,
i
);
else
{
if
(
!
strcmp
(
tokens
[
0
],
"dir"
)){
if
(
!
strcmp
(
tokens
[
0
],
"dir"
)
&&
i
==
2
){
funcDir
(
tokens
);
}
else
if
(
!
strcmp
(
tokens
[
0
],
"clear"
)){
...
...
assignment1/part1
deleted
100755 → 0
View file @
9e2d48fd
File deleted
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