Good evening, I sat down this afternoon to read through the dmenu-code and noticed some things that I fixed with attached patches.
PATCH 1: Use estrtol instead of atoi to make input-checks more thorough PATCH 2: Un-boolify according to what I already did in some other repos PATCH 3: config.def.h now contains the option to set the monitor-number at compile time. PATCH 4: As already discussed style(9) is the reference for future code changes. Given the codebase hasn't already been transformed, I did it. PATCH 5: Add myself to the LICENSE in case at least one of the above patches is merged. Please let me know what you think. Cheers FRIGN -- FRIGN <d...@frign.de>
>From 1034776002946a5aea403e31ecf3d14deeae7742 Mon Sep 17 00:00:00 2001 From: FRIGN <d...@frign.de> Date: Mon, 22 Dec 2014 14:17:21 +0100 Subject: [PATCH 1/5] Convert atoi -> estrtol Basically we can't assume user-input is well-formed. --- dmenu.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/dmenu.c b/dmenu.c index 94c70de..2d0a513 100644 --- a/dmenu.c +++ b/dmenu.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <ctype.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -25,6 +26,7 @@ struct Item { Bool out; }; +static long estrtol(const char *s, int base); static void appenditem(Item *item, Item **list, Item **last); static void calcoffsets(void); static char *cistrstr(const char *s, const char *sub); @@ -61,6 +63,29 @@ static int mon = -1; static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; static char *(*fstrstr)(const char *, const char *) = strstr; +static long +estrtol(const char *s, int base) +{ + char *end; + long n; + errno = 0; + n = strtol(s, &end, base); + if (*end != '\0') { + if (base == 0) { + printf("%s: not an integer\n", s); + exit(1); + } else { + printf("%s: not a base %d integer\n", s, base); + exit(1); + } + } + if (errno != 0) { + printf("%s:", s); + exit(1); + } + return n; +} + int main(int argc, char *argv[]) { Bool fast = False; @@ -84,9 +109,9 @@ main(int argc, char *argv[]) { usage(); /* these options take one argument */ else if(!strcmp(argv[i], "-l")) /* number of lines in vertical list */ - lines = atoi(argv[++i]); + lines = estrtol(argv[++i], 0); else if(!strcmp(argv[i], "-m")) - mon = atoi(argv[++i]); + mon = estrtol(argv[++i], 0); else if(!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ prompt = argv[++i]; else if(!strcmp(argv[i], "-fn")) /* font or font set */ -- 1.8.5.5
>From a21f32b46a3b74b13cd52184affd4f8d32c30c5b Mon Sep 17 00:00:00 2001 From: FRIGN <d...@frign.de> Date: Mon, 22 Dec 2014 14:31:33 +0100 Subject: [PATCH 2/5] Un-boolify codebase and refactor config.def.h Continue the work done in sbase/ubase and other suckless projects. Also, there's no need for vim-indent-comments or spelling errors. Instead, increase readability with some alignments. --- config.def.h | 26 ++++++++++++-------------- dmenu.c | 27 +++++++++++++-------------- draw.c | 10 +++++----- draw.h | 4 ++-- stest.c | 9 ++++----- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/config.def.h b/config.def.h index c2a23fa..79e55ef 100644 --- a/config.def.h +++ b/config.def.h @@ -1,17 +1,15 @@ /* See LICENSE file for copyright and license details. */ -/* vim: expandtab - */ -/* Default settings; can be overrided by command line. */ +/* Default settings; can be overridden by command line arguments. */ -static Bool topbar = True; /* -b option; if False, dmenu appears at bottom */ -static const char *font = NULL; /* -fn option; default X11 font or font set */ -static const char *prompt = NULL; /* -p option; prompt to the elft of input field */ -static const char *normbgcolor = "#222222"; /* -nb option; normal background */ -static const char *normfgcolor = "#bbbbbb"; /* -nf option; normal foreground */ -static const char *selbgcolor = "#005577"; /* -sb option; selected background */ -static const char *selfgcolor = "#eeeeee"; /* -sf option; selected foreground */ -static const char *outbgcolor = "#00ffff"; -static const char *outfgcolor = "#000000"; -/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ -static unsigned int lines = 0; +static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ +static const char *font = NULL; /* -fn option; default X11 font or font set */ +static const char *prompt = NULL; /* -p option; prompt to the elft of input field */ +static const char *normbgcolor = "#222222"; /* -nb option; normal background */ +static const char *normfgcolor = "#bbbbbb"; /* -nf option; normal foreground */ +static const char *selbgcolor = "#005577"; /* -sb option; selected background */ +static const char *selfgcolor = "#eeeeee"; /* -sf option; selected foreground */ +static const char *outbgcolor = "#00ffff"; +static const char *outfgcolor = "#000000"; +/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ +static unsigned int lines = 0; diff --git a/dmenu.c b/dmenu.c index 2d0a513..c723c06 100644 --- a/dmenu.c +++ b/dmenu.c @@ -23,7 +23,7 @@ typedef struct Item Item; struct Item { char *text; Item *left, *right; - Bool out; + int out; }; static long estrtol(const char *s, int base); @@ -88,8 +88,7 @@ estrtol(const char *s, int base) int main(int argc, char *argv[]) { - Bool fast = False; - int i; + int fast = 0, i; for(i = 1; i < argc; i++) /* these options take no arguments */ @@ -98,9 +97,9 @@ main(int argc, char *argv[]) { exit(EXIT_SUCCESS); } else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */ - topbar = False; + topbar = 0; else if(!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ - fast = True; + fast = 1; else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ fstrncmp = strncasecmp; fstrstr = cistrstr; @@ -191,7 +190,7 @@ drawmenu(void) { dc->x = 0; dc->y = 0; dc->h = bh; - drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol)); + drawrect(dc, 0, 0, mw, mh, 1, BG(dc, normcol)); if(prompt && *prompt) { dc->w = promptw; @@ -202,7 +201,7 @@ drawmenu(void) { dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw; drawtext(dc, text, normcol); if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) - drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol)); + drawrect(dc, curpos, 2, 1, dc->h - 4, 1, FG(dc, normcol)); if(lines > 0) { /* draw vertical list */ @@ -239,7 +238,7 @@ grabkeyboard(void) { /* try to grab keyboard, we may have to wait for another process to ungrab */ for(i = 0; i < 1000; i++) { - if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True, + if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), 1, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) return; usleep(1000); @@ -396,7 +395,7 @@ keypress(XKeyEvent *ev) { if(!(ev->state & ControlMask)) exit(EXIT_SUCCESS); if(sel) - sel->out = True; + sel->out = 1; break; case XK_Right: if(text[cursor] != '\0') { @@ -495,7 +494,7 @@ paste(void) { Atom da; /* we have been given the current selection, now insert it into input */ - XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False, + XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, 0, utf8, &da, &di, &dl, &dl, (unsigned char **)&p); insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p)); XFree(p); @@ -516,7 +515,7 @@ readstdin(void) { *p = '\0'; if(!(items[i].text = strdup(buf))) eprintf("cannot strdup %u bytes:", strlen(buf)+1); - items[i].out = False; + items[i].out = 0; if(strlen(items[i].text) > max) max = strlen(maxstr = items[i].text); } @@ -571,8 +570,8 @@ setup(void) { outcol[ColBG] = getcolor(dc, outbgcolor); outcol[ColFG] = getcolor(dc, outfgcolor); - clip = XInternAtom(dc->dpy, "CLIPBOARD", False); - utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); + clip = XInternAtom(dc->dpy, "CLIPBOARD", 0); + utf8 = XInternAtom(dc->dpy, "UTF8_STRING", 0); /* calculate menu geometry */ bh = dc->font.height + 2; @@ -625,7 +624,7 @@ setup(void) { match(); /* create menu window */ - swa.override_redirect = True; + swa.override_redirect = 1; swa.background_pixel = normcol[ColBG]; swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0, diff --git a/draw.c b/draw.c index 76f0c54..ad770f6 100644 --- a/draw.c +++ b/draw.c @@ -11,10 +11,10 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define DEFAULTFN "fixed" -static Bool loadfont(DC *dc, const char *fontstr); +static int loadfont(DC *dc, const char *fontstr); void -drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) { +drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, unsigned long color) { XSetForeground(dc->dpy, dc->gc, color); if(fill) XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); @@ -35,7 +35,7 @@ drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { if(mn < n) for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.'); - drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col)); + drawrect(dc, 0, 0, dc->w, dc->h, 1, BG(dc, col)); drawtextn(dc, buf, mn, col); } @@ -118,14 +118,14 @@ initfont(DC *dc, const char *fontstr) { dc->font.height = dc->font.ascent + dc->font.descent; } -Bool +int loadfont(DC *dc, const char *fontstr) { char *def, **missing, **names; int i, n; XFontStruct **xfonts; if(!*fontstr) - return False; + return 0; if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) { n = XFontsOfFontSet(dc->font.set, &xfonts, &names); for(i = 0; i < n; i++) { diff --git a/draw.h b/draw.h index 43a57bf..a6ef9de 100644 --- a/draw.h +++ b/draw.h @@ -7,7 +7,7 @@ enum { ColBG, ColFG, ColBorder, ColLast }; typedef struct { int x, y, w, h; - Bool invert; + int invert; Display *dpy; GC gc; Pixmap canvas; @@ -21,7 +21,7 @@ typedef struct { } font; } DC; /* draw context */ -void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color); +void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, unsigned long color); void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]); void eprintf(const char *fmt, ...); diff --git a/stest.c b/stest.c index 8fac42a..886d418 100644 --- a/stest.c +++ b/stest.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ #include <dirent.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -11,8 +10,8 @@ static void test(const char *, const char *); -static bool match = false; -static bool flag[26]; +static int match = 0; +static int flag[26]; static struct stat old, new; int @@ -30,7 +29,7 @@ main(int argc, char *argv[]) { perror(optarg); break; default: /* miscellaneous operators */ - FLAG(opt) = true; + FLAG(opt) = 1; break; case '?': /* error: unknown flag */ fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] [-n file] [-o file] [file...]\n", argv[0]); @@ -78,7 +77,7 @@ test(const char *path, const char *name) { && (!FLAG('x') || access(path, X_OK) == 0)) != FLAG('v')) { /* executable */ if(FLAG('q')) exit(0); - match = true; + match = 1; puts(name); } } -- 1.8.5.5
>From 53a628ea78c914d47eeb9abea421d4d6459f7a8d Mon Sep 17 00:00:00 2001 From: FRIGN <d...@frign.de> Date: Mon, 22 Dec 2014 15:47:26 +0100 Subject: [PATCH 3/5] Add monitor-number as configurable option --- config.def.h | 1 + dmenu.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index 79e55ef..205739f 100644 --- a/config.def.h +++ b/config.def.h @@ -2,6 +2,7 @@ /* Default settings; can be overridden by command line arguments. */ static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ +static int mon = -1; /* -m option; monitor number, -1 for default */ static const char *font = NULL; /* -fn option; default X11 font or font set */ static const char *prompt = NULL; /* -p option; prompt to the elft of input field */ static const char *normbgcolor = "#222222"; /* -nb option; normal background */ diff --git a/dmenu.c b/dmenu.c index c723c06..077c4ca 100644 --- a/dmenu.c +++ b/dmenu.c @@ -56,7 +56,6 @@ static Item *matches, *matchend; static Item *prev, *curr, *next, *sel; static Window win; static XIC xic; -static int mon = -1; #include "config.h" -- 1.8.5.5
>From b385b2f29ccf09b2b972ee007c51411217df368f Mon Sep 17 00:00:00 2001 From: FRIGN <d...@frign.de> Date: Mon, 22 Dec 2014 18:29:21 +0100 Subject: [PATCH 4/5] Refactor to style(9) As seen in sbase/ubase --- dmenu.c | 416 +++++++++++++++++++++++++++++++++------------------------------- draw.c | 88 ++++++++------ draw.h | 40 +++---- stest.c | 32 ++--- 4 files changed, 303 insertions(+), 273 deletions(-) diff --git a/dmenu.c b/dmenu.c index 077c4ca..936a7f6 100644 --- a/dmenu.c +++ b/dmenu.c @@ -6,12 +6,13 @@ #include <string.h> #include <strings.h> #include <unistd.h> -#include <X11/Xlib.h> #include <X11/Xatom.h> +#include <X11/Xlib.h> #include <X11/Xutil.h> #ifdef XINERAMA #include <X11/extensions/Xinerama.h> #endif + #include "draw.h" #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ @@ -23,24 +24,24 @@ typedef struct Item Item; struct Item { char *text; Item *left, *right; - int out; + int out; }; -static long estrtol(const char *s, int base); -static void appenditem(Item *item, Item **list, Item **last); -static void calcoffsets(void); -static char *cistrstr(const char *s, const char *sub); -static void drawmenu(void); -static void grabkeyboard(void); -static void insert(const char *str, ssize_t n); -static void keypress(XKeyEvent *ev); -static void match(void); -static size_t nextrune(int inc); -static void paste(void); -static void readstdin(void); -static void run(void); -static void setup(void); -static void usage(void); +static void appenditem(Item *, Item **, Item **); +static void calcoffsets(void); +static char *cistrstr(const char *, const char *); +static void drawmenu(void); +static void grabkeyboard(void); +static void insert(const char *, ssize_t); +static void keypress(XKeyEvent *); +static void match(void); +static size_t nextrune(int); +static void paste(void); +static void readstdin(void); +static void run(void); +static void setup(void); +static void usage(void); +static long estrtol(const char *, int); static char text[BUFSIZ] = ""; static int bh, mw, mh; @@ -62,89 +63,10 @@ static XIC xic; static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; static char *(*fstrstr)(const char *, const char *) = strstr; -static long -estrtol(const char *s, int base) -{ - char *end; - long n; - errno = 0; - n = strtol(s, &end, base); - if (*end != '\0') { - if (base == 0) { - printf("%s: not an integer\n", s); - exit(1); - } else { - printf("%s: not a base %d integer\n", s, base); - exit(1); - } - } - if (errno != 0) { - printf("%s:", s); - exit(1); - } - return n; -} - -int -main(int argc, char *argv[]) { - int fast = 0, i; - - for(i = 1; i < argc; i++) - /* these options take no arguments */ - if(!strcmp(argv[i], "-v")) { /* prints version information */ - puts("dmenu-"VERSION", © 2006-2014 dmenu engineers, see LICENSE for details"); - exit(EXIT_SUCCESS); - } - else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */ - topbar = 0; - else if(!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ - fast = 1; - else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ - fstrncmp = strncasecmp; - fstrstr = cistrstr; - } - else if(i+1 == argc) - usage(); - /* these options take one argument */ - else if(!strcmp(argv[i], "-l")) /* number of lines in vertical list */ - lines = estrtol(argv[++i], 0); - else if(!strcmp(argv[i], "-m")) - mon = estrtol(argv[++i], 0); - else if(!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ - prompt = argv[++i]; - else if(!strcmp(argv[i], "-fn")) /* font or font set */ - font = argv[++i]; - else if(!strcmp(argv[i], "-nb")) /* normal background color */ - normbgcolor = argv[++i]; - else if(!strcmp(argv[i], "-nf")) /* normal foreground color */ - normfgcolor = argv[++i]; - else if(!strcmp(argv[i], "-sb")) /* selected background color */ - selbgcolor = argv[++i]; - else if(!strcmp(argv[i], "-sf")) /* selected foreground color */ - selfgcolor = argv[++i]; - else - usage(); - - dc = initdc(); - initfont(dc, font); - - if(fast) { - grabkeyboard(); - readstdin(); - } - else { - readstdin(); - grabkeyboard(); - } - setup(); - run(); - - return 1; /* unreachable */ -} - void -appenditem(Item *item, Item **list, Item **last) { - if(*last) +appenditem(Item *item, Item **list, Item **last) +{ + if (*last) (*last)->right = item; else *list = item; @@ -155,34 +77,38 @@ appenditem(Item *item, Item **list, Item **last) { } void -calcoffsets(void) { +calcoffsets(void) +{ int i, n; - if(lines > 0) + if (lines > 0) n = lines * bh; else n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">")); + /* calculate which items will begin the next page and previous page */ - for(i = 0, next = curr; next; next = next->right) - if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n) + for (i = 0, next = curr; next; next = next->right) + if ((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n) break; - for(i = 0, prev = curr; prev && prev->left; prev = prev->left) - if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n) + for (i = 0, prev = curr; prev && prev->left; prev = prev->left) + if ((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n) break; } char * -cistrstr(const char *s, const char *sub) { +cistrstr(const char *s, const char *sub) +{ size_t len; - for(len = strlen(sub); *s; s++) - if(!strncasecmp(s, sub, len)) + for (len = strlen(sub); *s; s++) + if (!strncasecmp(s, sub, len)) return (char *)s; return NULL; } void -drawmenu(void) { +drawmenu(void) +{ int curpos; Item *item; @@ -191,7 +117,7 @@ drawmenu(void) { dc->h = bh; drawrect(dc, 0, 0, mw, mh, 1, BG(dc, normcol)); - if(prompt && *prompt) { + if (prompt && *prompt) { dc->w = promptw; drawtext(dc, prompt, selcol); dc->x = dc->w; @@ -199,25 +125,25 @@ drawmenu(void) { /* draw input field */ dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw; drawtext(dc, text, normcol); - if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) + if ((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w) drawrect(dc, curpos, 2, 1, dc->h - 4, 1, FG(dc, normcol)); - if(lines > 0) { + if (lines > 0) { /* draw vertical list */ dc->w = mw - dc->x; - for(item = curr; item != next; item = item->right) { + for (item = curr; item != next; item = item->right) { dc->y += dc->h; drawtext(dc, item->text, (item == sel) ? selcol : (item->out) ? outcol : normcol); } } - else if(matches) { + else if (matches) { /* draw horizontal list */ dc->x += inputw; dc->w = textw(dc, "<"); - if(curr->left) + if (curr->left) drawtext(dc, "<", normcol); - for(item = curr; item != next; item = item->right) { + for (item = curr; item != next; item = item->right) { dc->x += dc->w; dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">")); drawtext(dc, item->text, (item == sel) ? selcol : @@ -225,19 +151,20 @@ drawmenu(void) { } dc->w = textw(dc, ">"); dc->x = mw - dc->w; - if(next) + if (next) drawtext(dc, ">", normcol); } mapdc(dc, win, mw, mh); } void -grabkeyboard(void) { +grabkeyboard(void) +{ int i; /* try to grab keyboard, we may have to wait for another process to ungrab */ - for(i = 0; i < 1000; i++) { - if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), 1, + for (i = 0; i < 1000; i++) { + if (XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), 1, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) return; usleep(1000); @@ -246,29 +173,31 @@ grabkeyboard(void) { } void -insert(const char *str, ssize_t n) { - if(strlen(text) + n > sizeof text - 1) +insert(const char *str, ssize_t n) +{ + if (strlen(text) + n > sizeof text - 1) return; /* move existing text out of the way, insert new text, and update cursor */ memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); - if(n > 0) + if (n > 0) memcpy(&text[cursor], str, n); cursor += n; match(); } void -keypress(XKeyEvent *ev) { +keypress(XKeyEvent *ev) +{ char buf[32]; int len; KeySym ksym = NoSymbol; Status status; len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status); - if(status == XBufferOverflow) + if (status == XBufferOverflow) return; - if(ev->state & ControlMask) - switch(ksym) { + if (ev->state & ControlMask) + switch (ksym) { case XK_a: ksym = XK_Home; break; case XK_b: ksym = XK_Left; break; case XK_c: ksym = XK_Escape; break; @@ -293,9 +222,9 @@ keypress(XKeyEvent *ev) { insert(NULL, 0 - cursor); break; case XK_w: /* delete word */ - while(cursor > 0 && text[nextrune(-1)] == ' ') + while (cursor > 0 && text[nextrune(-1)] == ' ') insert(NULL, nextrune(-1) - cursor); - while(cursor > 0 && text[nextrune(-1)] != ' ') + while (cursor > 0 && text[nextrune(-1)] != ' ') insert(NULL, nextrune(-1) - cursor); break; case XK_y: /* paste selection */ @@ -310,8 +239,8 @@ keypress(XKeyEvent *ev) { default: return; } - else if(ev->state & Mod1Mask) - switch(ksym) { + else if (ev->state & Mod1Mask) + switch (ksym) { case XK_g: ksym = XK_Home; break; case XK_G: ksym = XK_End; break; case XK_h: ksym = XK_Up; break; @@ -321,33 +250,33 @@ keypress(XKeyEvent *ev) { default: return; } - switch(ksym) { + switch (ksym) { default: - if(!iscntrl(*buf)) + if (!iscntrl(*buf)) insert(buf, len); break; case XK_Delete: - if(text[cursor] == '\0') + if (text[cursor] == '\0') return; cursor = nextrune(+1); /* fallthrough */ case XK_BackSpace: - if(cursor == 0) + if (cursor == 0) return; insert(NULL, nextrune(-1) - cursor); break; case XK_End: - if(text[cursor] != '\0') { + if (text[cursor] != '\0') { cursor = strlen(text); break; } - if(next) { + if (next) { /* jump to end of list and position items in reverse */ curr = matchend; calcoffsets(); curr = prev; calcoffsets(); - while(next && (curr = curr->right)) + while (next && (curr = curr->right)) calcoffsets(); } sel = matchend; @@ -355,7 +284,7 @@ keypress(XKeyEvent *ev) { case XK_Escape: exit(EXIT_FAILURE); case XK_Home: - if(sel == matches) { + if (sel == matches) { cursor = 0; break; } @@ -363,27 +292,27 @@ keypress(XKeyEvent *ev) { calcoffsets(); break; case XK_Left: - if(cursor > 0 && (!sel || !sel->left || lines > 0)) { + if (cursor > 0 && (!sel || !sel->left || lines > 0)) { cursor = nextrune(-1); break; } - if(lines > 0) + if (lines > 0) return; /* fallthrough */ case XK_Up: - if(sel && sel->left && (sel = sel->left)->right == curr) { + if (sel && sel->left && (sel = sel->left)->right == curr) { curr = prev; calcoffsets(); } break; case XK_Next: - if(!next) + if (!next) return; sel = curr = next; calcoffsets(); break; case XK_Prior: - if(!prev) + if (!prev) return; sel = curr = prev; calcoffsets(); @@ -391,27 +320,27 @@ keypress(XKeyEvent *ev) { case XK_Return: case XK_KP_Enter: puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); - if(!(ev->state & ControlMask)) + if (!(ev->state & ControlMask)) exit(EXIT_SUCCESS); - if(sel) + if (sel) sel->out = 1; break; case XK_Right: - if(text[cursor] != '\0') { + if (text[cursor] != '\0') { cursor = nextrune(+1); break; } - if(lines > 0) + if (lines > 0) return; /* fallthrough */ case XK_Down: - if(sel && sel->right && (sel = sel->right) == next) { + if (sel && sel->right && (sel = sel->right) == next) { curr = next; calcoffsets(); } break; case XK_Tab: - if(!sel) + if (!sel) return; strncpy(text, sel->text, sizeof text - 1); text[sizeof text - 1] = '\0'; @@ -423,7 +352,8 @@ keypress(XKeyEvent *ev) { } void -match(void) { +match(void) +{ static char **tokv = NULL; static int tokn = 0; @@ -434,28 +364,28 @@ match(void) { strcpy(buf, text); /* separate input text into tokens to be matched individually */ - for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " ")) - if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv))) + for (s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " ")) + if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv))) eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv); len = tokc ? strlen(tokv[0]) : 0; matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; - for(item = items; item && item->text; item++) { - for(i = 0; i < tokc; i++) - if(!fstrstr(item->text, tokv[i])) + for (item = items; item && item->text; item++) { + for (i = 0; i < tokc; i++) + if (!fstrstr(item->text, tokv[i])) break; - if(i != tokc) /* not all tokens match */ + if (i != tokc) /* not all tokens match */ continue; /* exact matches go first, then prefixes, then substrings */ - if(!tokc || !fstrncmp(tokv[0], item->text, len+1)) + if (!tokc || !fstrncmp(tokv[0], item->text, len+1)) appenditem(item, &matches, &matchend); - else if(!fstrncmp(tokv[0], item->text, len)) + else if (!fstrncmp(tokv[0], item->text, len)) appenditem(item, &lprefix, &prefixend); else appenditem(item, &lsubstr, &substrend); } - if(lprefix) { - if(matches) { + if (lprefix) { + if (matches) { matchend->right = lprefix; lprefix->left = matchend; } @@ -463,8 +393,8 @@ match(void) { matches = lprefix; matchend = prefixend; } - if(lsubstr) { - if(matches) { + if (lsubstr) { + if (matches) { matchend->right = lsubstr; lsubstr->left = matchend; } @@ -477,16 +407,18 @@ match(void) { } size_t -nextrune(int inc) { +nextrune(int inc) +{ ssize_t n; /* return location of next utf8 rune in the given direction (+1 or -1) */ - for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc); + for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc); return n; } void -paste(void) { +paste(void) +{ char *p, *q; int di; unsigned long dl; @@ -501,50 +433,52 @@ paste(void) { } void -readstdin(void) { +readstdin(void) +{ char buf[sizeof text], *p, *maxstr = NULL; size_t i, max = 0, size = 0; /* read each line from stdin and add it to the item list */ - for(i = 0; fgets(buf, sizeof buf, stdin); i++) { - if(i+1 >= size / sizeof *items) - if(!(items = realloc(items, (size += BUFSIZ)))) + for (i = 0; fgets(buf, sizeof buf, stdin); i++) { + if (i + 1 >= size / sizeof *items) + if (!(items = realloc(items, (size += BUFSIZ)))) eprintf("cannot realloc %u bytes:", size); - if((p = strchr(buf, '\n'))) + if ((p = strchr(buf, '\n'))) *p = '\0'; - if(!(items[i].text = strdup(buf))) + if (!(items[i].text = strdup(buf))) eprintf("cannot strdup %u bytes:", strlen(buf)+1); items[i].out = 0; - if(strlen(items[i].text) > max) + if (strlen(items[i].text) > max) max = strlen(maxstr = items[i].text); } - if(items) + if (items) items[i].text = NULL; inputw = maxstr ? textw(dc, maxstr) : 0; lines = MIN(lines, i); } void -run(void) { +run(void) +{ XEvent ev; - while(!XNextEvent(dc->dpy, &ev)) { - if(XFilterEvent(&ev, win)) + while (!XNextEvent(dc->dpy, &ev)) { + if (XFilterEvent(&ev, win)) continue; - switch(ev.type) { + switch (ev.type) { case Expose: - if(ev.xexpose.count == 0) + if (ev.xexpose.count == 0) mapdc(dc, win, mw, mh); break; case KeyPress: keypress(&ev.xkey); break; case SelectionNotify: - if(ev.xselection.property == utf8) + if (ev.xselection.property == utf8) paste(); break; case VisibilityNotify: - if(ev.xvisibility.state != VisibilityUnobscured) + if (ev.xvisibility.state != VisibilityUnobscured) XRaiseWindow(dc->dpy, win); break; } @@ -552,9 +486,10 @@ run(void) { } void -setup(void) { - int x, y, screen = DefaultScreen(dc->dpy); - Window root = RootWindow(dc->dpy, screen); +setup(void) +{ + int x, y, screen; + Window root; XSetWindowAttributes swa; XIM xim; #ifdef XINERAMA @@ -562,6 +497,9 @@ setup(void) { XineramaScreenInfo *info; #endif + screen = DefaultScreen(dc->dpy); + root = RootWindow(dc->dpy, screen); + normcol[ColBG] = getcolor(dc, normbgcolor); normcol[ColFG] = getcolor(dc, normfgcolor); selcol[ColBG] = getcolor(dc, selbgcolor); @@ -577,41 +515,40 @@ setup(void) { lines = MAX(lines, 0); mh = (lines + 1) * bh; #ifdef XINERAMA - if((info = XineramaQueryScreens(dc->dpy, &n))) { + if ((info = XineramaQueryScreens(dc->dpy, &n))) { int a, j, di, i = 0, area = 0; unsigned int du; Window w, pw, dw, *dws; XWindowAttributes wa; XGetInputFocus(dc->dpy, &w, &di); - if(mon != -1 && mon < n) + if (mon != -1 && mon < n) i = mon; - if(!i && w != root && w != PointerRoot && w != None) { + if (!i && w != root && w != PointerRoot && w != None) { /* find top-level window containing current input focus */ do { - if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws) + if (XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws) XFree(dws); - } while(w != root && w != pw); + } while (w != root && w != pw); /* find xinerama screen with which the window intersects most */ - if(XGetWindowAttributes(dc->dpy, pw, &wa)) - for(j = 0; j < n; j++) - if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { + if (XGetWindowAttributes(dc->dpy, pw, &wa)) + for (j = 0; j < n; j++) + if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { area = a; i = j; } } /* no focused window is on screen, so use pointer location instead */ - if(mon == -1 && !area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) - for(i = 0; i < n; i++) - if(INTERSECT(x, y, 1, 1, info[i])) + if (mon == -1 && !area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) + for (i = 0; i < n; i++) + if (INTERSECT(x, y, 1, 1, info[i])) break; x = info[i].x_org; y = info[i].y_org + (topbar ? 0 : info[i].height - mh); mw = info[i].width; XFree(info); - } - else + } else #endif { x = 0; @@ -641,9 +578,86 @@ setup(void) { drawmenu(); } +static long +estrtol(const char *s, int base) +{ + char *end; + long n; + + errno = 0; + n = strtol(s, &end, base); + if (*end != '\0') { + if (base == 0) + eprintf("%s: not an integer\n", s); + else + eprintf("%s: not a base %d integer\n", s, base); + } + if (errno != 0) { + eprintf("%s:", s); + } + return n; +} + void -usage(void) { - fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" - " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr); - exit(EXIT_FAILURE); +usage(void) +{ + eprintf("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" + " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n"); +} + +int +main(int argc, char *argv[]) +{ + int fast = 0, i; + + for (i = 1; i < argc; i++) + /* Flags without argument */ + if (!strcmp(argv[i], "-v")) { + puts("dmenu-"VERSION); + exit(0); + } else if (!strcmp(argv[i], "-b")) { + topbar = 0; + } else if (!strcmp(argv[i], "-f")) { + fast = 1; + } else if (!strcmp(argv[i], "-i")) { + fstrncmp = strncasecmp; + fstrstr = cistrstr; + } else if (i + 1 == argc) { + usage(); + + /* Flags with argument */ + } else if (!strcmp(argv[i], "-l")) { + lines = estrtol(argv[++i], 0); + } else if (!strcmp(argv[i], "-m")) { + mon = estrtol(argv[++i], 0); + } else if (!strcmp(argv[i], "-p")) { + prompt = argv[++i]; + } else if (!strcmp(argv[i], "-fn")) { + font = argv[++i]; + } else if (!strcmp(argv[i], "-nb")) { + normbgcolor = argv[++i]; + } else if (!strcmp(argv[i], "-nf")) { + normfgcolor = argv[++i]; + } else if (!strcmp(argv[i], "-sb")) { + selbgcolor = argv[++i]; + } else if (!strcmp(argv[i], "-sf")) { + selfgcolor = argv[++i]; + } else { + usage(); + } + + dc = initdc(); + initfont(dc, font); + + if (fast) { + grabkeyboard(); + readstdin(); + } else { + readstdin(); + grabkeyboard(); + } + setup(); + run(); + + return 1; /* unreachable */ } diff --git a/draw.c b/draw.c index ad770f6..b593dee 100644 --- a/draw.c +++ b/draw.c @@ -5,6 +5,7 @@ #include <stdlib.h> #include <string.h> #include <X11/Xlib.h> + #include "draw.h" #define MAX(a, b) ((a) > (b) ? (a) : (b)) @@ -14,38 +15,41 @@ static int loadfont(DC *dc, const char *fontstr); void -drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, unsigned long color) { +drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, unsigned long color) +{ XSetForeground(dc->dpy, dc->gc, color); - if(fill) + if (fill) XFillRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w, h); else XDrawRectangle(dc->dpy, dc->canvas, dc->gc, dc->x + x, dc->y + y, w-1, h-1); } void -drawtext(DC *dc, const char *text, unsigned long col[ColLast]) { +drawtext(DC *dc, const char *text, unsigned long col[ColLast]) +{ char buf[BUFSIZ]; size_t mn, n = strlen(text); /* shorten text if necessary */ - for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) - if(mn == 0) + for (mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--) + if (mn == 0) return; memcpy(buf, text, mn); - if(mn < n) - for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.'); + if (mn < n) + for (n = MAX(mn-3, 0); n < mn; buf[n++] = '.'); drawrect(dc, 0, 0, dc->w, dc->h, 1, BG(dc, col)); drawtextn(dc, buf, mn, col); } void -drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) { +drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) +{ int x = dc->x + dc->font.height/2; int y = dc->y + dc->font.ascent+1; XSetForeground(dc->dpy, dc->gc, FG(dc, col)); - if(dc->font.set) + if (dc->font.set) XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n); else { XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); @@ -54,14 +58,15 @@ drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) { } void -eprintf(const char *fmt, ...) { +eprintf(const char *fmt, ...) +{ va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - if(fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { + if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { fputc(' ', stderr); perror(NULL); } @@ -69,12 +74,13 @@ eprintf(const char *fmt, ...) { } void -freedc(DC *dc) { - if(dc->font.set) +freedc(DC *dc) +{ + if (dc->font.set) XFreeFontSet(dc->dpy, dc->font.set); - if(dc->font.xfont) + if (dc->font.xfont) XFreeFont(dc->dpy, dc->font.xfont); - if(dc->canvas) + if (dc->canvas) XFreePixmap(dc->dpy, dc->canvas); XFreeGC(dc->dpy, dc->gc); XCloseDisplay(dc->dpy); @@ -82,24 +88,26 @@ freedc(DC *dc) { } unsigned long -getcolor(DC *dc, const char *colstr) { +getcolor(DC *dc, const char *colstr) +{ Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); XColor color; - if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) + if (!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) eprintf("cannot allocate color '%s'\n", colstr); return color.pixel; } DC * -initdc(void) { +initdc(void) +{ DC *dc; - if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) + if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) fputs("no locale support\n", stderr); - if(!(dc = calloc(1, sizeof *dc))) + if (!(dc = calloc(1, sizeof *dc))) eprintf("cannot malloc %u bytes:", sizeof *dc); - if(!(dc->dpy = XOpenDisplay(NULL))) + if (!(dc->dpy = XOpenDisplay(NULL))) eprintf("cannot open display\n"); dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); @@ -108,50 +116,54 @@ initdc(void) { } void -initfont(DC *dc, const char *fontstr) { - if(!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { - if(fontstr != NULL) +initfont(DC *dc, const char *fontstr) +{ + if (!loadfont(dc, fontstr ? fontstr : DEFAULTFN)) { + if (fontstr != NULL) fprintf(stderr, "cannot load font '%s'\n", fontstr); - if(fontstr == NULL || !loadfont(dc, DEFAULTFN)) + if (fontstr == NULL || !loadfont(dc, DEFAULTFN)) eprintf("cannot load font '%s'\n", DEFAULTFN); } dc->font.height = dc->font.ascent + dc->font.descent; } int -loadfont(DC *dc, const char *fontstr) { +loadfont(DC *dc, const char *fontstr) +{ char *def, **missing, **names; int i, n; XFontStruct **xfonts; - if(!*fontstr) + if (!*fontstr) return 0; - if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) { + if ((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) { n = XFontsOfFontSet(dc->font.set, &xfonts, &names); - for(i = 0; i < n; i++) { + for (i = 0; i < n; i++) { dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent); dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent); dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width); } } - else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) { + else if ((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) { dc->font.ascent = dc->font.xfont->ascent; dc->font.descent = dc->font.xfont->descent; dc->font.width = dc->font.xfont->max_bounds.width; } - if(missing) + if (missing) XFreeStringList(missing); return dc->font.set || dc->font.xfont; } void -mapdc(DC *dc, Window win, unsigned int w, unsigned int h) { +mapdc(DC *dc, Window win, unsigned int w, unsigned int h) +{ XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0); } void -resizedc(DC *dc, unsigned int w, unsigned int h) { - if(dc->canvas) +resizedc(DC *dc, unsigned int w, unsigned int h) +{ + if (dc->canvas) XFreePixmap(dc->dpy, dc->canvas); dc->w = w; @@ -161,8 +173,9 @@ resizedc(DC *dc, unsigned int w, unsigned int h) { } int -textnw(DC *dc, const char *text, size_t len) { - if(dc->font.set) { +textnw(DC *dc, const char *text, size_t len) +{ + if (dc->font.set) { XRectangle r; XmbTextExtents(dc->font.set, text, len, NULL, &r); @@ -172,6 +185,7 @@ textnw(DC *dc, const char *text, size_t len) { } int -textw(DC *dc, const char *text) { +textw(DC *dc, const char *text) +{ return textnw(dc, text, strlen(text)) + dc->font.height; } diff --git a/draw.h b/draw.h index a6ef9de..aeba1b2 100644 --- a/draw.h +++ b/draw.h @@ -6,30 +6,30 @@ enum { ColBG, ColFG, ColBorder, ColLast }; typedef struct { - int x, y, w, h; - int invert; + int x, y, w, h; + int invert; Display *dpy; - GC gc; - Pixmap canvas; + GC gc; + Pixmap canvas; struct { - int ascent; - int descent; - int height; - int width; - XFontSet set; + int ascent; + int descent; + int height; + int width; + XFontSet set; XFontStruct *xfont; } font; } DC; /* draw context */ -void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, int fill, unsigned long color); -void drawtext(DC *dc, const char *text, unsigned long col[ColLast]); -void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]); -void eprintf(const char *fmt, ...); -void freedc(DC *dc); -unsigned long getcolor(DC *dc, const char *colstr); +void drawrect(DC *, int, int, unsigned int, unsigned int, int, unsigned long); +void drawtext(DC *, const char *, unsigned long[ColLast]); +void drawtextn(DC *, const char *, size_t, unsigned long[ColLast]); +void eprintf(const char *, ...); +void freedc(DC *); +unsigned long getcolor(DC *, const char *); DC *initdc(void); -void initfont(DC *dc, const char *fontstr); -void mapdc(DC *dc, Window win, unsigned int w, unsigned int h); -void resizedc(DC *dc, unsigned int w, unsigned int h); -int textnw(DC *dc, const char *text, size_t len); -int textw(DC *dc, const char *text); +void initfont(DC *, const char *); +void mapdc(DC *, Window, unsigned int, unsigned int); +void resizedc(DC *, unsigned int, unsigned int); +int textnw(DC *, const char *, size_t); +int textw(DC *, const char *); diff --git a/stest.c b/stest.c index 886d418..ca90ca5 100644 --- a/stest.c +++ b/stest.c @@ -3,8 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> #define FLAG(x) (flag[(x)-'a']) @@ -15,17 +15,18 @@ static int flag[26]; static struct stat old, new; int -main(int argc, char *argv[]) { +main(int argc, char *argv[]) +{ struct dirent *d; char buf[BUFSIZ], *p; DIR *dir; int opt; - while((opt = getopt(argc, argv, "abcdefghln:o:pqrsuvwx")) != -1) - switch(opt) { + while ((opt = getopt(argc, argv, "abcdefghln:o:pqrsuvwx")) != -1) + switch (opt) { case 'n': /* newer than file */ case 'o': /* older than file */ - if(!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old)))) + if (!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old)))) perror(optarg); break; default: /* miscellaneous operators */ @@ -35,17 +36,17 @@ main(int argc, char *argv[]) { fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] [-n file] [-o file] [file...]\n", argv[0]); exit(2); } - if(optind == argc) - while(fgets(buf, sizeof buf, stdin)) { - if((p = strchr(buf, '\n'))) + 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]))) { + 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) + 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); } @@ -56,10 +57,11 @@ main(int argc, char *argv[]) { } void -test(const char *path, const char *name) { +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 */ @@ -75,7 +77,7 @@ test(const char *path, const char *name) { && (!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')) + if (FLAG('q')) exit(0); match = 1; puts(name); -- 1.8.5.5
>From 07b66350f0d1f89557edf0b236fbbbac5d599539 Mon Sep 17 00:00:00 2001 From: FRIGN <d...@frign.de> Date: Mon, 22 Dec 2014 18:36:23 +0100 Subject: [PATCH 5/5] Add myself to License --- LICENSE | 1 + 1 file changed, 1 insertion(+) diff --git a/LICENSE b/LICENSE index 39c4b6e..0565b3e 100644 --- a/LICENSE +++ b/LICENSE @@ -7,6 +7,7 @@ MIT/X Consortium License © 2009 Evan Gates <evan.ga...@gmail.com> © 2006-2008 Sander van Dijk <a dot h dot vandijk at gmail dot com> © 2006-2007 MichaŠJaneczek <janeczek at gmail dot com> +© 2014 Laslo Hunhold <d...@frign.de> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), -- 1.8.5.5