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() periodic()
{ {
count = count + 1; count = count + 1;
printf(1, "alarm!\n"); printf("alarm!\n");
sigreturn(); sigreturn();
} }
@ -40,7 +40,7 @@ void
test0() test0()
{ {
int i; int i;
printf(1, "test0 start\n"); printf("test0 start\n");
count = 0; count = 0;
sigalarm(2, periodic); sigalarm(2, periodic);
for(i = 0; i < 1000*500000; i++){ for(i = 0; i < 1000*500000; i++){
@ -51,9 +51,9 @@ test0()
} }
sigalarm(0, 0); sigalarm(0, 0);
if(count > 0){ if(count > 0){
printf(1, "test0 passed\n"); printf("test0 passed\n");
} else { } else {
printf(1, "test0 failed\n"); printf("test0 failed\n");
} }
} }
@ -70,7 +70,7 @@ test1()
int i; int i;
int j; int j;
printf(1, "test1 start\n"); printf("test1 start\n");
count = 0; count = 0;
j = 0; j = 0;
sigalarm(2, periodic); sigalarm(2, periodic);
@ -81,8 +81,8 @@ test1()
} }
if(i != j || count < 10){ if(i != j || count < 10){
// i should equal j // i should equal j
printf(1, "test1 failed\n"); printf("test1 failed\n");
} else { } 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) { while((n = read(fd, buf, sizeof(buf))) > 0) {
if (write(1, buf, n) != n) { if (write(1, buf, n) != n) {
printf(1, "cat: write error\n"); printf("cat: write error\n");
exit(); exit();
} }
} }
if(n < 0){ if(n < 0){
printf(1, "cat: read error\n"); printf("cat: read error\n");
exit(); exit();
} }
} }
@ -33,7 +33,7 @@ main(int argc, char *argv[])
for(i = 1; i < argc; i++){ for(i = 1; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){ 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(); exit();
} }
cat(fd); cat(fd);

View file

@ -8,6 +8,6 @@ main(int argc, char *argv[])
int i; int i;
for(i = 1; i < argc; 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(); exit();
} }

View file

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

View file

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

View file

@ -20,19 +20,19 @@ main(void)
dup(0); // stderr dup(0); // stderr
for(;;){ for(;;){
printf(1, "init: starting sh\n"); printf("init: starting sh\n");
pid = fork(); pid = fork();
if(pid < 0){ if(pid < 0){
printf(1, "init: fork failed\n"); printf("init: fork failed\n");
exit(); exit();
} }
if(pid == 0){ if(pid == 0){
exec("sh", argv); exec("sh", argv);
printf(1, "init: exec sh failed\n"); printf("init: exec sh failed\n");
exit(); exit();
} }
while((wpid=wait()) >= 0 && wpid != pid){ 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; int i;
if(argc < 2){ if(argc < 2){
printf(2, "usage: kill pid...\n"); fprintf(2, "usage: kill pid...\n");
exit(); exit();
} }
for(i=1; i<argc; i++) for(i=1; i<argc; i++)

View file

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

View file

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

View file

@ -8,13 +8,13 @@ main(int argc, char *argv[])
int i; int i;
if(argc < 2){ if(argc < 2){
printf(2, "Usage: mkdir files...\n"); fprintf(2, "Usage: mkdir files...\n");
exit(); exit();
} }
for(i = 1; i < argc; i++){ for(i = 1; i < argc; i++){
if(mkdir(argv[i]) < 0){ 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; break;
} }
} }

View file

@ -49,13 +49,11 @@ printptr(int fd, uint64 x) {
// Print to the given fd. Only understands %d, %x, %p, %s. // Print to the given fd. Only understands %d, %x, %p, %s.
void void
printf(int fd, const char *fmt, ...) vprintf(int fd, const char *fmt, va_list ap)
{ {
va_list ap;
char *s; char *s;
int c, i, state; int c, i, state;
va_start(ap, fmt);
state = 0; state = 0;
for(i = 0; fmt[i]; i++){ for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff; 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; int i;
if(argc < 2){ if(argc < 2){
printf(2, "Usage: rm files...\n"); fprintf(2, "Usage: rm files...\n");
exit(); exit();
} }
for(i = 1; i < argc; i++){ for(i = 1; i < argc; i++){
if(unlink(argv[i]) < 0){ 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; break;
} }
} }

View file

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

View file

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

View file

@ -30,7 +30,8 @@ char* strcpy(char*, const char*);
void *memmove(void*, const void*, int); void *memmove(void*, const void*, int);
char* strchr(const char*, char c); char* strchr(const char*, char c);
int strcmp(const char*, const char*); 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); char* gets(char*, int max);
uint strlen(const char*); uint strlen(const char*);
void* memset(void*, int, uint); 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){ if(n < 0){
printf(1, "wc: read error\n"); printf("wc: read error\n");
exit(); exit();
} }
printf(1, "%d %d %d %s\n", l, w, c, name); printf("%d %d %d %s\n", l, w, c, name);
} }
int int
@ -44,7 +44,7 @@ main(int argc, char *argv[])
for(i = 1; i < argc; i++){ for(i = 1; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){ 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(); exit();
} }
wc(fd, argv[i]); wc(fd, argv[i]);