I want to make a program that imitates the use of cd in UNIX. Obviously the best option is chdir
. However when the user types cd /home
I get Segmentation fault
. When I use gdb, it prints:
Program received signal SIGSEGV, Segmentation fault. __rawmemchr_avx2 () at ../sysdeps/x86_64/multiarch/memchr-avx2.S:61
61 ../sysdeps/x86_64/multiarch/memchr-avx2.S: The file of directory does not exist.
The code looks like this:
void cd(char buff[]){
if(chdir(buff)==-1){
printf("Problem with directory\n");
}
}
int main(int argc, char *argv[]){
char* token;
char buffer[256];
printf("Welcome user\n");
sscanf("%s",buffer);
token=strtok(buff," ");
if(strcmp(token,"cd")==0||strcmp(token,"cd\n")==0){
cd(strtok(NULL," ");
}
return 0;
}
Than you for your time.