output delimiters even if the input file is empty
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 7 Mar 1990 04:50:40 +0000 (20:50 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 7 Mar 1990 04:50:40 +0000 (20:50 -0800)
SCCS-vsn: usr.bin/paste/paste.c 5.3

usr/src/usr.bin/paste/paste.c

index 8c664a1..4adfcfc 100644 (file)
@@ -25,7 +25,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)paste.c    5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)paste.c    5.3 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -115,18 +115,20 @@ parallel(argv)
        }
 
        for (opencnt = cnt; opencnt;) {
        }
 
        for (opencnt = cnt; opencnt;) {
-               for (cnt = -1, output = 0, lp = head; lp; lp = lp->next) {
+               for (output = 0, lp = head; lp; lp = lp->next) {
                        if (!lp->fp) {
                        if (!lp->fp) {
-                               if (cnt == -1)
-                                       cnt = 0;
+                               if (output && lp->cnt &&
+                                   (ch = delim[(lp->cnt - 1) % delimcnt]))
+                                       putchar(ch);
                                continue;
                        }
                        if (!fgets(buf, sizeof(buf), lp->fp)) {
                                if (!--opencnt)
                                        break;
                                lp->fp = NULL;
                                continue;
                        }
                        if (!fgets(buf, sizeof(buf), lp->fp)) {
                                if (!--opencnt)
                                        break;
                                lp->fp = NULL;
-                               if (cnt == -1)
-                                       cnt = 0;
+                               if (output && lp->cnt &&
+                                   (ch = delim[(lp->cnt - 1) % delimcnt]))
+                                       putchar(ch);
                                continue;
                        }
                        if (!(p = index(buf, '\n'))) {
                                continue;
                        }
                        if (!(p = index(buf, '\n'))) {
@@ -136,12 +138,17 @@ parallel(argv)
                                exit(1);
                        }
                        *p = '\0';
                                exit(1);
                        }
                        *p = '\0';
-                       if (cnt == -1)
-                               cnt = 0;
-                       else for (; cnt < lp->cnt; ++cnt)
-                               if (ch = delim[cnt % delimcnt])
-                                       putchar(ch);
-                       output = 1;
+                       /*
+                        * make sure that we don't print any delimiters
+                        * unless there's a non-empty file.
+                        */
+                       if (!output) {
+                               output = 1;
+                               for (cnt = 0; cnt < lp->cnt; ++cnt)
+                                       if (ch = delim[cnt % delimcnt])
+                                               putchar(ch);
+                       } else if (ch = delim[(lp->cnt - 1) % delimcnt])
+                               putchar(ch);
                        (void)printf("%s", buf);
                }
                if (output)
                        (void)printf("%s", buf);
                }
                if (output)