When using cd it will change directory to home if not specifying directory

This commit is contained in:
hellisabove
2022-04-25 16:51:36 +03:00
parent 7dbd28ff8d
commit 6c35c9f183
3 changed files with 314 additions and 307 deletions
+17 -10
View File
@@ -1,12 +1,13 @@
// C Program to design a shell in Linux // C Program to design a shell in Linux
#include<stdio.h> #include <stdio.h>
#include<string.h> #include <string.h>
#include<stdlib.h> #include <stdlib.h>
#include<unistd.h> #include <unistd.h>
#include<sys/types.h> #include <sys/types.h>
#include<sys/wait.h> #include <sys/wait.h>
#include<readline/readline.h> #include <readline/readline.h>
#include<readline/history.h> #include <readline/history.h>
#include <pwd.h>
#define MAXCOM 1000 // max number of letters to be supported #define MAXCOM 1000 // max number of letters to be supported
#define MAXLIST 100 // max number of commands to be supported #define MAXLIST 100 // max number of commands to be supported
@@ -126,8 +127,14 @@ int ownCmdHandler(char** parsed)
case 1: case 1:
exit(0); exit(0);
case 2: case 2:
chdir(parsed[1]); if(parsed[1]){
return 1; chdir(parsed[1]);
return 1;
} else {
char *home = getenv("HOME");
chdir(home);
return 1;
}
default: default:
break; break;
} }