@@ -45,8 +45,13 @@ int cmd_exit(struct tokens *tokens);
45
45
int cmd_help (struct tokens * tokens );
46
46
int cmd_pwd (struct tokens * tokens );
47
47
int cmd_cd (struct tokens * tokens );
48
+
48
49
char * procpathenv (char * , char * );
49
50
char * detpath (char * );
51
+ char * * args_proc (char * , struct tokens * );
52
+
53
+ int redirection (char * , int );
54
+
50
55
const int MAX_PATH_SIZE = 128 ;
51
56
52
57
/* Built-in command functions take token array (see parse.h) and return int */
@@ -100,31 +105,13 @@ int cmd_cd(struct tokens *tokens){
100
105
}
101
106
102
107
/* Execute program */
108
+ /*
109
+
110
+ */
103
111
int shell_exec (struct tokens * tokens ){
104
112
/* path processed by detpath and then we could use it */
105
113
char * path = detpath (tokens_get_token (tokens , 0 ));
106
- char * * args = (char * * )malloc (tokens -> tokens_length * sizeof (char * ));
107
- int i , j ;
108
- args [0 ] = path ;
109
- for (i = 1 , j = 1 ; j < tokens -> tokens_length ; j ++ ){
110
- if (!strcmp (tokens_get_token (tokens , j ), "<" ) || !strcmp (tokens_get_token (tokens , j ), ">" )) continue ;
111
- args [i ] = tokens_get_token (tokens , j );
112
- i ++ ;
113
- }
114
- args [i ] = NULL ;
115
-
116
- int filedesc ;
117
- if (tokens -> tokens_length > 2 ){
118
- filedesc = open (args [1 ], O_CREAT |O_TRUNC |O_WRONLY );
119
- if (filedesc == -1 )
120
- return -1 ;
121
- if (!strcmp (args [1 ], "<" )){
122
-
123
- }else if (!strcmp (args [1 ], ">" )){
124
- if (dup2 (filedesc , 1 ) < 0 )
125
- return -1 ;
126
- }
127
- }
114
+ char * * args = args_proc (path , tokens );
128
115
129
116
pid_t cpid ;
130
117
int status ;
@@ -135,7 +122,6 @@ int shell_exec(struct tokens *tokens){
135
122
} else if (cpid == 0 ){
136
123
/* executes program according to path and given arguments */
137
124
execv (path , args );
138
- // close(filedesc);
139
125
exit (0 );
140
126
} else {
141
127
/* cannot fork current process */
@@ -144,6 +130,46 @@ int shell_exec(struct tokens *tokens){
144
130
return 1 ;
145
131
}
146
132
133
+ /* processes arguments to determine what is this redirection or merely arguments passing */
134
+ char * * args_proc (char * path , struct tokens * tokens ){
135
+ int i ;
136
+ char * * args = (char * * )malloc ((tokens -> tokens_length + 1 ) * sizeof (char * ));
137
+ if (!args ) exit (1 );
138
+
139
+ args [0 ] = path ;
140
+ for (i = 1 ; i < tokens -> tokens_length ; i ++ )
141
+ args [i ] = tokens_get_token (tokens , i );
142
+ args [i ] = NULL ;
143
+
144
+ /* is it redirection? */
145
+ if (!strcmp (args [1 ], ">" ) || !strcmp (args [1 ], "<" )){
146
+ if (!strcmp (args [1 ], ">" )){
147
+ /* this is not really good solution, because it doesn't allow a pipe */
148
+ redirection (args [2 ], 1 );
149
+ }else {
150
+ redirection (args [2 ], 0 );
151
+ }
152
+ args [1 ] = NULL ;
153
+ }else {
154
+ /* arguments passing */
155
+ args [tokens -> tokens_length - 1 ] = NULL ;
156
+ }
157
+
158
+ return args ;
159
+ }
160
+
161
+ /* redirect stdin or stdout depends on stream */
162
+ int redirection (char * path , int stream ){
163
+ int fd ;
164
+
165
+ fd = open (path , O_CREAT |O_TRUNC |O_WRONLY );
166
+ if (fd == -1 ) return -1 ;
167
+
168
+ if (dup2 (fd , stream ) < 0 ) return -1 ;
169
+
170
+ return 0 ;
171
+ }
172
+
147
173
/*
148
174
Shell can use both absolute path and relative path.
149
175
1. Determine whether is there the symbol '/'.
0 commit comments