140947Sbostic /*- 240947Sbostic * Copyright (c) 1990 The Regents of the University of California. 340947Sbostic * All rights reserved. 440947Sbostic * 540947Sbostic * This code is derived from software contributed to Berkeley by 640947Sbostic * Cimarron D. Taylor of the University of California, Berkeley. 740947Sbostic * 840947Sbostic * %sccs.include.redist.c% 940947Sbostic */ 1040947Sbostic 1140947Sbostic #ifndef lint 12*49868Sbostic static char sccsid[] = "@(#)function.c 5.16 (Berkeley) 05/24/91"; 1340947Sbostic #endif /* not lint */ 1440947Sbostic 1545868Sbostic #include <sys/param.h> 1640947Sbostic #include <sys/stat.h> 1740947Sbostic #include <sys/wait.h> 1840947Sbostic #include <sys/mount.h> 1947192Sbostic #include <errno.h> 2040947Sbostic #include <grp.h> 2140947Sbostic #include <pwd.h> 2240947Sbostic #include <fts.h> 2340947Sbostic #include <unistd.h> 2440947Sbostic #include <tzfile.h> 2540947Sbostic #include <stdio.h> 2647192Sbostic #include <stdlib.h> 2742047Sbostic #include <string.h> 2840947Sbostic #include "find.h" 2940947Sbostic 3040947Sbostic #define FIND_EQUAL 0 3140947Sbostic #define FIND_LESSTHAN 1 3240947Sbostic #define FIND_GREATER 2 3340947Sbostic 3440947Sbostic #define COMPARE(a, b) { \ 3540947Sbostic switch(plan->flags) { \ 3640947Sbostic case FIND_EQUAL: \ 3740947Sbostic return(a == b); \ 3840947Sbostic case FIND_LESSTHAN: \ 3940947Sbostic return(a < b); \ 4040947Sbostic case FIND_GREATER: \ 4140947Sbostic return(a > b); \ 4240947Sbostic } \ 4342255Sbostic return(0); \ 4440947Sbostic } 4540947Sbostic 46*49868Sbostic static PLAN *palloc __P((enum ntype, int (*)())); 4740947Sbostic 4840947Sbostic /* 4940947Sbostic * find_parsenum -- 5040947Sbostic * Parse a string of the form [+-]# and return the value. 5140947Sbostic */ 5240947Sbostic long 5340947Sbostic find_parsenum(plan, option, str, endch) 5440947Sbostic PLAN *plan; 5540947Sbostic char *option, *str, *endch; 5640947Sbostic { 5740947Sbostic long value; 5840947Sbostic char *endchar; /* pointer to character ending conversion */ 5940947Sbostic 6040947Sbostic /* determine comparison from leading + or - */ 6140947Sbostic switch(*str) { 6240947Sbostic case '+': 6340947Sbostic ++str; 6440947Sbostic plan->flags = FIND_GREATER; 6540947Sbostic break; 6640947Sbostic case '-': 6740947Sbostic ++str; 6840947Sbostic plan->flags = FIND_LESSTHAN; 6940947Sbostic break; 7040947Sbostic default: 7140947Sbostic plan->flags = FIND_EQUAL; 7240947Sbostic break; 7340947Sbostic } 7440947Sbostic 7540947Sbostic /* 7640947Sbostic * convert the string with strtol(). Note, if strtol() returns zero 7740947Sbostic * and endchar points to the beginning of the string we know we have 7840947Sbostic * a syntax error. 7940947Sbostic */ 8040947Sbostic value = strtol(str, &endchar, 10); 8140947Sbostic if (!value && endchar == str || 8240947Sbostic endchar[0] && (!endch || endchar[0] != *endch)) 83*49868Sbostic err("%s: %s", option, "illegal numeric value"); 8440947Sbostic if (endch) 8540947Sbostic *endch = endchar[0]; 8640947Sbostic return(value); 8740947Sbostic } 8840947Sbostic 8940947Sbostic /* 9040947Sbostic * -atime n functions -- 9140947Sbostic * 9240947Sbostic * True if the difference between the file access time and the 9340947Sbostic * current time is n 24 hour periods. 9440947Sbostic * 9540947Sbostic */ 9640947Sbostic f_atime(plan, entry) 9740947Sbostic PLAN *plan; 9840947Sbostic FTSENT *entry; 9940947Sbostic { 10040947Sbostic extern time_t now; 10140947Sbostic 10242255Sbostic COMPARE((now - entry->fts_statb.st_atime + 10342255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 10440947Sbostic } 10540947Sbostic 10640947Sbostic PLAN * 10740947Sbostic c_atime(arg) 10840947Sbostic char *arg; 10940947Sbostic { 11040947Sbostic PLAN *new; 11140947Sbostic 11240947Sbostic ftsoptions &= ~FTS_NOSTAT; 11340947Sbostic 114*49868Sbostic new = palloc(N_ATIME, f_atime); 115*49868Sbostic new->t_data = find_parsenum(new, "-atime", arg, NULL); 11640947Sbostic return(new); 11740947Sbostic } 11840947Sbostic /* 11940947Sbostic * -ctime n functions -- 12040947Sbostic * 12140947Sbostic * True if the difference between the last change of file 12240947Sbostic * status information and the current time is n 24 hour periods. 12340947Sbostic */ 12440947Sbostic f_ctime(plan, entry) 12540947Sbostic PLAN *plan; 12640947Sbostic FTSENT *entry; 12740947Sbostic { 12840947Sbostic extern time_t now; 12940947Sbostic 13042255Sbostic COMPARE((now - entry->fts_statb.st_ctime + 13142255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 13240947Sbostic } 13340947Sbostic 13440947Sbostic PLAN * 13540947Sbostic c_ctime(arg) 13640947Sbostic char *arg; 13740947Sbostic { 13840947Sbostic PLAN *new; 13940947Sbostic 14040947Sbostic ftsoptions &= ~FTS_NOSTAT; 14140947Sbostic 142*49868Sbostic new = palloc(N_CTIME, f_ctime); 14340947Sbostic new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL); 14440947Sbostic return(new); 14540947Sbostic } 14640947Sbostic 14740947Sbostic /* 14840947Sbostic * -depth functions -- 14940947Sbostic * 15040947Sbostic * Always true, causes descent of the directory hierarchy to be done 15140947Sbostic * so that all entries in a directory are acted on before the directory 15240947Sbostic * itself. 15340947Sbostic */ 15440947Sbostic /* ARGSUSED */ 15540947Sbostic f_always_true(plan, entry) 15640947Sbostic PLAN *plan; 15740947Sbostic FTSENT *entry; 15840947Sbostic { 15942255Sbostic return(1); 16040947Sbostic } 16140947Sbostic 16240947Sbostic PLAN * 16340947Sbostic c_depth() 16440947Sbostic { 16545615Sbostic isdepth = 1; 16640947Sbostic 167*49868Sbostic return(palloc(N_DEPTH, f_always_true)); 16840947Sbostic } 16940947Sbostic 17040947Sbostic /* 17140947Sbostic * [-exec | -ok] utility [arg ... ] ; functions -- 17240947Sbostic * 17340947Sbostic * True if the executed utility returns a zero value as exit status. 17440947Sbostic * The end of the primary expression is delimited by a semicolon. If 17540947Sbostic * "{}" occurs anywhere, it gets replaced by the current pathname. 17640947Sbostic * The current directory for the execution of utility is the same as 17740947Sbostic * the current directory when the find utility was started. 17840947Sbostic * 17940947Sbostic * The primary -ok is different in that it requests affirmation of the 18040947Sbostic * user before executing the utility. 18140947Sbostic */ 18240947Sbostic f_exec(plan, entry) 18340947Sbostic register PLAN *plan; 18440947Sbostic FTSENT *entry; 18540947Sbostic { 18640947Sbostic register int cnt; 18747192Sbostic pid_t pid; 18847192Sbostic int status; 18940947Sbostic 19040947Sbostic for (cnt = 0; plan->e_argv[cnt]; ++cnt) 19140947Sbostic if (plan->e_len[cnt]) 19245616Sbostic brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 19345404Sbostic entry->fts_accpath, plan->e_len[cnt]); 19440947Sbostic 19545616Sbostic if (plan->flags && !queryuser(plan->e_argv)) 19642255Sbostic return(0); 19740947Sbostic 19840947Sbostic switch(pid = vfork()) { 19940947Sbostic case -1: 200*49868Sbostic err("fork: %s", strerror(errno)); 20140947Sbostic /* NOTREACHED */ 20240947Sbostic case 0: 20340947Sbostic execvp(plan->e_argv[0], plan->e_argv); 204*49868Sbostic err("%s: %s", plan->e_argv[0], strerror(errno)); 20540947Sbostic /* NOTREACHED */ 20640947Sbostic } 20747192Sbostic pid = waitpid(pid, &status, 0); 20847192Sbostic return(pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 20940947Sbostic } 21040947Sbostic 21140947Sbostic /* 21240947Sbostic * c_exec -- 21340947Sbostic * build three parallel arrays, one with pointers to the strings passed 21440947Sbostic * on the command line, one with (possibly duplicated) pointers to the 21540947Sbostic * argv array, and one with integer values that are lengths of the 21640947Sbostic * strings, but also flags meaning that the string has to be massaged. 21740947Sbostic */ 21840947Sbostic PLAN * 21940947Sbostic c_exec(argvp, isok) 22040947Sbostic char ***argvp; 22140947Sbostic int isok; 22240947Sbostic { 22340947Sbostic PLAN *new; /* node returned */ 22440947Sbostic register int cnt; 22540947Sbostic register char **argv, **ap, *p; 22640947Sbostic 22745615Sbostic if (!isrelative) 22845404Sbostic ftsoptions |= FTS_NOCHDIR; 22947192Sbostic isoutput = 1; 23040947Sbostic 231*49868Sbostic new = palloc(N_EXEC, f_exec); 23240947Sbostic new->flags = isok; 23340947Sbostic 23440947Sbostic for (ap = argv = *argvp;; ++ap) { 23540947Sbostic if (!*ap) 236*49868Sbostic err("%s: %s", 237*49868Sbostic isok ? "-ok" : "-exec", "no terminating \";\""); 23840947Sbostic if (**ap == ';') 23940947Sbostic break; 24040947Sbostic } 24140947Sbostic 24240947Sbostic cnt = ap - *argvp + 1; 24340947Sbostic new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 24440947Sbostic new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 24545868Sbostic new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 24640947Sbostic 24740947Sbostic for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 24840947Sbostic new->e_orig[cnt] = *argv; 24940947Sbostic for (p = *argv; *p; ++p) 25040947Sbostic if (p[0] == '{' && p[1] == '}') { 25145868Sbostic new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 25245868Sbostic new->e_len[cnt] = MAXPATHLEN; 25340947Sbostic break; 25440947Sbostic } 25540947Sbostic if (!*p) { 25640947Sbostic new->e_argv[cnt] = *argv; 25740947Sbostic new->e_len[cnt] = 0; 25840947Sbostic } 25940947Sbostic } 26040947Sbostic new->e_argv[cnt] = new->e_orig[cnt] = NULL; 26140947Sbostic 26240947Sbostic *argvp = argv + 1; 26340947Sbostic return(new); 26440947Sbostic } 26540947Sbostic 26640947Sbostic /* 26740947Sbostic * -follow functions -- 26840947Sbostic * 26940947Sbostic * Always true, causes symbolic links to be followed on a global 27040947Sbostic * basis. 27140947Sbostic */ 27240947Sbostic PLAN * 27340947Sbostic c_follow() 27440947Sbostic { 27540947Sbostic ftsoptions &= ~FTS_PHYSICAL; 27640947Sbostic ftsoptions |= FTS_LOGICAL; 27740947Sbostic 278*49868Sbostic return(palloc(N_FOLLOW, f_always_true)); 27940947Sbostic } 28040947Sbostic 28140947Sbostic /* 28240947Sbostic * -fstype functions -- 28340947Sbostic * 28440947Sbostic * True if the file is of a certain type. 28540947Sbostic */ 28640947Sbostic f_fstype(plan, entry) 28740947Sbostic PLAN *plan; 28840947Sbostic FTSENT *entry; 28940947Sbostic { 29042275Sbostic static dev_t curdev; /* need a guaranteed illegal dev value */ 29142275Sbostic static int first = 1; 29240947Sbostic struct statfs sb; 29342255Sbostic static short val; 29445626Sbostic char *p, save[2]; 29540947Sbostic 29640947Sbostic /* only check when we cross mount point */ 29742275Sbostic if (first || curdev != entry->fts_statb.st_dev) { 29843856Sbostic curdev = entry->fts_statb.st_dev; 29945626Sbostic 30045626Sbostic /* 30145626Sbostic * Statfs follows symlinks; find wants the link's file system, 30245626Sbostic * not where it points. 30345626Sbostic */ 30445626Sbostic if (entry->fts_info == FTS_SL || 30545626Sbostic entry->fts_info == FTS_SLNONE) { 30645626Sbostic if (p = rindex(entry->fts_accpath, '/')) 30745626Sbostic ++p; 30845626Sbostic else 30945626Sbostic p = entry->fts_accpath; 31045626Sbostic save[0] = p[0]; 31145626Sbostic p[0] = '.'; 31245626Sbostic save[1] = p[1]; 31345626Sbostic p[1] = '\0'; 31445626Sbostic 31545626Sbostic } else 31645626Sbostic p = NULL; 31745626Sbostic 318*49868Sbostic if (statfs(entry->fts_accpath, &sb)) 319*49868Sbostic err("%s: %s", entry->fts_accpath, strerror(errno)); 32045626Sbostic 32145626Sbostic if (p) { 32245626Sbostic p[0] = save[0]; 32345626Sbostic p[1] = save[1]; 32445626Sbostic } 32545626Sbostic 32642275Sbostic first = 0; 32742255Sbostic val = plan->flags == MOUNT_NONE ? sb.f_flags : sb.f_type; 32840947Sbostic } 32942255Sbostic return(plan->flags == MOUNT_NONE ? 33042255Sbostic val & MNT_LOCAL : val == plan->flags); 33140947Sbostic } 33240947Sbostic 33340947Sbostic PLAN * 33440947Sbostic c_fstype(arg) 33540947Sbostic char *arg; 33640947Sbostic { 33740947Sbostic register PLAN *new; 33840947Sbostic 33940947Sbostic ftsoptions &= ~FTS_NOSTAT; 34040947Sbostic 341*49868Sbostic new = palloc(N_FSTYPE, f_fstype); 34240947Sbostic switch(*arg) { 34342255Sbostic case 'l': 34442255Sbostic if (!strcmp(arg, "local")) { 34542255Sbostic new->flags = MOUNT_NONE; 34642255Sbostic return(new); 34742255Sbostic } 34842255Sbostic break; 34940947Sbostic case 'm': 35040947Sbostic if (!strcmp(arg, "mfs")) { 35140947Sbostic new->flags = MOUNT_MFS; 35240947Sbostic return(new); 35340947Sbostic } 35440947Sbostic break; 35540947Sbostic case 'n': 35640947Sbostic if (!strcmp(arg, "nfs")) { 35740947Sbostic new->flags = MOUNT_NFS; 35840947Sbostic return(new); 35940947Sbostic } 36040947Sbostic break; 36140947Sbostic case 'p': 36240947Sbostic if (!strcmp(arg, "pc")) { 36340947Sbostic new->flags = MOUNT_PC; 36440947Sbostic return(new); 36540947Sbostic } 36640947Sbostic break; 36740947Sbostic case 'u': 36840947Sbostic if (!strcmp(arg, "ufs")) { 36940947Sbostic new->flags = MOUNT_UFS; 37040947Sbostic return(new); 37140947Sbostic } 37240947Sbostic break; 37340947Sbostic } 374*49868Sbostic err("unknown file type %s", arg); 37540947Sbostic /* NOTREACHED */ 37640947Sbostic } 37740947Sbostic 37840947Sbostic /* 37940947Sbostic * -group gname functions -- 38040947Sbostic * 38140947Sbostic * True if the file belongs to the group gname. If gname is numeric and 38240947Sbostic * an equivalent of the getgrnam() function does not return a valid group 38340947Sbostic * name, gname is taken as a group ID. 38440947Sbostic */ 38540947Sbostic f_group(plan, entry) 38640947Sbostic PLAN *plan; 38740947Sbostic FTSENT *entry; 38840947Sbostic { 38942255Sbostic return(entry->fts_statb.st_gid == plan->g_data); 39040947Sbostic } 39140947Sbostic 39240947Sbostic PLAN * 39340947Sbostic c_group(gname) 39440947Sbostic char *gname; 39540947Sbostic { 39640947Sbostic PLAN *new; 39740947Sbostic struct group *g; 39840947Sbostic gid_t gid; 39940947Sbostic 40040947Sbostic ftsoptions &= ~FTS_NOSTAT; 40140947Sbostic 40240947Sbostic g = getgrnam(gname); 40340947Sbostic if (g == NULL) { 40440947Sbostic gid = atoi(gname); 40540947Sbostic if (gid == 0 && gname[0] != '0') 406*49868Sbostic err("%s: %s", "-group", "no such group"); 40740947Sbostic } else 40840947Sbostic gid = g->gr_gid; 40940947Sbostic 410*49868Sbostic new = palloc(N_GROUP, f_group); 41140947Sbostic new->g_data = gid; 41240947Sbostic return(new); 41340947Sbostic } 41440947Sbostic 41540947Sbostic /* 41640947Sbostic * -inum n functions -- 41740947Sbostic * 41840947Sbostic * True if the file has inode # n. 41940947Sbostic */ 42040947Sbostic f_inum(plan, entry) 42140947Sbostic PLAN *plan; 42240947Sbostic FTSENT *entry; 42340947Sbostic { 42442255Sbostic COMPARE(entry->fts_statb.st_ino, plan->i_data); 42540947Sbostic } 42640947Sbostic 42740947Sbostic PLAN * 42840947Sbostic c_inum(arg) 42940947Sbostic char *arg; 43040947Sbostic { 43140947Sbostic PLAN *new; 43240947Sbostic 43340947Sbostic ftsoptions &= ~FTS_NOSTAT; 43440947Sbostic 435*49868Sbostic new = palloc(N_INUM, f_inum); 43640947Sbostic new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL); 43740947Sbostic return(new); 43840947Sbostic } 43940947Sbostic 44040947Sbostic /* 44140947Sbostic * -links n functions -- 44240947Sbostic * 44340947Sbostic * True if the file has n links. 44440947Sbostic */ 44540947Sbostic f_links(plan, entry) 44640947Sbostic PLAN *plan; 44740947Sbostic FTSENT *entry; 44840947Sbostic { 44942255Sbostic COMPARE(entry->fts_statb.st_nlink, plan->l_data); 45040947Sbostic } 45140947Sbostic 45240947Sbostic PLAN * 45340947Sbostic c_links(arg) 45440947Sbostic char *arg; 45540947Sbostic { 45640947Sbostic PLAN *new; 45740947Sbostic 45840947Sbostic ftsoptions &= ~FTS_NOSTAT; 45940947Sbostic 460*49868Sbostic new = palloc(N_LINKS, f_links); 46145616Sbostic new->l_data = (nlink_t)find_parsenum(new, "-links", arg, (char *)NULL); 46240947Sbostic return(new); 46340947Sbostic } 46440947Sbostic 46540947Sbostic /* 46640947Sbostic * -ls functions -- 46740947Sbostic * 46840947Sbostic * Always true - prints the current entry to stdout in "ls" format. 46940947Sbostic */ 47040947Sbostic /* ARGSUSED */ 47140947Sbostic f_ls(plan, entry) 47240947Sbostic PLAN *plan; 47340947Sbostic FTSENT *entry; 47440947Sbostic { 47545615Sbostic void printlong(); 47645615Sbostic 47742255Sbostic printlong(entry->fts_path, entry->fts_accpath, &entry->fts_statb); 47842255Sbostic return(1); 47940947Sbostic } 48040947Sbostic 48140947Sbostic PLAN * 48240947Sbostic c_ls() 48340947Sbostic { 48440947Sbostic ftsoptions &= ~FTS_NOSTAT; 48545615Sbostic isoutput = 1; 48640947Sbostic 487*49868Sbostic return(palloc(N_LS, f_ls)); 48840947Sbostic } 48940947Sbostic 49040947Sbostic /* 49140947Sbostic * -name functions -- 49240947Sbostic * 49340947Sbostic * True if the basename of the filename being examined 49440947Sbostic * matches pattern using Pattern Matching Notation S3.14 49540947Sbostic */ 49640947Sbostic f_name(plan, entry) 49740947Sbostic PLAN *plan; 49840947Sbostic FTSENT *entry; 49940947Sbostic { 50042255Sbostic return(fnmatch(plan->c_data, entry->fts_name, FNM_QUOTE)); 50140947Sbostic } 50240947Sbostic 50340947Sbostic PLAN * 50440947Sbostic c_name(pattern) 50540947Sbostic char *pattern; 50640947Sbostic { 50740947Sbostic PLAN *new; 50840947Sbostic 509*49868Sbostic new = palloc(N_NAME, f_name); 51040947Sbostic new->c_data = pattern; 51140947Sbostic return(new); 51240947Sbostic } 51340947Sbostic 51440947Sbostic /* 51540947Sbostic * -newer file functions -- 51640947Sbostic * 51740947Sbostic * True if the current file has been modified more recently 51840947Sbostic * then the modification time of the file named by the pathname 51940947Sbostic * file. 52040947Sbostic */ 52140947Sbostic f_newer(plan, entry) 52240947Sbostic PLAN *plan; 52340947Sbostic FTSENT *entry; 52440947Sbostic { 52542255Sbostic return(entry->fts_statb.st_mtime > plan->t_data); 52640947Sbostic } 52740947Sbostic 52840947Sbostic PLAN * 52940947Sbostic c_newer(filename) 53040947Sbostic char *filename; 53140947Sbostic { 53240947Sbostic PLAN *new; 53340947Sbostic struct stat sb; 53440947Sbostic 53540947Sbostic ftsoptions &= ~FTS_NOSTAT; 53640947Sbostic 537*49868Sbostic if (stat(filename, &sb)) 538*49868Sbostic err("%s: %s", filename, strerror(errno)); 539*49868Sbostic new = palloc(N_NEWER, f_newer); 54040947Sbostic new->t_data = sb.st_mtime; 54140947Sbostic return(new); 54240947Sbostic } 54340947Sbostic 54440947Sbostic /* 54540947Sbostic * -nogroup functions -- 54640947Sbostic * 54740947Sbostic * True if file belongs to a user ID for which the equivalent 54840947Sbostic * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 54940947Sbostic */ 55040947Sbostic /* ARGSUSED */ 55140947Sbostic f_nogroup(plan, entry) 55240947Sbostic PLAN *plan; 55340947Sbostic FTSENT *entry; 55440947Sbostic { 55545615Sbostic char *group_from_gid(); 55645615Sbostic 55745615Sbostic return(group_from_gid(entry->fts_statb.st_gid, 1) ? 1 : 0); 55840947Sbostic } 55940947Sbostic 56040947Sbostic PLAN * 56140947Sbostic c_nogroup() 56240947Sbostic { 56340947Sbostic ftsoptions &= ~FTS_NOSTAT; 56440947Sbostic 565*49868Sbostic return(palloc(N_NOGROUP, f_nogroup)); 56640947Sbostic } 56740947Sbostic 56840947Sbostic /* 56940947Sbostic * -nouser functions -- 57040947Sbostic * 57140947Sbostic * True if file belongs to a user ID for which the equivalent 57240947Sbostic * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 57340947Sbostic */ 57440947Sbostic /* ARGSUSED */ 57540947Sbostic f_nouser(plan, entry) 57640947Sbostic PLAN *plan; 57740947Sbostic FTSENT *entry; 57840947Sbostic { 57945615Sbostic char *user_from_uid(); 58045615Sbostic 58145615Sbostic return(user_from_uid(entry->fts_statb.st_uid, 1) ? 1 : 0); 58240947Sbostic } 58340947Sbostic 58440947Sbostic PLAN * 58540947Sbostic c_nouser() 58640947Sbostic { 58740947Sbostic ftsoptions &= ~FTS_NOSTAT; 58840947Sbostic 589*49868Sbostic return(palloc(N_NOUSER, f_nouser)); 59040947Sbostic } 59140947Sbostic 59240947Sbostic /* 59340947Sbostic * -perm functions -- 59440947Sbostic * 59540947Sbostic * The mode argument is used to represent file mode bits. If it starts 59640947Sbostic * with a leading digit, it's treated as an octal mode, otherwise as a 59740947Sbostic * symbolic mode. 59840947Sbostic */ 59940947Sbostic f_perm(plan, entry) 60040947Sbostic PLAN *plan; 60140947Sbostic FTSENT *entry; 60240947Sbostic { 60340947Sbostic mode_t mode; 60440947Sbostic 60542255Sbostic mode = entry->fts_statb.st_mode & 60640947Sbostic (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 60740947Sbostic if (plan->flags) 60840947Sbostic return((plan->m_data | mode) == mode); 60940947Sbostic else 61040947Sbostic return(mode == plan->m_data); 61140947Sbostic /* NOTREACHED */ 61240947Sbostic } 61340947Sbostic 61440947Sbostic PLAN * 61540947Sbostic c_perm(perm) 61640947Sbostic char *perm; 61740947Sbostic { 61840947Sbostic PLAN *new; 61947192Sbostic mode_t *set; 62040947Sbostic 62140947Sbostic ftsoptions &= ~FTS_NOSTAT; 62240947Sbostic 623*49868Sbostic new = palloc(N_PERM, f_perm); 62440947Sbostic 62540947Sbostic if (*perm == '-') { 62640947Sbostic new->flags = 1; 62740947Sbostic ++perm; 62840947Sbostic } 62940947Sbostic 63044764Strent if ((set = setmode(perm)) == NULL) 631*49868Sbostic err("%s: %s", "-perm", "illegal mode string"); 63240947Sbostic 63344764Strent new->m_data = getmode(set, 0); 63440947Sbostic return(new); 63540947Sbostic } 63640947Sbostic 63740947Sbostic /* 63840947Sbostic * -print functions -- 63940947Sbostic * 64040947Sbostic * Always true, causes the current pathame to be written to 64140947Sbostic * standard output. 64240947Sbostic */ 64340947Sbostic /* ARGSUSED */ 64440947Sbostic f_print(plan, entry) 64540947Sbostic PLAN *plan; 64640947Sbostic FTSENT *entry; 64740947Sbostic { 64842255Sbostic (void)printf("%s\n", entry->fts_path); 64942255Sbostic return(1); 65040947Sbostic } 65140947Sbostic 65240947Sbostic PLAN * 65340947Sbostic c_print() 65440947Sbostic { 65545615Sbostic isoutput = 1; 65640947Sbostic 657*49868Sbostic return(palloc(N_PRINT, f_print)); 65840947Sbostic } 65940947Sbostic 66040947Sbostic /* 66140947Sbostic * -prune functions -- 66240947Sbostic * 66340947Sbostic * Prune a portion of the hierarchy. 66440947Sbostic */ 66540947Sbostic /* ARGSUSED */ 66640947Sbostic f_prune(plan, entry) 66740947Sbostic PLAN *plan; 66840947Sbostic FTSENT *entry; 66940947Sbostic { 67040947Sbostic extern FTS *tree; 67140947Sbostic 672*49868Sbostic if (fts_set(tree, entry, FTS_SKIP)) 673*49868Sbostic err("%s: %s", entry->fts_path, strerror(errno)); 67442255Sbostic return(1); 67540947Sbostic } 67640947Sbostic 67740947Sbostic PLAN * 67840947Sbostic c_prune() 67940947Sbostic { 680*49868Sbostic return(palloc(N_PRUNE, f_prune)); 68140947Sbostic } 68240947Sbostic 68340947Sbostic /* 68440947Sbostic * -size n[c] functions -- 68540947Sbostic * 68640947Sbostic * True if the file size in bytes, divided by an implementation defined 68740947Sbostic * value and rounded up to the next integer, is n. If n is followed by 68840947Sbostic * a c, the size is in bytes. 68940947Sbostic */ 69040947Sbostic #define FIND_SIZE 512 69140947Sbostic static int divsize = 1; 69240947Sbostic 69340947Sbostic f_size(plan, entry) 69440947Sbostic PLAN *plan; 69540947Sbostic FTSENT *entry; 69640947Sbostic { 69740947Sbostic off_t size; 69840947Sbostic 69942255Sbostic size = divsize ? (entry->fts_statb.st_size + FIND_SIZE - 1) / 70042255Sbostic FIND_SIZE : entry->fts_statb.st_size; 70140947Sbostic COMPARE(size, plan->o_data); 70240947Sbostic } 70340947Sbostic 70440947Sbostic PLAN * 70540947Sbostic c_size(arg) 70640947Sbostic char *arg; 70740947Sbostic { 70840947Sbostic PLAN *new; 70940947Sbostic char endch; 71040947Sbostic 71140947Sbostic ftsoptions &= ~FTS_NOSTAT; 71240947Sbostic 713*49868Sbostic new = palloc(N_SIZE, f_size); 71440947Sbostic new->o_data = find_parsenum(new, "-size", arg, &endch); 71540947Sbostic if (endch == 'c') 71640947Sbostic divsize = 0; 71740947Sbostic return(new); 71840947Sbostic } 71940947Sbostic 72040947Sbostic /* 72140947Sbostic * -type c functions -- 72240947Sbostic * 72340947Sbostic * True if the type of the file is c, where c is b, c, d, p, or f for 72440947Sbostic * block special file, character special file, directory, FIFO, or 72540947Sbostic * regular file, respectively. 72640947Sbostic */ 72740947Sbostic f_type(plan, entry) 72840947Sbostic PLAN *plan; 72940947Sbostic FTSENT *entry; 73040947Sbostic { 73144764Strent return((entry->fts_statb.st_mode & S_IFMT) == plan->m_data); 73240947Sbostic } 73340947Sbostic 73440947Sbostic PLAN * 73540947Sbostic c_type(typestring) 73640947Sbostic char *typestring; 73740947Sbostic { 73840947Sbostic PLAN *new; 73940947Sbostic mode_t mask; 74040947Sbostic 74140947Sbostic ftsoptions &= ~FTS_NOSTAT; 74240947Sbostic 74340947Sbostic switch (typestring[0]) { 74440947Sbostic case 'b': 74540947Sbostic mask = S_IFBLK; 74640947Sbostic break; 74740947Sbostic case 'c': 74840947Sbostic mask = S_IFCHR; 74940947Sbostic break; 75040947Sbostic case 'd': 75140947Sbostic mask = S_IFDIR; 75240947Sbostic break; 75340947Sbostic case 'f': 75440947Sbostic mask = S_IFREG; 75540947Sbostic break; 75640947Sbostic case 'l': 75740947Sbostic mask = S_IFLNK; 75840947Sbostic break; 75940947Sbostic case 'p': 76040947Sbostic mask = S_IFIFO; 76140947Sbostic break; 76240947Sbostic case 's': 76340947Sbostic mask = S_IFSOCK; 76440947Sbostic break; 76540947Sbostic default: 766*49868Sbostic err("%s: %s", "-type", "unknown type"); 76740947Sbostic } 76840947Sbostic 769*49868Sbostic new = palloc(N_TYPE, f_type); 77040947Sbostic new->m_data = mask; 77140947Sbostic return(new); 77240947Sbostic } 77340947Sbostic 77440947Sbostic /* 77540947Sbostic * -user uname functions -- 77640947Sbostic * 77740947Sbostic * True if the file belongs to the user uname. If uname is numeric and 77840947Sbostic * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 77940947Sbostic * return a valid user name, uname is taken as a user ID. 78040947Sbostic */ 78140947Sbostic f_user(plan, entry) 78240947Sbostic PLAN *plan; 78340947Sbostic FTSENT *entry; 78440947Sbostic { 78542255Sbostic return(entry->fts_statb.st_uid == plan->u_data); 78640947Sbostic } 78740947Sbostic 78840947Sbostic PLAN * 78940947Sbostic c_user(username) 79040947Sbostic char *username; 79140947Sbostic { 79240947Sbostic PLAN *new; 79340947Sbostic struct passwd *p; 79440947Sbostic uid_t uid; 79540947Sbostic 79640947Sbostic ftsoptions &= ~FTS_NOSTAT; 79740947Sbostic 79840947Sbostic p = getpwnam(username); 79940947Sbostic if (p == NULL) { 80040947Sbostic uid = atoi(username); 80140947Sbostic if (uid == 0 && username[0] != '0') 802*49868Sbostic err("%s: %s", "-user", "no such user"); 80340947Sbostic } else 80440947Sbostic uid = p->pw_uid; 80540947Sbostic 806*49868Sbostic new = palloc(N_USER, f_user); 80740947Sbostic new->u_data = uid; 80840947Sbostic return(new); 80940947Sbostic } 81040947Sbostic 81140947Sbostic /* 81240947Sbostic * -xdev functions -- 81340947Sbostic * 81440947Sbostic * Always true, causes find not to decend past directories that have a 81540947Sbostic * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 81640947Sbostic */ 81740947Sbostic PLAN * 81840947Sbostic c_xdev() 81940947Sbostic { 82042275Sbostic ftsoptions |= FTS_XDEV; 82140947Sbostic 822*49868Sbostic return(palloc(N_XDEV, f_always_true)); 82340947Sbostic } 82440947Sbostic 82540947Sbostic /* 82640947Sbostic * ( expression ) functions -- 82740947Sbostic * 82840947Sbostic * True if expression is true. 82940947Sbostic */ 83040947Sbostic f_expr(plan, entry) 83140947Sbostic PLAN *plan; 83240947Sbostic FTSENT *entry; 83340947Sbostic { 83440947Sbostic register PLAN *p; 83540947Sbostic register int state; 83640947Sbostic 83740947Sbostic for (p = plan->p_data[0]; 83840947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 83940947Sbostic return(state); 84040947Sbostic } 84140947Sbostic 84240947Sbostic /* 84349864Sbostic * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 84440947Sbostic * eliminated during phase 2 of find_formplan() --- the '(' node is converted 84549864Sbostic * to a N_EXPR node containing the expression and the ')' node is discarded. 84640947Sbostic */ 84740947Sbostic PLAN * 84840947Sbostic c_openparen() 84940947Sbostic { 850*49868Sbostic return(palloc(N_OPENPAREN, (int (*)())-1)); 85140947Sbostic } 85240947Sbostic 85340947Sbostic PLAN * 85440947Sbostic c_closeparen() 85540947Sbostic { 856*49868Sbostic return(palloc(N_CLOSEPAREN, (int (*)())-1)); 85740947Sbostic } 85840947Sbostic 85940947Sbostic /* 86040947Sbostic * -mtime n functions -- 86140947Sbostic * 86240947Sbostic * True if the difference between the file modification time and the 86340947Sbostic * current time is n 24 hour periods. 86440947Sbostic */ 86540947Sbostic f_mtime(plan, entry) 86640947Sbostic PLAN *plan; 86740947Sbostic FTSENT *entry; 86840947Sbostic { 86940947Sbostic extern time_t now; 87040947Sbostic 87142255Sbostic COMPARE((now - entry->fts_statb.st_mtime + SECSPERDAY - 1) / 87242255Sbostic SECSPERDAY, plan->t_data); 87340947Sbostic } 87440947Sbostic 87540947Sbostic PLAN * 87640947Sbostic c_mtime(arg) 87740947Sbostic char *arg; 87840947Sbostic { 87940947Sbostic PLAN *new; 88040947Sbostic 88140947Sbostic ftsoptions &= ~FTS_NOSTAT; 88240947Sbostic 883*49868Sbostic new = palloc(N_MTIME, f_mtime); 88440947Sbostic new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL); 88540947Sbostic return(new); 88640947Sbostic } 88740947Sbostic 88840947Sbostic /* 88940947Sbostic * ! expression functions -- 89040947Sbostic * 89140947Sbostic * Negation of a primary; the unary NOT operator. 89240947Sbostic */ 89340947Sbostic f_not(plan, entry) 89440947Sbostic PLAN *plan; 89540947Sbostic FTSENT *entry; 89640947Sbostic { 89740947Sbostic register PLAN *p; 89840947Sbostic register int state; 89940947Sbostic 90040947Sbostic for (p = plan->p_data[0]; 90140947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 90240947Sbostic return(!state); 90340947Sbostic } 90440947Sbostic 90540947Sbostic PLAN * 90640947Sbostic c_not() 90740947Sbostic { 908*49868Sbostic return(palloc(N_NOT, f_not)); 90940947Sbostic } 91040947Sbostic 91140947Sbostic /* 91240947Sbostic * expression -o expression functions -- 91340947Sbostic * 91440947Sbostic * Alternation of primaries; the OR operator. The second expression is 91540947Sbostic * not evaluated if the first expression is true. 91640947Sbostic */ 91740947Sbostic f_or(plan, entry) 91840947Sbostic PLAN *plan; 91940947Sbostic FTSENT *entry; 92040947Sbostic { 92140947Sbostic register PLAN *p; 92240947Sbostic register int state; 92340947Sbostic 92440947Sbostic for (p = plan->p_data[0]; 92540947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 92640947Sbostic 92740947Sbostic if (state) 92842255Sbostic return(1); 92940947Sbostic 93040947Sbostic for (p = plan->p_data[1]; 93140947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 93240947Sbostic return(state); 93340947Sbostic } 93440947Sbostic 93540947Sbostic PLAN * 93640947Sbostic c_or() 93740947Sbostic { 938*49868Sbostic return(palloc(N_OR, f_or)); 939*49868Sbostic } 940*49868Sbostic 941*49868Sbostic static PLAN * 942*49868Sbostic palloc(t, f) 943*49868Sbostic enum ntype t; 944*49868Sbostic int (*f)(); 945*49868Sbostic { 94640947Sbostic PLAN *new; 94740947Sbostic 948*49868Sbostic if (new = malloc(sizeof(PLAN))) { 949*49868Sbostic new->type = t; 950*49868Sbostic new->eval = f; 951*49868Sbostic new->flags = 0; 952*49868Sbostic new->next = NULL; 953*49868Sbostic return(new); 954*49868Sbostic } 955*49868Sbostic err("%s", strerror(errno)); 956*49868Sbostic /* NOTREACHED */ 95740947Sbostic } 958