-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash.c
156 lines (133 loc) · 2.71 KB
/
bash.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define true 1
#define false 0
struct command {
char command[10];
char arg[5];
};
void shell_start()
{
char prompt[100];
char *PWD;
strcpy(prompt, "My-Shell: ");
PWD = get_current_dir_name();
strcat(prompt, PWD);
strcat(prompt, " >");
printf("%s ", prompt);
}
void parse_string(char *cmd)
{
char *envp[] = {NULL};
char *command, *args, *p, buff[10] = "/bin/";
command = cmd;
while(isspace(*command))
command++;
p = strchr(command, ' ');
if (p)
{
*p = 0; args = p + 1;
printf("%d \n" , (int)strlen(args));
while (isspace(*args))
args++;
}
else
{
args = NULL;
}
strcat(buff, cmd);
char *argv[] = {buff, args, NULL};
char *n = "&";
if(args)
{
if(p = strstr(args, n))
{
printf("i got it %s\n", p);
n = p-1;
n[0] = 0;
if(fork() == 0)
{
execvp(argv[0], argv);
printf("unknown command \n");
exit(0);
}
else
{
return;
}
}
//here is the way to go if & is not there but args is there
else
{
//do fork and run the execv command
pid_t pid;
int child_status;
pid = fork();
if(pid == 0)
{
execvp(argv[0], argv);
printf("unknown command \n");
exit(0);
}
if(pid == -1)
perror("error");
else {
/* This is run by the parent. Wait for the child
to terminate. */
pid_t tpid = wait(&child_status);
do
{
if(tpid !=pid) break;
} while(tpid != pid);
// return child_status;
}
}
}
//if args is not present then for and execute and wait for parent
else
{
pid_t pid;
int child_status;
pid = fork();
if(pid == 0)
{
execvp(argv[0], argv);
printf("unknown command \n");
exit(0);
}
if(pid == -1)
perror("error");
else {
/* This is run by the parent. Wait for the child
to terminate. */
pid_t tpid = wait(&child_status);
do
{
if(tpid !=pid) break;
} while(tpid != pid);
// return child_status;
}
}
}
int main()
{
char cmd[18];
time_t result = time(NULL);
printf("Hello you are logged in at %s from This terminal \n Autor : Rahul Jain\n", asctime(localtime(&result)));
while(1)
{
shell_start();
fgets(cmd,18, stdin);
cmd[strlen(cmd) - 1] = 0;
if(strcmp(cmd, "quit") == 0 || strcmp(cmd, "exit") == 0)
{
printf("Bye.. \n");
break;
}
parse_string(cmd);
}
return 0;
}