/* See LICENSE file for copyright and license details. */
-#include <stdbool.h>
+#include <sys/stat.h>
+
+#include <dirent.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <sys/stat.h>
-#define OPER(x) (oper[(x)-'a'])
+#include "arg.h"
+char *argv0;
-static bool test(const char *);
+#define FLAG(x) (flag[(x)-'a'])
-static bool quiet = false;
-static bool oper[26];
+static void test(const char *, const char *);
+static void usage(void);
+
+static int match = 0;
+static int flag[26];
static struct stat old, new;
-int
-main(int argc, char *argv[]) {
- char buf[BUFSIZ], *p;
- bool match = false;
- int opt;
+static void
+test(const char *path, const char *name)
+{
+ struct stat st, ln;
- while((opt = getopt(argc, argv, "C:bcdefghn:o:pqrsuwx")) != -1)
- switch(opt) {
- case 'C': /* tests relative to directory */
- if(chdir(optarg) == -1) {
- perror(optarg);
- exit(2);
- }
- break;
- case 'n': /* newer than file */
- case 'o': /* older than file */
- if(!(OPER(opt) = stat(optarg, (opt == 'n' ? &new : &old)) == 0))
- perror(optarg);
- break;
- case 'q': /* quiet (no output, just status) */
- quiet = true;
- break;
- default: /* miscellaneous operators */
- OPER(opt) = true;
- break;
- case '?': /* error: unknown flag */
- fprintf(stderr, "usage: %s [-bcdefghpqrsuwx] [-C dir] [-n file] [-o file] [file...]\n", argv[0]);
- exit(2);
- }
- if(optind == argc)
- while(fgets(buf, sizeof buf, stdin)) {
- if(*(p = &buf[strlen(buf)-1]) == '\n')
- *p = '\0';
- match |= test(buf);
- }
- else
- while(optind < argc)
- match |= test(argv[optind++]);
+ if ((!stat(path, &st) && (FLAG('a') || name[0] != '.') /* hidden files */
+ && (!FLAG('b') || S_ISBLK(st.st_mode)) /* block special */
+ && (!FLAG('c') || S_ISCHR(st.st_mode)) /* character special */
+ && (!FLAG('d') || S_ISDIR(st.st_mode)) /* directory */
+ && (!FLAG('e') || access(path, F_OK) == 0) /* exists */
+ && (!FLAG('f') || S_ISREG(st.st_mode)) /* regular file */
+ && (!FLAG('g') || st.st_mode & S_ISGID) /* set-group-id flag */
+ && (!FLAG('h') || (!lstat(path, &ln) && S_ISLNK(ln.st_mode))) /* symbolic link */
+ && (!FLAG('n') || st.st_mtime > new.st_mtime) /* newer than file */
+ && (!FLAG('o') || st.st_mtime < old.st_mtime) /* older than file */
+ && (!FLAG('p') || S_ISFIFO(st.st_mode)) /* named pipe */
+ && (!FLAG('r') || access(path, R_OK) == 0) /* readable */
+ && (!FLAG('s') || st.st_size > 0) /* not empty */
+ && (!FLAG('u') || st.st_mode & S_ISUID) /* set-user-id flag */
+ && (!FLAG('w') || access(path, W_OK) == 0) /* writable */
+ && (!FLAG('x') || access(path, X_OK) == 0)) != FLAG('v')) { /* executable */
+ if (FLAG('q'))
+ exit(0);
+ match = 1;
+ puts(name);
+ }
+}
- return match ? 0 : 1;
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] "
+ "[-n file] [-o file] [file...]\n", argv0);
+ exit(2); /* like test(1) return > 1 on error */
}
-bool
-test(const char *path) {
- struct stat st;
+int
+main(int argc, char *argv[])
+{
+ struct dirent *d;
+ char path[PATH_MAX], *line = NULL, *file;
+ size_t linesiz = 0;
+ ssize_t n;
+ DIR *dir;
+ int r;
- if((!OPER('b') || (stat(path, &st) == 0 && S_ISBLK(st.st_mode))) /* block special */
- && (!OPER('c') || (stat(path, &st) == 0 && S_ISCHR(st.st_mode))) /* character special */
- && (!OPER('d') || (stat(path, &st) == 0 && S_ISDIR(st.st_mode))) /* directory */
- && (!OPER('e') || (access(path, F_OK) == 0)) /* exists */
- && (!OPER('f') || (stat(path, &st) == 0 && S_ISREG(st.st_mode))) /* regular file */
- && (!OPER('g') || (stat(path, &st) == 0 && (st.st_mode & S_ISGID))) /* set-group-id flag */
- && (!OPER('h') || (lstat(path, &st) == 0 && S_ISLNK(st.st_mode))) /* symbolic link */
- && (!OPER('n') || (stat(path, &st) == 0 && st.st_mtime > new.st_mtime)) /* newer than file */
- && (!OPER('o') || (stat(path, &st) == 0 && st.st_mtime < old.st_mtime)) /* older than file */
- && (!OPER('p') || (stat(path, &st) == 0 && S_ISFIFO(st.st_mode))) /* named pipe */
- && (!OPER('r') || (access(path, R_OK) == 0)) /* readable */
- && (!OPER('s') || (stat(path, &st) == 0 && st.st_size > 0)) /* not empty */
- && (!OPER('u') || (stat(path, &st) == 0 && (st.st_mode & S_ISUID))) /* set-user-id flag */
- && (!OPER('w') || (access(path, W_OK) == 0)) /* writable */
- && (!OPER('x') || (access(path, X_OK) == 0))) { /* executable */
- if(quiet)
- exit(0);
- puts(path);
- return true;
+ ARGBEGIN {
+ case 'n': /* newer than file */
+ case 'o': /* older than file */
+ file = EARGF(usage());
+ if (!(FLAG(ARGC()) = !stat(file, (ARGC() == 'n' ? &new : &old))))
+ perror(file);
+ break;
+ default:
+ /* miscellaneous operators */
+ if (strchr("abcdefghlpqrsuvwx", ARGC()))
+ FLAG(ARGC()) = 1;
+ else
+ usage(); /* unknown flag */
+ } ARGEND;
+
+ if (!argc) {
+ /* read list from stdin */
+ while ((n = getline(&line, &linesiz, stdin)) > 0) {
+ if (n && line[n - 1] == '\n')
+ line[n - 1] = '\0';
+ test(line, line);
+ }
+ free(line);
+ } else {
+ for (; argc; argc--, argv++) {
+ if (FLAG('l') && (dir = opendir(*argv))) {
+ /* test directory contents */
+ while ((d = readdir(dir))) {
+ r = snprintf(path, sizeof path, "%s/%s",
+ *argv, d->d_name);
+ if (r >= 0 && (size_t)r < sizeof path)
+ test(path, d->d_name);
+ }
+ closedir(dir);
+ } else {
+ test(*argv, *argv);
+ }
+ }
}
- else
- return false;
+ return match ? 0 : 1;
}