Search code examples
cexitterminate

C programming return 0 does not terminate the program


I wrote this shell for my operating systems course, and for some reason my main function doesn't terminate when I type "exit", its supposed to break from the while loop and then return 0 to main, which should end the program, but instead it just keeps going.

In fact, it will tell me its "a bout to exit" and then it will go back into the while loop where it keeps running as usual.

Anybody see the problem? Thank you for all your help

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define LENGTH 1000
#define MAXCMD 11
void init(char *temp);
void clean(char **orderedIds);

int main (int argc, char *argv[], char *envp[])
{ 
    char temp[LENGTH];
    char * tok;
    char c = '\0';
    int *ret_status; 
    pid_t pid; 
    printf("\n$");
    int i, j = 0;   
    while(c !=EOF) 
    { 
        //while not ^D // Source: LinuxGazzette Ramankutty
        c = getchar();    
        if(c=='\n')
        { //enter command
            char **orderedIds = malloc(MAXCMD * sizeof(char*)); 
            for (i=0; i<MAXCMD; i++)
            {
                orderedIds[i] = malloc(MAXCMD * sizeof(char*)); 
            }
            int k=0; 
            tok = strtok(temp, " "); 
            while (tok !=NULL)
            { 
                strcpy(orderedIds[k], tok);
                k++;
                tok = strtok (NULL, " ");
            } 
            orderedIds[k] = NULL; //END with NULL 
            init(temp); //initialize the array 
            pid = fork(); 
            if (pid != 0)
            {
                //printf("Waiting for child (%d)\n", pid);
                pid = wait(ret_status);
            } 
            else 
            {
                if (strcmp(orderedIds[0],"exit")==0)
                { //check for exit
                    printf("Thank you, now exiting\n");
                    break;
                } 
                if (execvp(orderedIds[0], orderedIds))
                {
                    printf("%s\n", orderedIds[0]);
                    puts(strerror(errno));
                    exit(127);
                }
            }
            clean(orderedIds);
            printf("%s", &c);
            printf("$ ");
        } 
        else 
        {
            strncat(temp, &c, 1);                                      
        }
    }                                                                          
    printf("\n I'm a bout to exit!");                                          
    return 0;                                                                  
    exit(0);                                                                   
}                                                                                  

void init(char *temp)
{
    //Function to initialize/rest=et the array                   
    int i;                                                                     
    for(i=0; i<LENGTH; i++)
    {                                                   
        temp[i] = 0;                                                       
    }                                                                          
}

void clean(char **orderedIds)
{
    //garbage collection                                 
    int i;                                                                     
    for(i=0; i<MAXCMD; i++)
    {                                                   
        free(orderedIds[i]);                                               
    }                                                                          
    free(orderedIds);                                                          
}                                                                                  

Solution

  • You do a fork(), and it's in the child process where you are checking for the command "exit". The child process does indeed exit, but the parent process keeps running. What you should do is check for internal commands (like "exit") before you call fork().