0

Im having a problem with cd command. Everything else seems to work, like compiling and running a program, and ls. I run ls, ls -1 and works just fine. When I run cd or cd Desktop nothing happens.

I'm creating a fork and then executing a process. I exit with exit or pressing CTRL+D.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#define SIZE 255

void printPrompt(){

    printf("\nuser@shell > ");   //print prompt

}

void readPrompt(char string[],int *check){   //read prompt line from user

    if( (scanf(" %[^\n]",string)==EOF) ){
        //printf("\nTEST\n");
        *check=0;
    }
    printf("\n");
}

void RunSimple(char line[]){    //creating a fork and running a program with exe fuction. 

    char **args=malloc(8 * sizeof(char *));
    char *parsed;
    int i=0,pid;

    parsed = strtok(line," ");

    while (parsed != NULL) {
        args[i] = parsed;
        i++;

        parsed = strtok(NULL," ");
    }

    pid = fork();

    if(pid == 0){
        execvp (args[0],args);
    }

    waitpid(pid,NULL,0);
}

int main(int argc, char **argv){

    char line[SIZE];
    int check=1;

    while(1){

        printPrompt();
        readPrompt(line,&check);

        if( (!strcmp(line,"exit")) || (check==0) ){
            break;
        }

        RunSimple(line);
    }
}

Thank you.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    You probably find that any commands whose sole purpose is to move around files or output things to `stdout` or `stderr` will work fine, but ones which change the state of the environment won't. A forked process gets a copy of the parent's environment, and from then on can't affect the parent's environment - see https://stackoverflow.com/questions/9360679/can-the-child-process-affect-parent-process-environment, for example. – Jake Lishman Dec 04 '19 at 19:17
  • Your parent process must perform the `cd`. You cannot `cd` in the child shell because it does not change the path in the parent shell. See, for example, [Why doesn't the cd command work in my shell program?](https://stackoverflow.com/q/3740504/608639), [How can I implement cd command in my own shell in C?](https://stackoverflow.com/q/43749628/608639), [How to implement your own cd command in your own shell](https://stackoverflow.com/q/26928907/608639), [Why doesn't “cd” work in a shell script?](https://stackoverflow.com/q/255414/608639), etc. – jww Dec 04 '19 at 19:25
  • Also im not permited from using fuctions that create a new shell for running commands (like /bin/sh) and the fuction system(). So im guesing running cd commands is out of option. – Stelios Spector Dec 04 '19 at 20:14

1 Answers1

1

cd must be implemented as a builtin as the current working directory is a property of the process. Thus you cd command has to change the state of the shell something that a subprocess can't do. There are other things like the handling of environment variables which must be handled so.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143