Design and Develop a shell that should support at least 20 commands.
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
void parse(char *cmd, char **arguvec)
{
while(*cmd != '\0')
{
while( *cmd == ' ' || *cmd == '\t' || *cmd == '\n')
*cmd++='\0';
*arguvec++=cmd;
while(*cmd != '\0' && *cmd != ' ' && *cmd != '\t' && *cmd != '\n')
cmd++;
}
*arguvec = '\0';
}
void cmdexecutes(char **arguvec)
{
pid_t pid;
int status;
pid = fork();
if(pid < 0)
{
printf("\n error in creating fork");
exit(0);
}
else if(pid == 0)
{
if(execvp (*arguvec, arguvec) < 0)
{
printf("\n error in calling exec");
exit(0);
}
}
else
{
while(wait(&status) != pid);
}
}
int main()
{
char cmd[1024];
char *arguvec[20];
while(1)
{
printf("[MYSHELL]");
gets(cmd);
parse(cmd, arguvec);
if( strcmp(arguvec[0], "exit") == 0)
{
exit(0);
}
cmdexecutes(arguvec);
}
return 0;
}
Output:
ubuntu@ubuntu-VirtualBox:~/lab$ gedit 1.c
ubuntu@ubuntu-VirtualBox:~/lab$ cc 1.c
ubuntu@ubuntu-VirtualBox:~/lab$ ./a.out
[MYSHELL]ls
1.c 1.c~ a.out
[MYSHELL]who
ubuntu tty7 2015-11-25 09:04
ubuntu pts/1 2015-11-25 09:27 (:0)
[MYSHELL]ps
PID TTY TIME CMD
2610 pts/1 00:00:01 bash
2825 pts/1 00:00:00 a.out
2828 pts/1 00:00:00 ps
[MYSHELL]date
Wed Nov 25 09:38:46 IST 2015
[MYSHELL]cal
November 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
[MYSHELL]pwd
/home/ubuntu/lab
[MYSHELL]
Comments
Post a Comment