2011-11-19 20:54:55 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-07-19 22:34:51 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-11-28 00:35:09 +02:00
|
|
|
#include <dirent.h>
|
2015-07-19 22:34:51 +03:00
|
|
|
#include <limits.h>
|
2011-11-19 20:54:55 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-12-09 12:49:44 +02:00
|
|
|
#include <string.h>
|
2011-11-19 20:54:55 +02:00
|
|
|
#include <unistd.h>
|
2015-07-19 22:34:51 +03:00
|
|
|
|
|
|
|
#include "arg.h"
|
|
|
|
char *argv0;
|
2011-11-19 20:54:55 +02:00
|
|
|
|
2011-11-28 00:35:09 +02:00
|
|
|
#define FLAG(x) (flag[(x)-'a'])
|
2011-11-19 20:54:55 +02:00
|
|
|
|
2011-11-28 00:35:09 +02:00
|
|
|
static void test(const char *, const char *);
|
2015-07-19 22:34:51 +03:00
|
|
|
static void usage(void);
|
2011-11-19 20:54:55 +02:00
|
|
|
|
2015-07-19 22:34:51 +03:00
|
|
|
static int match = 0;
|
|
|
|
static int flag[26];
|
2011-11-19 20:54:55 +02:00
|
|
|
static struct stat old, new;
|
|
|
|
|
2015-07-19 22:34:51 +03:00
|
|
|
static void
|
|
|
|
test(const char *path, const char *name)
|
|
|
|
{
|
2011-11-28 00:35:09 +02:00
|
|
|
struct stat st, ln;
|
2011-11-19 20:54:55 +02:00
|
|
|
|
2015-07-19 22:34:51 +03:00
|
|
|
if ((!stat(path, &st) && (FLAG('a') || name[0] != '.') /* hidden files */
|
2011-11-28 00:35:09 +02:00
|
|
|
&& (!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 */
|
2013-08-13 20:15:04 +03:00
|
|
|
&& (!FLAG('x') || access(path, X_OK) == 0)) != FLAG('v')) { /* executable */
|
2015-07-19 22:34:51 +03:00
|
|
|
if (FLAG('q'))
|
2011-11-19 20:54:55 +02:00
|
|
|
exit(0);
|
2015-07-19 22:34:51 +03:00
|
|
|
match = 1;
|
2011-11-28 00:35:09 +02:00
|
|
|
puts(name);
|
2011-11-19 20:54:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-19 22:34:51 +03:00
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match ? 0 : 1;
|
|
|
|
}
|