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
b4cea012
Commit
b4cea012
authored
Aug 15, 2019
by
desiredeveloper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
final changes
parent
f4d2735d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
22 deletions
+60
-22
assignment1/Makefile
assignment1/Makefile
+0
-0
assignment1/a.out
assignment1/a.out
+0
-0
assignment1/demo
assignment1/demo
+5
-0
assignment1/myshell.c
assignment1/myshell.c
+55
-22
No files found.
assignment1/Makefile
0 → 100644
View file @
b4cea012
assignment1/a.out
View file @
b4cea012
No preview for this file type
assignment1/demo
0 → 100644
View file @
b4cea012
echo hi
echo hi >> file
pwd
echo hi >> file
pwd
\ No newline at end of file
assignment1/myshell.c
View file @
b4cea012
...
...
@@ -4,12 +4,24 @@
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
extern
char
**
environ
;
// extern char **environ;
int
fileExists
(
char
*
path
){
return
access
(
path
,
F_OK
)
!=
-
1
?
1
:
0
;
}
int
folderExists
(
const
char
*
path
)
{
struct
stat
stats
;
stat
(
path
,
&
stats
);
return
S_ISDIR
(
stats
.
st_mode
)
?
1
:
0
;
}
void
funcExit
()
{
...
...
@@ -18,9 +30,13 @@ void funcExit()
void
funcEnv
()
{
int
i
;
for
(
i
=
0
;
environ
[
i
]
!=
NULL
;
i
++
)
printf
(
"%s
\n
"
,
environ
[
i
]);
// int i;
// for (i = 0; environ[i] != NULL; i++)
// printf("%s\n", environ[i]);
setenv
(
"COURSE"
,
"CS-744"
,
1
);
setenv
(
"ASSIGNMENT"
,
"ASSIGNMENT_1"
,
1
);
printf
(
"COURSE=%s
\n
ASSIGNMENT=%s
\n
PWD=%s
\n
"
,
getenv
(
"COURSE"
),
getenv
(
"ASSIGNMENT"
),
getenv
(
"PWD"
));
}
void
funcClear
()
...
...
@@ -31,7 +47,10 @@ void funcClear()
void
funcDir
(
char
**
tokens
)
{
char
a
[
150
]
=
"ls -la "
;
system
(
strcat
(
a
,
tokens
[
1
]));
if
(
folderExists
(
tokens
[
1
])
||
fileExists
(
tokens
[
1
]))
system
(
strcat
(
a
,
tokens
[
1
]));
else
printf
(
"ERROR:Directory does not exist
\n
"
);
}
void
funcPwd
()
...
...
@@ -50,7 +69,7 @@ void funcCd(char *fpath)
if
(
fpath
&&
chdir
(
fpath
)
!=
0
)
{
printf
(
"ERROR:
Directory doesn't exist!
\n
"
);
printf
(
"ERROR:Directory doesn't exist!
\n
"
);
return
;
}
printf
(
"BEFORE:
\n
OLDPWD=%s
\n
PWD=%s
\n
"
,
getenv
(
"OLDPWD"
),
getenv
(
"PWD"
));
...
...
@@ -86,12 +105,20 @@ void ioRedirect(char **tokens, int n)
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
);
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
{
if
(
fileExists
(
tokens
[
n
-
1
]))
{
fp
=
freopen
(
tokens
[
n
-
1
],
"r"
,
stdin
);
}
else
{
printf
(
"ERROR:File does not exist
\n
"
);
exit
(
0
);
}
}
strcpy
(
temp
,
"/bin/"
);
strcat
(
temp
,
arglist
[
0
]);
if
(
execv
(
temp
,
arglist
)
==
-
1
)
...
...
@@ -120,7 +147,7 @@ void handler(int sig)
pid_t
pid
;
pid
=
wait
(
NULL
);
printf
(
"
\n
Background process [%d] finished.
\n
"
,
pid
);
printf
(
"
\n
MyShell:
Background process [%d] finished.
\n
"
,
pid
);
signal
(
SIGCHLD
,
NULL
);
getcwd
(
temp
,
150
);
sprintf
(
buffer
,
"%s:$ "
,
temp
);
...
...
@@ -323,7 +350,7 @@ int main(int argc, char *argv[])
fp
=
fopen
(
argv
[
1
],
"r"
);
if
(
fp
<
0
)
{
printf
(
"
File doesn't exists.
"
);
printf
(
"
ERROR:File doesn't exist
"
);
return
-
1
;
}
}
...
...
@@ -336,11 +363,8 @@ int main(int argc, char *argv[])
bzero
(
line
,
sizeof
(
line
));
if
(
argc
==
2
)
{
// batch mode
if
(
fgets
(
line
,
sizeof
(
line
),
fp
)
==
NULL
)
{
// file reading finished
if
(
fscanf
(
fp
,
"%[^
\n
]
\n
"
,
line
)
==
EOF
)
break
;
}
line
[
strlen
(
line
)
-
1
]
=
'\0'
;
}
else
{
// interactive mode
...
...
@@ -369,7 +393,7 @@ int main(int argc, char *argv[])
if
(
!
io
)
io
=
strcmp
(
tokens
[
i
],
">>"
)
==
0
||
strcmp
(
tokens
[
i
],
">"
)
==
0
||
strcmp
(
tokens
[
i
],
"<"
)
==
0
;
}
t1
=
clock
();
if
(
parallel
)
exParallel
(
tokens
,
i
);
else
if
(
serial
)
...
...
@@ -380,34 +404,43 @@ int main(int argc, char *argv[])
ioRedirect
(
tokens
,
i
);
else
{
t1
=
clock
();
if
(
!
strcmp
(
tokens
[
0
],
"dir"
)
&&
i
==
2
)
{
funcDir
(
tokens
);
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
else
if
(
!
strcmp
(
tokens
[
0
],
"clear"
))
{
funcClear
();
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
else
if
(
!
strcmp
(
tokens
[
0
],
"quit"
))
{
funcExit
();
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
else
if
(
!
strcmp
(
tokens
[
0
],
"env"
))
{
funcEnv
();
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
else
if
(
!
strcmp
(
tokens
[
0
],
"cd"
))
{
funcCd
(
tokens
[
1
]);
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
else
{
system
(
line
);
}
}
t1
=
clock
()
-
t1
;
printf
(
"%f sec
\n
"
,
(
double
)
t1
/
CLOCKS_PER_SEC
);
}
// Freeing the allocated memory
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
)
...
...
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