* same with xv6
[mascara-docs.git] / i386 / MIT / course / src / git.lab / user / writemotd.c
blobcd6db8bf9348a9430feeb7fc04c4e2904e38b5f6
1 #include <inc/lib.h>
3 void
4 umain(int argc, char **argv)
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);