Skip to content

Commit c3da66d

Browse files
author
hyqooo
committed
First version of redirection
1 parent f617076 commit c3da66d

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

shell.c

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ int cmd_exit(struct tokens *tokens);
4545
int cmd_help(struct tokens *tokens);
4646
int cmd_pwd(struct tokens *tokens);
4747
int cmd_cd(struct tokens *tokens);
48+
4849
char *procpathenv(char *, char *);
4950
char *detpath(char *);
51+
char **args_proc(char *, struct tokens *);
52+
53+
int redirection(char *, int);
54+
5055
const int MAX_PATH_SIZE = 128;
5156

5257
/* Built-in command functions take token array (see parse.h) and return int */
@@ -100,31 +105,13 @@ int cmd_cd(struct tokens *tokens){
100105
}
101106

102107
/* Execute program */
108+
/*
109+
110+
*/
103111
int shell_exec(struct tokens *tokens){
104112
/* path processed by detpath and then we could use it */
105113
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);
128115

129116
pid_t cpid;
130117
int status;
@@ -135,7 +122,6 @@ int shell_exec(struct tokens *tokens){
135122
} else if (cpid == 0){
136123
/* executes program according to path and given arguments */
137124
execv(path, args);
138-
// close(filedesc);
139125
exit(0);
140126
} else {
141127
/* cannot fork current process */
@@ -144,6 +130,46 @@ int shell_exec(struct tokens *tokens){
144130
return 1;
145131
}
146132

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+
147173
/*
148174
Shell can use both absolute path and relative path.
149175
1. Determine whether is there the symbol '/'.

0 commit comments

Comments
 (0)