No functional change or bugfix but queue(3) is the for a reason. That
way the code even is a tad clearer to me (and two lines shorter).

Feedback/OK?

Index: tee.c
===================================================================
RCS file: /cvs/src/usr.bin/tee/tee.c,v
retrieving revision 1.11
diff -u -p -r1.11 tee.c
--- tee.c       28 Oct 2016 07:22:59 -0000      1.11
+++ tee.c       2 Jul 2017 02:14:54 -0000
@@ -32,6 +32,7 @@

#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/queue.h>

#include <err.h>
#include <errno.h>
@@ -42,30 +43,26 @@
#include <string.h>
#include <unistd.h>

-struct list {
-       struct list *next;
+struct file {
+       SLIST_ENTRY(file) next;
        int fd;
        char *name;
-};
-struct list *head;
+} *fp;
+SLIST_HEAD(, file) head;

static void
add(int fd, char *name)
{
-       struct list *p;
-
-       if ((p = malloc(sizeof(*p))) == NULL)
+       if ((fp = malloc(sizeof(struct file))) == NULL)
                err(1, NULL);
-       p->fd = fd;
-       p->name = name;
-       p->next = head;
-       head = p;
+       fp->fd = fd;
+       fp->name = name;
+       SLIST_INSERT_HEAD(&head, fp, next);
}

int
main(int argc, char *argv[])
{
-       struct list *p;
        int fd;
        ssize_t n, rval, wval;
        char *bp;
@@ -75,6 +72,8 @@ main(int argc, char *argv[])
        if (pledge("stdio wpath cpath", NULL) == -1)
                err(1, "pledge");

+       SLIST_INIT(&head);
+
        append = 0;
        while ((ch = getopt(argc, argv, "ai")) != -1) {
                switch(ch) {
@@ -109,12 +108,12 @@ main(int argc, char *argv[])
                err(1, "pledge");

        while ((rval = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
-               for (p = head; p; p = p->next) {
+               SLIST_FOREACH(fp, &head, next) {
                        n = rval;
                        bp = buf;
                        do {
-                               if ((wval = write(p->fd, bp, n)) == -1) {
-                                       warn("%s", p->name);
+                               if ((wval = write(fp->fd, bp, n)) == -1) {
+                                       warn("%s", fp->name);
                                        exitval = 1;
                                        break;
                                }
@@ -127,12 +126,11 @@ main(int argc, char *argv[])
                exitval = 1;
        }

-       for (p = head; p; p = p->next) {
-               if (close(p->fd) == -1) {
-                       warn("%s", p->name);
+       SLIST_FOREACH(fp, &head, next)
+               if (close(fp->fd) == -1) {
+                       warn("%s", fp->name);
                        exitval = 1;
                }
-       }

        return exitval;
}

Reply via email to