Skip to content

Commit 0a22c81

Browse files
author
hyqooo
committed
Empty input is doesn't crash program
1 parent 80ec4ee commit 0a22c81

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

shell.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,28 @@ int cmd_cd(struct tokens *tokens){
109109
So what have I found?
110110
FIXME: program corrupts it own files
111111
FIXME: '>' creates executable files
112-
FIXME: '<' not even works
113112
FIXME: core fault
113+
FIXME: '<' after using stops work of the shell
114114
*/
115115
int shell_exec(struct tokens *tokens){
116116
/* path processed by detpath and then we could use it */
117117
char *path = detpath(tokens_get_token(tokens, 0));
118118
char **args = args_proc(path, tokens);
119+
120+
if (!path || !args) return -1;
119121

122+
int saved_stdout = dup(1);
123+
int saved_stdin = dup(0);
120124
pid_t cpid;
121125
int status;
122126
cpid = fork();
123127
/* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
124128
if (cpid > 0) {
125129
wait(&status);
130+
fflush(stdout);
131+
fflush(stdin);
132+
dup2(saved_stdout, 1);
133+
dup2(saved_stdin, 0);
126134
} else if (cpid == 0){
127135
/* executes program according to path and given arguments */
128136
execv(path, args);
@@ -180,6 +188,8 @@ int redirection(char *path, int stream){
180188
5. If it absent we lookup on the PATH environment variables.
181189
*/
182190
char* detpath(char *ppath){
191+
if (ppath == NULL)
192+
return NULL;
183193
/* ppath is absolute path */
184194
if (*ppath == '/')
185195
return ppath;

0 commit comments

Comments
 (0)