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*44764Strent static char sccsid[] = "@(#)function.c 5.7 (Berkeley) 06/30/90"; 1340947Sbostic #endif /* not lint */ 1440947Sbostic 1540947Sbostic #include <sys/types.h> 1640947Sbostic #include <sys/stat.h> 1740947Sbostic #include <sys/wait.h> 1840947Sbostic #include <sys/mount.h> 1940947Sbostic #include <grp.h> 2040947Sbostic #include <pwd.h> 2140947Sbostic #include <fts.h> 2240947Sbostic #include <unistd.h> 2340947Sbostic #include <tzfile.h> 2440947Sbostic #include <stdio.h> 2542047Sbostic #include <string.h> 2640947Sbostic #include "find.h" 2740947Sbostic 2840947Sbostic #define FIND_EQUAL 0 2940947Sbostic #define FIND_LESSTHAN 1 3040947Sbostic #define FIND_GREATER 2 3140947Sbostic 3240947Sbostic #define COMPARE(a, b) { \ 3340947Sbostic switch(plan->flags) { \ 3440947Sbostic case FIND_EQUAL: \ 3540947Sbostic return(a == b); \ 3640947Sbostic case FIND_LESSTHAN: \ 3740947Sbostic return(a < b); \ 3840947Sbostic case FIND_GREATER: \ 3940947Sbostic return(a > b); \ 4040947Sbostic } \ 4142255Sbostic return(0); \ 4240947Sbostic } 4340947Sbostic 4440947Sbostic #define NEW(t, f) { \ 4540947Sbostic new = (PLAN *)emalloc(sizeof(PLAN)); \ 4640947Sbostic new->type = t; \ 4740947Sbostic new->eval = f; \ 4840947Sbostic new->flags = 0; \ 4940947Sbostic new->next = NULL; \ 5040947Sbostic } 5140947Sbostic 5240947Sbostic /* 5340947Sbostic * find_parsenum -- 5440947Sbostic * Parse a string of the form [+-]# and return the value. 5540947Sbostic */ 5640947Sbostic long 5740947Sbostic find_parsenum(plan, option, str, endch) 5840947Sbostic PLAN *plan; 5940947Sbostic char *option, *str, *endch; 6040947Sbostic { 6140947Sbostic long value; 6240947Sbostic char *endchar; /* pointer to character ending conversion */ 6340947Sbostic 6440947Sbostic /* determine comparison from leading + or - */ 6540947Sbostic switch(*str) { 6640947Sbostic case '+': 6740947Sbostic ++str; 6840947Sbostic plan->flags = FIND_GREATER; 6940947Sbostic break; 7040947Sbostic case '-': 7140947Sbostic ++str; 7240947Sbostic plan->flags = FIND_LESSTHAN; 7340947Sbostic break; 7440947Sbostic default: 7540947Sbostic plan->flags = FIND_EQUAL; 7640947Sbostic break; 7740947Sbostic } 7840947Sbostic 7940947Sbostic /* 8040947Sbostic * convert the string with strtol(). Note, if strtol() returns zero 8140947Sbostic * and endchar points to the beginning of the string we know we have 8240947Sbostic * a syntax error. 8340947Sbostic */ 8440947Sbostic value = strtol(str, &endchar, 10); 8540947Sbostic if (!value && endchar == str || 8640947Sbostic endchar[0] && (!endch || endchar[0] != *endch)) 8740947Sbostic bad_arg(option, "illegal numeric value"); 8840947Sbostic if (endch) 8940947Sbostic *endch = endchar[0]; 9040947Sbostic return(value); 9140947Sbostic } 9240947Sbostic 9340947Sbostic /* 9440947Sbostic * -atime n functions -- 9540947Sbostic * 9640947Sbostic * True if the difference between the file access time and the 9740947Sbostic * current time is n 24 hour periods. 9840947Sbostic * 9940947Sbostic */ 10040947Sbostic f_atime(plan, entry) 10140947Sbostic PLAN *plan; 10240947Sbostic FTSENT *entry; 10340947Sbostic { 10440947Sbostic extern time_t now; 10540947Sbostic 10642255Sbostic COMPARE((now - entry->fts_statb.st_atime + 10742255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 10840947Sbostic } 10940947Sbostic 11040947Sbostic PLAN * 11140947Sbostic c_atime(arg) 11240947Sbostic char *arg; 11340947Sbostic { 11440947Sbostic PLAN *new; 11540947Sbostic 11640947Sbostic ftsoptions &= ~FTS_NOSTAT; 11740947Sbostic 11840947Sbostic NEW(T_ATIME, f_atime); 11940947Sbostic new->t_data = find_parsenum(new, "-atime", arg, (char *)NULL); 12040947Sbostic return(new); 12140947Sbostic } 12240947Sbostic /* 12340947Sbostic * -ctime n functions -- 12440947Sbostic * 12540947Sbostic * True if the difference between the last change of file 12640947Sbostic * status information and the current time is n 24 hour periods. 12740947Sbostic */ 12840947Sbostic f_ctime(plan, entry) 12940947Sbostic PLAN *plan; 13040947Sbostic FTSENT *entry; 13140947Sbostic { 13240947Sbostic extern time_t now; 13340947Sbostic 13442255Sbostic COMPARE((now - entry->fts_statb.st_ctime + 13542255Sbostic SECSPERDAY - 1) / SECSPERDAY, plan->t_data); 13640947Sbostic } 13740947Sbostic 13840947Sbostic PLAN * 13940947Sbostic c_ctime(arg) 14040947Sbostic char *arg; 14140947Sbostic { 14240947Sbostic PLAN *new; 14340947Sbostic 14440947Sbostic ftsoptions &= ~FTS_NOSTAT; 14540947Sbostic 14640947Sbostic NEW(T_CTIME, f_ctime); 14740947Sbostic new->t_data = find_parsenum(new, "-ctime", arg, (char *)NULL); 14840947Sbostic return(new); 14940947Sbostic } 15040947Sbostic 15140947Sbostic /* 15240947Sbostic * -depth functions -- 15340947Sbostic * 15440947Sbostic * Always true, causes descent of the directory hierarchy to be done 15540947Sbostic * so that all entries in a directory are acted on before the directory 15640947Sbostic * itself. 15740947Sbostic */ 15840947Sbostic /* ARGSUSED */ 15940947Sbostic f_always_true(plan, entry) 16040947Sbostic PLAN *plan; 16140947Sbostic FTSENT *entry; 16240947Sbostic { 16342255Sbostic return(1); 16440947Sbostic } 16540947Sbostic 16640947Sbostic PLAN * 16740947Sbostic c_depth() 16840947Sbostic { 16940947Sbostic extern int depth; 17040947Sbostic PLAN *new; 17140947Sbostic 17240947Sbostic depth = 1; 17340947Sbostic 17440947Sbostic NEW(T_DEPTH, f_always_true); 17540947Sbostic return(new); 17640947Sbostic } 17740947Sbostic 17840947Sbostic /* 17940947Sbostic * [-exec | -ok] utility [arg ... ] ; functions -- 18040947Sbostic * 18140947Sbostic * True if the executed utility returns a zero value as exit status. 18240947Sbostic * The end of the primary expression is delimited by a semicolon. If 18340947Sbostic * "{}" occurs anywhere, it gets replaced by the current pathname. 18440947Sbostic * The current directory for the execution of utility is the same as 18540947Sbostic * the current directory when the find utility was started. 18640947Sbostic * 18740947Sbostic * The primary -ok is different in that it requests affirmation of the 18840947Sbostic * user before executing the utility. 18940947Sbostic */ 19040947Sbostic f_exec(plan, entry) 19140947Sbostic register PLAN *plan; 19240947Sbostic FTSENT *entry; 19340947Sbostic { 19440947Sbostic register int cnt; 19540947Sbostic char *find_subst(); 19640947Sbostic union wait pstat; 19740947Sbostic pid_t pid, waitpid(); 19840947Sbostic 19940947Sbostic for (cnt = 0; plan->e_argv[cnt]; ++cnt) 20040947Sbostic if (plan->e_len[cnt]) 20140947Sbostic find_subst(plan->e_orig[cnt], &plan->e_argv[cnt], 20242255Sbostic entry->fts_path, plan->e_len[cnt]); 20340947Sbostic 20440947Sbostic if (plan->flags && !find_queryuser(plan->e_argv)) 20542255Sbostic return(0); 20640947Sbostic 20740947Sbostic switch(pid = vfork()) { 20840947Sbostic case -1: 20940947Sbostic (void)fprintf(stderr, "find: fork: %s.\n", strerror(errno)); 21040947Sbostic exit(1); 21140947Sbostic /* NOTREACHED */ 21240947Sbostic case 0: 21340947Sbostic execvp(plan->e_argv[0], plan->e_argv); 21440947Sbostic (void)fprintf(stderr, 21540947Sbostic "find: %s: %s.\n", plan->e_argv[0], strerror(errno)); 21640947Sbostic exit(1); 21740947Sbostic /* NOTREACHED */ 21840947Sbostic } 21940947Sbostic pid = waitpid(pid, &pstat, 0); 22042255Sbostic return(pid != -1 && !pstat.w_status); 22140947Sbostic } 22240947Sbostic 22340947Sbostic /* 22440947Sbostic * c_exec -- 22540947Sbostic * build three parallel arrays, one with pointers to the strings passed 22640947Sbostic * on the command line, one with (possibly duplicated) pointers to the 22740947Sbostic * argv array, and one with integer values that are lengths of the 22840947Sbostic * strings, but also flags meaning that the string has to be massaged. 22940947Sbostic */ 23040947Sbostic PLAN * 23140947Sbostic c_exec(argvp, isok) 23240947Sbostic char ***argvp; 23340947Sbostic int isok; 23440947Sbostic { 23540947Sbostic PLAN *new; /* node returned */ 23640947Sbostic register int cnt; 23740947Sbostic register char **argv, **ap, *p; 23840947Sbostic 23940947Sbostic ftsoptions |= FTS_NOCHDIR; 24040947Sbostic output_specified = 1; 24140947Sbostic 24240947Sbostic NEW(T_EXEC, f_exec); 24340947Sbostic new->flags = isok; 24440947Sbostic 24540947Sbostic for (ap = argv = *argvp;; ++ap) { 24640947Sbostic if (!*ap) 24740947Sbostic bad_arg(isok ? "-ok" : "-exec", "no terminating \";\""); 24840947Sbostic if (**ap == ';') 24940947Sbostic break; 25040947Sbostic } 25140947Sbostic 25240947Sbostic cnt = ap - *argvp + 1; 25340947Sbostic new->e_argv = (char **)emalloc((u_int)cnt * sizeof(char *)); 25440947Sbostic new->e_orig = (char **)emalloc((u_int)cnt * sizeof(char *)); 25540947Sbostic new->e_len = (int *)emalloc((u_int)cnt * sizeof(u_char)); 25640947Sbostic 25740947Sbostic for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) { 25840947Sbostic new->e_orig[cnt] = *argv; 25940947Sbostic for (p = *argv; *p; ++p) 26040947Sbostic if (p[0] == '{' && p[1] == '}') { 26140947Sbostic new->e_argv[cnt] = emalloc((u_int)1024); 26240947Sbostic new->e_len[cnt] = 1024; 26340947Sbostic break; 26440947Sbostic } 26540947Sbostic if (!*p) { 26640947Sbostic new->e_argv[cnt] = *argv; 26740947Sbostic new->e_len[cnt] = 0; 26840947Sbostic } 26940947Sbostic } 27040947Sbostic new->e_argv[cnt] = new->e_orig[cnt] = NULL; 27140947Sbostic 27240947Sbostic *argvp = argv + 1; 27340947Sbostic return(new); 27440947Sbostic } 27540947Sbostic 27640947Sbostic /* 27740947Sbostic * -follow functions -- 27840947Sbostic * 27940947Sbostic * Always true, causes symbolic links to be followed on a global 28040947Sbostic * basis. 28140947Sbostic */ 28240947Sbostic PLAN * 28340947Sbostic c_follow() 28440947Sbostic { 28540947Sbostic PLAN *new; 28640947Sbostic 28740947Sbostic ftsoptions &= ~FTS_PHYSICAL; 28840947Sbostic ftsoptions |= FTS_LOGICAL; 28940947Sbostic 29040947Sbostic NEW(T_FOLLOW, f_always_true); 29140947Sbostic return(new); 29240947Sbostic } 29340947Sbostic 29440947Sbostic /* 29540947Sbostic * -fstype functions -- 29640947Sbostic * 29740947Sbostic * True if the file is of a certain type. 29840947Sbostic */ 29940947Sbostic f_fstype(plan, entry) 30040947Sbostic PLAN *plan; 30140947Sbostic FTSENT *entry; 30240947Sbostic { 30342275Sbostic static dev_t curdev; /* need a guaranteed illegal dev value */ 30442275Sbostic static int first = 1; 30540947Sbostic struct statfs sb; 30642255Sbostic static short val; 30740947Sbostic 30840947Sbostic /* only check when we cross mount point */ 30942275Sbostic if (first || curdev != entry->fts_statb.st_dev) { 31043856Sbostic curdev = entry->fts_statb.st_dev; 31142255Sbostic if (statfs(entry->fts_accpath, &sb)) { 31240947Sbostic (void)fprintf(stderr, "find: %s: %s.\n", 31342255Sbostic entry->fts_accpath, strerror(errno)); 31440947Sbostic exit(1); 31540947Sbostic } 31642275Sbostic first = 0; 31742255Sbostic val = plan->flags == MOUNT_NONE ? sb.f_flags : sb.f_type; 31840947Sbostic } 31942255Sbostic return(plan->flags == MOUNT_NONE ? 32042255Sbostic val & MNT_LOCAL : val == plan->flags); 32140947Sbostic } 32240947Sbostic 32340947Sbostic PLAN * 32440947Sbostic c_fstype(arg) 32540947Sbostic char *arg; 32640947Sbostic { 32740947Sbostic register PLAN *new; 32840947Sbostic 32940947Sbostic ftsoptions &= ~FTS_NOSTAT; 33040947Sbostic 33140947Sbostic NEW(T_FSTYPE, f_fstype); 33240947Sbostic switch(*arg) { 33342255Sbostic case 'l': 33442255Sbostic if (!strcmp(arg, "local")) { 33542255Sbostic new->flags = MOUNT_NONE; 33642255Sbostic return(new); 33742255Sbostic } 33842255Sbostic break; 33940947Sbostic case 'm': 34040947Sbostic if (!strcmp(arg, "mfs")) { 34140947Sbostic new->flags = MOUNT_MFS; 34240947Sbostic return(new); 34340947Sbostic } 34440947Sbostic break; 34540947Sbostic case 'n': 34640947Sbostic if (!strcmp(arg, "nfs")) { 34740947Sbostic new->flags = MOUNT_NFS; 34840947Sbostic return(new); 34940947Sbostic } 35040947Sbostic break; 35140947Sbostic case 'p': 35240947Sbostic if (!strcmp(arg, "pc")) { 35340947Sbostic new->flags = MOUNT_PC; 35440947Sbostic return(new); 35540947Sbostic } 35640947Sbostic break; 35740947Sbostic case 'u': 35840947Sbostic if (!strcmp(arg, "ufs")) { 35940947Sbostic new->flags = MOUNT_UFS; 36040947Sbostic return(new); 36140947Sbostic } 36240947Sbostic break; 36340947Sbostic } 36440947Sbostic (void)fprintf(stderr, "find: unknown file type %s.\n", arg); 36540947Sbostic exit(1); 36640947Sbostic /* NOTREACHED */ 36740947Sbostic } 36840947Sbostic 36940947Sbostic /* 37040947Sbostic * -group gname functions -- 37140947Sbostic * 37240947Sbostic * True if the file belongs to the group gname. If gname is numeric and 37340947Sbostic * an equivalent of the getgrnam() function does not return a valid group 37440947Sbostic * name, gname is taken as a group ID. 37540947Sbostic */ 37640947Sbostic f_group(plan, entry) 37740947Sbostic PLAN *plan; 37840947Sbostic FTSENT *entry; 37940947Sbostic { 38042255Sbostic return(entry->fts_statb.st_gid == plan->g_data); 38140947Sbostic } 38240947Sbostic 38340947Sbostic PLAN * 38440947Sbostic c_group(gname) 38540947Sbostic char *gname; 38640947Sbostic { 38740947Sbostic PLAN *new; 38840947Sbostic struct group *g; 38940947Sbostic gid_t gid; 39040947Sbostic 39140947Sbostic ftsoptions &= ~FTS_NOSTAT; 39240947Sbostic 39340947Sbostic g = getgrnam(gname); 39440947Sbostic if (g == NULL) { 39540947Sbostic gid = atoi(gname); 39640947Sbostic if (gid == 0 && gname[0] != '0') 39740947Sbostic bad_arg("-group", "no such group"); 39840947Sbostic } else 39940947Sbostic gid = g->gr_gid; 40040947Sbostic 40140947Sbostic NEW(T_GROUP, f_group); 40240947Sbostic new->g_data = gid; 40340947Sbostic return(new); 40440947Sbostic } 40540947Sbostic 40640947Sbostic /* 40740947Sbostic * -inum n functions -- 40840947Sbostic * 40940947Sbostic * True if the file has inode # n. 41040947Sbostic */ 41140947Sbostic f_inum(plan, entry) 41240947Sbostic PLAN *plan; 41340947Sbostic FTSENT *entry; 41440947Sbostic { 41542255Sbostic COMPARE(entry->fts_statb.st_ino, plan->i_data); 41640947Sbostic } 41740947Sbostic 41840947Sbostic PLAN * 41940947Sbostic c_inum(arg) 42040947Sbostic char *arg; 42140947Sbostic { 42240947Sbostic PLAN *new; 42340947Sbostic 42440947Sbostic ftsoptions &= ~FTS_NOSTAT; 42540947Sbostic 42640947Sbostic NEW(T_INUM, f_inum); 42740947Sbostic new->i_data = find_parsenum(new, "-inum", arg, (char *)NULL); 42840947Sbostic return(new); 42940947Sbostic } 43040947Sbostic 43140947Sbostic /* 43240947Sbostic * -links n functions -- 43340947Sbostic * 43440947Sbostic * True if the file has n links. 43540947Sbostic */ 43640947Sbostic f_links(plan, entry) 43740947Sbostic PLAN *plan; 43840947Sbostic FTSENT *entry; 43940947Sbostic { 44042255Sbostic COMPARE(entry->fts_statb.st_nlink, plan->l_data); 44140947Sbostic } 44240947Sbostic 44340947Sbostic PLAN * 44440947Sbostic c_links(arg) 44540947Sbostic char *arg; 44640947Sbostic { 44740947Sbostic PLAN *new; 44840947Sbostic 44940947Sbostic ftsoptions &= ~FTS_NOSTAT; 45040947Sbostic 45140947Sbostic NEW(T_LINKS, f_links); 45240947Sbostic new->l_data = find_parsenum(new, "-links", arg, (char *)NULL); 45340947Sbostic return(new); 45440947Sbostic } 45540947Sbostic 45640947Sbostic /* 45740947Sbostic * -ls functions -- 45840947Sbostic * 45940947Sbostic * Always true - prints the current entry to stdout in "ls" format. 46040947Sbostic */ 46140947Sbostic /* ARGSUSED */ 46240947Sbostic f_ls(plan, entry) 46340947Sbostic PLAN *plan; 46440947Sbostic FTSENT *entry; 46540947Sbostic { 46642255Sbostic printlong(entry->fts_path, entry->fts_accpath, &entry->fts_statb); 46742255Sbostic return(1); 46840947Sbostic } 46940947Sbostic 47040947Sbostic PLAN * 47140947Sbostic c_ls() 47240947Sbostic { 47340947Sbostic PLAN *new; 47440947Sbostic 47540947Sbostic ftsoptions &= ~FTS_NOSTAT; 47640947Sbostic output_specified = 1; 47740947Sbostic 47840947Sbostic NEW(T_LS, f_ls); 47940947Sbostic return(new); 48040947Sbostic } 48140947Sbostic 48240947Sbostic /* 48340947Sbostic * -name functions -- 48440947Sbostic * 48540947Sbostic * True if the basename of the filename being examined 48640947Sbostic * matches pattern using Pattern Matching Notation S3.14 48740947Sbostic */ 48840947Sbostic f_name(plan, entry) 48940947Sbostic PLAN *plan; 49040947Sbostic FTSENT *entry; 49140947Sbostic { 49242255Sbostic return(fnmatch(plan->c_data, entry->fts_name, FNM_QUOTE)); 49340947Sbostic } 49440947Sbostic 49540947Sbostic PLAN * 49640947Sbostic c_name(pattern) 49740947Sbostic char *pattern; 49840947Sbostic { 49940947Sbostic PLAN *new; 50040947Sbostic 50140947Sbostic NEW(T_NAME, f_name); 50240947Sbostic new->c_data = pattern; 50340947Sbostic return(new); 50440947Sbostic } 50540947Sbostic 50640947Sbostic /* 50740947Sbostic * -newer file functions -- 50840947Sbostic * 50940947Sbostic * True if the current file has been modified more recently 51040947Sbostic * then the modification time of the file named by the pathname 51140947Sbostic * file. 51240947Sbostic */ 51340947Sbostic f_newer(plan, entry) 51440947Sbostic PLAN *plan; 51540947Sbostic FTSENT *entry; 51640947Sbostic { 51742255Sbostic return(entry->fts_statb.st_mtime > plan->t_data); 51840947Sbostic } 51940947Sbostic 52040947Sbostic PLAN * 52140947Sbostic c_newer(filename) 52240947Sbostic char *filename; 52340947Sbostic { 52440947Sbostic PLAN *new; 52540947Sbostic struct stat sb; 52640947Sbostic 52740947Sbostic ftsoptions &= ~FTS_NOSTAT; 52840947Sbostic 52940947Sbostic if (stat(filename, &sb)) { 53040947Sbostic (void)fprintf(stderr, "find: %s: %s.\n", 53140947Sbostic filename, strerror(errno)); 53240947Sbostic exit(1); 53340947Sbostic } 53440947Sbostic NEW(T_NEWER, f_newer); 53540947Sbostic new->t_data = sb.st_mtime; 53640947Sbostic return(new); 53740947Sbostic } 53840947Sbostic 53940947Sbostic /* 54040947Sbostic * -nogroup functions -- 54140947Sbostic * 54240947Sbostic * True if file belongs to a user ID for which the equivalent 54340947Sbostic * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. 54440947Sbostic */ 54540947Sbostic /* ARGSUSED */ 54640947Sbostic f_nogroup(plan, entry) 54740947Sbostic PLAN *plan; 54840947Sbostic FTSENT *entry; 54940947Sbostic { 55042255Sbostic return(group_from_gid(entry->fts_statb.st_gid, 1)); 55140947Sbostic } 55240947Sbostic 55340947Sbostic PLAN * 55440947Sbostic c_nogroup() 55540947Sbostic { 55640947Sbostic PLAN *new; 55740947Sbostic 55840947Sbostic ftsoptions &= ~FTS_NOSTAT; 55940947Sbostic 56040947Sbostic NEW(T_NOGROUP, f_nogroup); 56140947Sbostic return(new); 56240947Sbostic } 56340947Sbostic 56440947Sbostic /* 56540947Sbostic * -nouser functions -- 56640947Sbostic * 56740947Sbostic * True if file belongs to a user ID for which the equivalent 56840947Sbostic * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. 56940947Sbostic */ 57040947Sbostic /* ARGSUSED */ 57140947Sbostic f_nouser(plan, entry) 57240947Sbostic PLAN *plan; 57340947Sbostic FTSENT *entry; 57440947Sbostic { 57542255Sbostic return(user_from_uid(entry->fts_statb.st_uid, 1)); 57640947Sbostic } 57740947Sbostic 57840947Sbostic PLAN * 57940947Sbostic c_nouser() 58040947Sbostic { 58140947Sbostic PLAN *new; 58240947Sbostic 58340947Sbostic ftsoptions &= ~FTS_NOSTAT; 58440947Sbostic 58540947Sbostic NEW(T_NOUSER, f_nouser); 58640947Sbostic return(new); 58740947Sbostic } 58840947Sbostic 58940947Sbostic /* 59040947Sbostic * -perm functions -- 59140947Sbostic * 59240947Sbostic * The mode argument is used to represent file mode bits. If it starts 59340947Sbostic * with a leading digit, it's treated as an octal mode, otherwise as a 59440947Sbostic * symbolic mode. 59540947Sbostic */ 59640947Sbostic f_perm(plan, entry) 59740947Sbostic PLAN *plan; 59840947Sbostic FTSENT *entry; 59940947Sbostic { 60040947Sbostic mode_t mode; 60140947Sbostic 60242255Sbostic mode = entry->fts_statb.st_mode & 60340947Sbostic (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); 60440947Sbostic if (plan->flags) 60540947Sbostic return((plan->m_data | mode) == mode); 60640947Sbostic else 60740947Sbostic return(mode == plan->m_data); 60840947Sbostic /* NOTREACHED */ 60940947Sbostic } 61040947Sbostic 61140947Sbostic PLAN * 61240947Sbostic c_perm(perm) 61340947Sbostic char *perm; 61440947Sbostic { 61540947Sbostic PLAN *new; 616*44764Strent mode_t *set; 61740947Sbostic 61840947Sbostic ftsoptions &= ~FTS_NOSTAT; 61940947Sbostic 62040947Sbostic NEW(T_PERM, f_perm); 62140947Sbostic 62240947Sbostic if (*perm == '-') { 62340947Sbostic new->flags = 1; 62440947Sbostic ++perm; 62540947Sbostic } 62640947Sbostic 627*44764Strent if ((set = setmode(perm)) == NULL) 62840947Sbostic bad_arg("-perm", "illegal mode string"); 62940947Sbostic 630*44764Strent new->m_data = getmode(set, 0); 63140947Sbostic return(new); 63240947Sbostic } 63340947Sbostic 63440947Sbostic /* 63540947Sbostic * -print functions -- 63640947Sbostic * 63740947Sbostic * Always true, causes the current pathame to be written to 63840947Sbostic * standard output. 63940947Sbostic */ 64040947Sbostic /* ARGSUSED */ 64140947Sbostic f_print(plan, entry) 64240947Sbostic PLAN *plan; 64340947Sbostic FTSENT *entry; 64440947Sbostic { 64542255Sbostic (void)printf("%s\n", entry->fts_path); 64642255Sbostic return(1); 64740947Sbostic } 64840947Sbostic 64940947Sbostic PLAN * 65040947Sbostic c_print() 65140947Sbostic { 65240947Sbostic PLAN *new; 65340947Sbostic 65440947Sbostic output_specified = 1; 65540947Sbostic 65640947Sbostic NEW(T_PRINT, f_print); 65740947Sbostic return(new); 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 67240947Sbostic if (ftsset(tree, entry, FTS_SKIP)) { 67340947Sbostic (void)fprintf(stderr, 67442255Sbostic "find: %s: %s.\n", entry->fts_path, strerror(errno)); 67540947Sbostic exit(1); 67640947Sbostic } 67742255Sbostic return(1); 67840947Sbostic } 67940947Sbostic 68040947Sbostic PLAN * 68140947Sbostic c_prune() 68240947Sbostic { 68340947Sbostic PLAN *new; 68440947Sbostic 68540947Sbostic NEW(T_PRUNE, f_prune); 68640947Sbostic return(new); 68740947Sbostic } 68840947Sbostic 68940947Sbostic /* 69040947Sbostic * -size n[c] functions -- 69140947Sbostic * 69240947Sbostic * True if the file size in bytes, divided by an implementation defined 69340947Sbostic * value and rounded up to the next integer, is n. If n is followed by 69440947Sbostic * a c, the size is in bytes. 69540947Sbostic */ 69640947Sbostic #define FIND_SIZE 512 69740947Sbostic static int divsize = 1; 69840947Sbostic 69940947Sbostic f_size(plan, entry) 70040947Sbostic PLAN *plan; 70140947Sbostic FTSENT *entry; 70240947Sbostic { 70340947Sbostic off_t size; 70440947Sbostic 70542255Sbostic size = divsize ? (entry->fts_statb.st_size + FIND_SIZE - 1) / 70642255Sbostic FIND_SIZE : entry->fts_statb.st_size; 70740947Sbostic COMPARE(size, plan->o_data); 70840947Sbostic } 70940947Sbostic 71040947Sbostic PLAN * 71140947Sbostic c_size(arg) 71240947Sbostic char *arg; 71340947Sbostic { 71440947Sbostic PLAN *new; 71540947Sbostic char endch; 71640947Sbostic 71740947Sbostic ftsoptions &= ~FTS_NOSTAT; 71840947Sbostic 71940947Sbostic NEW(T_SIZE, f_size); 72040947Sbostic new->o_data = find_parsenum(new, "-size", arg, &endch); 72140947Sbostic if (endch == 'c') 72240947Sbostic divsize = 0; 72340947Sbostic return(new); 72440947Sbostic } 72540947Sbostic 72640947Sbostic /* 72740947Sbostic * -type c functions -- 72840947Sbostic * 72940947Sbostic * True if the type of the file is c, where c is b, c, d, p, or f for 73040947Sbostic * block special file, character special file, directory, FIFO, or 73140947Sbostic * regular file, respectively. 73240947Sbostic */ 73340947Sbostic f_type(plan, entry) 73440947Sbostic PLAN *plan; 73540947Sbostic FTSENT *entry; 73640947Sbostic { 737*44764Strent return((entry->fts_statb.st_mode & S_IFMT) == plan->m_data); 73840947Sbostic } 73940947Sbostic 74040947Sbostic PLAN * 74140947Sbostic c_type(typestring) 74240947Sbostic char *typestring; 74340947Sbostic { 74440947Sbostic PLAN *new; 74540947Sbostic mode_t mask; 74640947Sbostic 74740947Sbostic ftsoptions &= ~FTS_NOSTAT; 74840947Sbostic 74940947Sbostic switch (typestring[0]) { 75040947Sbostic case 'b': 75140947Sbostic mask = S_IFBLK; 75240947Sbostic break; 75340947Sbostic case 'c': 75440947Sbostic mask = S_IFCHR; 75540947Sbostic break; 75640947Sbostic case 'd': 75740947Sbostic mask = S_IFDIR; 75840947Sbostic break; 75940947Sbostic case 'f': 76040947Sbostic mask = S_IFREG; 76140947Sbostic break; 76240947Sbostic case 'l': 76340947Sbostic mask = S_IFLNK; 76440947Sbostic break; 76540947Sbostic case 'p': 76640947Sbostic mask = S_IFIFO; 76740947Sbostic break; 76840947Sbostic case 's': 76940947Sbostic mask = S_IFSOCK; 77040947Sbostic break; 77140947Sbostic default: 77240947Sbostic bad_arg("-type", "unknown type"); 77340947Sbostic } 77440947Sbostic 77540947Sbostic NEW(T_TYPE, f_type); 77640947Sbostic new->m_data = mask; 77740947Sbostic return(new); 77840947Sbostic } 77940947Sbostic 78040947Sbostic /* 78140947Sbostic * -user uname functions -- 78240947Sbostic * 78340947Sbostic * True if the file belongs to the user uname. If uname is numeric and 78440947Sbostic * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not 78540947Sbostic * return a valid user name, uname is taken as a user ID. 78640947Sbostic */ 78740947Sbostic f_user(plan, entry) 78840947Sbostic PLAN *plan; 78940947Sbostic FTSENT *entry; 79040947Sbostic { 79142255Sbostic return(entry->fts_statb.st_uid == plan->u_data); 79240947Sbostic } 79340947Sbostic 79440947Sbostic PLAN * 79540947Sbostic c_user(username) 79640947Sbostic char *username; 79740947Sbostic { 79840947Sbostic PLAN *new; 79940947Sbostic struct passwd *p; 80040947Sbostic uid_t uid; 80140947Sbostic 80240947Sbostic ftsoptions &= ~FTS_NOSTAT; 80340947Sbostic 80440947Sbostic p = getpwnam(username); 80540947Sbostic if (p == NULL) { 80640947Sbostic uid = atoi(username); 80740947Sbostic if (uid == 0 && username[0] != '0') 80840947Sbostic bad_arg("-user", "no such user"); 80940947Sbostic } else 81040947Sbostic uid = p->pw_uid; 81140947Sbostic 81240947Sbostic NEW(T_USER, f_user); 81340947Sbostic new->u_data = uid; 81440947Sbostic return(new); 81540947Sbostic } 81640947Sbostic 81740947Sbostic /* 81840947Sbostic * -xdev functions -- 81940947Sbostic * 82040947Sbostic * Always true, causes find not to decend past directories that have a 82140947Sbostic * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) 82240947Sbostic */ 82340947Sbostic PLAN * 82440947Sbostic c_xdev() 82540947Sbostic { 82640947Sbostic PLAN *new; 82740947Sbostic 82840947Sbostic ftsoptions &= ~FTS_NOSTAT; 82942275Sbostic ftsoptions |= FTS_XDEV; 83040947Sbostic 83140947Sbostic NEW(T_XDEV, f_always_true); 83240947Sbostic return(new); 83340947Sbostic } 83440947Sbostic 83540947Sbostic /* 83640947Sbostic * ( expression ) functions -- 83740947Sbostic * 83840947Sbostic * True if expression is true. 83940947Sbostic */ 84040947Sbostic f_expr(plan, entry) 84140947Sbostic PLAN *plan; 84240947Sbostic FTSENT *entry; 84340947Sbostic { 84440947Sbostic register PLAN *p; 84540947Sbostic register int state; 84640947Sbostic 84740947Sbostic for (p = plan->p_data[0]; 84840947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 84940947Sbostic return(state); 85040947Sbostic } 85140947Sbostic 85240947Sbostic /* 85340947Sbostic * T_OPENPAREN and T_CLOSEPAREN nodes are temporary place markers. They are 85440947Sbostic * eliminated during phase 2 of find_formplan() --- the '(' node is converted 85540947Sbostic * to a T_EXPR node containing the expression and the ')' node is discarded. 85640947Sbostic */ 85740947Sbostic PLAN * 85840947Sbostic c_openparen() 85940947Sbostic { 86040947Sbostic PLAN *new; 86140947Sbostic 86240947Sbostic NEW(T_OPENPAREN, (int (*)())-1); 86340947Sbostic return(new); 86440947Sbostic } 86540947Sbostic 86640947Sbostic PLAN * 86740947Sbostic c_closeparen() 86840947Sbostic { 86940947Sbostic PLAN *new; 87040947Sbostic 87140947Sbostic NEW(T_CLOSEPAREN, (int (*)())-1); 87240947Sbostic return(new); 87340947Sbostic } 87440947Sbostic 87540947Sbostic /* 87640947Sbostic * -mtime n functions -- 87740947Sbostic * 87840947Sbostic * True if the difference between the file modification time and the 87940947Sbostic * current time is n 24 hour periods. 88040947Sbostic */ 88140947Sbostic f_mtime(plan, entry) 88240947Sbostic PLAN *plan; 88340947Sbostic FTSENT *entry; 88440947Sbostic { 88540947Sbostic extern time_t now; 88640947Sbostic 88742255Sbostic COMPARE((now - entry->fts_statb.st_mtime + SECSPERDAY - 1) / 88842255Sbostic SECSPERDAY, plan->t_data); 88940947Sbostic } 89040947Sbostic 89140947Sbostic PLAN * 89240947Sbostic c_mtime(arg) 89340947Sbostic char *arg; 89440947Sbostic { 89540947Sbostic PLAN *new; 89640947Sbostic 89740947Sbostic ftsoptions &= ~FTS_NOSTAT; 89840947Sbostic 89940947Sbostic NEW(T_MTIME, f_mtime); 90040947Sbostic new->t_data = find_parsenum(new, "-mtime", arg, (char *)NULL); 90140947Sbostic return(new); 90240947Sbostic } 90340947Sbostic 90440947Sbostic /* 90540947Sbostic * ! expression functions -- 90640947Sbostic * 90740947Sbostic * Negation of a primary; the unary NOT operator. 90840947Sbostic */ 90940947Sbostic f_not(plan, entry) 91040947Sbostic PLAN *plan; 91140947Sbostic FTSENT *entry; 91240947Sbostic { 91340947Sbostic register PLAN *p; 91440947Sbostic register int state; 91540947Sbostic 91640947Sbostic for (p = plan->p_data[0]; 91740947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 91840947Sbostic return(!state); 91940947Sbostic } 92040947Sbostic 92140947Sbostic PLAN * 92240947Sbostic c_not() 92340947Sbostic { 92440947Sbostic PLAN *new; 92540947Sbostic 92640947Sbostic NEW(T_NOT, f_not); 92740947Sbostic return(new); 92840947Sbostic } 92940947Sbostic 93040947Sbostic /* 93140947Sbostic * expression -o expression functions -- 93240947Sbostic * 93340947Sbostic * Alternation of primaries; the OR operator. The second expression is 93440947Sbostic * not evaluated if the first expression is true. 93540947Sbostic */ 93640947Sbostic f_or(plan, entry) 93740947Sbostic PLAN *plan; 93840947Sbostic FTSENT *entry; 93940947Sbostic { 94040947Sbostic register PLAN *p; 94140947Sbostic register int state; 94240947Sbostic 94340947Sbostic for (p = plan->p_data[0]; 94440947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 94540947Sbostic 94640947Sbostic if (state) 94742255Sbostic return(1); 94840947Sbostic 94940947Sbostic for (p = plan->p_data[1]; 95040947Sbostic p && (state = (p->eval)(p, entry)); p = p->next); 95140947Sbostic return(state); 95240947Sbostic } 95340947Sbostic 95440947Sbostic PLAN * 95540947Sbostic c_or() 95640947Sbostic { 95740947Sbostic PLAN *new; 95840947Sbostic 95940947Sbostic NEW(T_OR, f_or); 96040947Sbostic return(new); 96140947Sbostic } 962