Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SHELL
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chaitanya Shravan
SHELL
Commits
2c6337f7
Commit
2c6337f7
authored
Dec 02, 2022
by
Chaitanya Shravan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
f588830c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
220 additions
and
0 deletions
+220
-0
my_shell.c
my_shell.c
+220
-0
No files found.
my_shell.c
0 → 100644
View file @
2c6337f7
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
int
tokenNo
=
0
;
int
fc
=
0
;
int
bg_pgrp
=
0
,
bg_count
=
0
;
void
termHandler
(){
}
void
intHandler
()
{
if
(
fc
)
{
kill
(
fc
,
SIGKILL
);
waitpid
(
fc
,
NULL
,
0
);
fc
=
0
;
printf
(
"
\n
"
);
}
else
{
printf
(
"
\n
$ "
);}
fflush
(
stdout
);
}
/* Splits the string by space and returns the array of tokens
*/
char
**
tokenize
(
char
*
line
)
{
char
**
tokens
=
(
char
**
)
malloc
(
MAX_NUM_TOKENS
*
sizeof
(
char
*
));
char
*
token
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
int
i
,
tokenIndex
=
0
;
tokenNo
=
0
;
for
(
i
=
0
;
i
<
strlen
(
line
);
i
++
){
char
readChar
=
line
[
i
];
if
(
readChar
==
' '
||
readChar
==
'\n'
||
readChar
==
'\t'
){
token
[
tokenIndex
]
=
'\0'
;
if
(
tokenIndex
!=
0
){
tokens
[
tokenNo
]
=
(
char
*
)
malloc
(
MAX_TOKEN_SIZE
*
sizeof
(
char
));
strcpy
(
tokens
[
tokenNo
++
],
token
);
tokenIndex
=
0
;
}
}
else
{
token
[
tokenIndex
++
]
=
readChar
;
}
}
free
(
token
);
tokens
[
tokenNo
]
=
NULL
;
return
tokens
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
char
line
[
MAX_INPUT_SIZE
];
char
**
tokens
;
int
i
,
status
;
//signal(SIGTERM,SIG_IGN);
// signal(SIGINT,intHandler);
struct
sigaction
sa_INT
=
{
0
};
sa_INT
.
sa_handler
=
&
intHandler
;
sa_INT
.
sa_flags
=
SA_RESTART
;
sigaction
(
SIGINT
,
&
sa_INT
,
NULL
);
while
(
1
)
{
int
ret
=
waitpid
(
-
1
,
&
status
,
WNOHANG
);
if
(
ret
>
0
){
printf
(
"Shell: Background process finished with pid : %d
\n
"
,
ret
);
bg_count
--
;
}
/* BEGIN: TAKING INPUT */
bzero
(
line
,
sizeof
(
line
));
printf
(
"$ "
);
scanf
(
"%[^
\n
]"
,
line
);
getchar
();
//printf("Command entered: %s (remove this debug output later)\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
if
(
tokens
[
0
]
==
NULL
)
{
continue
;
}
if
(
line
[
0
]
==
'c'
&&
line
[
1
]
==
'd'
)
// support for cd command
{
if
(
tokens
[
2
]
!=
NULL
)
{
printf
(
"Incorrect Number of Arguments For CD Command
\n
"
);
// Freeing the allocated memory
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
){
free
(
tokens
[
i
]);
}
free
(
tokens
);
continue
;
}
if
(
chdir
(
tokens
[
1
])
!=
0
)
printf
(
"Shell: Incorrect command
\n
"
);
continue
;
}
//exit Command
char
*
exit
=
"exit"
;
if
(
strcmp
(
tokens
[
0
],
exit
)
==
0
)
{
//signal(SIGTERM,termHandler);
if
(
bg_count
){
//printf("Group To Exit :%d\n",bg_pgrp);
killpg
(
bg_pgrp
,
SIGTERM
);
//printf("Still Here\n");
int
ret
;
while
((
ret
=
waitpid
(
-
1
,
NULL
,
0
))
!=
-
1
)
printf
(
"Shell: Background process finished with pid : %d
\n
"
,
ret
);
}
printf
(
"exit
\n
"
);
// Freeing the allocated memory
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
){
free
(
tokens
[
i
]);
}
free
(
tokens
);
break
;
//continue;
}
// BG execution
//printf("%s\n",tokens[tokenNo-1]);
if
(
*
tokens
[
tokenNo
-
1
]
==
'&'
)
// BG execution support
{
//printf("Adding support for background execution soon!!\n");
fc
=
fork
();
if
(
fc
<
0
)
{
printf
(
"Unable to create a child process!!
\n
"
);}
else
if
(
fc
==
0
)
//child process code
{
//printf("BG Process\n");
tokens
[
tokenNo
-
1
]
=
NULL
;
execvp
(
tokens
[
0
],
tokens
);
printf
(
"Command execution failed!!
\n
"
);
_exit
(
1
);
}
else
{
//parent code
if
(
!
bg_count
)
{
setpgid
(
fc
,
fc
);
bg_pgrp
=
getpgid
(
fc
);
bg_count
=
1
;
}
else
{
setpgid
(
fc
,
bg_pgrp
);
bg_count
++
;
}
//printf("BG Group : %d\n ", getpgid(fc)); fflush(stdout);
printf
(
"[%d]
\n
"
,
fc
);
fc
=
0
;
//int wc = wait(NULL);
}
// Freeing the allocated memory
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
){
free
(
tokens
[
i
]);
}
free
(
tokens
);
continue
;
}
fc
=
fork
();
if
(
fc
<
0
)
{
printf
(
"Unable to create a child process!!
\n
"
);}
else
if
(
fc
==
0
)
//child process code
{
execvp
(
tokens
[
0
],
tokens
);
printf
(
"Command execution failed!!
\n
"
);
_exit
(
1
);
}
else
{
//parent code
int
wc
=
waitpid
(
fc
,
&
status
,
0
);
fc
=
0
;
//printf("process exited with pid : %d\n",wc);
}
// for(i=0;tokens[i]!=NULL;i++){
// printf("found token %s (remove this debug output later)\n", tokens[i]);
// }
// Freeing the allocated memory
for
(
i
=
0
;
tokens
[
i
]
!=
NULL
;
i
++
){
free
(
tokens
[
i
]);
}
free
(
tokens
);
}
return
0
;
}
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