shell programming problem

Discussion in 'C Programming' started by jorntk@yahoo.com, Aug 30, 2003.

  1. Guest

    #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.
    , Aug 30, 2003
    #1
    1. Advertising

  2. Artie Gold Guest

    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=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.

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

    --ag


    --
    Artie Gold -- Austin, Texas
    Artie Gold, Aug 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,700
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    601
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,078
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    864
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. Samuel A. Falvo II
    Replies:
    0
    Views:
    483
    Samuel A. Falvo II
    Oct 8, 2008
Loading...

Share This Page