From a3741201eeaed08b857fb502d29bb1feb4132656 Mon Sep 17 00:00:00 2001 From: Niclas Hofmann Date: Wed, 15 Aug 2018 11:29:35 +0200 Subject: [PATCH 1/3] Add mouse feature to print path of node --- xdu.c | 25 +++++++++++++++++++++++++ xwin.c | 16 +++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/xdu.c b/xdu.c index 785cfa7..f8e6a12 100644 --- a/xdu.c +++ b/xdu.c @@ -38,8 +38,16 @@ * - 'worksforme' Makefile * - -Wall -Werror -Wextra -pedantic * + * changed mouse actions Wed Aug 15 2018 11:25:00 PM CEST + * Niclas Hofmann, niclas@molgen.mpg.de + * - middle mouse button: print path of node that had been clicked + * - right mouse button: reset (former middle button action) + * - 'worksforme' Makefile + * - -Wall -Werror -Wextra -pedantic + * */ +#define _GNU_SOURCE #include #include #include @@ -684,6 +692,23 @@ void press(int x, int y) } } +void printpath(int x, int y) +{ + struct node *np; + char* path; + + np = findnode(&top, x, y); + if (np == NULL) + return; + path = np->name; + while(np->parent != NULL) { + asprintf(&path, "%s/%s", np->parent->name, path); + np = np->parent; + } + path += 6; + printf("%s\n", path); +} + void reset(void) { topp = ⊤ diff --git a/xwin.c b/xwin.c index 4654084..05d3e45 100644 --- a/xwin.c +++ b/xwin.c @@ -37,6 +37,7 @@ /* IMPORTS: routines that this module vectors out to */ extern int press(); +extern int printpath(); extern int reset(); extern int uponechild(); extern int downonechild(); @@ -123,6 +124,7 @@ static XrmOptionDescRec options[] = { /* action routines */ static void a_goto(); +static void a_printpath(); static void a_reset(); static void a_quit(); static void a_reorder(); @@ -138,6 +140,7 @@ static void a_removehelp(); static XtActionsRec actionsTable[] = { {"reset", a_reset}, + {"printpath", a_printpath}, {"goto", a_goto}, {"quit", a_quit}, {"reorder", a_reorder}, @@ -181,7 +184,8 @@ static char defaultTranslations[] = "\ 9: ncol(9)\n\ 0: ncol(10)\n\ : goto()\n\ -: reset()\n\ +: printpath()\n\ +: reset()\n\ "; #define UNUSED1(x) (void)(x) @@ -220,6 +224,16 @@ Cardinal *num_params; press(event->xbutton.x, event->xbutton.y); } +static void a_printpath(w, event, params, num_params) +Widget w; +XEvent *event; +String *params; +Cardinal *num_params; +{ + UNUSED(w, params, num_params); + printpath(event->xbutton.x, event->xbutton.y); +} + static void a_reset(w, event, params, num_params) Widget w; XEvent *event; From 5b0eb9bd95a72a24f8ca1c2bb776c8e5596ff42f Mon Sep 17 00:00:00 2001 From: Niclas Hofmann Date: Wed, 15 Aug 2018 14:01:33 +0200 Subject: [PATCH 2/3] Memory leak fixed for printpath routine checked with `du -k /etc | valgrind --leak-check=full --show-leak-kinds=all ./xdu` --- xdu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xdu.c b/xdu.c index f8e6a12..434c1da 100644 --- a/xdu.c +++ b/xdu.c @@ -696,17 +696,21 @@ void printpath(int x, int y) { struct node *np; char* path; + char* tmp_path; np = findnode(&top, x, y); if (np == NULL) return; - path = np->name; + asprintf(&path, "%s", np->name); while(np->parent != NULL) { + tmp_path = path; asprintf(&path, "%s/%s", np->parent->name, path); np = np->parent; + free(tmp_path); } - path += 6; - printf("%s\n", path); + tmp_path = path + 6; + printf("%s\n", tmp_path); + free(path); } void reset(void) From 9f32f3298a4e3315bcaef3ac7cda66ebe7c4c051 Mon Sep 17 00:00:00 2001 From: Niclas Hofmann Date: Thu, 23 Aug 2018 16:56:44 +0200 Subject: [PATCH 3/3] Create global root prefix --- xdu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xdu.c b/xdu.c index 434c1da..7412f60 100644 --- a/xdu.c +++ b/xdu.c @@ -61,6 +61,8 @@ #define MAXPATH 4096 /* max total pathname length */ #define NCOLS 5 /* default number of columns in display */ +char* root_prefix = "[root]"; + /* What we IMPORT from xwin.c */ extern int xsetup(), xmainloop(), xdrawrect(), xrepaint(); @@ -223,7 +225,7 @@ int main(argc, argv) int argc; char **argv; { - top.name = strdup("[root]"); + top.name = root_prefix; top.size = -1; xsetup(&argc, argv); @@ -708,7 +710,7 @@ void printpath(int x, int y) np = np->parent; free(tmp_path); } - tmp_path = path + 6; + tmp_path = path + strlen(root_prefix); printf("%s\n", tmp_path); free(path); }