From f2333596609ff57ff9774882a08a433fc61b44da Mon Sep 17 00:00:00 2001 From: Adrien Schildknecht Date: Sun, 21 Dec 2014 02:21:13 +0100 Subject: [PATCH] Revert "Return the path of the deepest accessible child" This reverts commit 1bd171cc95244543b80181b30112bfc7bc752dee. --- xcwd.c | 116 +++++++++++++++++++++++++++------------------------------ 1 file changed, 54 insertions(+), 62 deletions(-) diff --git a/xcwd.c b/xcwd.c index 154554f..ff50bce 100644 --- a/xcwd.c +++ b/xcwd.c @@ -1,4 +1,4 @@ -/* This is xcwd written by Adrien Schildknecht (c) 2013-2014 +/* This is xcwd written by Adrien Schildknecht (c) 2013 * Email: adrien+dev@schischi.me * Feel free to copy and redistribute in terms of the * BSD license @@ -22,21 +22,19 @@ # include #endif -//#define DEBUG +#define DEBUG 0 #define XA_STRING (XInternAtom(dpy, "STRING", 0)) #define XA_CARDINAL (XInternAtom(dpy, "CARDINAL", 0)) #define XA_WM_STATE (XInternAtom(dpy, "WM_STATE", 0)) -#ifdef DEBUG - #define LOG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) -#else - #define LOG(fmt, ...) do { } while (0) -#endif +#define LOG(fmt, ...) \ + do { if (DEBUG) fprintf(stderr, fmt, __VA_ARGS__); } while (0) Display *dpy; -struct proc_list { +typedef struct processes_s *processes_t; +struct processes_s { struct proc_s { long pid; long ppid; @@ -48,18 +46,18 @@ struct proc_list { size_t n; }; -static int process_name_cmp(const void *p1, const void *p2) +int nameCmp(const void *p1, const void *p2) { return strcasecmp(((struct proc_s *)p1)->name, ((struct proc_s *)p2)->name); } -static int process_ppid_cmp(const void *p1, const void *p2) +int ppidCmp(const void *p1, const void *p2) { return ((struct proc_s *)p1)->ppid - ((struct proc_s *)p2)->ppid; } -static Window focused_window(void) +static Window focusedWindow() { Atom type; Window focuswin, root, *children; @@ -95,7 +93,7 @@ static Window focused_window(void) return 0; } -static long pid_of_window(Window win) +static long windowPid(Window focuswin) { Atom nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1); Atom type; @@ -104,7 +102,7 @@ static long pid_of_window(Window win) unsigned long nitems, after; unsigned char *data; - status = XGetWindowProperty(dpy, win, nameAtom, 0, 1024, 0, + status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0, XA_CARDINAL, &type, &format, &nitems, &after, &data); if (status == Success && data) { pid = *((long*)data); @@ -116,7 +114,7 @@ static long pid_of_window(Window win) return pid; } -static char* window_strings(Window win, long unsigned int *size, char *hint) +static char* windowStrings(Window focuswin, long unsigned int *size, char* hint) { Atom nameAtom = XInternAtom(dpy, hint, 1); Atom type; @@ -126,7 +124,7 @@ static char* window_strings(Window win, long unsigned int *size, char *hint) unsigned char *data = 0; char *ret = NULL; - if (XGetWindowProperty(dpy, win, nameAtom, 0, 1024, 0, AnyPropertyType, + if (XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0, AnyPropertyType, &type, &format, size, &after, &data) == Success) { if (data) { if (type == XA_STRING) { @@ -146,22 +144,22 @@ static char* window_strings(Window win, long unsigned int *size, char *hint) return ret; } -static void processes_free(struct proc_list * p) +static void freeProcesses(processes_t p) { free(p->ps); free(p); } -static struct proc_list * processes_list(void) +static processes_t getProcesses(void) { - struct proc_list * p = NULL; + processes_t p = NULL; #ifdef LINUX glob_t globbuf; unsigned int i, j, k; char line[201] = {0}; glob("/proc/[0-9]*", GLOB_NOSORT, NULL, &globbuf); - p = malloc(sizeof(struct proc_list)); + p = malloc(sizeof(struct processes_s)); p->ps = malloc(globbuf.gl_pathc * sizeof(struct proc_s)); LOG("Found %zu processes\n", globbuf.gl_pathc); @@ -175,8 +173,7 @@ static struct proc_list * processes_list(void) tn = fopen(name, "r"); if (tn == NULL) continue; - if (fread(line, 200, 1, tn) == 0) - continue; + fread(line, 200, 1, tn); p->ps[j].pid = atoi(strtok(line, " ")); k = snprintf(p->ps[j].name, 32, "%s", strtok(NULL, " ") + 1); p->ps[j].name[k - 1] = 0; @@ -192,7 +189,7 @@ static struct proc_list * processes_list(void) #endif #ifdef BSD unsigned int count; - p = malloc(sizeof(struct proc_list)); + p = malloc(sizeof(struct processes_s)); struct kinfo_proc *kp; size_t len = 0; int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0 }; @@ -228,7 +225,7 @@ static struct proc_list * processes_list(void) return p; } -static int read_process_path(struct proc_s *proc) +static int readPath(struct proc_s *proc) { #ifdef LINUX char buf[255]; @@ -259,40 +256,35 @@ static int read_process_path(struct proc_s *proc) return 1; } -static int child_cwd(struct proc_list * p, struct proc_s *key) +static int cwdOfDeepestChild(processes_t p, long pid) { int i; - struct proc_s *res = (struct proc_s *)bsearch(key, p->ps, p->n, - sizeof(struct proc_s), process_ppid_cmp); - struct proc_s new_key = { .pid = key->ppid }; + struct proc_s key = { .pid = pid, .ppid = pid}, + *res = NULL, *lastRes = NULL; - if (res == NULL) { - LOG("--> %ld pid\n", key->pid); - return read_process_path(key); + do { + if(res) { + lastRes = res; + key.ppid = res->pid; + } + res = (struct proc_s *)bsearch(&key, p->ps, p->n, + sizeof(struct proc_s), ppidCmp); + } while(res); + + if(!lastRes) { + return readPath(&key); } - for(i = 0; res != p->ps && (res - i)->ppid == res->ppid; ++i) { - new_key.ppid = (res - i)->pid; - LOG("--> %ld ppid\n", new_key.ppid); - if(child_cwd(p, &new_key)) + + for(i = 0; lastRes != p->ps && (lastRes - i)->ppid == lastRes->ppid; ++i) + if(readPath((lastRes - i))) return 1; - } - for(i = 1; res != p->ps + p->n && (res + i)->ppid == res->ppid; ++i) { - new_key.ppid = (res + i)->pid; - LOG("--> %ld ppid\n", new_key.ppid); - if(child_cwd(p, &new_key)) + for(i = 1; lastRes != p->ps + p->n && (lastRes + i)->ppid == lastRes->ppid; ++i) + if(readPath((lastRes + i))) return 1; - } - return read_process_path(key); + return 0; } -static int deepest_child_cwd(struct proc_list * p, long pid) -{ - struct proc_s key = { .pid = pid, .ppid = pid}; - - return child_cwd(p, &key); -} - -int get_home_dir(void) +int getHomeDirectory() { LOG("%s", "getenv $HOME...\n"); fprintf(stdout, "%s\n", getenv("HOME")); @@ -304,32 +296,32 @@ int main(int argc, const char *argv[]) (void)argc; (void)argv; - struct proc_list * p; + processes_t p; long pid; int ret = EXIT_SUCCESS; - Window w = focused_window(); + Window w = focusedWindow(); if (w == None) - return get_home_dir(); + return getHomeDirectory(); - pid = pid_of_window(w); - p = processes_list(); + pid = windowPid(w); + p = getProcesses(); if(!p) - return get_home_dir(); + return getHomeDirectory(); if(pid != -1) - qsort(p->ps, p->n, sizeof(struct proc_s), process_ppid_cmp); + qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp); else { long unsigned int size; unsigned int i; char* strings; struct proc_s *res = NULL, key; - qsort(p->ps, p->n, sizeof(struct proc_s), process_name_cmp); - strings = window_strings(w, &size, "WM_CLASS"); + qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp); + strings = windowStrings(w, &size, "WM_CLASS"); for(i = 0; i < size; i += strlen(strings + i) + 1) { LOG("pidof %s\n", strings + i); strcpy(key.name, strings + i); res = (struct proc_s *)bsearch(&key, p->ps, p->n, - sizeof(struct proc_s), process_name_cmp); + sizeof(struct proc_s), nameCmp); if(res) break; } @@ -340,9 +332,9 @@ int main(int argc, const char *argv[]) if (size) free(strings); } - if (pid == -1 || !deepest_child_cwd(p, pid)) - ret = get_home_dir(); - processes_free(p); + if (pid == -1 || !cwdOfDeepestChild(p, pid)) + ret = getHomeDirectory(); + freeProcesses(p); return ret; }