From bb4424df072332243890714b055e978a5c70adf3 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Sat, 19 Nov 2011 19:54:55 +0100 Subject: replace lsx with stest --- Makefile | 24 +++++++++--------- config.mk | 2 +- dmenu_run | 6 +++-- lsx.1 | 11 -------- lsx.c | 43 ------------------------------- stest.1 | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stest.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 189 insertions(+), 69 deletions(-) delete mode 100644 lsx.1 delete mode 100644 lsx.c create mode 100644 stest.1 create mode 100644 stest.c diff --git a/Makefile b/Makefile index 929b108..c127f6a 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ include config.mk -SRC = dmenu.c draw.c lsx.c +SRC = dmenu.c draw.c stest.c OBJ = ${SRC:.c=.o} -all: options dmenu lsx +all: options dmenu stest options: @echo dmenu build options: @@ -24,18 +24,18 @@ dmenu: dmenu.o draw.o @echo CC -o $@ @${CC} -o $@ dmenu.o draw.o ${LDFLAGS} -lsx: lsx.o +stest: stest.o @echo CC -o $@ - @${CC} -o $@ lsx.o ${LDFLAGS} + @${CC} -o $@ stest.o ${LDFLAGS} clean: @echo cleaning - @rm -f dmenu lsx ${OBJ} dmenu-${VERSION}.tar.gz + @rm -f dmenu stest ${OBJ} dmenu-${VERSION}.tar.gz dist: clean @echo creating dist tarball @mkdir -p dmenu-${VERSION} - @cp LICENSE Makefile README config.mk dmenu.1 draw.h dmenu_run lsx.1 ${SRC} dmenu-${VERSION} + @cp LICENSE Makefile README config.mk dmenu.1 draw.h dmenu_run stest.1 ${SRC} dmenu-${VERSION} @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} @gzip dmenu-${VERSION}.tar @rm -rf dmenu-${VERSION} @@ -43,24 +43,24 @@ dist: clean install: all @echo installing executables to ${DESTDIR}${PREFIX}/bin @mkdir -p ${DESTDIR}${PREFIX}/bin - @cp -f dmenu dmenu_run lsx ${DESTDIR}${PREFIX}/bin + @cp -f dmenu dmenu_run stest ${DESTDIR}${PREFIX}/bin @chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu @chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu_run - @chmod 755 ${DESTDIR}${PREFIX}/bin/lsx + @chmod 755 ${DESTDIR}${PREFIX}/bin/stest @echo installing manual pages to ${DESTDIR}${MANPREFIX}/man1 @mkdir -p ${DESTDIR}${MANPREFIX}/man1 @sed "s/VERSION/${VERSION}/g" < dmenu.1 > ${DESTDIR}${MANPREFIX}/man1/dmenu.1 - @sed "s/VERSION/${VERSION}/g" < lsx.1 > ${DESTDIR}${MANPREFIX}/man1/lsx.1 + @sed "s/VERSION/${VERSION}/g" < stest.1 > ${DESTDIR}${MANPREFIX}/man1/stest.1 @chmod 644 ${DESTDIR}${MANPREFIX}/man1/dmenu.1 - @chmod 644 ${DESTDIR}${MANPREFIX}/man1/lsx.1 + @chmod 644 ${DESTDIR}${MANPREFIX}/man1/stest.1 uninstall: @echo removing executables from ${DESTDIR}${PREFIX}/bin @rm -f ${DESTDIR}${PREFIX}/bin/dmenu @rm -f ${DESTDIR}${PREFIX}/bin/dmenu_run - @rm -f ${DESTDIR}${PREFIX}/bin/lsx + @rm -f ${DESTDIR}${PREFIX}/bin/stest @echo removing manual page from ${DESTDIR}${MANPREFIX}/man1 @rm -f ${DESTDIR}${MANPREFIX}/man1/dmenu.1 - @rm -f ${DESTDIR}${MANPREFIX}/man1/lsx.1 + @rm -f ${DESTDIR}${MANPREFIX}/man1/stest.1 .PHONY: all options clean dist install uninstall diff --git a/config.mk b/config.mk index f66389d..87a87f9 100644 --- a/config.mk +++ b/config.mk @@ -17,7 +17,7 @@ INCS = -I${X11INC} LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} # flags -CPPFLAGS = -D_BSD_SOURCE -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} +CPPFLAGS = -D_BSD_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} CFLAGS = -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS} LDFLAGS = -s ${LIBS} diff --git a/dmenu_run b/dmenu_run index 2747919..a15df0f 100755 --- a/dmenu_run +++ b/dmenu_run @@ -5,8 +5,10 @@ if [ ! -d "`dirname "$CACHE"`" ]; then fi ( IFS=: - if [ "`ls -dt $PATH "$CACHE" | head -n 1`" != "$CACHE" ]; then - lsx $PATH | sort -u > "$CACHE" + if ls -d $PATH | stest -q -n "$CACHE"; then + for dir in $PATH; do + ls $dir | stest -C $dir -fx + done | sort -u > "$CACHE" fi ) cmd=`dmenu "$@" < "$CACHE"` && exec sh -c "$cmd" diff --git a/lsx.1 b/lsx.1 deleted file mode 100644 index 1b2a15e..0000000 --- a/lsx.1 +++ /dev/null @@ -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. diff --git a/lsx.c b/lsx.c deleted file mode 100644 index cb016cf..0000000 --- a/lsx.c +++ /dev/null @@ -1,43 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include -#include -#include -#include - -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); -} diff --git a/stest.1 b/stest.1 new file mode 100644 index 0000000..cafd4bb --- /dev/null +++ b/stest.1 @@ -0,0 +1,87 @@ +.TH STEST 1 dmenu\-VERSION +.SH NAME +stest \- filter a list of files by properties +.SH SYNOPSIS +.B stest +.RB [ -bcdefghpqrsuwx ] +.RB [ -C +.IR dir ] +.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 as +arguments, stest will read a list of files from stdin, one path per line. +.SH OPTIONS +.TP +.BI \-C " dir" +Tests files relative to directory +.IR dir . +.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 +.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) diff --git a/stest.c b/stest.c new file mode 100644 index 0000000..a5596ea --- /dev/null +++ b/stest.c @@ -0,0 +1,85 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include + +#define OPER(x) (oper[(x)-'a']) + +static bool test(const char *); + +static bool quiet = false; +static bool oper[26]; +static struct stat old, new; + +int +main(int argc, char *argv[]) { + char buf[BUFSIZ], *p; + bool match = false; + int opt; + + 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++]); + + return match ? 0 : 1; +} + +bool +test(const char *path) { + struct stat st; + + 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; + } + else + return false; +} -- cgit v1.2.3-54-g00ecf From 8cc28cb4265b0c088ea909e58f42342b2214722e Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Sun, 27 Nov 2011 23:35:09 +0100 Subject: simplify stest --- dmenu_run | 6 ++-- stest.1 | 15 +++++----- stest.c | 94 ++++++++++++++++++++++++++++++--------------------------------- 3 files changed, 53 insertions(+), 62 deletions(-) diff --git a/dmenu_run b/dmenu_run index a15df0f..e0a3e13 100755 --- a/dmenu_run +++ b/dmenu_run @@ -5,10 +5,8 @@ if [ ! -d "`dirname "$CACHE"`" ]; then fi ( IFS=: - if ls -d $PATH | stest -q -n "$CACHE"; then - for dir in $PATH; do - ls $dir | stest -C $dir -fx - done | sort -u > "$CACHE" + if stest -dqr -n "$CACHE" $PATH; then + stest -flx $PATH | sort -u > "$CACHE" fi ) cmd=`dmenu "$@" < "$CACHE"` && exec sh -c "$cmd" diff --git a/stest.1 b/stest.1 index cafd4bb..1f590ec 100644 --- a/stest.1 +++ b/stest.1 @@ -3,9 +3,7 @@ stest \- filter a list of files by properties .SH SYNOPSIS .B stest -.RB [ -bcdefghpqrsuwx ] -.RB [ -C -.IR dir ] +.RB [ -abcdefghlpqrsuwx ] .RB [ -n .IR file ] .RB [ -o @@ -15,13 +13,11 @@ stest \- filter a list of files by properties .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 as -arguments, stest will read a list of files from stdin, one path per line. +Files which pass all tests are printed to stdout. .SH OPTIONS .TP -.BI \-C " dir" -Tests files relative to directory -.IR dir . +.B \-a +Test hidden files. .TP .B \-b Test that files are block specials. @@ -44,6 +40,9 @@ Test that files have their set-group-ID flag set. .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 . diff --git a/stest.c b/stest.c index a5596ea..e75d321 100644 --- a/stest.c +++ b/stest.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include #include @@ -6,80 +7,73 @@ #include #include -#define OPER(x) (oper[(x)-'a']) +#define FLAG(x) (flag[(x)-'a']) -static bool test(const char *); +static void test(const char *, const char *); -static bool quiet = false; -static bool oper[26]; +static bool match = false; +static bool flag[26]; static struct stat old, new; int main(int argc, char *argv[]) { - char buf[BUFSIZ], *p; - bool match = false; + struct dirent *d; + char buf[BUFSIZ]; + DIR *dir; int opt; - while((opt = getopt(argc, argv, "C:bcdefghn:o:pqrsuwx")) != -1) + while((opt = getopt(argc, argv, "abcdefghln: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)) + if(!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old)))) perror(optarg); break; - case 'q': /* quiet (no output, just status) */ - quiet = true; - break; default: /* miscellaneous operators */ - OPER(opt) = true; + FLAG(opt) = true; break; case '?': /* error: unknown flag */ - fprintf(stderr, "usage: %s [-bcdefghpqrsuwx] [-C dir] [-n file] [-o file] [file...]\n", argv[0]); + 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 = &buf[strlen(buf)-1]) == '\n') - *p = '\0'; - match |= test(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 - while(optind < argc) - match |= test(argv[optind++]); + else + test(argv[optind], argv[optind]); return match ? 0 : 1; } -bool -test(const char *path) { - struct stat st; +void +test(const char *path, const char *name) { + struct stat st, ln; - 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) + 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')) exit(0); - puts(path); - return true; + match = true; + puts(name); } - else - return false; } -- cgit v1.2.3-54-g00ecf From dcd6e771a1867353c14c46fc37e28d6d313cb3b1 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Sun, 27 Nov 2011 23:37:01 +0100 Subject: stest: remove unneeded lstat --- stest.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stest.c b/stest.c index e75d321..9dbf9c1 100644 --- a/stest.c +++ b/stest.c @@ -54,8 +54,7 @@ void test(const char *path, const char *name) { struct stat st, ln; - if(!stat(path, &st) && !lstat(path, &ln) - && ( FLAG('a') || name[0] != '.') /* hidden */ + 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 */ -- cgit v1.2.3-54-g00ecf From 32ef0f5662bfe8b8891ba6341d3a27818fb55508 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Sun, 27 Nov 2011 23:40:48 +0100 Subject: stest: cleanup --- stest.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stest.c b/stest.c index 9dbf9c1..b4dba64 100644 --- a/stest.c +++ b/stest.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -54,7 +53,7 @@ void test(const char *path, const char *name) { struct stat st, ln; - if(!stat(path, &st) && ( FLAG('a') || name[0] != '.') /* hidden files */ + 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 */ -- cgit v1.2.3-54-g00ecf From 6b1d658d1224cd108d64a6c340d47b66ad99816d Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Fri, 9 Dec 2011 11:49:44 +0100 Subject: stest: restore stream testing --- stest.1 | 3 ++- stest.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/stest.1 b/stest.1 index 1f590ec..bb48f45 100644 --- a/stest.1 +++ b/stest.1 @@ -13,7 +13,8 @@ stest \- filter a list of files by properties .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. +Files which pass all tests are printed to stdout. If no files are given, stest +reads files from stdin. .SH OPTIONS .TP .B \-a diff --git a/stest.c b/stest.c index b4dba64..e1dcf36 100644 --- a/stest.c +++ b/stest.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -17,7 +18,7 @@ static struct stat old, new; int main(int argc, char *argv[]) { struct dirent *d; - char buf[BUFSIZ]; + char buf[BUFSIZ], *p; DIR *dir; int opt; @@ -35,6 +36,12 @@ main(int argc, char *argv[]) { 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 */ -- cgit v1.2.3-54-g00ecf