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*59613Sbostic static char sccsid[] = "@(#)function.c 5.26 (Berkeley) 05/01/93"; 1340947Sbostic #endif /* not lint */ 1440947Sbostic 1545868Sbostic #include <sys/param.h> 1651833Sbostic #include <sys/ucred.h> 1740947Sbostic #include <sys/stat.h> 1840947Sbostic #include <sys/wait.h> 1940947Sbostic #include <sys/mount.h> 2054558Sbostic 2159612Sbostic #include <err.h> 2247192Sbostic #include <errno.h> 2359612Sbostic #include <fnmatch.h> 2459612Sbostic #include <fts.h> 2540947Sbostic #include <grp.h> 2640947Sbostic #include <pwd.h> 2740947Sbostic #include <stdio.h> 2847192Sbostic #include <stdlib.h> 2942047Sbostic #include <string.h> 3059612Sbostic #include <tzfile.h> 3159612Sbostic #include <unistd.h> 3259612Sbostic 3340947Sbostic #include "find.h" 3440947Sbostic 3559612Sbostic #define COMPARE(a, b) { \ 3659612Sbostic switch (plan->flags) { \ 3759612Sbostic case F_EQUAL: \ 3859612Sbostic return (a == b); \ 3959612Sbostic case F_LESSTHAN: \ 4059612Sbostic return (a < b); \ 4159612Sbostic case F_GREATER: \ 4259612Sbostic return (a > b); \ 4359612Sbostic default: \ 4459612Sbostic abort(); \ 4559612Sbostic } \ 4640947Sbostic } 4740947Sbostic 4859612Sbostic static PLAN *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *)))); 4940947Sbostic 5040947Sbostic /* 5140947Sbostic * find_parsenum -- 5240947Sbostic * Parse a string of the form [+-]# and return the value. 5340947Sbostic */ 5455710Sbostic static long 5559612Sbostic find_parsenum(plan, option, vp, endch) 5640947Sbostic PLAN *plan; 5759612Sbostic char *option, *vp, *endch; 5840947Sbostic { 5940947Sbostic long value; 60*59613Sbostic char *endchar, *str; /* Pointer to character ending conversion. */ 6140947Sbostic 62*59613Sbostic /* Determine comparison from leading + or -. */ 6359612Sbostic str = vp; 6459612Sbostic switch (*str) { 6540947Sbostic case '+': 6640947Sbostic ++str; 6750437Sbostic plan->flags = F_GREATER; 6840947Sbostic break; 6940947Sbostic case '-': 7040947Sbostic ++str; 7150437Sbostic plan->flags = F_LESSTHAN; 7240947Sbostic break; 7340947Sbostic default: 7450437Sbostic plan->flags = F_EQUAL; 7540947Sbostic break; 7640947Sbostic } 7740947Sbostic 7840947Sbostic /* 7955710Sbostic * Convert the string with strtol(). Note, if strtol() returns zero 8040947Sbostic * and endchar points to the beginning of the string we know we have 8140947Sbostic * a syntax error. 8240947Sbostic */ 8340947Sbostic value = strtol(str, &endchar, 10); 8455710Sbostic if (value == 0 && endchar == str) 8559612Sbostic errx(1, "%s: %s: illegal numeric value", option, vp); 8655710Sbostic if (endchar[0] && (endch == NULL || endchar[0] != *endch)) 8759612Sbostic errx(1, "%s: %s: illegal trailing character", option, vp); 8840947Sbostic if (endch) 8940947Sbostic *endch = endchar[0]; 9059612Sbostic return (value); 9140947Sbostic } 9240947Sbostic 9340947Sbostic /* 94*59613Sbostic * The value of n for the inode times (atime, ctime, and mtime) is a range, 95*59613Sbostic * i.e. n matches from (n - 1) to n 24 hour periods. This interacts with 96*59613Sbostic * -n, such that "-mtime -1" would be less than 0 days, which isn't what the 97*59613Sbostic * user wanted. Correct so that -1 is "less than 1". 98*59613Sbostic */ 99*59613Sbostic #define TIME_CORRECT(p, ttype) \ 100*59613Sbostic if ((p)->type == ttype && (p)->flags == F_LESSTHAN) \ 101*59613Sbostic ++((p)->t_data); 102*59613Sbostic 103*59613Sbostic /* 10440947Sbostic * -atime n functions -- 10540947Sbostic * 10640947Sbostic * True if the difference between the file access time and the 10740947Sbostic * current time is n 24 hour periods. 10840947Sbostic */ 10959612Sbostic int 11040947Sbostic f_atime(plan, entry) 11140947Sbostic PLAN *plan; 11240947Sbostic FTSENT *entry; 11340947Sbostic { 11440947Sbostic extern time_t now; 11540947Sbostic 11652239Sbostic COMPARE((now - entry->fts_statp->st_atime + 11742255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 11840947Sbostic } 11940947Sbostic 12040947Sbostic PLAN * 12140947Sbostic c_atime(arg) 12240947Sbostic char *arg; 12340947Sbostic { 12440947Sbostic PLAN *new; 12540947Sbostic 12640947Sbostic ftsoptions &= ~FTS_NOSTAT; 12740947Sbostic 12849868Sbostic new = palloc(N_ATIME, f_atime); 12958066Smarc new->t_data = find_parsenum(new, "-atime", arg, NULL); 130*59613Sbostic TIME_CORRECT(new, N_ATIME); 13159612Sbostic return (new); 13240947Sbostic } 13340947Sbostic /* 13440947Sbostic * -ctime n functions -- 13540947Sbostic * 13640947Sbostic * True if the difference between the last change of file 13740947Sbostic * status information and the current time is n 24 hour periods. 13840947Sbostic */ 13959612Sbostic int 14040947Sbostic f_ctime(plan, entry) 14140947Sbostic PLAN *plan; 14240947Sbostic FTSENT *entry; 14340947Sbostic { 14440947Sbostic extern time_t now; 14540947Sbostic 14652239Sbostic COMPARE((now - entry->fts_statp->st_ctime + 14742255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 14840947Sbostic } 14940947Sbostic 15040947Sbostic PLAN * 15140947Sbostic c_ctime(arg) 15240947Sbostic char *arg; 15340947Sbostic { 15440947Sbostic PLAN *new; 15540947Sbostic 15640947Sbostic ftsoptions &= ~FTS_NOSTAT; 15740947Sbostic 15849868Sbostic new = palloc(N_CTIME, f_ctime); 15958066Smarc new->t_data = find_parsenum(new, "-ctime", arg, NULL); 160*59613Sbostic TIME_CORRECT(new, N_CTIME); 16159612Sbostic return (new); 16240947Sbostic } 16340947Sbostic 16440947Sbostic /* 16540947Sbostic * -depth functions -- 16640947Sbostic * 16740947Sbostic * Always true, causes descent of the directory hierarchy to be done 16840947Sbostic * so that all entries in a directory are acted on before the directory 16940947Sbostic * itself. 17040947Sbostic */ 17159612Sbostic int 17240947Sbostic f_always_true(plan, entry) 17340947Sbostic PLAN *plan; 17440947Sbostic FTSENT *entry; 17540947Sbostic { 17659612Sbostic return (1); 17740947Sbostic } 17840947Sbostic 17940947Sbostic PLAN * 18040947Sbostic c_depth() 18140947Sbostic { 18245615Sbostic isdepth = 1; 18340947Sbostic 18459612Sbostic return (palloc(N_DEPTH, f_always_true)); 18540947Sbostic } 18640947Sbostic 18740947Sbostic /* 18840947Sbostic * [-exec | -ok] utility [arg ... ] ; functions -- 18940947Sbostic * 19040947Sbostic * True if the executed utility returns a zero value as exit status. 19140947Sbostic * The end of the primary expression is delimited by a semicolon. If 19240947Sbostic * "{}" occurs anywhere, it gets replaced by the current pathname. 19340947Sbostic * The current directory for the execution of utility is the same as 19440947Sbostic * the current directory when the find utility was started. 19540947Sbostic * 19640947Sbostic * The primary -ok is different in that it requests affirmation of the 19740947Sbostic * user before executing the utility. 19840947Sbostic */ 19959612Sbostic int 20040947Sbostic f_exec(plan, entry) 20140947Sbostic register PLAN *plan; 20240947Sbostic FTSENT *entry; 20340947Sbostic { 20449869Sbostic extern int dotfd; 20540947Sbostic register int cnt; 20647192Sbostic pid_t pid; 20747192Sbostic int status; 20840947Sbostic 20940947Sbostic for (cnt = 0; plan->e_argv[cnt]; ++cnt) 21040947Sbostic if (plan->e_len[cnt]) 21145616Sbostic brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 21249869Sbostic entry->fts_path, plan->e_len[cnt]); 21340947Sbostic 21450437Sbostic if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv)) 21559612Sbostic return (0); 21640947Sbostic 21759612Sbostic switch (pid = vfork()) { 21840947Sbostic case -1: 21959612Sbostic err(1, "fork"); 22040947Sbostic /* NOTREACHED */ 22140947Sbostic case 0: 22249869Sbostic if (fchdir(dotfd)) { 22359612Sbostic warn("chdir"); 22449869Sbostic _exit(1); 22549869Sbostic } 22640947Sbostic execvp(plan->e_argv[0], plan->e_argv); 22759612Sbostic warn("%s", plan->e_argv[0]); 22849869Sbostic _exit(1); 22940947Sbostic } 23047192Sbostic pid = waitpid(pid, &status, 0); 23159612Sbostic return (pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status)); 23240947Sbostic } 23340947Sbostic 23440947Sbostic /* 23540947Sbostic * c_exec -- 23640947Sbostic * build three parallel arrays, one with pointers to the strings passed 23740947Sbostic * on the command line, one with (possibly duplicated) pointers to the 23840947Sbostic * argv array, and one with integer values that are lengths of the 23940947Sbostic * strings, but also flags meaning that the string has to be massaged. 24040947Sbostic */ 24140947Sbostic PLAN * 24240947Sbostic c_exec(argvp, isok) 24340947Sbostic char ***argvp; 24440947Sbostic int isok; 24540947Sbostic { 24640947Sbostic PLAN *new; /* node returned */ 24740947Sbostic register int cnt; 24840947Sbostic register char **argv, **ap, *p; 24940947Sbostic 25047192Sbostic isoutput = 1; 25140947Sbostic 25249868Sbostic new = palloc(N_EXEC, f_exec); 25350437Sbostic if (isok) 25450437Sbostic new->flags = F_NEEDOK; 25540947Sbostic 25640947Sbostic for (ap = argv = *argvp;; ++ap) { 25740947Sbostic if (!*ap) 25859612Sbostic errx(1, 25959612Sbostic "%s: no terminating \";\"", isok ? "-ok" : "-exec"); 26040947Sbostic if (**ap == ';') 26140947Sbostic break; 26240947Sbostic } 26340947Sbostic 26440947Sbostic cnt = ap - *argvp + 1; 26540947Sbostic new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 26640947Sbostic new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 26745868Sbostic new->e_len = (int *)emalloc((u_int)cnt * sizeof(int)); 26840947Sbostic 26940947Sbostic for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 27040947Sbostic new->e_orig[cnt] = *argv; 27140947Sbostic for (p = *argv; *p; ++p) 27240947Sbostic if (p[0] == '{' && p[1] == '}') { 27345868Sbostic new->e_argv[cnt] = emalloc((u_int)MAXPATHLEN); 27445868Sbostic new->e_len[cnt] = MAXPATHLEN; 27540947Sbostic break; 27640947Sbostic } 27740947Sbostic if (!*p) { 27840947Sbostic new->e_argv[cnt] = *argv; 27940947Sbostic new->e_len[cnt] = 0; 28040947Sbostic } 28140947Sbostic } 28240947Sbostic new->e_argv[cnt] = new->e_orig[cnt] = NULL; 28340947Sbostic 28440947Sbostic *argvp = argv + 1; 28559612Sbostic return (new); 28640947Sbostic } 28740947Sbostic 28840947Sbostic /* 28940947Sbostic * -follow functions -- 29040947Sbostic * 29140947Sbostic * Always true, causes symbolic links to be followed on a global 29240947Sbostic * basis. 29340947Sbostic */ 29440947Sbostic PLAN * 29540947Sbostic c_follow() 29640947Sbostic { 29740947Sbostic ftsoptions &= ~FTS_PHYSICAL; 29840947Sbostic ftsoptions |= FTS_LOGICAL; 29940947Sbostic 30059612Sbostic return (palloc(N_FOLLOW, f_always_true)); 30140947Sbostic } 30240947Sbostic 30340947Sbostic /* 30440947Sbostic * -fstype functions -- 30540947Sbostic * 30640947Sbostic * True if the file is of a certain type. 30740947Sbostic */ 30859612Sbostic int 30940947Sbostic f_fstype(plan, entry) 31040947Sbostic PLAN *plan; 31140947Sbostic FTSENT *entry; 31240947Sbostic { 31342275Sbostic static dev_t curdev; /* need a guaranteed illegal dev value */ 31442275Sbostic static int first = 1; 31540947Sbostic struct statfs sb; 31642255Sbostic static short val; 31745626Sbostic char *p, save[2]; 31840947Sbostic 31950437Sbostic /* Only check when we cross mount point. */ 32052239Sbostic if (first || curdev != entry->fts_statp->st_dev) { 32152239Sbostic curdev = entry->fts_statp->st_dev; 32245626Sbostic 32345626Sbostic /* 32445626Sbostic * Statfs follows symlinks; find wants the link's file system, 32545626Sbostic * not where it points. 32645626Sbostic */ 32745626Sbostic if (entry->fts_info == FTS_SL || 32845626Sbostic entry->fts_info == FTS_SLNONE) { 32959612Sbostic if (p = strrchr(entry->fts_accpath, '/')) 33045626Sbostic ++p; 33145626Sbostic else 33245626Sbostic p = entry->fts_accpath; 33345626Sbostic save[0] = p[0]; 33445626Sbostic p[0] = '.'; 33545626Sbostic save[1] = p[1]; 33645626Sbostic p[1] = '\0'; 33745626Sbostic 33845626Sbostic } else 33945626Sbostic p = NULL; 34045626Sbostic 34149868Sbostic if (statfs(entry->fts_accpath, &sb)) 34259612Sbostic err(1, "%s", entry->fts_accpath); 34345626Sbostic 34445626Sbostic if (p) { 34545626Sbostic p[0] = save[0]; 34645626Sbostic p[1] = save[1]; 34745626Sbostic } 34845626Sbostic 34942275Sbostic first = 0; 35059612Sbostic switch (plan->flags) { 35150437Sbostic case F_MTFLAG: 35250437Sbostic val = sb.f_flags; 35350437Sbostic break; 35450437Sbostic case F_MTTYPE: 35550437Sbostic val = sb.f_type; 35650437Sbostic break; 35759612Sbostic default: 35859612Sbostic abort(); 35950437Sbostic } 36040947Sbostic } 36150437Sbostic switch(plan->flags) { 36250437Sbostic case F_MTFLAG: 36359612Sbostic return (val & plan->mt_data); 36450437Sbostic case F_MTTYPE: 36559612Sbostic return (val == plan->mt_data); 36659612Sbostic default: 36759612Sbostic abort(); 36850437Sbostic } 36940947Sbostic } 37040947Sbostic 37140947Sbostic PLAN * 37240947Sbostic c_fstype(arg) 37340947Sbostic char *arg; 37440947Sbostic { 37540947Sbostic register PLAN *new; 37640947Sbostic 37740947Sbostic ftsoptions &= ~FTS_NOSTAT; 37840947Sbostic 37949868Sbostic new = palloc(N_FSTYPE, f_fstype); 38059612Sbostic switch (*arg) { 38142255Sbostic case 'l': 38242255Sbostic if (!strcmp(arg, "local")) { 38350437Sbostic new->flags = F_MTFLAG; 38450437Sbostic new->mt_data = MNT_LOCAL; 38559612Sbostic return (new); 38642255Sbostic } 38742255Sbostic break; 38840947Sbostic case 'm': 38940947Sbostic if (!strcmp(arg, "mfs")) { 39050437Sbostic new->flags = F_MTTYPE; 39150437Sbostic new->mt_data = MOUNT_MFS; 39259612Sbostic return (new); 39340947Sbostic } 39440947Sbostic break; 39540947Sbostic case 'n': 39640947Sbostic if (!strcmp(arg, "nfs")) { 39750437Sbostic new->flags = F_MTTYPE; 39850437Sbostic new->mt_data = MOUNT_NFS; 39959612Sbostic return (new); 40040947Sbostic } 40140947Sbostic break; 40240947Sbostic case 'p': 40340947Sbostic if (!strcmp(arg, "pc")) { 40450437Sbostic new->flags = F_MTTYPE; 40550437Sbostic new->mt_data = MOUNT_PC; 40659612Sbostic return (new); 40740947Sbostic } 40840947Sbostic break; 40950437Sbostic case 'r': 41050437Sbostic if (!strcmp(arg, "rdonly")) { 41150437Sbostic new->flags = F_MTFLAG; 41250437Sbostic new->mt_data = MNT_RDONLY; 41359612Sbostic return (new); 41450437Sbostic } 41550437Sbostic break; 41640947Sbostic case 'u': 41740947Sbostic if (!strcmp(arg, "ufs")) { 41850437Sbostic new->flags = F_MTTYPE; 41950437Sbostic new->mt_data = MOUNT_UFS; 42059612Sbostic return (new); 42140947Sbostic } 42240947Sbostic break; 42340947Sbostic } 42459612Sbostic errx(1, "%s: unknown file type", arg); 42540947Sbostic /* NOTREACHED */ 42640947Sbostic } 42740947Sbostic 42840947Sbostic /* 42940947Sbostic * -group gname functions -- 43040947Sbostic * 43140947Sbostic * True if the file belongs to the group gname. If gname is numeric and 43240947Sbostic * an equivalent of the getgrnam() function does not return a valid group 43340947Sbostic * name, gname is taken as a group ID. 43440947Sbostic */ 43559612Sbostic int 43640947Sbostic f_group(plan, entry) 43740947Sbostic PLAN *plan; 43840947Sbostic FTSENT *entry; 43940947Sbostic { 44059612Sbostic return (entry->fts_statp->st_gid == plan->g_data); 44140947Sbostic } 44240947Sbostic 44340947Sbostic PLAN * 44440947Sbostic c_group(gname) 44540947Sbostic char *gname; 44640947Sbostic { 44740947Sbostic PLAN *new; 44840947Sbostic struct group *g; 44940947Sbostic gid_t gid; 45040947Sbostic 45140947Sbostic ftsoptions &= ~FTS_NOSTAT; 45240947Sbostic 45340947Sbostic g = getgrnam(gname); 45440947Sbostic if (g == NULL) { 45540947Sbostic gid = atoi(gname); 45640947Sbostic if (gid == 0 && gname[0] != '0') 45759612Sbostic errx(1, "-group: %s: no such group", gname); 45840947Sbostic } else 45940947Sbostic gid = g->gr_gid; 46040947Sbostic 46149868Sbostic new = palloc(N_GROUP, f_group); 46240947Sbostic new->g_data = gid; 46359612Sbostic return (new); 46440947Sbostic } 46540947Sbostic 46640947Sbostic /* 46740947Sbostic * -inum n functions -- 46840947Sbostic * 46940947Sbostic * True if the file has inode # n. 47040947Sbostic */ 47159612Sbostic int 47240947Sbostic f_inum(plan, entry) 47340947Sbostic PLAN *plan; 47440947Sbostic FTSENT *entry; 47540947Sbostic { 47652239Sbostic COMPARE(entry->fts_statp->st_ino, plan->i_data); 47740947Sbostic } 47840947Sbostic 47940947Sbostic PLAN * 48040947Sbostic c_inum(arg) 48140947Sbostic char *arg; 48240947Sbostic { 48340947Sbostic PLAN *new; 48440947Sbostic 48540947Sbostic ftsoptions &= ~FTS_NOSTAT; 48640947Sbostic 48749868Sbostic new = palloc(N_INUM, f_inum); 48858066Smarc new->i_data = find_parsenum(new, "-inum", arg, NULL); 48959612Sbostic return (new); 49040947Sbostic } 49140947Sbostic 49240947Sbostic /* 49340947Sbostic * -links n functions -- 49440947Sbostic * 49540947Sbostic * True if the file has n links. 49640947Sbostic */ 49759612Sbostic int 49840947Sbostic f_links(plan, entry) 49940947Sbostic PLAN *plan; 50040947Sbostic FTSENT *entry; 50140947Sbostic { 50252239Sbostic COMPARE(entry->fts_statp->st_nlink, plan->l_data); 50340947Sbostic } 50440947Sbostic 50540947Sbostic PLAN * 50640947Sbostic c_links(arg) 50740947Sbostic char *arg; 50840947Sbostic { 50940947Sbostic PLAN *new; 51040947Sbostic 51140947Sbostic ftsoptions &= ~FTS_NOSTAT; 51240947Sbostic 51349868Sbostic new = palloc(N_LINKS, f_links); 51458066Smarc new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); 51559612Sbostic return (new); 51640947Sbostic } 51740947Sbostic 51840947Sbostic /* 51940947Sbostic * -ls functions -- 52040947Sbostic * 52140947Sbostic * Always true - prints the current entry to stdout in "ls" format. 52240947Sbostic */ 52359612Sbostic int 52440947Sbostic f_ls(plan, entry) 52540947Sbostic PLAN *plan; 52640947Sbostic FTSENT *entry; 52740947Sbostic { 52852239Sbostic printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); 52959612Sbostic return (1); 53040947Sbostic } 53140947Sbostic 53240947Sbostic PLAN * 53340947Sbostic c_ls() 53440947Sbostic { 53540947Sbostic ftsoptions &= ~FTS_NOSTAT; 53645615Sbostic isoutput = 1; 53740947Sbostic 53859612Sbostic return (palloc(N_LS, f_ls)); 53940947Sbostic } 54040947Sbostic 54140947Sbostic /* 54250437Sbostic * -mtime n functions -- 54350437Sbostic * 54450437Sbostic * True if the difference between the file modification time and the 54550437Sbostic * current time is n 24 hour periods. 54650437Sbostic */ 54759612Sbostic int 54850437Sbostic f_mtime(plan, entry) 54950437Sbostic PLAN *plan; 55050437Sbostic FTSENT *entry; 55150437Sbostic { 55250437Sbostic extern time_t now; 55350437Sbostic 55452239Sbostic COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) / 55550437Sbostic SECSPERDAY, plan->t_data); 55650437Sbostic } 55750437Sbostic 55850437Sbostic PLAN * 55950437Sbostic c_mtime(arg) 56050437Sbostic char *arg; 56150437Sbostic { 56250437Sbostic PLAN *new; 56350437Sbostic 56450437Sbostic ftsoptions &= ~FTS_NOSTAT; 56550437Sbostic 56650437Sbostic new = palloc(N_MTIME, f_mtime); 56758066Smarc new->t_data = find_parsenum(new, "-mtime", arg, NULL); 568*59613Sbostic TIME_CORRECT(new, N_MTIME); 56959612Sbostic return (new); 57050437Sbostic } 57150437Sbostic 57250437Sbostic /* 57340947Sbostic * -name functions -- 57440947Sbostic * 57540947Sbostic * True if the basename of the filename being examined 57640947Sbostic * matches pattern using Pattern Matching Notation S3.14 57740947Sbostic */ 57859612Sbostic int 57940947Sbostic f_name(plan, entry) 58040947Sbostic PLAN *plan; 58140947Sbostic FTSENT *entry; 58240947Sbostic { 58359612Sbostic return (!fnmatch(plan->c_data, entry->fts_name, 0)); 58440947Sbostic } 58540947Sbostic 58640947Sbostic PLAN * 58740947Sbostic c_name(pattern) 58840947Sbostic char *pattern; 58940947Sbostic { 59040947Sbostic PLAN *new; 59140947Sbostic 59249868Sbostic new = palloc(N_NAME, f_name); 59340947Sbostic new->c_data = pattern; 59459612Sbostic return (new); 59540947Sbostic } 59640947Sbostic 59740947Sbostic /* 59840947Sbostic * -newer file functions -- 59940947Sbostic * 60040947Sbostic * True if the current file has been modified more recently 60140947Sbostic * then the modification time of the file named by the pathname 60240947Sbostic * file. 60340947Sbostic */ 60459612Sbostic int 60540947Sbostic f_newer(plan, entry) 60640947Sbostic PLAN *plan; 60740947Sbostic FTSENT *entry; 60840947Sbostic { 60959612Sbostic return (entry->fts_statp->st_mtime > plan->t_data); 61040947Sbostic } 61140947Sbostic 61240947Sbostic PLAN * 61340947Sbostic c_newer(filename) 61440947Sbostic char *filename; 61540947Sbostic { 61640947Sbostic PLAN *new; 61740947Sbostic struct stat sb; 61840947Sbostic 61940947Sbostic ftsoptions &= ~FTS_NOSTAT; 62040947Sbostic 62149868Sbostic if (stat(filename, &sb)) 62259612Sbostic err(1, "%s", filename); 62349868Sbostic new = palloc(N_NEWER, f_newer); 62440947Sbostic new->t_data = sb.st_mtime; 62559612Sbostic return (new); 62640947Sbostic } 62740947Sbostic 62840947Sbostic /* 62940947Sbostic * -nogroup functions -- 63040947Sbostic * 63140947Sbostic * True if file belongs to a user ID for which the equivalent 63240947Sbostic * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 63340947Sbostic */ 63459612Sbostic int 63540947Sbostic f_nogroup(plan, entry) 63640947Sbostic PLAN *plan; 63740947Sbostic FTSENT *entry; 63840947Sbostic { 63945615Sbostic char *group_from_gid(); 64045615Sbostic 64159612Sbostic return (group_from_gid(entry->fts_statp->st_gid, 1) ? 1 : 0); 64240947Sbostic } 64340947Sbostic 64440947Sbostic PLAN * 64540947Sbostic c_nogroup() 64640947Sbostic { 64740947Sbostic ftsoptions &= ~FTS_NOSTAT; 64840947Sbostic 64959612Sbostic return (palloc(N_NOGROUP, f_nogroup)); 65040947Sbostic } 65140947Sbostic 65240947Sbostic /* 65340947Sbostic * -nouser functions -- 65440947Sbostic * 65540947Sbostic * True if file belongs to a user ID for which the equivalent 65640947Sbostic * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 65740947Sbostic */ 65859612Sbostic int 65940947Sbostic f_nouser(plan, entry) 66040947Sbostic PLAN *plan; 66140947Sbostic FTSENT *entry; 66240947Sbostic { 66345615Sbostic char *user_from_uid(); 66445615Sbostic 66559612Sbostic return (user_from_uid(entry->fts_statp->st_uid, 1) ? 1 : 0); 66640947Sbostic } 66740947Sbostic 66840947Sbostic PLAN * 66940947Sbostic c_nouser() 67040947Sbostic { 67140947Sbostic ftsoptions &= ~FTS_NOSTAT; 67240947Sbostic 67359612Sbostic return (palloc(N_NOUSER, f_nouser)); 67440947Sbostic } 67540947Sbostic 67640947Sbostic /* 67750437Sbostic * -path functions -- 67850437Sbostic * 67950437Sbostic * True if the path of the filename being examined 68050437Sbostic * matches pattern using Pattern Matching Notation S3.14 68150437Sbostic */ 68259612Sbostic int 68350437Sbostic f_path(plan, entry) 68450437Sbostic PLAN *plan; 68550437Sbostic FTSENT *entry; 68650437Sbostic { 68759612Sbostic return (!fnmatch(plan->c_data, entry->fts_path, 0)); 68850437Sbostic } 68950437Sbostic 69050437Sbostic PLAN * 69150437Sbostic c_path(pattern) 69250437Sbostic char *pattern; 69350437Sbostic { 69450437Sbostic PLAN *new; 69550437Sbostic 69650437Sbostic new = palloc(N_NAME, f_path); 69750437Sbostic new->c_data = pattern; 69859612Sbostic return (new); 69950437Sbostic } 70050437Sbostic 70150437Sbostic /* 70240947Sbostic * -perm functions -- 70340947Sbostic * 70440947Sbostic * The mode argument is used to represent file mode bits. If it starts 70540947Sbostic * with a leading digit, it's treated as an octal mode, otherwise as a 70640947Sbostic * symbolic mode. 70740947Sbostic */ 70859612Sbostic int 70940947Sbostic f_perm(plan, entry) 71040947Sbostic PLAN *plan; 71140947Sbostic FTSENT *entry; 71240947Sbostic { 71340947Sbostic mode_t mode; 71440947Sbostic 71552239Sbostic mode = entry->fts_statp->st_mode & 71640947Sbostic (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 71750437Sbostic if (plan->flags == F_ATLEAST) 71859612Sbostic return ((plan->m_data | mode) == mode); 71940947Sbostic else 72059612Sbostic return (mode == plan->m_data); 72140947Sbostic /* NOTREACHED */ 72240947Sbostic } 72340947Sbostic 72440947Sbostic PLAN * 72540947Sbostic c_perm(perm) 72640947Sbostic char *perm; 72740947Sbostic { 72840947Sbostic PLAN *new; 72947192Sbostic mode_t *set; 73040947Sbostic 73140947Sbostic ftsoptions &= ~FTS_NOSTAT; 73240947Sbostic 73349868Sbostic new = palloc(N_PERM, f_perm); 73440947Sbostic 73540947Sbostic if (*perm == '-') { 73650437Sbostic new->flags = F_ATLEAST; 73740947Sbostic ++perm; 73840947Sbostic } 73940947Sbostic 74044764Strent if ((set = setmode(perm)) == NULL) 74159612Sbostic err(1, "-perm: %s: illegal mode string", perm); 74240947Sbostic 74344764Strent new->m_data = getmode(set, 0); 74459612Sbostic return (new); 74540947Sbostic } 74640947Sbostic 74740947Sbostic /* 74840947Sbostic * -print functions -- 74940947Sbostic * 75040947Sbostic * Always true, causes the current pathame to be written to 75140947Sbostic * standard output. 75240947Sbostic */ 75359612Sbostic int 75440947Sbostic f_print(plan, entry) 75540947Sbostic PLAN *plan; 75640947Sbostic FTSENT *entry; 75740947Sbostic { 75842255Sbostic (void)printf("%s\n", entry->fts_path); 75959612Sbostic return (1); 76040947Sbostic } 76140947Sbostic 76240947Sbostic PLAN * 76340947Sbostic c_print() 76440947Sbostic { 76545615Sbostic isoutput = 1; 76640947Sbostic 76759612Sbostic return (palloc(N_PRINT, f_print)); 76840947Sbostic } 76940947Sbostic 77040947Sbostic /* 77140947Sbostic * -prune functions -- 77240947Sbostic * 77340947Sbostic * Prune a portion of the hierarchy. 77440947Sbostic */ 77559612Sbostic int 77640947Sbostic f_prune(plan, entry) 77740947Sbostic PLAN *plan; 77840947Sbostic FTSENT *entry; 77940947Sbostic { 78040947Sbostic extern FTS *tree; 78140947Sbostic 78249868Sbostic if (fts_set(tree, entry, FTS_SKIP)) 78359612Sbostic err(1, "%s", entry->fts_path); 78459612Sbostic return (1); 78540947Sbostic } 78640947Sbostic 78740947Sbostic PLAN * 78840947Sbostic c_prune() 78940947Sbostic { 79059612Sbostic return (palloc(N_PRUNE, f_prune)); 79140947Sbostic } 79240947Sbostic 79340947Sbostic /* 79440947Sbostic * -size n[c] functions -- 79540947Sbostic * 79640947Sbostic * True if the file size in bytes, divided by an implementation defined 79740947Sbostic * value and rounded up to the next integer, is n. If n is followed by 79840947Sbostic * a c, the size is in bytes. 79940947Sbostic */ 80040947Sbostic #define FIND_SIZE 512 80140947Sbostic static int divsize = 1; 80240947Sbostic 80359612Sbostic int 80440947Sbostic f_size(plan, entry) 80540947Sbostic PLAN *plan; 80640947Sbostic FTSENT *entry; 80740947Sbostic { 80840947Sbostic off_t size; 80940947Sbostic 81052239Sbostic size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / 81152239Sbostic FIND_SIZE : entry->fts_statp->st_size; 81240947Sbostic COMPARE(size, plan->o_data); 81340947Sbostic } 81440947Sbostic 81540947Sbostic PLAN * 81640947Sbostic c_size(arg) 81740947Sbostic char *arg; 81840947Sbostic { 81940947Sbostic PLAN *new; 82040947Sbostic char endch; 82140947Sbostic 82240947Sbostic ftsoptions &= ~FTS_NOSTAT; 82340947Sbostic 82449868Sbostic new = palloc(N_SIZE, f_size); 82555710Sbostic endch = 'c'; 82658066Smarc new->o_data = find_parsenum(new, "-size", arg, &endch); 82740947Sbostic if (endch == 'c') 82840947Sbostic divsize = 0; 82959612Sbostic return (new); 83040947Sbostic } 83140947Sbostic 83240947Sbostic /* 83340947Sbostic * -type c functions -- 83440947Sbostic * 83540947Sbostic * True if the type of the file is c, where c is b, c, d, p, or f for 83640947Sbostic * block special file, character special file, directory, FIFO, or 83740947Sbostic * regular file, respectively. 83840947Sbostic */ 83959612Sbostic int 84040947Sbostic f_type(plan, entry) 84140947Sbostic PLAN *plan; 84240947Sbostic FTSENT *entry; 84340947Sbostic { 84459612Sbostic return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data); 84540947Sbostic } 84640947Sbostic 84740947Sbostic PLAN * 84840947Sbostic c_type(typestring) 84940947Sbostic char *typestring; 85040947Sbostic { 85140947Sbostic PLAN *new; 85240947Sbostic mode_t mask; 85340947Sbostic 85440947Sbostic ftsoptions &= ~FTS_NOSTAT; 85540947Sbostic 85640947Sbostic switch (typestring[0]) { 85740947Sbostic case 'b': 85840947Sbostic mask = S_IFBLK; 85940947Sbostic break; 86040947Sbostic case 'c': 86140947Sbostic mask = S_IFCHR; 86240947Sbostic break; 86340947Sbostic case 'd': 86440947Sbostic mask = S_IFDIR; 86540947Sbostic break; 86640947Sbostic case 'f': 86740947Sbostic mask = S_IFREG; 86840947Sbostic break; 86940947Sbostic case 'l': 87040947Sbostic mask = S_IFLNK; 87140947Sbostic break; 87240947Sbostic case 'p': 87340947Sbostic mask = S_IFIFO; 87440947Sbostic break; 87540947Sbostic case 's': 87640947Sbostic mask = S_IFSOCK; 87740947Sbostic break; 87840947Sbostic default: 87959612Sbostic errx(1, "-type: %s: unknown type", typestring); 88040947Sbostic } 88140947Sbostic 88249868Sbostic new = palloc(N_TYPE, f_type); 88340947Sbostic new->m_data = mask; 88459612Sbostic return (new); 88540947Sbostic } 88640947Sbostic 88740947Sbostic /* 88840947Sbostic * -user uname functions -- 88940947Sbostic * 89040947Sbostic * True if the file belongs to the user uname. If uname is numeric and 89140947Sbostic * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 89240947Sbostic * return a valid user name, uname is taken as a user ID. 89340947Sbostic */ 89459612Sbostic int 89540947Sbostic f_user(plan, entry) 89640947Sbostic PLAN *plan; 89740947Sbostic FTSENT *entry; 89840947Sbostic { 89959612Sbostic return (entry->fts_statp->st_uid == plan->u_data); 90040947Sbostic } 90140947Sbostic 90240947Sbostic PLAN * 90340947Sbostic c_user(username) 90440947Sbostic char *username; 90540947Sbostic { 90640947Sbostic PLAN *new; 90740947Sbostic struct passwd *p; 90840947Sbostic uid_t uid; 90940947Sbostic 91040947Sbostic ftsoptions &= ~FTS_NOSTAT; 91140947Sbostic 91240947Sbostic p = getpwnam(username); 91340947Sbostic if (p == NULL) { 91440947Sbostic uid = atoi(username); 91540947Sbostic if (uid == 0 && username[0] != '0') 91659612Sbostic errx(1, "-user: %s: no such user", username); 91740947Sbostic } else 91840947Sbostic uid = p->pw_uid; 91940947Sbostic 92049868Sbostic new = palloc(N_USER, f_user); 92140947Sbostic new->u_data = uid; 92259612Sbostic return (new); 92340947Sbostic } 92440947Sbostic 92540947Sbostic /* 92640947Sbostic * -xdev functions -- 92740947Sbostic * 92840947Sbostic * Always true, causes find not to decend past directories that have a 92940947Sbostic * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 93040947Sbostic */ 93140947Sbostic PLAN * 93240947Sbostic c_xdev() 93340947Sbostic { 93442275Sbostic ftsoptions |= FTS_XDEV; 93540947Sbostic 93659612Sbostic return (palloc(N_XDEV, f_always_true)); 93740947Sbostic } 93840947Sbostic 93940947Sbostic /* 94040947Sbostic * ( expression ) functions -- 94140947Sbostic * 94240947Sbostic * True if expression is true. 94340947Sbostic */ 94459612Sbostic int 94540947Sbostic f_expr(plan, entry) 94640947Sbostic PLAN *plan; 94740947Sbostic FTSENT *entry; 94840947Sbostic { 94940947Sbostic register PLAN *p; 95040947Sbostic register int state; 95140947Sbostic 95240947Sbostic for (p = plan->p_data[0]; 95340947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 95459612Sbostic return (state); 95540947Sbostic } 95640947Sbostic 95740947Sbostic /* 95849864Sbostic * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are 95940947Sbostic * eliminated during phase 2 of find_formplan() --- the '(' node is converted 96049864Sbostic * to a N_EXPR node containing the expression and the ')' node is discarded. 96140947Sbostic */ 96240947Sbostic PLAN * 96340947Sbostic c_openparen() 96440947Sbostic { 96559612Sbostic return (palloc(N_OPENPAREN, (int (*)())-1)); 96640947Sbostic } 96740947Sbostic 96840947Sbostic PLAN * 96940947Sbostic c_closeparen() 97040947Sbostic { 97159612Sbostic return (palloc(N_CLOSEPAREN, (int (*)())-1)); 97240947Sbostic } 97340947Sbostic 97440947Sbostic /* 97540947Sbostic * ! expression functions -- 97640947Sbostic * 97740947Sbostic * Negation of a primary; the unary NOT operator. 97840947Sbostic */ 97959612Sbostic int 98040947Sbostic f_not(plan, entry) 98140947Sbostic PLAN *plan; 98240947Sbostic FTSENT *entry; 98340947Sbostic { 98440947Sbostic register PLAN *p; 98540947Sbostic register int state; 98640947Sbostic 98740947Sbostic for (p = plan->p_data[0]; 98840947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 98959612Sbostic return (!state); 99040947Sbostic } 99140947Sbostic 99240947Sbostic PLAN * 99340947Sbostic c_not() 99440947Sbostic { 99559612Sbostic return (palloc(N_NOT, f_not)); 99640947Sbostic } 99740947Sbostic 99840947Sbostic /* 99940947Sbostic * expression -o expression functions -- 100040947Sbostic * 100140947Sbostic * Alternation of primaries; the OR operator. The second expression is 100240947Sbostic * not evaluated if the first expression is true. 100340947Sbostic */ 100459612Sbostic int 100540947Sbostic f_or(plan, entry) 100640947Sbostic PLAN *plan; 100740947Sbostic FTSENT *entry; 100840947Sbostic { 100940947Sbostic register PLAN *p; 101040947Sbostic register int state; 101140947Sbostic 101240947Sbostic for (p = plan->p_data[0]; 101340947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 101440947Sbostic 101540947Sbostic if (state) 101659612Sbostic return (1); 101740947Sbostic 101840947Sbostic for (p = plan->p_data[1]; 101940947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 102059612Sbostic return (state); 102140947Sbostic } 102240947Sbostic 102340947Sbostic PLAN * 102440947Sbostic c_or() 102540947Sbostic { 102659612Sbostic return (palloc(N_OR, f_or)); 102749868Sbostic } 102849868Sbostic 102949868Sbostic static PLAN * 103049868Sbostic palloc(t, f) 103149868Sbostic enum ntype t; 103259612Sbostic int (*f) __P((PLAN *, FTSENT *)); 103349868Sbostic { 103440947Sbostic PLAN *new; 103540947Sbostic 103649868Sbostic if (new = malloc(sizeof(PLAN))) { 103749868Sbostic new->type = t; 103849868Sbostic new->eval = f; 103949868Sbostic new->flags = 0; 104049868Sbostic new->next = NULL; 104159612Sbostic return (new); 104249868Sbostic } 104359612Sbostic err(1, NULL); 104449868Sbostic /* NOTREACHED */ 104540947Sbostic } 1046