-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
84 lines (82 loc) · 1.4 KB
/
Copy pathmyshell.c
File metadata and controls
84 lines (82 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
#define clear() printf("\033[H\033[J");
void initshell(){
clear();
printf(" _________\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|________ \n");
printf(" |\n");
printf(" |\n");
printf(" | \n");
printf("_________| A S H \n");
printf("\n\nWelcome %s\n\n",getenv("USER"));
sleep(2);
}
void printcurr(){
char a[1000];
getcwd(a,sizeof(a));
printf("%s\n",a);
}
int input(char* a){
char *b;
b=readline("$ ");
add_history(b);
if(strlen(b)!=0){
strcpy(a,b);
return 0;
}
free(b);
return 1;
}
void run(char *a,char **b){
if(strcmp(b[0],"cd")==0){
if(b[1]==NULL)
chdir("/home/sarthak");
else
chdir(b[1]);
}
else if(strcmp(b[0],"pwd")==0){
printcurr();
}
else if(strcmp(b[0],"exit")==0)
exit(0);
else{
pid_t pid=fork();
if(pid<0) printf("Forking failed\n");
else if(pid==0){
if(execvp(b[0],b)==-1)
printf("Not a valid command\n");
exit(0);
}
else{
wait(NULL);
}
}
}
void parse(char *a,char **b){
int i;
for(i=0;i<100;i++){
b[i]=strsep(&a," ");
if(b[i]==NULL) break;
}
}
int main(){
char inputs[100],*parsed[100];
initshell();
while(1){
input(inputs);
parse(inputs,parsed);
run(inputs,parsed);
}
free(parsed);
return 0;
}