user printf(1 -> printf(

This commit is contained in:
Robert Morris 2019-08-27 13:13:03 -04:00
parent a3f6d9fd1e
commit 64b93d175a
18 changed files with 352 additions and 336 deletions

Binary file not shown.

View file

@ -30,7 +30,7 @@ void
periodic()
{
count = count + 1;
printf(1, "alarm!\n");
printf("alarm!\n");
sigreturn();
}
@ -40,7 +40,7 @@ void
test0()
{
int i;
printf(1, "test0 start\n");
printf("test0 start\n");
count = 0;
sigalarm(2, periodic);
for(i = 0; i < 1000*500000; i++){
@ -51,9 +51,9 @@ test0()
}
sigalarm(0, 0);
if(count > 0){
printf(1, "test0 passed\n");
printf("test0 passed\n");
} else {
printf(1, "test0 failed\n");
printf("test0 failed\n");
}
}
@ -70,7 +70,7 @@ test1()
int i;
int j;
printf(1, "test1 start\n");
printf("test1 start\n");
count = 0;
j = 0;
sigalarm(2, periodic);
@ -81,8 +81,8 @@ test1()
}
if(i != j || count < 10){
// i should equal j
printf(1, "test1 failed\n");
printf("test1 failed\n");
} else {
printf(1, "test1 passed\n");
printf("test1 passed\n");
}
}

View file

@ -11,12 +11,12 @@ cat(int fd)
while((n = read(fd, buf, sizeof(buf))) > 0) {
if (write(1, buf, n) != n) {
printf(1, "cat: write error\n");
printf("cat: write error\n");
exit();
}
}
if(n < 0){
printf(1, "cat: read error\n");
printf("cat: read error\n");
exit();
}
}
@ -33,7 +33,7 @@ main(int argc, char *argv[])
for(i = 1; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){
printf(1, "cat: cannot open %s\n", argv[i]);
printf("cat: cannot open %s\n", argv[i]);
exit();
}
cat(fd);

View file

@ -8,6 +8,6 @@ main(int argc, char *argv[])
int i;
for(i = 1; i < argc; i++)
printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n");
printf("%s%s", argv[i], i+1 < argc ? " " : "\n");
exit();
}

View file

@ -8,9 +8,9 @@
#define N 1000
void
printf(int fd, const char *s, ...)
print(const char *s)
{
write(fd, s, strlen(s));
write(1, s, strlen(s));
}
void
@ -18,7 +18,7 @@ forktest(void)
{
int n, pid;
printf(1, "fork test\n");
print("fork test\n");
for(n=0; n<N; n++){
pid = fork();
@ -29,23 +29,23 @@ forktest(void)
}
if(n == N){
printf(1, "fork claimed to work N times!\n", N);
print("fork claimed to work N times!\n");
exit();
}
for(; n > 0; n--){
if(wait() < 0){
printf(1, "wait stopped early\n");
print("wait stopped early\n");
exit();
}
}
if(wait() != -1){
printf(1, "wait got too many\n");
print("wait got too many\n");
exit();
}
printf(1, "fork test OK\n");
print("fork test OK\n");
}
int

View file

@ -42,7 +42,7 @@ main(int argc, char *argv[])
char *pattern;
if(argc <= 1){
printf(2, "usage: grep pattern [file ...]\n");
fprintf(2, "usage: grep pattern [file ...]\n");
exit();
}
pattern = argv[1];
@ -54,7 +54,7 @@ main(int argc, char *argv[])
for(i = 2; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){
printf(1, "grep: cannot open %s\n", argv[i]);
printf("grep: cannot open %s\n", argv[i]);
exit();
}
grep(pattern, fd);

View file

@ -20,19 +20,19 @@ main(void)
dup(0); // stderr
for(;;){
printf(1, "init: starting sh\n");
printf("init: starting sh\n");
pid = fork();
if(pid < 0){
printf(1, "init: fork failed\n");
printf("init: fork failed\n");
exit();
}
if(pid == 0){
exec("sh", argv);
printf(1, "init: exec sh failed\n");
printf("init: exec sh failed\n");
exit();
}
while((wpid=wait()) >= 0 && wpid != pid){
//printf(1, "zombie!\n");
//printf("zombie!\n");
}
}
}

View file

@ -8,7 +8,7 @@ main(int argc, char **argv)
int i;
if(argc < 2){
printf(2, "usage: kill pid...\n");
fprintf(2, "usage: kill pid...\n");
exit();
}
for(i=1; i<argc; i++)

View file

@ -6,10 +6,10 @@ int
main(int argc, char *argv[])
{
if(argc != 3){
printf(2, "Usage: ln old new\n");
fprintf(2, "Usage: ln old new\n");
exit();
}
if(link(argv[1], argv[2]) < 0)
printf(2, "link %s %s: failed\n", argv[1], argv[2]);
fprintf(2, "link %s %s: failed\n", argv[1], argv[2]);
exit();
}

View file

@ -31,24 +31,24 @@ ls(char *path)
struct stat st;
if((fd = open(path, 0)) < 0){
printf(2, "ls: cannot open %s\n", path);
fprintf(2, "ls: cannot open %s\n", path);
return;
}
if(fstat(fd, &st) < 0){
printf(2, "ls: cannot stat %s\n", path);
fprintf(2, "ls: cannot stat %s\n", path);
close(fd);
return;
}
switch(st.type){
case T_FILE:
printf(1, "%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
break;
case T_DIR:
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
printf(1, "ls: path too long\n");
printf("ls: path too long\n");
break;
}
strcpy(buf, path);
@ -60,10 +60,10 @@ ls(char *path)
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if(stat(buf, &st) < 0){
printf(1, "ls: cannot stat %s\n", buf);
printf("ls: cannot stat %s\n", buf);
continue;
}
printf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
}
break;
}

View file

@ -8,13 +8,13 @@ main(int argc, char *argv[])
int i;
if(argc < 2){
printf(2, "Usage: mkdir files...\n");
fprintf(2, "Usage: mkdir files...\n");
exit();
}
for(i = 1; i < argc; i++){
if(mkdir(argv[i]) < 0){
printf(2, "mkdir: %s failed to create\n", argv[i]);
fprintf(2, "mkdir: %s failed to create\n", argv[i]);
break;
}
}

View file

@ -49,13 +49,11 @@ printptr(int fd, uint64 x) {
// Print to the given fd. Only understands %d, %x, %p, %s.
void
printf(int fd, const char *fmt, ...)
vprintf(int fd, const char *fmt, va_list ap)
{
va_list ap;
char *s;
int c, i, state;
va_start(ap, fmt);
state = 0;
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
@ -95,3 +93,21 @@ printf(int fd, const char *fmt, ...)
}
}
}
void
fprintf(int fd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fd, fmt, ap);
}
void
printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(1, fmt, ap);
}

View file

@ -8,13 +8,13 @@ main(int argc, char *argv[])
int i;
if(argc < 2){
printf(2, "Usage: rm files...\n");
fprintf(2, "Usage: rm files...\n");
exit();
}
for(i = 1; i < argc; i++){
if(unlink(argv[i]) < 0){
printf(2, "rm: %s failed to delete\n", argv[i]);
fprintf(2, "rm: %s failed to delete\n", argv[i]);
break;
}
}

View file

@ -76,14 +76,14 @@ runcmd(struct cmd *cmd)
if(ecmd->argv[0] == 0)
exit();
exec(ecmd->argv[0], ecmd->argv);
printf(2, "exec %s failed\n", ecmd->argv[0]);
fprintf(2, "exec %s failed\n", ecmd->argv[0]);
break;
case REDIR:
rcmd = (struct redircmd*)cmd;
close(rcmd->fd);
if(open(rcmd->file, rcmd->mode) < 0){
printf(2, "open %s failed\n", rcmd->file);
fprintf(2, "open %s failed\n", rcmd->file);
exit();
}
runcmd(rcmd->cmd);
@ -133,7 +133,7 @@ runcmd(struct cmd *cmd)
int
getcmd(char *buf, int nbuf)
{
printf(2, "$ ");
fprintf(2, "$ ");
memset(buf, 0, nbuf);
gets(buf, nbuf);
if(buf[0] == 0) // EOF
@ -161,7 +161,7 @@ main(void)
// Chdir must be called by the parent, not the child.
buf[strlen(buf)-1] = 0; // chop \n
if(chdir(buf+3) < 0)
printf(2, "cannot cd %s\n", buf+3);
fprintf(2, "cannot cd %s\n", buf+3);
continue;
}
if(fork1() == 0)
@ -174,7 +174,7 @@ main(void)
void
panic(char *s)
{
printf(2, "%s\n", s);
fprintf(2, "%s\n", s);
exit();
}
@ -334,7 +334,7 @@ parsecmd(char *s)
cmd = parseline(&s, es);
peek(&s, es, "");
if(s != es){
printf(2, "leftovers: %s\n", s);
fprintf(2, "leftovers: %s\n", s);
panic("syntax");
}
nulterminate(cmd);

View file

@ -20,14 +20,14 @@ main(int argc, char *argv[])
char path[] = "stressfs0";
char data[512];
printf(1, "stressfs starting\n");
printf("stressfs starting\n");
memset(data, 'a', sizeof(data));
for(i = 0; i < 4; i++)
if(fork() > 0)
break;
printf(1, "write %d\n", i);
printf("write %d\n", i);
path[8] += i;
fd = open(path, O_CREATE | O_RDWR);
@ -36,7 +36,7 @@ main(int argc, char *argv[])
write(fd, data, sizeof(data));
close(fd);
printf(1, "read\n");
printf("read\n");
fd = open(path, O_RDONLY);
for (i = 0; i < 20; i++)

View file

@ -30,7 +30,8 @@ char* strcpy(char*, const char*);
void *memmove(void*, const void*, int);
char* strchr(const char*, char c);
int strcmp(const char*, const char*);
void printf(int, const char*, ...);
void fprintf(int, const char*, ...);
void printf(const char*, ...);
char* gets(char*, int max);
uint strlen(const char*);
void* memset(void*, int, uint);

File diff suppressed because it is too large Load diff

View file

@ -26,10 +26,10 @@ wc(int fd, char *name)
}
}
if(n < 0){
printf(1, "wc: read error\n");
printf("wc: read error\n");
exit();
}
printf(1, "%d %d %d %s\n", l, w, c, name);
printf("%d %d %d %s\n", l, w, c, name);
}
int
@ -44,7 +44,7 @@ main(int argc, char *argv[])
for(i = 1; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){
printf(1, "wc: cannot open %s\n", argv[i]);
printf("wc: cannot open %s\n", argv[i]);
exit();
}
wc(fd, argv[i]);