-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
115 lines (95 loc) · 1.86 KB
/
Copy pathmain.c
File metadata and controls
115 lines (95 loc) · 1.86 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
109
110
111
112
113
114
#include <stdio.h>
#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#define MAXSIZE_CMD 20
int help();
int quit();
int do_ls();
int do_zy();
char cmd[MAXSIZE_CMD];
struct my_command cmd_set[] = {
{0, NULL, "help", help}, /* help display info about this shell*/
{0, NULL, "quit", quit}, /* help display info about this shell*/
{0, NULL, "ls", do_ls},
{0, NULL, "zy", do_zy},
{0, NULL, NULL, NULL}
};
int do_zy(void) {
printf("Hello, zy!!!\n");
return 0;
}
int do_ls(void) {
int fd;
DIR *dir;
struct dirent *stru_dir;
if ((fd = open("/", O_RDONLY)) < 0) {
perror("opening a dir ");
}
if ( (dir = fdopendir(fd)) == NULL) {
perror("open a dir by fd");
}
while((stru_dir = readdir(dir)) != NULL ) {
if (strcmp(stru_dir->d_name, ".") != 0
&& strcmp(stru_dir->d_name, "..") != 0)
{
printf("%s\t", stru_dir->d_name);
}
}
printf("\n");
return 0;
}
int quit() {
exit(-1);
}
int help() {
printf("This is a program be used for teach \n\
You can type some command example: \n\
cd , ls , quit and so on \n");
return 0;
}
int findcmd(char *user_cmd)
{
struct my_command *p = cmd_set;
int ret;
while(p->name) {
if (!strncmp(p->name, user_cmd, strlen(cmd))){
ret=p->cmd(0, NULL);
return ret;
}
p++;
}
return 127;
}
void main_loop() {
int count = 0;
char c;
int ret;
printf("kingshell$ ");
fflush(stdout);
while ((c=getchar())!='\n' && count < MAXSIZE_CMD) {
cmd[count++] = c;
}
if ( count > MAXSIZE_CMD) {
perror("command is too long");
exit(-1);
}
cmd[count] = '\0'; //end of string(cmd)
ret=findcmd(cmd);
if (ret==127) {
system(cmd);
}
}
int main(int argc,char **argv) {
while(1) {
main_loop();
}
/*
* option
*/
// getopt();
}