1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Cimarron D. Taylor of the University of California, Berkeley. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)option.c 8.2 (Berkeley) 4/16/94 33 * $FreeBSD: head/usr.bin/find/option.c 247730 2013-03-03 20:10:56Z dwmalone $ 34 */ 35 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 39 #include <err.h> 40 #include <fts.h> 41 #include <regex.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "find.h" 47 48 static int typecompare(const void *, const void *); 49 50 /* NB: the following table must be sorted lexically. */ 51 /* Options listed with C++ comments are in gnu find, but not our find */ 52 static OPTION const options[] = { 53 { "!", c_simple, f_not, 0 }, 54 { "(", c_simple, f_openparen, 0 }, 55 { ")", c_simple, f_closeparen, 0 }, 56 { "-a", c_and, NULL, 0 }, 57 { "-amin", c_Xmin, f_Xmin, F_TIME_A }, 58 { "-and", c_and, NULL, 0 }, 59 { "-anewer", c_newer, f_newer, F_TIME_A }, 60 { "-atime", c_Xtime, f_Xtime, F_TIME_A }, 61 { "-cmin", c_Xmin, f_Xmin, F_TIME_C }, 62 { "-cnewer", c_newer, f_newer, F_TIME_C }, 63 { "-ctime", c_Xtime, f_Xtime, F_TIME_C }, 64 { "-d", c_depth, f_depth, 0 }, 65 // -daystart 66 { "-delete", c_delete, f_delete, 0 }, 67 { "-depth", c_depth, f_depth, 0 }, 68 { "-empty", c_empty, f_empty, 0 }, 69 { "-exec", c_exec, f_exec, 0 }, 70 { "-execdir", c_exec, f_exec, F_EXECDIR }, 71 { "-false", c_simple, f_false, 0 }, 72 { "-flags", c_flags, f_flags, 0 }, 73 // -fls 74 { "-follow", c_follow, f_always_true, 0 }, 75 // -fprint 76 // -fprint0 77 // -fprintf 78 { "-fstype", c_fstype, f_fstype, 0 }, 79 { "-gid", c_group, f_group, 0 }, 80 { "-group", c_group, f_group, 0 }, 81 { "-ignore_readdir_race",c_ignore_readdir_race, f_always_true,0 }, 82 { "-ilname", c_name, f_name, F_LINK | F_IGNCASE }, 83 { "-iname", c_name, f_name, F_IGNCASE }, 84 { "-inum", c_inum, f_inum, 0 }, 85 { "-ipath", c_name, f_path, F_IGNCASE }, 86 { "-iregex", c_regex, f_regex, F_IGNCASE }, 87 { "-iwholename",c_name, f_path, F_IGNCASE }, 88 { "-links", c_links, f_links, 0 }, 89 { "-lname", c_name, f_name, F_LINK }, 90 { "-ls", c_ls, f_ls, 0 }, 91 { "-maxdepth", c_mXXdepth, f_always_true, F_MAXDEPTH }, 92 { "-mindepth", c_mXXdepth, f_always_true, 0 }, 93 { "-mmin", c_Xmin, f_Xmin, 0 }, 94 { "-mnewer", c_newer, f_newer, 0 }, 95 { "-mount", c_xdev, f_always_true, 0 }, 96 { "-mtime", c_Xtime, f_Xtime, 0 }, 97 { "-name", c_name, f_name, 0 }, 98 { "-newer", c_newer, f_newer, 0 }, 99 { "-neweraa", c_newer, f_newer, F_TIME_A | F_TIME2_A }, 100 { "-newerac", c_newer, f_newer, F_TIME_A | F_TIME2_C }, 101 { "-neweram", c_newer, f_newer, F_TIME_A }, 102 { "-newerat", c_newer, f_newer, F_TIME_A | F_TIME2_T }, 103 { "-newerca", c_newer, f_newer, F_TIME_C | F_TIME2_A }, 104 { "-newercc", c_newer, f_newer, F_TIME_C | F_TIME2_C }, 105 { "-newercm", c_newer, f_newer, F_TIME_C }, 106 { "-newerct", c_newer, f_newer, F_TIME_C | F_TIME2_T }, 107 { "-newerma", c_newer, f_newer, F_TIME2_A }, 108 { "-newermc", c_newer, f_newer, F_TIME2_C }, 109 { "-newermm", c_newer, f_newer, 0 }, 110 { "-newermt", c_newer, f_newer, F_TIME2_T }, 111 { "-nogroup", c_nogroup, f_nogroup, 0 }, 112 { "-noignore_readdir_race",c_ignore_readdir_race, f_always_true,0 }, 113 { "-noleaf", c_simple, f_always_true, 0 }, 114 { "-not", c_simple, f_not, 0 }, 115 { "-nouser", c_nouser, f_nouser, 0 }, 116 { "-o", c_simple, f_or, 0 }, 117 { "-ok", c_exec, f_exec, F_NEEDOK }, 118 { "-okdir", c_exec, f_exec, F_NEEDOK | F_EXECDIR }, 119 { "-or", c_simple, f_or, 0 }, 120 { "-path", c_name, f_path, 0 }, 121 { "-perm", c_perm, f_perm, 0 }, 122 { "-print", c_print, f_print, 0 }, 123 { "-print0", c_print, f_print0, 0 }, 124 // -printf 125 { "-prune", c_simple, f_prune, 0 }, 126 { "-quit", c_simple, f_quit, 0 }, 127 { "-regex", c_regex, f_regex, 0 }, 128 { "-samefile", c_samefile, f_inum, 0 }, 129 { "-size", c_size, f_size, 0 }, 130 { "-sparse", c_sparse, f_sparse, 0 }, 131 { "-true", c_simple, f_always_true, 0 }, 132 { "-type", c_type, f_type, 0 }, 133 { "-uid", c_user, f_user, 0 }, 134 { "-user", c_user, f_user, 0 }, 135 { "-wholename", c_name, f_path, 0 }, 136 { "-xdev", c_xdev, f_always_true, 0 }, 137 // -xtype 138 }; 139 140 /* 141 * find_create -- 142 * create a node corresponding to a command line argument. 143 * 144 * TODO: 145 * add create/process function pointers to node, so we can skip 146 * this switch stuff. 147 */ 148 PLAN * 149 find_create(char ***argvp) 150 { 151 OPTION *p; 152 PLAN *new; 153 char **argv; 154 155 argv = *argvp; 156 157 if ((p = lookup_option(*argv)) == NULL) 158 errx(1, "%s: unknown primary or operator", *argv); 159 ++argv; 160 161 new = (p->create)(p, &argv); 162 *argvp = argv; 163 return (new); 164 } 165 166 OPTION * 167 lookup_option(const char *name) 168 { 169 OPTION tmp; 170 171 tmp.name = name; 172 return ((OPTION *)bsearch(&tmp, options, 173 sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare)); 174 } 175 176 static int 177 typecompare(const void *a, const void *b) 178 { 179 return (strcmp(((const OPTION *)a)->name, ((const OPTION *)b)->name)); 180 } 181