-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin.c
166 lines (155 loc) · 3.2 KB
/
builtin.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
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "main.h"
#include "builtin.h"
#include "parser.h"
#include "lista.h"
int isBuiltIn(char * cmd)
{
char * commands[] = {"cd", "history", "pwd", "exit", "jobs", "bg", "fg", "kill", "echo"};
int i;
for (i = 0; i < 9; i++)
if (strcmp(commands[i], cmd) == 0) return i;
return -1;
}
void callBuiltIn(int cmd_id, char * arg)
{
switch(cmd_id)
{
case 0:
cd();
break;
case 1:
print_history();
break;
case 2:
pwd();
break;
case 3:
exit(0);
break;
case 4:
jobs();
break;
case 5:
bg(arg);
break;
case 6:
fg(arg);
break;
case 7:
kill_cmd(arg);
break;
default:
break;
}
}
void add_history(char * cmd)
{
struct node *tmp = history->next;
struct node *prev = history;
if(history->cmd == NULL)
{
history->cmd = malloc(strlen(cmd) + 1);
strcpy(history->cmd, cmd);
}
else
{
while (tmp != NULL)
{
prev = tmp;
tmp = tmp->next;
}
tmp = malloc(sizeof(struct node));
tmp->cmd = malloc(strlen(cmd));
tmp->next = NULL;
strcpy(tmp->cmd, cmd);
prev->next = tmp;
}
}
void print_history()
{
struct node *tmp = history;
int i = 1;
printf("%d %s\n", i, tmp->cmd);
while(tmp->next != NULL)
{
i++;
tmp = tmp->next;
printf("%d %s\n", i, tmp->cmd);
}
}
void cd()
{
int error;
if(parsed[1] == NULL) error = chdir(userdir);
else error = chdir(parsed[1]);
if (error == -1) printf("bash: cd: %s: %s\n", parsed[1],strerror(errno));
}
void pwd()
{
char path[256];
getcwd(path, 256);
printf("%s\n", path);
}
void jobs()
{
NODE *aux = childs->first;
ITEM * item;
while (aux != NULL)
{
item = aux->item;
printf("[%d] %s %s\n",item->pid, item->status, item->command);
aux = aux->next;
}
}
void bg(char * arg)
{
if(!ListIsEmpty(childs)) {
char * end;
int n;
pid_t pid;
if(arg == NULL)
pid = ListLastStoppedToBg(childs);
else {
n = strtol(arg, &end, 10);
printf("n: %d", n);
pid = ListToBg(childs, n);
}
printf("pid escolhido: %d", pid);
kill(pid, SIGCONT);
}
}
void fg(char * arg)
{
if(!ListIsEmpty(childs)) {
char * end;
int n;
pid_t pid, pidfg;
int status;
if(arg == NULL)
pid = ListLastToFg(childs);
else {
n = strtol(arg, &end, 10);
pid = ListToFg(childs, n);
}
child_handler_lock = 1;
pidfg = waitpid(pid, &status, WUNTRACED);
child_handler_lock = 0;
if ((pidfg >= 0) && (WIFSTOPPED(status) == 0)) ListRemoveByPid(childs, childPid);
}
}
void kill_cmd(char * arg)
{
if(!ListIsEmpty(childs)) {
char * end;
int n;
NODE * aux = childs->last;
if(arg == NULL) n = aux->item->pid;
else n = strtol(arg, &end, 10);
kill(n, SIGTERM);
}
}