dmenu/stest.c

80 lines
2.8 KiB
C
Raw Normal View History

2011-11-19 19:54:55 +01:00
/* See LICENSE file for copyright and license details. */
2011-11-27 23:35:09 +01:00
#include <dirent.h>
2011-11-19 19:54:55 +01:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
2011-11-27 23:35:09 +01:00
#define FLAG(x) (flag[(x)-'a'])
2011-11-19 19:54:55 +01:00
2011-11-27 23:35:09 +01:00
static void test(const char *, const char *);
2011-11-19 19:54:55 +01:00
2011-11-27 23:35:09 +01:00
static bool match = false;
static bool flag[26];
2011-11-19 19:54:55 +01:00
static struct stat old, new;
int
main(int argc, char *argv[]) {
2011-11-27 23:35:09 +01:00
struct dirent *d;
char buf[BUFSIZ];
DIR *dir;
2011-11-19 19:54:55 +01:00
int opt;
2011-11-27 23:35:09 +01:00
while((opt = getopt(argc, argv, "abcdefghln:o:pqrsuwx")) != -1)
2011-11-19 19:54:55 +01:00
switch(opt) {
case 'n': /* newer than file */
case 'o': /* older than file */
2011-11-27 23:35:09 +01:00
if(!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old))))
2011-11-19 19:54:55 +01:00
perror(optarg);
break;
default: /* miscellaneous operators */
2011-11-27 23:35:09 +01:00
FLAG(opt) = true;
2011-11-19 19:54:55 +01:00
break;
case '?': /* error: unknown flag */
2011-11-27 23:35:09 +01:00
fprintf(stderr, "usage: %s [-abcdefghlpqrsuwx] [-n file] [-o file] [file...]\n", argv[0]);
2011-11-19 19:54:55 +01:00
exit(2);
}
2011-11-27 23:35:09 +01:00
for(; optind < argc; optind++)
if(FLAG('l') && (dir = opendir(argv[optind]))) {
/* test directory contents */
while((d = readdir(dir)))
if(snprintf(buf, sizeof buf, "%s/%s", argv[optind], d->d_name) < sizeof buf)
test(buf, d->d_name);
closedir(dir);
2011-11-19 19:54:55 +01:00
}
2011-11-27 23:35:09 +01:00
else
test(argv[optind], argv[optind]);
2011-11-19 19:54:55 +01:00
return match ? 0 : 1;
}
2011-11-27 23:35:09 +01:00
void
test(const char *path, const char *name) {
struct stat st, ln;
2011-11-19 19:54:55 +01:00
2011-11-27 23:35:09 +01:00
if(!stat(path, &st) && !lstat(path, &ln)
&& ( FLAG('a') || name[0] != '.') /* hidden */
&& (!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)) { /* executable */
if(FLAG('q'))
2011-11-19 19:54:55 +01:00
exit(0);
2011-11-27 23:35:09 +01:00
match = true;
puts(name);
2011-11-19 19:54:55 +01:00
}
}