still serv_close bug...
[mit-jos.git] / user / writemotd.c
blob515a89e2366e97f97f7f0cdd09622766b56fa3bd
1 #include <inc/lib.h>
3 void
4 umain(void)
6 int rfd, wfd;
7 char buf[512];
8 int n, r;
10 if ((rfd = open("/newmotd", O_RDONLY)) < 0)
11 panic("open /newmotd: %e", rfd);
12 if ((wfd = open("/motd", O_RDWR)) < 0)
13 panic("open /motd: %e", wfd);
14 cprintf("file descriptors %d %d\n", rfd, wfd);
15 if (rfd == wfd)
16 panic("open /newmotd and /motd give same file descriptor");
18 cprintf("OLD MOTD\n===\n");
19 while ((n = read(wfd, buf, sizeof buf-1)) > 0)
20 sys_cputs(buf, n);
21 cprintf("===\n");
22 seek(wfd, 0);
24 if ((r = ftruncate(wfd, 0)) < 0)
25 panic("truncate /motd: %e", r);
27 cprintf("NEW MOTD\n===\n");
28 while ((n = read(rfd, buf, sizeof buf-1)) > 0) {
29 sys_cputs(buf, n);
30 if ((r = write(wfd, buf, n)) != n)
31 panic("write /motd: %e", r);
33 cprintf("===\n");
35 if (n < 0)
36 panic("read /newmotd: %e", n);
38 close(rfd);
39 close(wfd);