Thread: C program strange error

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Question C program strange error

    Hi, I wrote the following code which creates a simple shell (like bash). It works fine, can take a command along with arguments and run it or give error if it doesn't exist. However, the strange problem is that it doesn;t execute anything the first time "enter" is hit!! But after that it works perfectly.

    I couldn't find the bug, any help would be great. Thanks
    ---------------------------------------------------------------------------------------------

    Code:
    #include <string.h>
    #include <errno.h>
    
    #define TRUE 1
    
    void type_prompt();
    void *read_command(char *argv[]);
    int n = 0;
    
    int main(int argc, char *argv[], char *envp[])
    {
    char *command;
    int status;
    argv[0] = NULL;
    
    while (TRUE) /* repeat forever */
    {
    type_prompt(); /* display prompt on screen */
    read_command(argv); /* read input from terminal */
    
    if (fork() != 0) /* fork off child process */
    {
    waitpid(-1, &status, 0); /* wait for child to exit */
    }
    else
    {
    if (execve(argv[0], argv, envp) == -1) /* execute command */
    printf("Ouch, can't execute: %s\n", strerror(errno));
    }
    }
    return 0;
    }
    
    void type_prompt()
    {
    printf("[arajgarh]$ ");
    }
    
    void *read_command(char *argv[])
    {
    n = 0;
    char temp[256];
    
    gets(temp);
    argv[n++] = strtok (temp," ");
    
    while (argv[n-1] != NULL)
    argv[n++] = strtok (NULL, " ");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well the first problem is, your code is poorly (ie not at all) indented.

    > read_command(argv);
    You re-use argv in an entirely inappropriate manner
    It also returns void* for no good reason (you don't return anything, and it's ignored anyway)

    > gets(temp);
    Read the FAQ - it explains why gets() is the world's most dangerous function

    > char temp[256];
    This is a local variable - image that it's contents will be trashed as soon as the function exits.
    Bad news for all those pointers you extracted using strtok
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You obviously didn't bother to heed my advice when you posted this exact same thing on linuxquestions.org about fixing all the warnings and getting rid of gets().
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    Thanks for helping. Now I understand the issue about temp being local and causing problems.

    itsme86, thanks for your code in linuxquestions.org.
    Heeding the advice about warnings and gets() was one thing, but I also needed to know WHY the current one is not working, and now I know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM