Skip to content

Commit 0fbcba6

Browse files
committed
Make sh.c compile using a normal libc
1 parent 2aa02a3 commit 0fbcba6

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sh

sh.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Shell.
22

3-
#include "types.h"
4-
#include "user.h"
5-
#include "fcntl.h"
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <unistd.h>
7+
#include <fcntl.h>
8+
#include <sys/wait.h>
69

710
// Parsed command representation
811
#define EXEC 1
@@ -65,7 +68,7 @@ runcmd(struct cmd *cmd)
6568
struct redircmd *rcmd;
6669

6770
if(cmd == 0)
68-
exit();
71+
exit(1);
6972

7073
switch(cmd->type){
7174
default:
@@ -74,17 +77,17 @@ runcmd(struct cmd *cmd)
7477
case EXEC:
7578
ecmd = (struct execcmd*)cmd;
7679
if(ecmd->argv[0] == 0)
77-
exit();
78-
exec(ecmd->argv[0], ecmd->argv);
79-
printf(2, "exec %s failed\n", ecmd->argv[0]);
80+
exit(1);
81+
execv(ecmd->argv[0], ecmd->argv);
82+
dprintf(2, "exec %s failed\n", ecmd->argv[0]);
8083
break;
8184

8285
case REDIR:
8386
rcmd = (struct redircmd*)cmd;
8487
close(rcmd->fd);
8588
if(open(rcmd->file, rcmd->mode) < 0){
86-
printf(2, "open %s failed\n", rcmd->file);
87-
exit();
89+
dprintf(2, "open %s failed\n", rcmd->file);
90+
exit(1);
8891
}
8992
runcmd(rcmd->cmd);
9093
break;
@@ -93,7 +96,7 @@ runcmd(struct cmd *cmd)
9396
lcmd = (struct listcmd*)cmd;
9497
if(fork1() == 0)
9598
runcmd(lcmd->left);
96-
wait();
99+
wait(NULL);
97100
runcmd(lcmd->right);
98101
break;
99102

@@ -117,8 +120,8 @@ runcmd(struct cmd *cmd)
117120
}
118121
close(p[0]);
119122
close(p[1]);
120-
wait();
121-
wait();
123+
wait(NULL);
124+
wait(NULL);
122125
break;
123126

124127
case BACK:
@@ -127,15 +130,15 @@ runcmd(struct cmd *cmd)
127130
runcmd(bcmd->cmd);
128131
break;
129132
}
130-
exit();
133+
exit(1);
131134
}
132135

133136
int
134137
getcmd(char *buf, int nbuf)
135138
{
136-
printf(2, "$ ");
139+
dprintf(2, "$ ");
137140
memset(buf, 0, nbuf);
138-
gets(buf, nbuf);
141+
fgets(buf, nbuf, stdin);
139142
if(buf[0] == 0) // EOF
140143
return -1;
141144
return 0;
@@ -161,21 +164,21 @@ main(void)
161164
// Chdir must be called by the parent, not the child.
162165
buf[strlen(buf)-1] = 0; // chop \n
163166
if(chdir(buf+3) < 0)
164-
printf(2, "cannot cd %s\n", buf+3);
167+
dprintf(2, "cannot cd %s\n", buf+3);
165168
continue;
166169
}
167170
if(fork1() == 0)
168171
runcmd(parsecmd(buf));
169-
wait();
172+
wait(NULL);
170173
}
171-
exit();
174+
exit(1);
172175
}
173176

174177
void
175178
panic(char *s)
176179
{
177-
printf(2, "%s\n", s);
178-
exit();
180+
dprintf(2, "%s\n", s);
181+
exit(1);
179182
}
180183

181184
int
@@ -334,7 +337,7 @@ parsecmd(char *s)
334337
cmd = parseline(&s, es);
335338
peek(&s, es, "");
336339
if(s != es){
337-
printf(2, "leftovers: %s\n", s);
340+
dprintf(2, "leftovers: %s\n", s);
338341
panic("syntax");
339342
}
340343
nulterminate(cmd);
@@ -386,10 +389,10 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
386389
cmd = redircmd(cmd, q, eq, O_RDONLY, 0);
387390
break;
388391
case '>':
389-
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
392+
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREAT, 1);
390393
break;
391394
case '+': // >>
392-
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
395+
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREAT, 1);
393396
break;
394397
}
395398
}

0 commit comments

Comments
 (0)