home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / f / find37.zip / find-3.7 / find / find.c < prev    next >
C/C++ Source or Header  |  1992-04-24  |  11KB  |  404 lines

  1. /* find -- search for files in a directory hierarchy
  2.    Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* GNU find was written by Eric Decker (cire@cisco.com),
  19.    with enhancements by David MacKenzie (djm@gnu.ai.mit.edu),
  20.    Jay Plett (jay@silence.princeton.nj.us),
  21.    and Tim Wood (axolotl!tim@toad.com). */
  22.  
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <stdio.h>
  26. #if defined(USG) || defined(STDC_HEADERS)
  27. #include <string.h>
  28. #define index strchr
  29. #define rindex strrchr
  30. #else
  31. #include <strings.h>
  32. #endif
  33. #include "defs.h"
  34. #include "modetype.h"
  35.  
  36. #ifndef S_IFLNK
  37. #define lstat stat
  38. #endif
  39.  
  40. int lstat ();
  41. int stat ();
  42.  
  43. #define apply_predicate(pathname, stat_buf_ptr, node)    \
  44.   (*(node)->pred_func)((pathname), (stat_buf_ptr), (node))
  45.  
  46. boolean parse_open ();
  47. boolean parse_close ();
  48. char *savedir ();
  49. void error ();
  50. void scan_directory ();
  51.  
  52. /* Name this program was run with. */
  53. char *program_name;
  54.  
  55. /* All predicates for each path to process. */
  56. struct predicate *predicates;
  57.  
  58. /* The last predicate allocated. */
  59. struct predicate *last_pred;
  60.  
  61. /* The root of the evaluation tree. */
  62. struct predicate *eval_tree;
  63.  
  64. /* If true, process directory before contents.  True unless -depth given. */
  65. boolean do_dir_first;
  66.  
  67. /* If >=0, don't descend more than this many levels of subdirectories. */
  68. int maxdepth;
  69.  
  70. /* If >=0, don't process files above this level. */
  71. int mindepth;
  72.  
  73. /* Current depth; 0 means current path is a command line arg. */
  74. int curdepth;
  75.  
  76. /* Seconds between 00:00 1/1/70 and either one day before now
  77.    (the default), or the start of today (if -daystart is given). */
  78. time_t cur_day_start;
  79.  
  80. /* If true, cur_day_start has been adjusted to the start of the day. */
  81. boolean full_days;
  82.  
  83. /* If true, do not assume that files in directories with nlink == 2
  84.    are non-directories. */
  85. boolean no_leaf_check;
  86.  
  87. /* If true, don't cross filesystem boundaries. */
  88. boolean stay_on_filesystem;
  89.  
  90. /* If true, don't descend past current directory.
  91.    Can be set by -prune, -maxdepth, and -xdev. */
  92. boolean stop_at_current_level;
  93.  
  94. /* If true, we have called stat on the current path. */
  95. boolean have_stat;
  96.  
  97. /* Status value to return to system. */
  98. int exit_status;
  99.  
  100. /* Length of current path. */
  101. int path_length;
  102.  
  103. /* Pointer to the function used to stat files. */
  104. int (*xstat) ();
  105.  
  106. #ifdef DEBUG_STAT
  107. int
  108. debug_stat (file, bufp)
  109.      char *file;
  110.      struct stat *bufp;
  111. {
  112.   fprintf (stderr, "debug_stat (%s)\n", file);
  113.   return lstat (file, bufp);
  114. }
  115. #endif /* DEBUG_STAT */
  116.  
  117. void
  118. main (argc, argv)
  119.      int argc;
  120.      char *argv[];
  121. {
  122.   int i;
  123.   PFB parse_function;        /* Pointer to who is to do the parsing. */
  124.   struct predicate *cur_pred;
  125.   char *predicate_name;        /* Name of predicate being parsed. */
  126.  
  127.   program_name = argv[0];
  128.  
  129.   predicates = NULL;
  130.   last_pred = NULL;
  131.   do_dir_first = true;
  132.   maxdepth = mindepth = -1;
  133.   cur_day_start = time ((time_t *) 0) - DAYSECS;
  134.   full_days = false;
  135.   no_leaf_check = false;
  136.   stay_on_filesystem = false;
  137.   exit_status = 0;
  138. #ifdef DEBUG_STAT
  139.   xstat = debug_stat;
  140. #else /* !DEBUG_STAT */
  141.   xstat = lstat;
  142. #endif /* !DEBUG_STAT */
  143.  
  144. #ifdef DEBUG
  145.   printf ("cur_day_start = %s", ctime (&cur_day_start));
  146. #endif /* DEBUG */
  147.  
  148.   /* Find where in ARGV the predicates begin. */
  149.   for (i = 1; i < argc && index ("-!(),", argv[i][0]) == NULL; i++)
  150.     /* Do nothing. */ ;
  151.  
  152.   /* Enclose the expression in `( ... )' so a default -print will
  153.      apply to the whole expression. */
  154.   parse_open (argv, &argc);
  155.   /* Build the input order list. */
  156.   while (i < argc)
  157.     {
  158.       if (index ("-!(),", argv[i][0]) == NULL)
  159.     usage ("paths must precede expression");
  160.       predicate_name = argv[i];
  161.       parse_function = find_parser (predicate_name);
  162.       if (parse_function == NULL)
  163.     error (1, 0, "invalid predicate `%s'", predicate_name);
  164.       i++;
  165.       if (!(*parse_function) (argv, &i))
  166.     {
  167.       if (argv[i] == NULL)
  168.         error (1, 0, "missing argument to `%s'", predicate_name);
  169.       else
  170.         error (1, 0, "invalid argument to `%s'", predicate_name);
  171.     }
  172.     }
  173.   if (predicates->pred_next == NULL)
  174.     {
  175.       /* No predicates that do something other than set a global variable
  176.      were given; remove the unneeded initial `(' and add `-print'. */
  177.       cur_pred = predicates;
  178.       predicates = last_pred = predicates->pred_next;
  179.       free ((char *) cur_pred);
  180.       parse_print (argv, &argc);
  181.     }
  182.   else if (!no_side_effects (predicates->pred_next))
  183.     {
  184.       /* One or more predicates that produce output were given;
  185.      remove the unneeded initial `('. */
  186.       cur_pred = predicates;
  187.       predicates = predicates->pred_next;
  188.       free ((char *) cur_pred);
  189.     }
  190.   else
  191.     {
  192.       /* `( user-supplied-expression ) -print'. */
  193.       parse_close (argv, &argc);
  194.       parse_print (argv, &argc);
  195.     }
  196.  
  197. #ifdef    DEBUG
  198.   printf ("Predicate List:\n");
  199.   print_list (predicates);
  200. #endif /* DEBUG */
  201.  
  202.   /* Done parsing the predicates.  Build the evaluation tree. */
  203.   cur_pred = predicates;
  204.   eval_tree = get_expr (&cur_pred, NO_PREC);
  205. #ifdef    DEBUG
  206.   printf ("Eval Tree:\n");
  207.   print_tree (eval_tree, 0);
  208. #endif /* DEBUG */
  209.  
  210.   /* Rearrange the eval tree in optimal-predicate order. */
  211.   opt_expr (&eval_tree);
  212.  
  213.   /* Determine the point, if any, at which to stat the file. */
  214.   mark_stat (eval_tree);
  215.  
  216. #ifdef DEBUG
  217.   printf ("Optimized Eval Tree:\n");
  218.   print_tree (eval_tree, 0);
  219. #endif /* DEBUG */
  220.  
  221.   /* If no paths given, default to ".". */
  222.   for (i = 1; i < argc && index ("-!(),", argv[i][0]) == NULL; i++)
  223.     {
  224.       curdepth = 0;
  225.       path_length = strlen (argv[i]);
  226.       process_path (argv[i], false);
  227.     }
  228.   if (i == 1)
  229.     {
  230.       curdepth = 0;
  231.       path_length = 1;
  232.       process_path (".", false);
  233.     }
  234.  
  235.   exit (exit_status);
  236. }
  237.  
  238. /* Recursively descend path PATHNAME, applying the predicates.
  239.    LEAF is nonzero if PATHNAME is in a directory that has no
  240.    unexamined subdirectories, and therefore it is not a directory.
  241.    This allows us to avoid stat as long as possible for leaf files.
  242.  
  243.    Return nonzero iff PATHNAME is a directory. */
  244.  
  245. int
  246. process_path (pathname, leaf)
  247.      char *pathname;
  248.      boolean leaf;
  249. {
  250.   struct stat stat_buf;
  251.   int pathlen;            /* Length of PATHNAME. */
  252.   static dev_t root_dev;    /* Device ID of current argument pathname. */
  253.  
  254.   pathlen = strlen (pathname);
  255.  
  256.   /* Assume non-directory initially. */
  257.   stat_buf.st_mode = 0;
  258.  
  259.   if (leaf)
  260.     have_stat = false;
  261.   else
  262.     {
  263.       if ((*xstat) (pathname, &stat_buf) != 0)
  264.     {
  265.       fflush (stdout);
  266.       error (0, errno, "%s", pathname);
  267.       exit_status = 1;
  268.       return 0;
  269.     }
  270.       have_stat = true;
  271.     }
  272.  
  273.   if (!S_ISDIR (stat_buf.st_mode))
  274.     {
  275.       if (curdepth >= mindepth)
  276.     apply_predicate (pathname, &stat_buf, eval_tree);
  277.       return 0;
  278.     }
  279.  
  280.   stop_at_current_level = maxdepth >= 0 && curdepth >= maxdepth;
  281.  
  282.   if (stay_on_filesystem)
  283.     {
  284.       if (curdepth == 0)
  285.     root_dev = stat_buf.st_dev;
  286.       else if (stat_buf.st_dev != root_dev)
  287.     stop_at_current_level = true;
  288.     }
  289.  
  290.   if (do_dir_first && curdepth >= mindepth)
  291.     apply_predicate (pathname, &stat_buf, eval_tree);
  292.  
  293.   if (stop_at_current_level == false)
  294.     /* Scan directory on disk. */
  295.     scan_directory (pathname, pathlen, &stat_buf);
  296.  
  297.   if (do_dir_first == false && curdepth >= mindepth)
  298.     apply_predicate (pathname, &stat_buf, eval_tree);
  299.  
  300.   return 1;
  301. }
  302.  
  303. /* Scan directory PATHNAME and recurse through process_path for each entry.
  304.    PATHLEN is the length of PATHNAME.
  305.    STATP is the results of *xstat on it. */
  306.  
  307. void
  308. scan_directory (pathname, pathlen, statp)
  309.      char *pathname;
  310.      int pathlen;
  311.      struct stat *statp;
  312. {
  313.   char *name_space;        /* Names of files in PATHNAME. */
  314.   int subdirs_left;        /* Number of unexamined subdirs in PATHNAME. */
  315.  
  316.   subdirs_left = statp->st_nlink - 2; /* Account for name and ".". */
  317.  
  318.   errno = 0;
  319.   /* On some systems (VAX 4.3BSD+NFS), NFS mount points have st_size < 0.  */
  320.   name_space = savedir (pathname, statp->st_size > 0 ? statp->st_size : 512);
  321.   if (name_space == NULL)
  322.     {
  323.       if (errno)
  324.     {
  325.       fflush (stdout);
  326.       error (0, errno, "%s", pathname);
  327.       exit_status = 1;
  328.     }
  329.       else
  330.     {
  331.       fflush (stdout);
  332.       error (1, 0, "virtual memory exhausted");
  333.     }
  334.     }
  335.   else
  336.     {
  337.       register char *namep;    /* Current point in `name_space'. */
  338.       char *cur_path;        /* Full path of each file to process. */
  339.       unsigned cur_path_size;    /* Bytes allocated for `cur_path'. */
  340.       register unsigned file_len; /* Length of each path to process. */
  341.       register unsigned pathname_len; /* PATHLEN plus trailing '/'. */
  342.  
  343.       if (pathname[pathlen - 1] == '/')
  344.     pathname_len = pathlen + 1; /* For '\0'; already have '/'. */
  345.       else
  346.     pathname_len = pathlen + 2; /* For '/' and '\0'. */
  347.       cur_path_size = 0;
  348.       cur_path = NULL;
  349.  
  350.       for (namep = name_space; *namep; namep += file_len - pathname_len + 1)
  351.     {
  352.       /* Append this directory entry's name to the path being searched. */
  353.       file_len = pathname_len + strlen (namep);
  354.       if (file_len > cur_path_size)
  355.         {
  356.           while (file_len > cur_path_size)
  357.         cur_path_size += 1024;
  358.           if (cur_path)
  359.         free (cur_path);
  360.           cur_path = xmalloc (cur_path_size);
  361.           strcpy (cur_path, pathname);
  362.           cur_path[pathname_len - 2] = '/';
  363.         }
  364.       strcpy (cur_path + pathname_len - 1, namep);
  365.  
  366.       curdepth++;
  367.       if (!no_leaf_check)
  368.         /* Normal case optimization.
  369.            On normal Unix filesystems, a directory that has no
  370.            subdirectories has two links: its name, and ".".  Any
  371.            additional links are to the ".." entries of its
  372.            subdirectories.  Once we have processed as many
  373.            subdirectories as there are additional links, we know
  374.            that the rest of the entries are non-directories --
  375.            in other words, leaf files. */
  376.         subdirs_left -= process_path (cur_path, subdirs_left == 0);
  377.       else
  378.         /* There might be weird (NFS?) filesystems mounted,
  379.            which don't have Unix-like directory link counts. */
  380.         process_path (cur_path, false);
  381.       curdepth--;
  382.     }
  383.       if (cur_path)
  384.     free (cur_path);
  385.       free (name_space);
  386.     }
  387. }
  388.  
  389. /* Return true if there are no side effects in any of the predicates in
  390.    predicate list PRED, false if there are any. */
  391.  
  392. boolean
  393. no_side_effects (pred)
  394.      struct predicate *pred;
  395. {
  396.   while (pred != NULL)
  397.     {
  398.       if (pred->side_effects)
  399.     return (false);
  400.       pred = pred->pred_next;
  401.     }
  402.   return (true);
  403. }
  404.