(1) Don't forget to terminate the argument list (2 out of 3 places!),
authorDonn Seeley <donn@ucbvax.Berkeley.EDU>
Wed, 6 Mar 1991 08:37:52 +0000 (00:37 -0800)
committerDonn Seeley <donn@ucbvax.Berkeley.EDU>
Wed, 6 Mar 1991 08:37:52 +0000 (00:37 -0800)
(2) Add -f flag to force xargs to ignore program status.  (3) Fix includes.

SCCS-vsn: usr.bin/xargs/xargs.c 5.7

usr/src/usr.bin/xargs/xargs.c

index b59b4c5..85edbae 100644 (file)
@@ -15,20 +15,23 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)xargs.c    5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)xargs.c    5.7 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <errno.h>
 #include <stdio.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <unistd.h>
 #include <limits.h>
 #include "pathnames.h"
 
 #define        DEF_ARGC        255
 
 int tflag;
 #include <limits.h>
 #include "pathnames.h"
 
 #define        DEF_ARGC        255
 
 int tflag;
+int fflag;
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -39,12 +42,12 @@ main(argc, argv)
        register int ch;
        register char *p, *bp, *endbp, **bxp, **endxp, **xp;
        int cnt, indouble, insingle, nargs, nline;
        register int ch;
        register char *p, *bp, *endbp, **bxp, **endxp, **xp;
        int cnt, indouble, insingle, nargs, nline;
-       char *mark, *prog, **xargs, *malloc();
+       char *mark, *prog, **xargs;
 
        nargs = DEF_ARGC;
        nline = _POSIX2_LINE_MAX;
 
 
        nargs = DEF_ARGC;
        nline = _POSIX2_LINE_MAX;
 
-       while ((ch = getopt(argc, argv, "n:s:t")) != EOF)
+       while ((ch = getopt(argc, argv, "n:s:tf")) != EOF)
                switch(ch) {
                case 'n':
                        if ((nargs = atoi(optarg)) <= 0) {
                switch(ch) {
                case 'n':
                        if ((nargs = atoi(optarg)) <= 0) {
@@ -63,6 +66,9 @@ main(argc, argv)
                case 't':
                        tflag = 1;
                        break;
                case 't':
                        tflag = 1;
                        break;
+               case 'f':
+                       fflag = 1;
+                       break;
                case '?':
                default:
                        usage();
                case '?':
                default:
                        usage();
@@ -103,6 +109,7 @@ main(argc, argv)
                        if (p == bp)            /* nothing to display */
                                exit(0);
                        if (mark == p) {        /* nothing since last arg end */
                        if (p == bp)            /* nothing to display */
                                exit(0);
                        if (mark == p) {        /* nothing since last arg end */
+                               *xp = NULL;
                                run(prog, xargs);
                                exit(0);
                        }
                                run(prog, xargs);
                                exit(0);
                        }
@@ -123,6 +130,7 @@ addarg:                     *xp++ = mark;
                                           "xargs: unterminated quote.\n");
                                        exit(1);
                                }
                                           "xargs: unterminated quote.\n");
                                        exit(1);
                                }
+                               *xp = NULL;
                                run(prog, xargs);
                                if (ch == EOF)
                                        exit(0);
                                run(prog, xargs);
                                if (ch == EOF)
                                        exit(0);
@@ -203,7 +211,7 @@ run(prog, argv)
                   "xargs: waitpid: %s.\n", strerror(errno));
                exit(1);
        }
                   "xargs: waitpid: %s.\n", strerror(errno));
                exit(1);
        }
-       if (pstat.w_status)
+       if (!fflag && pstat.w_status)
                exit(1);
 }
 
                exit(1);
 }
 
@@ -216,6 +224,6 @@ enomem()
 usage()
 {
        (void)fprintf(stderr,
 usage()
 {
        (void)fprintf(stderr,
-           "xargs: [-t] [-n number] [-s size] [utility [argument ...]]\n");
+           "xargs: [-t] [-f] [-n number] [-s size] [utility [argument ...]]\n");
        exit(1);
 }
        exit(1);
 }