Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > shell programming problem

Reply
Thread Tools

shell programming problem

 
 
jorntk@yahoo.com
Guest
Posts: n/a
 
      08-30-2003
#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[i]=p;
p=strtok(NULL," ");
}
arg_list[i]=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.
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      08-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> #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:

news:comp.unix.programmer

> #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[i]=p;
> p=strtok(NULL," ");
> }
> arg_list[i]=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.

news:comp.unix.programmer is the place for you to ask.

--ag


--
Artie Gold -- Austin, Texas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using subprocess module to launch a shell shell script that itselfforks a process Samuel A. Falvo II Python 0 10-08-2008 12:43 AM
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments