Thread: Need help please

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    Need help please

    Hi, i want to make a little command line shell that executes programs with any required arguments.
    But i dont understand what is the problem, why the program donīt work.

    Code:
    main() {
    	char cmd[20];
    	char s[20];
    	int pid,i,status;
    	while(1) {
    		printf("Console$: ");
    		fgets(cmd,sizeof(cmd),stdin);
    		for(i=0; i<strlen(cmd); i++) {
    			if(cmd[i]!=' ')
    				strcpy(s,cmd);
    			if(strcmp(s , "exit")==0) {
    				printf("Shell Terminate\n");
    				break;
    			}
    		}
    		pid = fork();
    		if(pid==0)
    			execv(s,NULL);
    		else
    			wait(&status);
    	}
    }

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    From your code sample, it looks as though what you really want is the system() call, not a fork/exec, as the system() call does this for you, and also parses out the command line.

    I'm pretty sure that your command line parser is not doing what you want it to, as it just repeatedly copies the contents of cmd into s.

    (Also, your exit check doesn't do anything useful.)

    I hope this helps.
    Last edited by filker0; 10-05-2005 at 11:52 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    so how can i make this work:
    if i put
    >Console$: ls

    i want the program execute the ls command and if i put:

    >Console$: exit

    I quit the program, but it seems he canīt execute any command.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(cmd[i]!=' ')
    > strcpy(s,cmd);
    Why are you doing this inside a for loop?
    compare a char, copy a string (no change there then) and compare.

    The biggest problem at the moment is that you don't remove the \n from the buffer which fgets() returns to you.
    See the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by maritos
    so how can i make this work:
    if i put
    >Console$: ls

    i want the program execute the ls command and if i put:

    >Console$: exit

    I quit the program, but it seems he canīt execute any command.
    From your questions, and your code, I'm guessing that you're pretty new to C programming. The reason the "exit" command doesn't work is that you're doing a break from inside a nested loop, and break only takes you out one level. I don't know what you're trying to achieve with the strcpy() in the inner loop, but if you look at the FAQ, check out the man page for the system() library call, and simply walk through your code step-by-step, you'll find that the problem is with your code logic.

    Here is a version of your program that I put together in about 5 minutes -- it does what you're looking for.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
       char cmd[128];       /* a command line buffer */
       int status = 0;      /* return status from system() call */
    
       while (1)  /* loop until done */
       {
          printf("Console$: ");     /* prompt the user */
          fflush(stdout);           /* display the prompt */
          if (fgets(cmd, sizeof(cmd), stdin) < 0)  /* get a response */
          {  /* read error -- assume end of input */
             printf("Read error on stdin, exiting.\n"); /* print an error */
             break;  /* get out of the loop. */
          }
          if (cmd[strlen(cmd) - 1] < 0x20)  /* trim off a trailing newline */
          {  /* some control character as last character */
             cmd[strlen(cmd) - 1] = '\0'; /* replace it with a nul */
          }
          if (strcmp("exit", cmd) == 0) /* check for "exit" */
          {  /* found the exit string */
             printf("Shell Terminated\n");  /* tell the user */
             break;        /* get out of the loop */
          }
          else
          {  /* Wasn't the exit string */
             printf("About to execute '%s'\n", cmd); /* echo the cmd. */
             status = system(cmd);  /* execute the command */
             if (status != 0)  /* see if there was a problem with the cmd */
             {  /* there was */
                perror(cmd);  /* display an error message */
             }
          }
       }
    
       exit(0); /* exit the program */
    }
    I hope this helps. (If I find out that this was for a homework assignment, I'll be somewhat annoyed.)

Popular pages Recent additions subscribe to a feed