code-style improvements - lsw - lists window titles of X clients to stdout
 (HTM) git clone git://git.suckless.org/lsw
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 8d5beffdb4522df02fc5b80003de94a248b20e70
 (DIR) parent a2e5856f22739a9056fa41789369948450cd7e36
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sat, 25 Jun 2022 10:19:04 +0200
       
       code-style improvements
       
       Diffstat:
         M lsw.c                               |      81 ++++++++++++++++---------------
       
       1 file changed, 43 insertions(+), 38 deletions(-)
       ---
 (DIR) diff --git a/lsw.c b/lsw.c
       @@ -5,62 +5,67 @@
        #include <X11/Xlib.h>
        #include <X11/Xutil.h>
        
       -static const char *getname(Window);
       -static void lsw(Window);
       -
        static Atom netwmname;
        static Display *dpy;
        
       -int
       -main(int argc, char *argv[]) {
       -        int i;
       +char *
       +getname(Window win)
       +{
       +        static char buf[BUFSIZ];
       +        char **list;
       +        int n;
       +        XTextProperty prop;
        
       -        if(!(dpy = XOpenDisplay(NULL))) {
       -                fprintf(stderr, "%s: cannot open display\n", argv[0]);
       -                exit(EXIT_FAILURE);
       +        if (!XGetTextProperty(dpy, win, &prop, netwmname) || prop.nitems == 0)
       +                if (!XGetWMName(dpy, win, &prop) || prop.nitems == 0)
       +                        return "";
       +        if (!XmbTextPropertyToTextList(dpy, &prop, &list, &n) && n > 0) {
       +                strncpy(buf, list[0], sizeof buf);
       +                XFreeStringList(list);
       +        } else {
       +                strncpy(buf, (char *)prop.value, sizeof buf);
                }
       -        netwmname = XInternAtom(dpy, "_NET_WM_NAME", False);
       -
       -        if(argc < 2)
       -                lsw(DefaultRootWindow(dpy));
       -        else for(i = 1; i < argc; i++)
       -                lsw(strtol(argv[i], NULL, 0));
       +        XFree(prop.value);
       +        buf[sizeof buf - 1] = '\0';
        
       -        XCloseDisplay(dpy);
       -        return EXIT_SUCCESS;
       +        return buf;
        }
        
        void
       -lsw(Window win) {
       +lsw(Window win)
       +{
                unsigned int n;
                Window *wins, *w, dw;
                XWindowAttributes wa;
        
       -        if(!XQueryTree(dpy, win, &dw, &dw, &wins, &n) || !n)
       +        if (!XQueryTree(dpy, win, &dw, &dw, &wins, &n) || !n)
                        return;
       -        for(w = &wins[n-1]; w >= &wins[0]; w--)
       -                if(XGetWindowAttributes(dpy, *w, &wa)
       +        for (w = &wins[n-1]; w >= &wins[0]; w--)
       +                if (XGetWindowAttributes(dpy, *w, &wa)
                        && !wa.override_redirect && wa.map_state == IsViewable)
                                printf("0x%07lx %s\n", *w, getname(*w));
                XFree(wins);
        }
        
       -const char *
       -getname(Window win) {
       -        static char buf[BUFSIZ];
       -        char **list;
       -        int n;
       -        XTextProperty prop;
       +int
       +main(int argc, char *argv[])
       +{
       +        int i;
        
       -        if(!XGetTextProperty(dpy, win, &prop, netwmname) || prop.nitems == 0)
       -                if(!XGetWMName(dpy, win, &prop) || prop.nitems == 0)
       -                        return "";
       -        if(!XmbTextPropertyToTextList(dpy, &prop, &list, &n) && n > 0) {
       -                strncpy(buf, list[0], sizeof buf);
       -                XFreeStringList(list);
       -        } else
       -                strncpy(buf, (char *)prop.value, sizeof buf);
       -        XFree(prop.value);
       -        buf[sizeof buf - 1] = '\0';
       -        return buf;
       +        if (!(dpy = XOpenDisplay(NULL))) {
       +                fprintf(stderr, "%s: cannot open display\n", argv[0]);
       +                exit(EXIT_FAILURE);
       +        }
       +        netwmname = XInternAtom(dpy, "_NET_WM_NAME", False);
       +
       +        if (argc < 2) {
       +                lsw(DefaultRootWindow(dpy));
       +        } else {
       +                for (i = 1; i < argc; i++)
       +                        lsw(strtol(argv[i], NULL, 0));
       +        }
       +
       +        XCloseDisplay(dpy);
       +
       +        return EXIT_SUCCESS;
        }