Simple Shell
Simple Shell
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
void init_shell()
{
clear();
printf("\n\n\t-We're starting in 3 Seconds...-");
printf("\n\n\n\n*******************"
"***********************");
char* username = getenv("USER");
printf("\n\n\nhaha@%s", username);
printf("\n");
sleep(3);
clear();
}
// system commands
void execute_commands(char** command)
{
pid_t pid = fork();
if (pid == 0)
{
execvp(command[0], command);
exit(0);
}
else
{
wait(NULL);
return;
}
}
//piped commands
void execute_pipes(char** parsed, char** parsedpipe)
{
int fd[2];
pid_t p1, p2;
p1 = fork();
if (p1 == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execvp(parsed[0], parsed);
}
else
{
p2 = fork();
if (p2 == 0)
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
execvp(parsedpipe[0], parsedpipe);
}
else
{
wait(NULL);
wait(NULL);
}
}
}
void help()
{
puts("\n***WELCOME !!! Here's shell HELP... ***"
"\n\n-Use the shell at your own risk... :D"
"\n Commands included : "
"\n> cd"
"\n> ls"
"\n> help"
"\n> exit"
"\n> corona"
"\n> haha"
"\n> other basic commands available in Ubuntu Terminal"
"\n> pipe handling"
"\n> Help will go away in 15 seconds...");
return;
}
//new commands...
int execute_extra_commands(char** parsed)
{
int NoOfOwnCmds = 5, i, switchOwnArg = 0;
char* ListOfOwnCmds[NoOfOwnCmds];
char* username;
ListOfOwnCmds[0] = "exit";
ListOfOwnCmds[1] = "cd";
ListOfOwnCmds[2] = "help";
ListOfOwnCmds[3] = "haha";
ListOfOwnCmds[4] = "corona";
switch (switchOwnArg) {
case 1:
printf("\nGoodbye\n");
exit(0);
case 2:
chdir(parsed[1]);
return 1;
case 3:
help();
return 1;
case 4:
username = getenv("USER");
printf("\nHello %s.\nMind that this is "
"not a place to play around."
"\nUse help to know more..\n",
username);
return 1;
case 5:
printf("\nHello.. I hope you're fine and Safe."
"\nYou shouldn't execute such commands. "
"\nBTW please stay home and isolated"
"\n And enjoy healthy Holidays....\n");
return 1;
default:
break;
}
return 0;
}
//separates space-wise
void read_space(char* line, char** parsed)
{
for (int i = 0; i < max_commands; i++)
{
parsed[i] = strsep(&line, " "); //token-wise separation on " "
if (parsed[i] == NULL) //if no space is there
break;
if (strlen(parsed[i]) == 0)
i--;
}
}
//finds out what type of command it is.. pipe ? own ? built-in ? space ?
int which_type(char* line, char** parsed, char** parsedpipe)
{
char* strpiped[2];
int piped = 0;
if (piped)
{
read_space(strpiped[0], parsed);
read_space(strpiped[1], parsedpipe);
}
else
read_space(line, parsed);
if (execute_extra_commands(parsed))
return 0;
else
return 1 + piped; //if piped than greater than 1 else 1
}
int main()
{
char input[max_len], *commands[max_commands];
char* commandsPiped[max_commands];
int execFlag = 0;
help();
sleep(15);
clear();
init_shell();
while (1) {
printDir();
if (takeInput(input)) //takes input using readline library
continue;
execFlag = which_type(input,commands,commandsPiped); //flag to know
command-type
if (execFlag == 1)
execute_commands(commands);
if (execFlag == 2)
execute_pipes(commands, commandsPiped);
}
return 0;
}