shell programming problem

J

jorntk

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

/*limit the lenght of input to 256 character and number
of argument to 10*/
#define MAX 256
#define CMAX 10
char logout[]= "logout";

int spawn(char* program, char** arg_list)
{
pid_t child_pid;
if(!strcmp(program,logout))
{
exit(0);
}
if ((child_pid=fork())==0)
{
execvp(program,arg_list);
perror("execvp");
exit(1);
}
}

int main()
{
char input[MAX];
char *p;
char *arg_list[CMAX+1];
int i;


while(1)
{
printf("Shell> ");
if (fgets(input,sizeof input, stdin)!=NULL)
{
if(strchr(input,'\n'))
*strchr(input,'\n')=0;
else
{
while (!strchr(input,'\n'))
fgets(input, sizeof input,stdin);
exit(1);
}
}
p=strtok(input," ");
for(i=0;i<CMAX;++i)
{
arg_list=p;
p=strtok(NULL," ");
}
arg_list=0;

spawn(arg_list[0], arg_list);
}
return 0;
}

the above program can accept commnand from user and execute it, i need
advice on how to expand its function to include pipe function. other
thing is where should i place code to exit when logout is enter, i
have try checking in spawn process it works but the program will
return segmentation error when click enter with not other command.
thanks in advance.
 
A

Artie Gold

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Use of this header (which is not a standard C header) makes me
suspect that your question is off topic here and that should take
your question to:

#include <sys/types.h>

Hmmm, I'm starting to think my suspicion is correct.
#include <string.h>

/*limit the lenght of input to 256 character and number
of argument to 10*/
#define MAX 256
#define CMAX 10
char logout[]= "logout";

int spawn(char* program, char** arg_list)
{
pid_t child_pid;
if(!strcmp(program,logout))
{
exit(0);
}
if ((child_pid=fork())==0)
{
execvp(program,arg_list);
perror("execvp");
exit(1);
}
}

int main()
{
char input[MAX];
char *p;
char *arg_list[CMAX+1];
int i;


while(1)
{
printf("Shell> ");
if (fgets(input,sizeof input, stdin)!=NULL)
{
if(strchr(input,'\n'))
*strchr(input,'\n')=0;
else
{
while (!strchr(input,'\n'))
fgets(input, sizeof input,stdin);
exit(1);
}
}
p=strtok(input," ");
for(i=0;i<CMAX;++i)
{
arg_list=p;
p=strtok(NULL," ");
}
arg_list=0;

spawn(arg_list[0], arg_list);
}
return 0;
}

the above program can accept commnand from user and execute it, i need
advice on how to expand its function to include pipe function. other
thing is where should i place code to exit when logout is enter, i
have try checking in spawn process it works but the program will
return segmentation error when click enter with not other command.
thanks in advance.


Yup.
Off topic here.

is the place for you to ask.

--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top