function.c
来自「早期freebsd实现」· C语言 代码 · 共 1,070 行 · 第 1/2 页
C
1,070 行
ftsoptions &= ~FTS_NOSTAT; new = palloc(N_LINKS, f_links); new->l_data = (nlink_t)find_parsenum(new, "-links", arg, NULL); return (new);} /* * -ls functions -- * * Always true - prints the current entry to stdout in "ls" format. */intf_ls(plan, entry) PLAN *plan; FTSENT *entry;{ printlong(entry->fts_path, entry->fts_accpath, entry->fts_statp); return (1);} PLAN *c_ls(){ ftsoptions &= ~FTS_NOSTAT; isoutput = 1; return (palloc(N_LS, f_ls));}/* * -mtime n functions -- * * True if the difference between the file modification time and the * current time is n 24 hour periods. */intf_mtime(plan, entry) PLAN *plan; FTSENT *entry;{ extern time_t now; COMPARE((now - entry->fts_statp->st_mtime + SECSPERDAY - 1) / SECSPERDAY, plan->t_data);} PLAN *c_mtime(arg) char *arg;{ PLAN *new; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_MTIME, f_mtime); new->t_data = find_parsenum(new, "-mtime", arg, NULL); TIME_CORRECT(new, N_MTIME); return (new);}/* * -name functions -- * * True if the basename of the filename being examined * matches pattern using Pattern Matching Notation S3.14 */intf_name(plan, entry) PLAN *plan; FTSENT *entry;{ return (!fnmatch(plan->c_data, entry->fts_name, 0));} PLAN *c_name(pattern) char *pattern;{ PLAN *new; new = palloc(N_NAME, f_name); new->c_data = pattern; return (new);} /* * -newer file functions -- * * True if the current file has been modified more recently * then the modification time of the file named by the pathname * file. */intf_newer(plan, entry) PLAN *plan; FTSENT *entry;{ return (entry->fts_statp->st_mtime > plan->t_data);} PLAN *c_newer(filename) char *filename;{ PLAN *new; struct stat sb; ftsoptions &= ~FTS_NOSTAT; if (stat(filename, &sb)) err(1, "%s", filename); new = palloc(N_NEWER, f_newer); new->t_data = sb.st_mtime; return (new);} /* * -nogroup functions -- * * True if file belongs to a user ID for which the equivalent * of the getgrnam() 9.2.1 [POSIX.1] function returns NULL. */intf_nogroup(plan, entry) PLAN *plan; FTSENT *entry;{ char *group_from_gid(); return (group_from_gid(entry->fts_statp->st_gid, 1) ? 0 : 1);} PLAN *c_nogroup(){ ftsoptions &= ~FTS_NOSTAT; return (palloc(N_NOGROUP, f_nogroup));} /* * -nouser functions -- * * True if file belongs to a user ID for which the equivalent * of the getpwuid() 9.2.2 [POSIX.1] function returns NULL. */intf_nouser(plan, entry) PLAN *plan; FTSENT *entry;{ char *user_from_uid(); return (user_from_uid(entry->fts_statp->st_uid, 1) ? 0 : 1);} PLAN *c_nouser(){ ftsoptions &= ~FTS_NOSTAT; return (palloc(N_NOUSER, f_nouser));} /* * -path functions -- * * True if the path of the filename being examined * matches pattern using Pattern Matching Notation S3.14 */intf_path(plan, entry) PLAN *plan; FTSENT *entry;{ return (!fnmatch(plan->c_data, entry->fts_path, 0));} PLAN *c_path(pattern) char *pattern;{ PLAN *new; new = palloc(N_NAME, f_path); new->c_data = pattern; return (new);} /* * -perm functions -- * * The mode argument is used to represent file mode bits. If it starts * with a leading digit, it's treated as an octal mode, otherwise as a * symbolic mode. */intf_perm(plan, entry) PLAN *plan; FTSENT *entry;{ mode_t mode; mode = entry->fts_statp->st_mode & (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO); if (plan->flags == F_ATLEAST) return ((plan->m_data | mode) == mode); else return (mode == plan->m_data); /* NOTREACHED */} PLAN *c_perm(perm) char *perm;{ PLAN *new; mode_t *set; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_PERM, f_perm); if (*perm == '-') { new->flags = F_ATLEAST; ++perm; } if ((set = setmode(perm)) == NULL) err(1, "-perm: %s: illegal mode string", perm); new->m_data = getmode(set, 0); return (new);} /* * -print functions -- * * Always true, causes the current pathame to be written to * standard output. */intf_print(plan, entry) PLAN *plan; FTSENT *entry;{ (void)printf("%s\n", entry->fts_path); return (1);} PLAN *c_print(){ isoutput = 1; return (palloc(N_PRINT, f_print));} /* * -prune functions -- * * Prune a portion of the hierarchy. */intf_prune(plan, entry) PLAN *plan; FTSENT *entry;{ extern FTS *tree; if (fts_set(tree, entry, FTS_SKIP)) err(1, "%s", entry->fts_path); return (1);} PLAN *c_prune(){ return (palloc(N_PRUNE, f_prune));} /* * -size n[c] functions -- * * True if the file size in bytes, divided by an implementation defined * value and rounded up to the next integer, is n. If n is followed by * a c, the size is in bytes. */#define FIND_SIZE 512static int divsize = 1;intf_size(plan, entry) PLAN *plan; FTSENT *entry;{ off_t size; size = divsize ? (entry->fts_statp->st_size + FIND_SIZE - 1) / FIND_SIZE : entry->fts_statp->st_size; COMPARE(size, plan->o_data);} PLAN *c_size(arg) char *arg;{ PLAN *new; char endch; ftsoptions &= ~FTS_NOSTAT; new = palloc(N_SIZE, f_size); endch = 'c'; new->o_data = find_parsenum(new, "-size", arg, &endch); if (endch == 'c') divsize = 0; return (new);} /* * -type c functions -- * * True if the type of the file is c, where c is b, c, d, p, or f for * block special file, character special file, directory, FIFO, or * regular file, respectively. */intf_type(plan, entry) PLAN *plan; FTSENT *entry;{ return ((entry->fts_statp->st_mode & S_IFMT) == plan->m_data);} PLAN *c_type(typestring) char *typestring;{ PLAN *new; mode_t mask; ftsoptions &= ~FTS_NOSTAT; switch (typestring[0]) { case 'b': mask = S_IFBLK; break; case 'c': mask = S_IFCHR; break; case 'd': mask = S_IFDIR; break; case 'f': mask = S_IFREG; break; case 'l': mask = S_IFLNK; break; case 'p': mask = S_IFIFO; break; case 's': mask = S_IFSOCK; break; default: errx(1, "-type: %s: unknown type", typestring); } new = palloc(N_TYPE, f_type); new->m_data = mask; return (new);} /* * -user uname functions -- * * True if the file belongs to the user uname. If uname is numeric and * an equivalent of the getpwnam() S9.2.2 [POSIX.1] function does not * return a valid user name, uname is taken as a user ID. */intf_user(plan, entry) PLAN *plan; FTSENT *entry;{ return (entry->fts_statp->st_uid == plan->u_data);} PLAN *c_user(username) char *username;{ PLAN *new; struct passwd *p; uid_t uid; ftsoptions &= ~FTS_NOSTAT; p = getpwnam(username); if (p == NULL) { uid = atoi(username); if (uid == 0 && username[0] != '0') errx(1, "-user: %s: no such user", username); } else uid = p->pw_uid; new = palloc(N_USER, f_user); new->u_data = uid; return (new);} /* * -xdev functions -- * * Always true, causes find not to decend past directories that have a * different device ID (st_dev, see stat() S5.6.2 [POSIX.1]) */PLAN *c_xdev(){ ftsoptions |= FTS_XDEV; return (palloc(N_XDEV, f_always_true));}/* * ( expression ) functions -- * * True if expression is true. */intf_expr(plan, entry) PLAN *plan; FTSENT *entry;{ register PLAN *p; register int state; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); return (state);} /* * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers. They are * eliminated during phase 2 of find_formplan() --- the '(' node is converted * to a N_EXPR node containing the expression and the ')' node is discarded. */PLAN *c_openparen(){ return (palloc(N_OPENPAREN, (int (*)())-1));} PLAN *c_closeparen(){ return (palloc(N_CLOSEPAREN, (int (*)())-1));} /* * ! expression functions -- * * Negation of a primary; the unary NOT operator. */intf_not(plan, entry) PLAN *plan; FTSENT *entry;{ register PLAN *p; register int state; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); return (!state);} PLAN *c_not(){ return (palloc(N_NOT, f_not));} /* * expression -o expression functions -- * * Alternation of primaries; the OR operator. The second expression is * not evaluated if the first expression is true. */intf_or(plan, entry) PLAN *plan; FTSENT *entry;{ register PLAN *p; register int state; for (p = plan->p_data[0]; p && (state = (p->eval)(p, entry)); p = p->next); if (state) return (1); for (p = plan->p_data[1]; p && (state = (p->eval)(p, entry)); p = p->next); return (state);}PLAN *c_or(){ return (palloc(N_OR, f_or));}static PLAN *palloc(t, f) enum ntype t; int (*f) __P((PLAN *, FTSENT *));{ PLAN *new; if ((new = malloc(sizeof(PLAN))) == NULL) err(1, NULL); new->type = t; new->eval = f; new->flags = 0; new->next = NULL; return (new);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?