Thread: Why wont my function exit correctly?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Why wont my function exit correctly?

    Code:
    void RunShellLoop(struct prompt *pr)
    {
       Bool stillGoing = TRUE;
       Bool execvp_worked = FALSE; 
       ssize_t readReturn = 0;
       char *input = NULL;
       char *lastCommand = NULL;
       char **Inputs = NULL;
       char *old_command = NULL;
       char command_search;
       size_t bufLen = 0;
       int rows = 0;
       int old_col = 0;
       int col = 0;
       int x = 0;
       int i = 0;
       int len_old_command = 0;
       int num_of_inputs = 0;
    
       while(stillGoing)
       {
          /* The buffer will contain the new line character and the null char */
          readReturn = getline(&input, &bufLen, stdin);
          
          /* Get rid of any new line that may be there. */
          for(x = 0; x < strlen(input) + 1; ++x)
          {
             if(input[x] == '\n')
             {
                input[x] = '\0';
             }
          }
    
          
          if(readReturn == -1)
          {
             stillGoing = FALSE;
             printf("\n");
          }
          else if(strncmp(input, "exit", 4) == 0)
          {
             stillGoing = FALSE;
             break;
          }
          else if(execute_command(input) != TRUE)
          {
             addNode(input);
             if(readReturn == 3 && strncmp(input, "!!", 2) == 0)
             {
                printf("You typed: %s\n", lastCommand);
             }
             else if(strncmp(input, "!", 1) == 0)
             {
    	    for(cptr=tptr; cptr != NULL; cptr=cptr->next)
    	    {
    	        if(cptr->x[0] == input[1])
    	        {
    	           printf("You typed: %s\n", cptr->x);
                       break;
                    }
                }
                PrintPrompt(pr);
             }
       
    	 else 
             {
                /*lastCommand = realloc(lastCommand, strlen(input) + 1);
    	     strncpy(lastCommand, input, strlen(input) + 1);*/
                for(cptr=tptr; cptr != NULL; cptr=cptr->next)
    	    {
    	       if(cptr->next == NULL)
    	       {
                      printf("You typed: %s\n", cptr->x);
                   }
                }
                PrintPrompt(pr);          
             }
          }
          else
    	PrintPrompt(pr);
    
          input = NULL;
       }
       
       for(cptr = tptr; cptr != NULL; cptr = cptr->next)
       {
         printf("%s\n", cptr->x);
         }
     
       for(cptr = tptr; cptr != NULL; )
       {
          struct node *tempptr;
          tempptr = cptr->next;
          free(cptr); 
          cptr = tempptr;
       }     
    
       free(input);
       free(lastCommand);
       
    }
    If I enter input 3 times, I have to type exit three times to get the function to exit. If I type exit on the first input it exits on the first try. Anyone have any idea why?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    here is the addnode function btw:

    Code:
    void addNode(char *input)
    {
       struct node *nptr;
       cptr = tptr;
    
       if ((nptr = malloc(sizeof(struct node))) != NULL)
       {
          nptr->x = input;
          nptr->next = NULL;
    
          if(tptr == NULL)
          {
             tptr = nptr;
          }
          else
          {
             while(cptr->next != NULL)
             {
                cptr = cptr->next;
             }
             /*cptr->next = malloc(sizeof(struct node));*/
             cptr->next = nptr;
          }
       }
       else
       {
          fprintf(stderr, "%s", "Not enough memory.\n");
          exit(EXIT_FAILURE);
       }
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why I didn't look more closely:
    Code:
    PC-lint for C/C++ (NT) Ver. 8.00e, Copyright Gimpel Software 1985-2001
    
    --- Module:   test.c 
    test.c 7 error [Warning 565] tag 'node' not previously seen, assumed file-level scope
    test.c 8 error [Error 40] Undeclared identifier 'cptr'
    test.c 8 error [Error 40] Undeclared identifier 'tptr'
    test.c 8 error [Error 63] Expected an lvalue
    test.c 10 error [Error 84] sizeof object is zero or object is undefined
    test.c 10 error [Info 774] Boolean within 'if' always evaluates to False [Reference: file test.c: line 10]
    test.c 10 error [Info 831] Reference cited in prior message
    test.c 12 error [Error 115] struct/union not defined
    test.c 12 error [Error 40] Undeclared identifier 'x'
    test.c 12 error [Error 63] Expected an lvalue
    test.c 13 error [Error 115] struct/union not defined
    test.c 13 error [Error 40] Undeclared identifier 'next'
    test.c 13 error [Error 63] Expected an lvalue
    test.c 15 error [Error 40] Undeclared identifier 'tptr'
    test.c 17 error [Error 40] Undeclared identifier 'tptr'
    test.c 17 error [Error 63] Expected an lvalue
    test.c 21 error [Error 40] Undeclared identifier 'cptr'
    test.c 21 error [Error 10] Expecting a structure or union
    test.c 21 error [Error 40] Undeclared identifier 'next'
    test.c 23 error [Error 40] Undeclared identifier 'cptr'
    test.c 23 error [Error 40] Undeclared identifier 'cptr'
    test.c 23 error [Error 10] Expecting a structure or union
    test.c 23 error [Error 40] Undeclared identifier 'next'
    test.c 23 error [Error 63] Expected an lvalue
    test.c 26 error [Error 40] Undeclared identifier 'cptr'
    test.c 26 error [Error 10] Expecting a structure or union
    test.c 26 error [Error 40] Undeclared identifier 'next'
    test.c 26 error [Error 63] Expected an lvalue
    test.c 38 error [Error 40] Undeclared identifier 'Bool'
    test.c 38 error [Warning 522] Expected void type, assignment, increment or decrement
    test.c 38 error [Error 10] Expecting ';'
    test.c 39 error [Error 40] Undeclared identifier 'Bool'
    test.c 39 error [Warning 522] Expected void type, assignment, increment or decrement
    test.c 39 error [Error 10] Expecting ';'
    test.c 40 error [Error 40] Undeclared identifier 'ssize_t'
    test.c 40 error [Warning 522] Expected void type, assignment, increment or decrement
    test.c 40 error [Error 10] Expecting ';'
    test.c 41 error [Error 42] Expected a statement
    test.c 42 error [Error 42] Expected a statement
    test.c 43 error [Error 42] Expected a statement
    test.c 44 error [Error 42] Expected a statement
    test.c 45 error [Error 42] Expected a statement
    test.c 46 error [Error 78] Symbol 'size_t' typedef'ed at line 53, file c:\progra~1\borland\bcc55\include\_stddef.h used in expression
    c:\progra~1\borland\bcc55\include\_stddef.h 53 error [Info 830] Location cited in prior message
    test.c 46 error [Warning 522] Expected void type, assignment, increment or decrement
    test.c 46 error [Error 10] Expecting ';'
    test.c 47 error [Error 42] Expected a statement
    test.c 48 error [Error 42] Expected a statement
    test.c 49 error [Error 42] Expected a statement
    test.c 50 error [Error 42] Expected a statement
    test.c 51 error [Error 42] Expected a statement
    test.c 52 error [Error 42] Expected a statement
    test.c 53 error [Error 42] Expected a statement
    test.c 55 error [Error 40] Undeclared identifier 'stillGoing'
    test.c 58 error [Error 40] Undeclared identifier 'readReturn'
    test.c 58 error [Info 718] Symbol 'getline' undeclared, assumed to return int
    test.c 58 error [Info 746] call to function 'getline()' not made in the presence of a prototype
    test.c 58 error [Error 40] Undeclared identifier 'input'
    test.c 58 error [Error 40] Undeclared identifier 'bufLen'
    test.c 58 error [Error 63] Expected an lvalue
    test.c 61 error [Error 40] Undeclared identifier 'x'
    test.c 61 error [Error 63] Expected an lvalue
    test.c 61 error [Error 40] Undeclared identifier 'x'
    test.c 61 error [Error 40] Undeclared identifier 'input'
    test.c 61 error [Warning 574] Signed-unsigned mix with relational
    test.c 61 error [Info 737] Loss of sign in promotion from int to unsigned int
    test.c 61 error [Error 40] Undeclared identifier 'x'
    test.c 61 error [Error 52] Expected an lvalue
    test.c 63 error [Error 40] Undeclared identifier 'input'
    test.c 63 error [Warning 409] Expecting a pointer or array
    test.c 63 error [Error 40] Undeclared identifier 'x'
    test.c 65 error [Error 40] Undeclared identifier 'input'
    test.c 65 error [Warning 409] Expecting a pointer or array
    test.c 65 error [Error 40] Undeclared identifier 'x'
    test.c 65 error [Error 63] Expected an lvalue
    test.c 70 error [Error 40] Undeclared identifier 'readReturn'
    test.c 72 error [Error 40] Undeclared identifier 'stillGoing'
    test.c 72 error [Error 40] Undeclared identifier 'FALSE'
    test.c 72 error [Error 63] Expected an lvalue
    test.c 75 error [Error 40] Undeclared identifier 'input'
    test.c 77 error [Error 40] Undeclared identifier 'stillGoing'
    test.c 77 error [Error 40] Undeclared identifier 'FALSE'
    test.c 77 error [Error 63] Expected an lvalue
    test.c 80 error [Info 718] Symbol 'execute_command' undeclared, assumed to return int
    test.c 80 error [Info 746] call to function 'execute_command()' not made in the presence of a prototype
    test.c 80 error [Error 40] Undeclared identifier 'input'
    test.c 80 error [Error 40] Undeclared identifier 'TRUE'
    test.c 82 error [Error 40] Undeclared identifier 'input'
    test.c 83 error [Error 40] Undeclared identifier 'readReturn'
    test.c 83 error [Error 40] Undeclared identifier 'input'
    test.c 85 error [Error 40] Undeclared identifier 'lastCommand'
    test.c 85 error [Warning 560] argument no. 2 should be a pointer
    test.c 87 error [Error 40] Undeclared identifier 'input'
    test.c 89 error [Error 40] Undeclared identifier 'cptr'
    test.c 89 error [Error 40] Undeclared identifier 'tptr'
    test.c 89 error [Error 63] Expected an lvalue
    test.c 89 error [Error 40] Undeclared identifier 'cptr'
    test.c 89 error [Error 40] Undeclared identifier 'cptr'
    test.c 89 error [Error 40] Undeclared identifier 'cptr'
    test.c 89 error [Error 10] Expecting a structure or union
    test.c 89 error [Error 40] Undeclared identifier 'next'
    test.c 89 error [Error 63] Expected an lvalue
    test.c 91 error [Error 40] Undeclared identifier 'cptr'
    test.c 91 error [Error 10] Expecting a structure or union
    test.c 91 error [Error 40] Undeclared identifier 'x'
    test.c 91 error [Warning 409] Expecting a pointer or array
    test.c 91 error [Error 40] Undeclared identifier 'input'
    test.c 91 error [Warning 409] Expecting a pointer or array
    test.c 93 error [Error 40] Undeclared identifier 'cptr'
    test.c 93 error [Error 10] Expecting a structure or union
    test.c 93 error [Error 40] Undeclared identifier 'x'
    test.c 93 error [Warning 560] argument no. 2 should be a pointer
    test.c 97 error [Info 718] Symbol 'PrintPrompt' undeclared, assumed to return int
    test.c 97 error [Info 746] call to function 'PrintPrompt()' not made in the presence of a prototype
    test.c 104 error [Error 40] Undeclared identifier 'cptr'
    test.c 104 error [Error 40] Undeclared identifier 'tptr'
    test.c 104 error [Error 63] Expected an lvalue
    test.c 104 error [Error 40] Undeclared identifier 'cptr'
    test.c 104 error [Error 40] Undeclared identifier 'cptr'
    test.c 104 error [Error 40] Undeclared identifier 'cptr'
    test.c 104 error [Error 10] Expecting a structure or union
    test.c 104 error [Error 40] Undeclared identifier 'next'
    test.c 104 error [Error 63] Expected an lvalue
    test.c 106 error [Error 40] Undeclared identifier 'cptr'
    test.c 106 error [Error 10] Expecting a structure or union
    test.c 106 error [Error 40] Undeclared identifier 'next'
    test.c 108 error [Error 40] Undeclared identifier 'cptr'
    test.c 108 error [Error 10] Expecting a structure or union
    test.c 108 error [Error 40] Undeclared identifier 'x'
    test.c 108 error [Warning 560] argument no. 2 should be a pointer
    test.c 117 error [Error 40] Undeclared identifier 'input'
    test.c 117 error [Error 63] Expected an lvalue
    test.c 120 error [Error 40] Undeclared identifier 'cptr'
    test.c 120 error [Error 40] Undeclared identifier 'tptr'
    test.c 120 error [Error 63] Expected an lvalue
    test.c 120 error [Error 40] Undeclared identifier 'cptr'
    test.c 120 error [Error 40] Undeclared identifier 'cptr'
    test.c 120 error [Error 40] Undeclared identifier 'cptr'
    test.c 120 error [Error 10] Expecting a structure or union
    test.c 120 error [Error 40] Undeclared identifier 'next'
    test.c 120 error [Error 63] Expected an lvalue
    test.c 122 error [Error 40] Undeclared identifier 'cptr'
    test.c 122 error [Error 10] Expecting a structure or union
    test.c 122 error [Error 40] Undeclared identifier 'x'
    test.c 122 error [Warning 560] argument no. 2 should be a pointer
    test.c 125 error [Error 40] Undeclared identifier 'cptr'
    test.c 125 error [Error 40] Undeclared identifier 'tptr'
    test.c 125 error [Error 63] Expected an lvalue
    test.c 125 error [Error 40] Undeclared identifier 'cptr'
    test.c 128 error [Error 40] Undeclared identifier 'cptr'
    test.c 128 error [Error 10] Expecting a structure or union
    test.c 128 error [Error 40] Undeclared identifier 'next'
    test.c 129 error [Error 40] Undeclared identifier 'cptr'
    test.c 130 error [Error 40] Undeclared identifier 'cptr'
    test.c 130 error [Error 63] Expected an lvalue
    test.c 133 error [Error 40] Undeclared identifier 'input'
    test.c 134 error [Error 40] Undeclared identifier 'lastCommand'
    
    --- Global Wrap-up
    
    test.c 80 error [Warning 526] 'execute_command()' (line 80, file test.c) not defined
    test.c 80 error [Warning 628] no argument information provided for function 'execute_command()' (line 80, file test.c)
    test.c 36 error [Info 714] Symbol 'RunShellLoop(struct prompt *)' (line 36, file test.c) not referenced
    test.c 97 error [Warning 526] 'PrintPrompt()' (line 97, file test.c) not defined
    test.c 97 error [Warning 628] no argument information provided for function 'PrintPrompt()' (line 97, file test.c)
    test.c 58 error [Warning 526] 'getline()' (line 58, file test.c) not defined
    test.c 58 error [Warning 628] no argument information provided for function 'getline()' (line 58, file test.c)
    Outputting to file test.lob
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM