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.