forked from Tachone/TachMinishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
144 lines (129 loc) · 2.15 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
#include "builtin.h"
#include "parse.h"
#include "externs.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
typedef void (*CMD_HANDLER)(void);
typedef struct builtin_cmd
{
char *name;
CMD_HANDLER handler;
} BUILTIN_CMD;
void do_exit(void);
void do_cd(void);
void do_type(void);
void do_about(void);
void do_jobs(void);
void do_kill(void);
BUILTIN_CMD builtins[] =
{
{"exit", do_exit},
{"cd", do_cd},
{"type", do_type},
{"about",do_about},
{"jobs",do_jobs},
{"kill",do_kill},
{NULL, NULL}
};
/*
* 内部命令解析
* 返回1表示为内部命令,0表示不是内部命令
*/
int builtin(void)
{
/*
if (check("exit"))
do_exit();
else if (check("cd"))
do_cd();
else
return 0;
return 1;
*/
int i = 0;
int found = 0;
while (builtins[i].name != NULL)
{
if (check(builtins[i].name))
{
builtins[i].handler();
found = 1;
break;
}
i++;
}
return found;
}
void do_exit(void)
{
int Pgnum=0;
NODE* tmp=head->next;
while(tmp!=NULL)
{
Pgnum++;
tmp=tmp->next;
}
if(Pgnum!=0)
{
printf("There are programs in the background,are you sure to exit?y/N\n");
char c= getchar();
if(c=='N')
return ;
else
goto loop;
}
loop:
printf("exit\n");
exit(EXIT_SUCCESS);
}
void do_cd(void)
{
get_command(0);
int fd;
fd=open(*(cmd[0].args),O_RDONLY);
fchdir(fd);
close(fd);
}
void do_about(void)
{
printf("minishell created by Tach!\n");
}
void do_kill(void)
{
get_command(0);
int num=atoi(cmd[0].args[1]);
signal(SIGQUIT,SIG_DFL);
kill(num,SIGQUIT);
signal(SIGQUIT,SIG_IGN);
NODE *bng=head->next;
NODE *pre=head;
while(bng!=NULL)
{
if(bng->npid==num)
{
NODE* nxt=bng->next;
pre->next=nxt;
break;
}
pre=bng;
bng=bng->next;
}
}
void do_jobs(void)
{
NODE *prout=head->next;
while(prout!=NULL)
{
printf("%d %s\n",prout->npid,prout->backcn);
prout=prout->next;
}
}
void do_type(void)
{
printf("do_type ... \n");
}