-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
108 lines (94 loc) · 2.92 KB
/
Copy pathinput.c
File metadata and controls
108 lines (94 loc) · 2.92 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "headers.h"
bool execute_command(char *command_given) //command array has space seperated commands stored
{
char* command=redirection(command_given);
if (piped_command(command) == 1)
return pipe_call(command);
char *token;
token = strtok(command, " \t");
char **flag;
flag = malloc(20 * (sizeof(char *))); //allocating memory for the pointers that store each command line argument
int num_flags = 0;
while (token != NULL)
{
if (num_flags >= 20)
{
printf("bash: too many command line arguments\n");
return false;
}
flag[num_flags] = (char *)malloc(sizeof(char) * MAX);
strcpy(flag[num_flags], token); //copying each argument into array
num_flags++;
token = strtok(NULL, " ");
}
flag[num_flags] = NULL;
if (num_flags == 0)
return true;
else if (strcmp(flag[0], "exit") == 0)
exit(0);
else if (strcmp(flag[0], "cd") == 0)
cd(num_flags, flag);
else if (strcmp(flag[0], "pwd") == 0)
pwd();
else if (strcmp(flag[0], "echo") == 0)
echo(num_flags, flag);
else if (strcmp(flag[0], "ls") == 0)
ls(num_flags, flag);
else if (strcmp(flag[0], "repeat") == 0)
repeat(num_flags, flag);
else if (strcmp(flag[0], "history") == 0)
history(num_flags, flag);
else if (strcmp(flag[0], "pinfo") == 0)
pinfo(num_flags, flag);
else if (strcmp(flag[0], "jobs") == 0)
jobs(num_flags, flag);
else if (strcmp(flag[0], "sig") == 0)
job_kill(num_flags, flag);
else if (strcmp(flag[0], "fg") == 0)
fg(num_flags, flag);
else if (strcmp(flag[0], "bg") == 0)
bg(num_flags, flag);
else if (strcmp(flag[0], "replay") == 0)
replay(num_flags, flag);
else if (strcmp(flag[0], "") == 0)
return false;
else
child_parent(num_flags, flag); //rest of the commands implemented using execvp()
free(token);
for (int i = 0; i < num_flags; i++)
free(flag[i]);
free(flag);
return true;
}
char **get_commands(char *buffer) //returns a double pointer that points to an array of commands
{
int num = 1;
for (ll i = 0; i < strlen(buffer); i++)
{
if (buffer[i] == ';')
num++;
else if (buffer[i] == '\t' || buffer[i] == '\n')
buffer[i] = ' ';
}
char **command = malloc(num * sizeof(char *));
char *token;
token = strtok(buffer, ";");
int i = 0;
while (token != NULL)
{
command[i] = (char *)malloc(sizeof(char) * 1024);
strcpy(command[i], token);
i++;
token = strtok(NULL, ";");
}
free(token);
return command;
}
int commands_num(char *buffer) //counting the number of commands sepreated by ; given
{
int num = 1;
for (ll i = 0; i < strlen(buffer); i++)
if (buffer[i] == ';')
num++;
return num;
}