BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.bin / find / find.c
index ccbf6f1..22fc3a3 100644 (file)
@@ -1,24 +1,53 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993, 1994
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Cimarron D. Taylor of the University of California, Berkeley.
  *
  *
  * This code is derived from software contributed to Berkeley by
  * Cimarron D. Taylor of the University of California, Berkeley.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)find.c     5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)find.c     8.3 (Berkeley) 4/1/94";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/errno.h>
+
+#include <err.h>
+#include <errno.h>
 #include <fts.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <fts.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+
 #include "find.h"
 
 /*
 #include "find.h"
 
 /*
@@ -48,7 +77,7 @@ find_formplan(argv)
         * by c_name() with an argument of foo and `-->' represents the
         * plan->next pointer.
         */
         * by c_name() with an argument of foo and `-->' represents the
         * plan->next pointer.
         */
-       for (plan = NULL; *argv;) {
+       for (plan = tail = NULL; *argv;) {
                if (!(new = find_create(&argv)))
                        continue;
                if (plan == NULL)
                if (!(new = find_create(&argv)))
                        continue;
                if (plan == NULL)
@@ -101,7 +130,7 @@ find_formplan(argv)
        plan = paren_squish(plan);              /* ()'s */
        plan = not_squish(plan);                /* !'s */
        plan = or_squish(plan);                 /* -o's */
        plan = paren_squish(plan);              /* ()'s */
        plan = not_squish(plan);                /* !'s */
        plan = or_squish(plan);                 /* -o's */
-       return(plan);
+       return (plan);
 }
  
 FTS *tree;                     /* pointer to top of FTS hierarchy */
 }
  
 FTS *tree;                     /* pointer to top of FTS hierarchy */
@@ -111,19 +140,20 @@ FTS *tree;                        /* pointer to top of FTS hierarchy */
  *     take a search plan and an array of search paths and executes the plan
  *     over all FTSENT's returned for the given search paths.
  */
  *     take a search plan and an array of search paths and executes the plan
  *     over all FTSENT's returned for the given search paths.
  */
-void
+int
 find_execute(plan, paths)
        PLAN *plan;             /* search plan */
        char **paths;           /* array of pathnames to traverse */
 {
        register FTSENT *entry;
        PLAN *p;
 find_execute(plan, paths)
        PLAN *plan;             /* search plan */
        char **paths;           /* array of pathnames to traverse */
 {
        register FTSENT *entry;
        PLAN *p;
+       int rval;
     
     
-       if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
-               err("ftsopen: %s", strerror(errno));
+       if ((tree = fts_open(paths, ftsoptions, (int (*)())NULL)) == NULL)
+               err(1, "ftsopen");
 
 
-       while (entry = fts_read(tree)) {
-               switch(entry->fts_info) {
+       for (rval = 0; (entry = fts_read(tree)) != NULL;) {
+               switch (entry->fts_info) {
                case FTS_D:
                        if (isdepth)
                                continue;
                case FTS_D:
                        if (isdepth)
                                continue;
@@ -135,30 +165,28 @@ find_execute(plan, paths)
                case FTS_DNR:
                case FTS_ERR:
                case FTS_NS:
                case FTS_DNR:
                case FTS_ERR:
                case FTS_NS:
-                       (void)fprintf(stderr, "find: %s: %s\n", 
-                           entry->fts_path, strerror(errno));
+                       (void)fflush(stdout);
+                       warnx("%s: %s",
+                           entry->fts_path, strerror(entry->fts_errno));
+                       rval = 1;
                        continue;
                        continue;
-               case FTS_SL:
-                       if (entry->fts_level == FTS_ROOTLEVEL) {
-                               (void)fts_set(tree, entry, FTS_FOLLOW);
-                               continue;
-                       }
-                       break;
                }
                }
-
 #define        BADCH   " \t\n\\'\""
                if (isxargs && strpbrk(entry->fts_path, BADCH)) {
 #define        BADCH   " \t\n\\'\""
                if (isxargs && strpbrk(entry->fts_path, BADCH)) {
-                       (void)fprintf(stderr,
-                           "find: illegal path: %s\n", entry->fts_path);
+                       (void)fflush(stdout);
+                       warnx("%s: illegal path", entry->fts_path);
+                       rval = 1;
                        continue;
                }
                 
                /*
                        continue;
                }
                 
                /*
-                * call all the functions in the execution plan until one is
+                * Call all the functions in the execution plan until one is
                 * false or all have been executed.  This is where we do all
                 * the work specified by the user on the command line.
                 */
                for (p = plan; p && (p->eval)(p, entry); p = p->next);
        }
                 * false or all have been executed.  This is where we do all
                 * the work specified by the user on the command line.
                 */
                for (p = plan; p && (p->eval)(p, entry); p = p->next);
        }
-       (void)fts_close(tree);
+       if (errno)
+               err(1, "fts_read");
+       return (rval);
 }
 }