Here is the alpha version of the shell that I recently developed. This program runs fine in Linux. This shell will accept max 3 arguments.
Here's the program:
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/wait.h"
char InputData[100];
char Command[100];
char Parameter1[100];
char Parameter2[100];
unsigned int CommandLength = 0;
unsigned int Parameter1Length = 0;
unsigned int Parameter2Length = 0;
unsigned int i=0;
void read_command(void);
int main()
{
int status;
printf("[Balaji]>");
while(1)
{
read_command();
if(fork()!=0)
{
//waitpid(-1,&status,0);
wait(&status);
}
else
{
if((Parameter1[0] == '\0')&&(Parameter2[0]=='\0'))
{
execlp(Command,Command,'\0');
}
else if((Parameter1[0] != '\0')&&(Parameter2[0]=='\0'))
{
execlp(Command,Command,Parameter1,'\0');
}
else
{
execlp(Command,Command,Parameter1,Parameter2,'\0');
}
}
printf("[Balaji]>");
for(i=0;i<
CommandLength;i++)
{
Command[i] = '\0';
}
for(i=0;i<
Parameter1Length;i++)
{
Parameter1[i] = '\0';
}
for(i=0;i {
Parameter2[i] = '\0';
}
}
}
void read_command(void)
{
unsigned char ParameterStart = 0;
gets(InputData);
for(i=0;InputData[i]!='\0';i++)
{
if((InputData[i]!=' ')&&(ParameterStart==0))
{
Command[i]=InputData[i];
CommandLength = i;
}
else if((InputData[i]==' ')&&(ParameterStart ==0))
{
Command[i]='\0';
CommandLength=i;
ParameterStart = 1;
}
else if((InputData[i]!=' ')&&(ParameterStart==1))
{
Parameter1[i-CommandLength-1] = InputData[i];
Parameter1Length = i-CommandLength-1;
}
else if((InputData[i]==' ')&&(ParameterStart==1))
{
Parameter1[i-CommandLength] = '\0';
Parameter1Length = (i-CommandLength);
ParameterStart = 2;
}
else if((InputData[i]!=' ')&&(ParameterStart==2))
{
Parameter2[i-CommandLength-Parameter1Length-1] = InputData[i];
Parameter2Length = i-CommandLength-Parameter1Length-1;
}
else if((InputData[i]==' ')&&(ParameterStart==1))
{
Parameter2[i-CommandLength-Parameter1Length] = '\0';
Parameter2Length = (i-CommandLength-Parameter1Length);
ParameterStart = 3;
}
}
if((InputData[i]=='\0')&&(ParameterStart==0))
{
Parameter1[0] = '\0';
Parameter2[0] = '\0';
Parameter1Length = 1;
Parameter2Length = 1;
}
}
.
gcc -o balaji shell.c
./balaji