1

I am having issues with my custom shell cd command I created. Right now I am testing the base case where cd is the only argument, when that is the case its suppose to change the default directory to the /home directory. It makes it all the way to the error " ERROR Home" so I now my command cd is taking place but its not changing directory.


if(strcmp(command[0], "cd") == 0)
{
   int check;
   if(command[1] == NULL)
   {            
        check = chdir("/home");
        if(check != 0)
        {
           printf(" ERROR Home \n ");
        }
            
    }
    else
    {
        check = chdir(command[1]);
        if(check != 0)
        {
            printf("error command\n");
        } 
    } 
 } // end if

Am I overlooking something that would cause this runtime error?


Here is an updated screen cap what happening when I try the different ways to input the commands, I added a print line right before it attempts to execute to see whats inside the command[0]. My quit command works just not cd enter image description here

1

chdir() is telling you what the error is in its return status, but you're not putting that in the error message. Read up on the perror(3) library call on how to display friendly error messages.

1
  • edit: see next respons for code block – MegalomaniacGT Apr 20 '20 at 15:57
0

I figured it out, I had a double if statement. changed the second if to an else if and it runs! thank you for your efforts

Not the answer you're looking for? Browse other questions tagged or ask your own question.