BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.bin / find / find.c
index 61d6a40..22fc3a3 100644 (file)
@@ -1,82 +1,54 @@
 /*-
 /*-
- * Copyright (c) 1990 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
-char copyright[] =
-"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
- All rights reserved.\n";
-#endif /* not lint */
-
-#ifndef lint
-static char sccsid[] = "@(#)find.c     4.34 (Berkeley) %G%";
+static char sccsid[] = "@(#)find.c     8.3 (Berkeley) 4/1/94";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-#include <sys/errno.h>
+
+#include <err.h>
+#include <errno.h>
 #include <fts.h>
 #include <stdio.h>
 #include <fts.h>
 #include <stdio.h>
-#include "find.h"
 #include <string.h>
 #include <stdlib.h>
 
 #include <string.h>
 #include <stdlib.h>
 
-FTS *tree;                     /* pointer to top of FTS hierarchy */
-time_t now;                    /* time find was run */
-                               /* options for the ftsopen(3) call */
-int ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
-int isdeprecated;              /* using deprecated syntax */
-int isdepth;                   /* do directories on post-order visit */
-int isoutput;                  /* user specified output operator */
-int isrelative;                        /* can do -exec/ok on relative path */
-int isstopdnx;                 /* don't read unsearchable directories */
-
-main(argc, argv)
-       int argc;
-       char **argv;
-{
-       PLAN *plan;
-       char **p, **paths;
-       PLAN *find_formplan();
-       time_t time();
-       void newsyntax(), oldsyntax();
-    
-       (void)time(&now);                       /* initialize the time-of-day */
-
-       if (argc < 2)
-               usage();
-
-       paths = argv;
-
-       /*
-        * if arguments start with an option, treat it like new syntax;
-        * otherwise, if has a "-option" anywhere (which isn't an argument
-        * to another command) treat it as old syntax.
-        */
-       if (argv[1][0] != '-')
-               for (p = argv + 1; *p; ++p) {
-                       if (!strcmp(*p, "exec") || !strcmp(*p, "ok")) {
-                               while (p[1] && strcmp(*++p, ";"));
-                               continue;
-                       }
-                       if (**p == '-') {
-                               isdeprecated = 1;
-                               oldsyntax(&argv);
-                               break;
-                       }
-               }
-       if (!isdeprecated)
-               newsyntax(argc, &argv);
-    
-       plan = find_formplan(argv);             /* execution plan */
-       find_execute(plan, paths);
-}
+#include "find.h"
 
 /*
  * find_formplan --
 
 /*
  * find_formplan --
@@ -88,8 +60,6 @@ find_formplan(argv)
        char **argv;
 {
        PLAN *plan, *tail, *new;
        char **argv;
 {
        PLAN *plan, *tail, *new;
-       PLAN *c_print(), *find_create(), *find_squish_not(), *find_squish_or();
-       PLAN *find_squish_paren();
 
        /*
         * for each argument in the command line, determine what kind of node
 
        /*
         * for each argument in the command line, determine what kind of node
@@ -107,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)
@@ -157,102 +127,66 @@ find_formplan(argv)
         * operators are handled in order of precedence.
         */
 
         * operators are handled in order of precedence.
         */
 
-       plan = find_squish_paren(plan);         /* ()'s */
-       plan = find_squish_not(plan);           /* !'s */
-       plan = find_squish_or(plan);            /* -o's */
-       return(plan);
+       plan = paren_squish(plan);              /* ()'s */
+       plan = not_squish(plan);                /* !'s */
+       plan = or_squish(plan);                 /* -o's */
+       return (plan);
 }
  
 }
  
+FTS *tree;                     /* pointer to top of FTS hierarchy */
+
 /*
  * find_execute --
  *     take a search plan and an array of search paths and executes the plan
  *     over all FTSENT's returned for the given search paths.
  */
 /*
  * find_execute --
  *     take a search plan and an array of search paths and executes the plan
  *     over all FTSENT's returned for the given search paths.
  */
+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 need stat info, might as well quit when the directory isn't
-        * searchable.
-        */
-       if (!(ftsoptions & FTS_NOSTAT))
-               isstopdnx = 1;
-
-       if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL))) {
-               (void)fprintf(stderr, "find: ftsopen: %s.\n", strerror(errno));
-               exit(1);
-       }
+       if ((tree = fts_open(paths, ftsoptions, (int (*)())NULL)) == NULL)
+               err(1, "ftsopen");
 
 
-       while (entry = fts_read(tree)) {
-               switch(entry->fts_info) {
-               case FTS_DNR:
-                       (void)fprintf(stderr,
-                           "find: %s: unable to read.\n", entry->fts_path);
-                       continue;
-               case FTS_DNX: {
-                       /*
-                        * If can't search the directory, but able to read it,
-                        * and don't need stat information or to exec/ok the
-                        * file, use the fts_children list.
-                        */
-                       register char *t;
-
-                       if (isstopdnx)
-                               goto srcherr;
-                       errno = 0;
-                       entry = fts_children(tree);
-                       if (errno)
-                               goto srcherr;
-                       for (t = entry->fts_path; *t; ++t);
-                       *t = '/';
-                       for (; entry; entry = entry->fts_link) {
-                               (void)bcopy(entry->fts_name, t + 1,
-                                   entry->fts_namelen + 1);
-                               for (p = plan; p && (p->eval)(p, entry);
-                                   p = p->next);
-                       }
-                       continue;
-
-srcherr:               (void)fprintf(stderr,
-                           "find: %s: unable to search.\n", entry->fts_path);
-                       continue;
-               }
-               case FTS_ERR:
-                       (void)fprintf(stderr,
-                           "find: %s: %s.\n", entry->fts_path,
-                           strerror(errno));
-                       continue;
+       for (rval = 0; (entry = fts_read(tree)) != NULL;) {
+               switch (entry->fts_info) {
                case FTS_D:
                        if (isdepth)
                                continue;
                        break;
                case FTS_D:
                        if (isdepth)
                                continue;
                        break;
-               case FTS_DC:
-                       (void)fprintf(stderr,
-                           "find: directory cycle: %s.\n", entry->fts_path);
-                       continue;
                case FTS_DP:
                        if (!isdepth)
                                continue;
                        break;
                case FTS_DP:
                        if (!isdepth)
                                continue;
                        break;
+               case FTS_DNR:
+               case FTS_ERR:
                case FTS_NS:
                case FTS_NS:
-                       if (!(ftsoptions & FTS_NOSTAT)) {
-                               (void)fprintf(stderr,
-                                   "find: can't stat: %s.\n", entry->fts_path);
-                               continue;
-                       }
-                       break;
+                       (void)fflush(stdout);
+                       warnx("%s: %s",
+                           entry->fts_path, strerror(entry->fts_errno));
+                       rval = 1;
+                       continue;
                }
                }
-
+#define        BADCH   " \t\n\\'\""
+               if (isxargs && strpbrk(entry->fts_path, BADCH)) {
+                       (void)fflush(stdout);
+                       warnx("%s: illegal path", entry->fts_path);
+                       rval = 1;
+                       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);
 }
 }