commit
6664e4233f
7 changed files with 186 additions and 69 deletions
@ -1,11 +0,0 @@ |
||||
.TH LSX 1 dmenu\-VERSION |
||||
.SH NAME |
||||
lsx \- list executables |
||||
.SH SYNOPSIS |
||||
.B lsx |
||||
.RI [ directory ...] |
||||
.SH DESCRIPTION |
||||
.B lsx |
||||
lists the executables in each |
||||
.IR directory . |
||||
If none are given the current working directory is used. |
@ -1,43 +0,0 @@ |
||||
/* See LICENSE file for copyright and license details. */ |
||||
#include <dirent.h> |
||||
#include <errno.h> |
||||
#include <limits.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <unistd.h> |
||||
#include <sys/stat.h> |
||||
|
||||
static void lsx(const char *dir); |
||||
|
||||
static int status = EXIT_SUCCESS; |
||||
|
||||
int |
||||
main(int argc, char *argv[]) { |
||||
int i; |
||||
|
||||
if(argc < 2) |
||||
lsx("."); |
||||
else for(i = 1; i < argc; i++) |
||||
lsx(argv[i]); |
||||
return status; |
||||
} |
||||
|
||||
void |
||||
lsx(const char *dir) { |
||||
char buf[PATH_MAX]; |
||||
struct dirent *d; |
||||
struct stat st; |
||||
DIR *dp; |
||||
|
||||
for(dp = opendir(dir); dp && (d = readdir(dp)); errno = 0) |
||||
if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf |
||||
&& access(buf, X_OK) == 0 && stat(buf, &st) == 0 && S_ISREG(st.st_mode)) |
||||
puts(d->d_name); |
||||
|
||||
if(errno != 0) { |
||||
status = EXIT_FAILURE; |
||||
perror(dir); |
||||
} |
||||
if(dp) |
||||
closedir(dp); |
||||
} |
@ -0,0 +1,87 @@ |
||||
.TH STEST 1 dmenu\-VERSION |
||||
.SH NAME |
||||
stest \- filter a list of files by properties |
||||
.SH SYNOPSIS |
||||
.B stest |
||||
.RB [ -abcdefghlpqrsuwx ] |
||||
.RB [ -n |
||||
.IR file ] |
||||
.RB [ -o |
||||
.IR file ] |
||||
.RI [ file ...] |
||||
.SH DESCRIPTION |
||||
.B stest |
||||
takes a list of files and filters by the files' properties, analogous to |
||||
.IR test (1). |
||||
Files which pass all tests are printed to stdout. If no files are given, stest |
||||
reads files from stdin. |
||||
.SH OPTIONS |
||||
.TP |
||||
.B \-a |
||||
Test hidden files. |
||||
.TP |
||||
.B \-b |
||||
Test that files are block specials. |
||||
.TP |
||||
.B \-c |
||||
Test that files are character specials. |
||||
.TP |
||||
.B \-d |
||||
Test that files are directories. |
||||
.TP |
||||
.B \-e |
||||
Test that files exist. |
||||
.TP |
||||
.B \-f |
||||
Test that files are regular files. |
||||
.TP |
||||
.B \-g |
||||
Test that files have their set-group-ID flag set. |
||||
.TP |
||||
.B \-h |
||||
Test that files are symbolic links. |
||||
.TP |
||||
.B \-l |
||||
Test the contents of a directory given as an argument. |
||||
.TP |
||||
.BI \-n " file" |
||||
Test that files are newer than |
||||
.IR file . |
||||
.TP |
||||
.BI \-o " file" |
||||
Test that files are older than |
||||
.IR file . |
||||
.TP |
||||
.B \-p |
||||
Test that files are named pipes. |
||||
.TP |
||||
.B \-q |
||||
No files are printed, only the exit status is returned. |
||||
.TP |
||||
.B \-r |
||||
Test that files are readable. |
||||
.TP |
||||
.B \-s |
||||
Test that files are not empty. |
||||
.TP |
||||
.B \-u |
||||
Test that files have their set-user-ID flag set. |
||||
.TP |
||||
.B \-w |
||||
Test that files are writable. |
||||
.TP |
||||
.B \-x |
||||
Test that files are executable. |
||||
.SH EXIT STATUS |
||||
.TP |
||||
.B 0 |
||||
At least one file passed all tests. |
||||
.TP |
||||
.B 1 |
||||
No files passed all tests. |
||||
.TP |
||||
.B 2 |
||||
An error occurred. |
||||
.SH SEE ALSO |
||||
.IR dmenu (1), |
||||
.IR test (1) |
@ -0,0 +1,84 @@ |
||||
/* See LICENSE file for copyright and license details. */ |
||||
#include <dirent.h> |
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <sys/stat.h> |
||||
|
||||
#define FLAG(x) (flag[(x)-'a']) |
||||
|
||||
static void test(const char *, const char *); |
||||
|
||||
static bool match = false; |
||||
static bool flag[26]; |
||||
static struct stat old, new; |
||||
|
||||
int |
||||
main(int argc, char *argv[]) { |
||||
struct dirent *d; |
||||
char buf[BUFSIZ], *p; |
||||
DIR *dir; |
||||
int opt; |
||||
|
||||
while((opt = getopt(argc, argv, "abcdefghln:o:pqrsuwx")) != -1) |
||||
switch(opt) { |
||||
case 'n': /* newer than file */ |
||||
case 'o': /* older than file */ |
||||
if(!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old)))) |
||||
perror(optarg); |
||||
break; |
||||
default: /* miscellaneous operators */ |
||||
FLAG(opt) = true; |
||||
break; |
||||
case '?': /* error: unknown flag */ |
||||
fprintf(stderr, "usage: %s [-abcdefghlpqrsuwx] [-n file] [-o file] [file...]\n", argv[0]); |
||||
exit(2); |
||||
} |
||||
if(optind == argc) |
||||
while(fgets(buf, sizeof buf, stdin)) { |
||||
if((p = strchr(buf, '\n'))) |
||||
*p = '\0'; |
||||
test(buf, buf); |
||||
} |
||||
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); |
||||
} |
||||
else |
||||
test(argv[optind], argv[optind]); |
||||
|
||||
return match ? 0 : 1; |
||||
} |
||||
|
||||
void |
||||
test(const char *path, const char *name) { |
||||
struct stat st, ln; |
||||
|
||||
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)) { /* executable */ |
||||
if(FLAG('q')) |
||||
exit(0); |
||||
match = true; |
||||
puts(name); |
||||
} |
||||
} |
Loading…
Reference in new issue