-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinner.c
executable file
·183 lines (171 loc) · 4.78 KB
/
inner.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
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Filename: inner.c
* Description: Processing about inner CMD of myshell.
* Author: Xu Yuhao
* Created: 2014.7.30
*/
#include "myshell.h"
int inner_pro()
{
int fd2,fd_out;
/*pro with out_redirect > >> */
if(NUMBER_ARGS > 2)
{
if(strcmp(ARGS[NUMBER_ARGS-2],">")==0)
{
OUT_REDIRECT = 1;
OUT_FILE = ARGS[NUMBER_ARGS-1];
fd_out = open(OUT_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
fd2=dup(STDOUT_FILENO);
dup2(fd_out, STDOUT_FILENO); //redirect stdout to out file
close(fd_out);
free(ARGS[NUMBER_ARGS-1]);
free(ARGS[NUMBER_ARGS-2]);
NUMBER_ARGS -= 2;
}
else if(strcmp(ARGS[NUMBER_ARGS-2],">>")==0)
{
OUT_REDIRECT_APPEND = 1;
OUT_FILE = ARGS[NUMBER_ARGS-1];
fd_out = open(OUT_FILE, O_WRONLY|O_APPEND, 0666);
fd2=dup(STDOUT_FILENO);
dup2(fd_out, STDOUT_FILENO); //redirect stdout to out file
close(fd_out);
free(ARGS[NUMBER_ARGS-1]);
free(ARGS[NUMBER_ARGS-2]);
NUMBER_ARGS -= 2;
}
}
/*end of out_redirect*/
/*pro these inner CMD*/
if(strcmp(ARGS[0],"cd") == 0)
{
do_cd();
}
else if(strcmp(ARGS[0],"clr") == 0)
{
system("clear");
}
else if(strcmp(ARGS[0],"dir") == 0)
{
char *dir_path = NULL;
pwd = getpwuid(getuid());
char *path = (char *)malloc(sizeof(char)*50);
getcwd(path,50);
//If the path has "~",replace "~" by the real path.
if(ARGS[1][0] == '~')
{
dir_path = malloc(strlen(pwd->pw_dir)+strlen(ARGS[1]));
//'~' makes length 1 more,but instead of '\0'
if(dir_path == NULL) printf("dir:malloc failed.\n");
strcpy(dir_path,pwd->pw_dir);
strncpy(dir_path+strlen(pwd->pw_dir),ARGS[1]+1,strlen(ARGS[1]));
}
else //The path without "~"
{
dir_path = malloc(strlen(ARGS[1]+1));
if(dir_path == NULL) printf("dir:malloc failed.\n");
strcpy(dir_path,ARGS[1]);
}
do_dir(dir_path,0);/*recuison list files in directory*/
chdir(path);
free(dir_path);
free(path);
}
else if(strcmp(ARGS[0],"environ") == 0)
{
system("echo $PATH");
}
else if(strcmp(ARGS[0],"echo") == 0)
{
do_echo(NUMBER_ARGS);
}
else if(strcmp(ARGS[0],"help") == 0)
{
system("more readme.txt");
}
else if(strcmp(ARGS[0],"quit") == 0)
{
return 0; // should_run will be 0 and the program will quit
}
/*end of inner_CMD*/
/*return the redirect output to the standard stdout*/
if(OUT_REDIRECT || OUT_REDIRECT_APPEND)
{
dup2(fd2, STDOUT_FILENO);
close(fd2);
}
return 1;
}
void do_cd()
{
char *cd_path = NULL;
pwd = getpwuid(getuid());
if(NUMBER_ARGS == 1)
/*if the CMD is cd*/
{
cd_path = malloc(strlen(pwd->pw_dir)+1);
strcpy(cd_path,pwd->pw_dir);
}
else
{
/*If the path has "~",replace "~" by the real path.*/
if(ARGS[1][0] == '~')
{
cd_path = malloc(strlen(pwd->pw_dir)+strlen(ARGS[1]));
//'~' makes length 1 more,but instead of '\0'
if(cd_path == NULL) printf("cd:malloc failed.\n");
strcpy(cd_path,pwd->pw_dir);
strncpy(cd_path+strlen(pwd->pw_dir),ARGS[1]+1,strlen(ARGS[1]));
}
else //The path without "~"
{
cd_path = malloc(strlen(ARGS[1]+1));
if(cd_path == NULL) printf("cd:malloc failed.\n");
strcpy(cd_path,ARGS[1]);
}
}
if(chdir(cd_path)!= 0)
printf("myshell: cd: %s:%s\n",cd_path,strerror(errno));
free(cd_path);
}
void do_dir(char* dir,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
//test whether the directry exsits
if((dp=opendir(dir)) == NULL)
{
fprintf(stderr,"cannot open directory:%s\n",dir);
return;
}
chdir(dir); //enter the directory
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf); //get the stat struct
if(S_ISDIR(statbuf.st_mode))
{
/*test if this struct is a directory too*/
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 )
continue; /*test if it is "." or ".."*/
printf("%*s%s\n",depth," ",entry->d_name);
/*recursion of child directory*/
do_dir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth," ",entry->d_name);
}
chdir("..");
closedir(dp);
}
void do_echo()
{
int i;
for(i = 1; i < NUMBER_ARGS; i ++)
{
if(ARGS[i] == NULL)
break;
printf("%s ",ARGS[i]);
}
printf("\n");
}