LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to fed command to shell through programm (https://www.linuxquestions.org/questions/linux-general-1/how-to-fed-command-to-shell-through-programm-891389/)

vikas_choudharyy 07-12-2011 08:44 PM

how to fed command to shell through programm
 
THIS PROGRAM ACCEPTS ONLY COMMANDS WHICH ARE VALID WITHIN "/bin" I WANT TO MAKE IT GENERAL SO THAT IT CAN ACCEPTS ALL THE COMMANDS AND CAN WORK PROPERLY....
please help me....
thanx

//a prog to create a shell which do the same work as bash shell

#ifndef SHELL
#define SHELL
#include<stdio.h> // header files required for program
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>
#define SIZE_OF_BUFFER 100// buffer which is used for command line arg
#define SIZE_OF_COMMAND 30
extern char **environ;
#endif

int main()
{
int pid,i;
char buffer[SIZE_OF_BUFFER+1];
char *command[SIZE_OF_COMMAND];
int catch_status;
while(1)
{
char path1[50] = { "/bin/" };
char path2[50] = { "/usr/bin/" };
printf( "08MCMC02_05@shell:~>" );
scanf( " %[^\n]s ", buffer );
if(!strcmp(buffer , "exit"))
exit(0);

if(!strcmp(buffer,"cd"))
{
printf(":: It does not create child, try other command \n");
continue;
}
command[0] = strtok( buffer , " ");
for(i=1;i<SIZE_OF_COMMAND;i++)
{
command[i] = strtok( NULL , " " );
if( command[i] == NULL )
break;
}
command[i] = NULL;

pid = fork();
switch(pid)
{
case 0:
strcat( path1 , command[0] );
execve( path1 , command , environ );

case -1:
strcat( path2 , command[0] );
execve( path2 , command , environ );
printf( " error \n" );
break;

default:
wait( &catch_status );
break;
}
for(i = 0 ; i < SIZE_OF_COMMAND ; i++)
command[i] = '\0';

for(i = 0 ;i < SIZE_OF_BUFFER + 1 ; i++)
buffer[i] = '\0';

for(i = 0 ; i < 50 ; i++)
path1[i] = '\0';

for(i = 0 ; i < 50 ; i++)
path2[i] = '\0';

}

}

MTK358 07-13-2011 07:10 AM

First of all, use code tags to make it readable.


All times are GMT -5. The time now is 10:22 PM.