BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / find / find.c
index d1eaeae..a332682 100644 (file)
@@ -1,91 +1,52 @@
 /*-
 /*-
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1991 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.
  *
  * All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Cimarron D. Taylor of the University of California, Berkeley.
  *
- * Redistribution and use in source and binary forms are permitted provided
- * that: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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.32 (Berkeley) 7/1/90";
+static char sccsid[] = "@(#)find.c     5.3 (Berkeley) 5/25/91";
 #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 <fts.h>
 #include <stdio.h>
 #include <string.h>
 #include <fts.h>
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
+#include <stdlib.h>
 #include "find.h"
 
 #include "find.h"
 
-FTS *tree;                     /* pointer to top of FTS hierarchy */
-time_t now;                    /* time find was run */
-int ftsoptions;                        /* options passed to ftsopen() */
-int deprecated;                        /* old or new syntax */
-int depth;                     /* set by -depth option */
-int output_specified;          /* one of -print, -ok or -exec was specified */
-
-main(argc, argv)
-       int argc;
-       char **argv;
-{
-       PLAN *plan;
-       char **p, **paths;
-       PLAN *find_formplan();
-       time_t time();
-    
-       (void)time(&now);                       /* initialize the time-of-day */
-
-       if (argc < 2)
-               usage();
-
-       paths = argv;
-       ftsoptions = FTS_NOSTAT|FTS_PHYSICAL;
-
-       /*
-        * 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 == '-') {
-                               deprecated = 1;
-                               oldsyntax(&argv);
-                               break;
-                       }
-               }
-       if (!deprecated)
-               newsyntax(argc, &argv);
-    
-       plan = find_formplan(argv);             /* execution plan */
-       find_execute(plan, paths);
-}
-
 /*
  * find_formplan --
  *     process the command line and create a "plan" corresponding to the
 /*
  * find_formplan --
  *     process the command line and create a "plan" corresponding to the
@@ -96,8 +57,8 @@ 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();
+       PLAN *c_print(), *find_create(), *not_squish(), *or_squish();
+       PLAN *paren_squish();
 
        /*
         * for each argument in the command line, determine what kind of node
 
        /*
         * for each argument in the command line, determine what kind of node
@@ -132,7 +93,7 @@ find_formplan(argv)
         * the user might want the -print someplace else on the command line,
         * but there's no way to know that.
         */
         * the user might want the -print someplace else on the command line,
         * but there's no way to know that.
         */
-       if (!output_specified) {
+       if (!isoutput) {
                new = c_print();
                if (plan == NULL)
                        tail = plan = new;
                new = c_print();
                if (plan == NULL)
                        tail = plan = new;
@@ -165,63 +126,61 @@ 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 */
+       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 */
+
 /*
  * 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.
  */
+void
 find_execute(plan, paths)
        PLAN *plan;             /* search plan */
        char **paths;           /* array of pathnames to traverse */
 {
 find_execute(plan, paths)
        PLAN *plan;             /* search plan */
        char **paths;           /* array of pathnames to traverse */
 {
-       FTSENT *entry;          /* current fts entry */
+       register FTSENT *entry;
        PLAN *p;
     
        PLAN *p;
     
-       if (!(tree = ftsopen(paths, ftsoptions, NULL))) {
-               (void)fprintf(stderr, "find: ftsopen: %s.\n", strerror(errno));
-               exit(1);
-       }
-       while (entry = ftsread(tree)) {
+       if (!(tree = fts_open(paths, ftsoptions, (int (*)())NULL)))
+               err("ftsopen: %s", strerror(errno));
+
+       while (entry = fts_read(tree)) {
                switch(entry->fts_info) {
                switch(entry->fts_info) {
-               case FTS_DNR:
-                       (void)fprintf(stderr,
-                           "find: %s: unable to read.\n", entry->fts_path);
-                       continue;
-               case FTS_DNX:
-                       (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;
                case FTS_D:
                case FTS_D:
-                       if (depth)
+                       if (isdepth)
                                continue;
                        break;
                                continue;
                        break;
-               case FTS_DC:
-                       (void)fprintf(stderr,
-                           "find: directory cycle: %s.\n", entry->fts_path);
-                       continue;
                case FTS_DP:
                case FTS_DP:
-                       if (!depth)
+                       if (!isdepth)
                                continue;
                                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);
+                       (void)fprintf(stderr, "find: %s: %s\n", 
+                           entry->fts_path, strerror(errno));
+                       continue;
+               case FTS_SL:
+                       if (entry->fts_level == FTS_ROOTLEVEL) {
+                               (void)fts_set(tree, entry, FTS_FOLLOW);
                                continue;
                        }
                        break;
                }
 
                                continue;
                        }
                        break;
                }
 
+#define        BADCH   " \t\n\\'\""
+               if (isxargs && strpbrk(entry->fts_path, BADCH)) {
+                       (void)fprintf(stderr,
+                           "find: illegal path: %s\n", entry->fts_path);
+                       continue;
+               }
+                
                /*
                 * call all the functions in the execution plan until one is
                 * false or all have been executed.  This is where we do all
                /*
                 * call all the functions in the execution plan until one is
                 * false or all have been executed.  This is where we do all
@@ -229,5 +188,5 @@ find_execute(plan, paths)
                 */
                for (p = plan; p && (p->eval)(p, entry); p = p->next);
        }
                 */
                for (p = plan; p && (p->eval)(p, entry); p = p->next);
        }
-       (void)ftsclose(tree);
+       (void)fts_close(tree);
 }
 }