diff --git a/xdu.c b/xdu.c index 26ab8ec..841da30 100644 --- a/xdu.c +++ b/xdu.c @@ -66,7 +66,7 @@ char* root_prefix = "[root]"; /* - * What we IMPORT from xwin.c + * What we IMPORT from xwin.c */ extern int xsetup(), xmainloop(), @@ -74,18 +74,18 @@ extern int xsetup(), xrepaint(); /* - * What we EXPORT to xwin.c + * What we EXPORT to xwin.c */ /* - * extern int press(), reset(), repaint(), setorder(), reorder(); + * extern int press(), reset(), repaint(), setorder(), reorder(); */ /* - * extern nodeinfo(), helpinfo(); + * extern nodeinfo(), helpinfo(); */ int ncols = NCOLS; /* - * internal routines + * internal routines */ // char *strdup(); static void addtree(); @@ -96,7 +96,7 @@ static void clearrects(); static void sorttree(); /* - * order to sort paths by + * order to sort paths by */ #define ORD_FIRST 1 #define ORD_LAST 2 @@ -170,13 +170,13 @@ findnode(struct node* treep, int x, int y) && y >= treep->rect.top && y < treep->rect.top + treep->rect.height) { /* - * printf("found %s\n", treep->name); - */ + * printf("found %s\n", treep->name); + */ return treep; /* found */ } /* - * for each child + * for each child */ for (np = treep->child; np != NULL; np = np->peer) { if ((np2 = findnode(np, x, y)) != NODE_NULL) @@ -262,14 +262,14 @@ int main(int argc, char** argv) top.size = fix_tree(&top); /* - * dumptree(&top,0); + * dumptree(&top,0); */ sorttree(&top, order); topp = ⊤ /* - * don't display root if only one child + * don't display root if only one child */ if (numchildren(topp) == 1) topp = topp->child; @@ -281,10 +281,10 @@ int main(int argc, char** argv) static void parse_file(char* filename) { char buf[4096]; - char name[4096]; + char* name; + int spos = 0; long long size; FILE* fp; - char* buff; if (strcmp(filename, "-") == 0) { fp = stdin; @@ -295,31 +295,24 @@ static void parse_file(char* filename) } } while (fgets(buf, sizeof(buf), fp) != NULL) { + buf[strlen(buf) - 1] = '\0'; /* remove the trailing newline */ + sscanf(buf, "%lld %n", &size, &spos); + name = buf + spos; /* - * sscanf(buf, "%lld %s\n", &size, name); - */ - sscanf(buf, "%lld", &size); - buff = buf; - while (*buff != ' ' && *buff != '\t') { buff++; } - while (*buff == ' ' || *buff == '\t') { buff++; } - strncpy(name, buff, strlen(buff) - 1); - name[strlen(buff) - 1] = '\0'; - /* - * printf("%d %s\n", size, name); - */ + * printf("%d %s\n", size, name); + */ parse_entry(name, size); } fclose(fp); } /* - * bust up a path string and link it into the tree + * bust up a path string and link it into the tree */ static void parse_entry(char* name, long long size) { char* path[MAXDEPTH]; /* break up path into this list */ - char buf[MAXNAME]; /* temp space for path element - * name */ + char buf[MAXNAME]; /* temp space for path element *name */ int arg, indx; int length; /* nelson@reed.edu - trailing / fix */ @@ -330,8 +323,8 @@ static void parse_entry(char* name, long long size) length = strlen(name); if ((length > 0) && (name[length - 1] == '/')) { /* - * strip off trailing / (e.g. GNU du) - */ + * strip off trailing / (e.g. GNU du) + */ name[length - 1] = 0; } @@ -379,8 +372,8 @@ static int compare(struct node* n1, struct node* n2, int order) return strcmp(n1->name, n2->name); } /* - * not reached - */ + * not reached + */ case ORD_RSIZE: if (n1->size > n2->size) { return 1; @@ -390,8 +383,8 @@ static int compare(struct node* n1, struct node* n2, int order) return strcmp(n1->name, n2->name); } /* - * not reached - */ + * not reached + */ case ORD_ALPHA: return strcmp(n1->name, n2->name); break; @@ -400,60 +393,60 @@ static int compare(struct node* n1, struct node* n2, int order) break; case ORD_FIRST: /* - * return -1; - */ + * return -1; + */ return (n1->num - n2->num); break; case ORD_LAST: /* - * return 1; - */ + * return 1; + */ return (n2->num - n1->num); break; } /* - * shouldn't get here + * shouldn't get here */ fprintf(stderr, "xdu: bad insertion order\n"); return 0; } /* - * add path as a child of top - recursively + * add path as a child of top - recursively */ static void addtree(struct node* top, char* path[], long long size) { struct node* np; /* - * printf("addtree(\"%s\",\"%s\",%d)\n", top->name, path[0], size); + * printf("addtree(\"%s\",\"%s\",%d)\n", top->name, path[0], size); */ /* - * check all children for a match + * check all children for a match */ for (np = top->child; np != NULL; np = np->peer) { if (strcmp(path[0], np->name) == 0) { /* - * name matches - */ + * name matches + */ if (path[1] == NULL) { /* - * end of the chain, save size - */ + * end of the chain, save size + */ np->size = size; return; } /* - * recurse - */ + * recurse + */ addtree(np, &path[1], size); return; } } /* - * no child matched, add a new child + * no child matched, add a new child */ np = makenode(path[0], -1); np->parent = top; @@ -462,20 +455,20 @@ static void addtree(struct node* top, char* path[], long long size) if (path[1] == NULL) { /* - * end of the chain, save size - */ + * end of the chain, save size + */ np->size = size; return; } /* - * recurse + * recurse */ addtree(np, &path[1], size); return; } /* - * debug tree print + * debug tree print */ void dumptree(struct node* np, int level) { @@ -500,13 +493,13 @@ static void sorttree(struct node* np, int order) *np3; /* - * sort the trees of each of this nodes children + * sort the trees of each of this nodes children */ for (subnp = np->child; subnp != NODE_NULL; subnp = subnp->peer) { sorttree(subnp, order); } /* - * then put the given nodes children in order + * then put the given nodes children in order */ np0 = np; /* np0 points to node before np1 */ for (np1 = np->child; np1 != NODE_NULL; np1 = np1->peer) { @@ -514,8 +507,8 @@ static void sorttree(struct node* np, int order) for (np3 = np1->peer; np3 != NODE_NULL; np3 = np3->peer) { if (compare(np3, np1, order) < 0) { /* - * swap links - */ + * swap links + */ if (np0 == np) np0->child = np3; else @@ -524,8 +517,8 @@ static void sorttree(struct node* np, int order) np3->peer = np1; /* - * adjust pointers - */ + * adjust pointers + */ np1 = np3; np3 = np2; } @@ -551,11 +544,11 @@ static void drawchildren(struct node* nodep, struct rect rect) /* * printf("Drawing children of \"%s\", %d\n", nodep->name, - * nodep->size); + * nodep->size); */ /* * printf("In [%d,%d,%d,%d]\n", - * rect.left,rect.top,rect.width,rect.height); + * rect.left,rect.top,rect.width,rect.height); */ top = rect.top; @@ -565,8 +558,8 @@ static void drawchildren(struct node* nodep, struct rect rect) totalsize = nodep->size; if (totalsize == 0) { /* - * total the sizes of the children - */ + * total the sizes of the children + */ totalsize = 0; for (np = nodep->child; np != NULL; np = np->peer) totalsize += np->size; @@ -574,7 +567,7 @@ static void drawchildren(struct node* nodep, struct rect rect) } /* - * for each child + * for each child */ for (np = nodep->child; np != NULL; np = np->peer) { fractsize = totalsize ? np->size / (double)totalsize : 0; @@ -583,23 +576,23 @@ static void drawchildren(struct node* nodep, struct rect rect) { struct rect subrect; /* - * printf("%s, drawrect[%d,%d,%d,%d]\n", np->name, - * rect.left,top,rect.width,height); - */ + * printf("%s, drawrect[%d,%d,%d,%d]\n", np->name, + * rect.left,top,rect.width,height); + */ xdrawrect(np->name, np->size, rect.left, top, rect.width, height); /* - * save current screen rectangle for lookups - */ + * save current screen rectangle for lookups + */ np->rect.left = rect.left; np->rect.top = top; np->rect.width = rect.width; np->rect.height = height; /* - * draw children in subrectangle - */ + * draw children in subrectangle + */ subrect.left = rect.left + rect.width; subrect.top = top; subrect.width = rect.width; @@ -621,14 +614,14 @@ static void drawnode(struct node* nodep, struct rect rect) struct rect subrect; /* - * printf("Drawing \"%s\" %d\n", nodep->name, nodep->size); + * printf("Drawing \"%s\" %d\n", nodep->name, nodep->size); */ xdrawrect(nodep->name, nodep->size, rect.left, rect.top, rect.width, rect.height); /* - * save current screen rectangle for lookups + * save current screen rectangle for lookups */ nodep->rect.left = rect.left; nodep->rect.top = rect.top; @@ -636,7 +629,7 @@ static void drawnode(struct node* nodep, struct rect rect) nodep->rect.height = rect.height; /* - * draw children in subrectangle + * draw children in subrectangle */ subrect.left = rect.left + rect.width; subrect.top = rect.top; @@ -662,7 +655,7 @@ static void clearrects(struct node* nodep) nodep->rect.height = 0; /* - * for each child + * for each child */ for (np = nodep->child; np != NULL; np = np->peer) { clearrects(np); @@ -699,20 +692,6 @@ static void pwd(void) 100.0 * topp->size / rootp->size); } -#ifdef NEED_STRDUP -static char* strdup(char* s) -{ - int n; - char* cp; - - n = strlen(s); - cp = malloc(n + 1); - strcpy(cp, s); - - return cp; -} -#endif - /**************** External Entry Points ****************/ void press(int x, int y) @@ -720,21 +699,21 @@ void press(int x, int y) struct node* np; /* - * printf("press(%d,%d)...\n",x,y); + * printf("press(%d,%d)...\n",x,y); */ np = findnode(&top, x, y); /* - * printf("Found \"%s\"\n", np?np->name:"(null)"); + * printf("Found \"%s\"\n", np?np->name:"(null)"); */ if (np == topp) { /* - * already top, go up if possible - */ + * already top, go up if possible + */ if (np->parent != &top || numchildren(&top) != 1) np = np->parent; /* - * printf("Already top, parent = \"%s\"\n", np?np->name:"(null)"); - */ + * printf("Already top, parent = \"%s\"\n", np?np->name:"(null)"); + */ } if (np != NODE_NULL) { topp = np; @@ -776,7 +755,7 @@ void repaint(int width, int height) struct rect rect; /* - * define a rectangle to draw into + * define a rectangle to draw into */ rect.top = 0; rect.left = 0; @@ -786,7 +765,7 @@ void repaint(int width, int height) clearrects(&top); /* clear current rectangle info */ drawnode(topp, rect); /* draw tree into given rectangle */ #if 0 - pwd(); /* display current path */ + pwd(); /* display current path */ #endif } @@ -842,12 +821,12 @@ void nodeinfo(void) struct node* np; /* - * display current root path + * display current root path */ pwd(); /* - * display each child of this node + * display each child of this node */ for (np = topp->child; np != NULL; np = np->peer) { printf("%-12lld %s\n", np->size, np->name); diff --git a/xwin.c b/xwin.c index 5c0cb81..65518e8 100644 --- a/xwin.c +++ b/xwin.c @@ -36,7 +36,7 @@ #endif /* - * IMPORTS: routines that this module vectors out to + * IMPORTS: routines that this module vectors out to */ extern int press(); extern int printpath(); @@ -53,7 +53,7 @@ extern void savetops(char*, int); extern void savetosvg(char*, int, int, int); /* - * EXPORTS: routines that this module exports outside + * EXPORTS: routines that this module exports outside */ extern int xsetup(); extern int xmainloop(); @@ -62,24 +62,24 @@ extern void xrepaint(); extern void xdrawrect(); /* - * internal routines + * internal routines */ static void help_popup(); static void help_popdown(); static String fallback_resources[] = { - "*window.width: 600", - "*window.height: 480", - "*help.width: 500", - "*help.height: 330", - "*order: first", - "*psfile: xdu_out.ps", - "*svgfile: xdu_out", + "*window.width: 600", + "*window.height: 480", + "*help.width: 500", + "*help.height: 330", + "*order: first", + "*psfile: xdu_out.ps", + "*svgfile: xdu_out", NULL }; /* - * Application Resources + * Application Resources */ typedef struct { Pixel foreground; @@ -113,7 +113,7 @@ static XtResource application_resources[] = { }; /* - * Command Line Options + * Command Line Options */ static XrmOptionDescRec options[] = { { "-c", "*ncol", XrmoptionSepArg, NULL }, @@ -127,7 +127,7 @@ static XrmOptionDescRec options[] = { }; /* - * action routines + * action routines */ static void a_goto(); static void a_printpath(); @@ -162,36 +162,37 @@ static XtActionsRec actionsTable[] = { }; static char defaultTranslations[] = "\ -Q: quit()\n\ -Escape: quit()\n\ -:/: reset()\n\ -S: size()\n\ -P: saveps()\n\ -V: savesvg()\n\ -I: info()\n\ -H: help()\n\ -Help: help()\n\ -:?: help()\n\ -A: reorder(alpha)\n\ -N: reorder(size)\n\ -F: reorder(first)\n\ -L: reorder(last)\n\ -R: reorder(reverse)\n\ -U: uponechild()\n\ -D: downonechild()\n\ -1: ncol(1)\n\ -2: ncol(2)\n\ -3: ncol(3)\n\ -4: ncol(4)\n\ -5: ncol(5)\n\ -6: ncol(6)\n\ -7: ncol(7)\n\ -8: ncol(8)\n\ -9: ncol(9)\n\ -0: ncol(10)\n\ -: goto()\n\ -: printpath()\n\ -: reset()\n\ +Q: quit()\n\ +Escape: quit()\n\ +:/: reset()\n\ +S: size()\n\ +P: saveps()\n\ +V: savesvg()\n\ +I: info()\n\ +H: help()\n\ +Help: help()\n\ +:?: help()\n\ +A: reorder(alpha)\n\ +N: reorder(size)\n\ +F: reorder(first)\n\ +L: reorder(last)\n\ +R: reorder(reverse)\n\ +U: uponechild()\n\ +D: downonechild()\n\ +1: ncol(1)\n\ +2: ncol(2)\n\ +3: ncol(3)\n\ +4: ncol(4)\n\ +5: ncol(5)\n\ +6: ncol(6)\n\ +7: ncol(7)\n\ +8: ncol(8)\n\ +9: ncol(9)\n\ +0: ncol(10)\n\ +: goto()\n\ +: printpath()\n\ +: reset()\n\ +(2):quit()\n\ "; #define UNUSED1(x) (void)(x) @@ -210,15 +211,15 @@ static char defaultTranslations[] = "\ (__VA_ARGS__) /* - * action routines + * action routines */ static void a_quit(Widget w, XEvent* event, String* params, Cardinal* num_params) { UNUSED(event, params, num_params); - XtDestroyApplicationContext(XtWidgetToApplicationContext(w)); - exit(0); + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); + return; } static void @@ -317,7 +318,7 @@ a_help(Widget w, XEvent* event, String* params, Cardinal* num_params) { UNUSED(w, event, params, num_params); /* - * helpinfo(); + * helpinfo(); */ help_popup(); } @@ -330,7 +331,7 @@ a_removehelp(Widget w, XEvent* event, String* params, Cardinal* num_params) } /* - * callback routines + * callback routines */ static void setRepaintWhenIdle(); @@ -339,13 +340,13 @@ c_repaint(Widget w, XtPointer data, XEvent* event, Boolean* continue_to_dispatch { UNUSED(w, data, event, continue_to_dispatch); /* - * printf("Expose\n"); + * printf("Expose\n"); */ setRepaintWhenIdle(); } /* - * X Window related variables + * X Window related variables */ static Display* dpy; static int screen; @@ -415,7 +416,7 @@ a_savesvg(Widget w, XEvent* e, String* params, Cardinal* num_params) Widget toplevel; /* - * External Functions + * External Functions */ int xsetup(int* argcp, char** argv) @@ -427,7 +428,7 @@ int xsetup(int* argcp, char** argv) Arg args[5]; /* - * Create the top level Widget + * Create the top level Widget */ n = 0; XtSetArg(args[n], XtNtitle, "XDU Disk Usage Display ('h' for help)"); @@ -443,7 +444,7 @@ int xsetup(int* argcp, char** argv) trans_table = XtParseTranslationTable(defaultTranslations); /* - * Create a simple Label class widget to draw in + * Create a simple Label class widget to draw in */ n = 0; XtSetArg(args[n], XtNlabel, ""); @@ -452,7 +453,7 @@ int xsetup(int* argcp, char** argv) n); /* - * events + * events */ XtAddEventHandler(w, ExposureMask, False, c_repaint, NULL); XtAugmentTranslations(w, trans_table); @@ -460,7 +461,7 @@ int xsetup(int* argcp, char** argv) XtRealizeWidget(toplevel); /* - * We need these for the raw Xlib calls + * We need these for the raw Xlib calls */ win = XtWindow(w); dpy = XtDisplay(w); @@ -544,7 +545,7 @@ void xdrawrect(char* name, long long size, int x, int y, int width, int height) int cheight; /* - * printf("draw(%d,%d,%d,%d)\n", x, y, width, height ); + * printf("draw(%d,%d,%d,%d)\n", x, y, width, height ); */ XDrawRectangle(dpy, win, gc, x, y, width, height); @@ -580,7 +581,7 @@ void xdrawrect(char* name, long long size, int x, int y, int width, int height) return; /* - * print label + * print label */ textx = x + 4; texty = y + height / 2.0 + (overall.ascent - overall.descent) / 2.0 + 1.5; @@ -606,7 +607,7 @@ help_popup() } /* - * popup shell + * popup shell */ n = 0; XtSetArg(args[n], XtNtitle, "XDU Help"); @@ -615,7 +616,7 @@ help_popup() toplevel, args, n); /* - * form container + * form container */ n = 0; XtSetArg(args[n], XtNborderWidth, 0); @@ -625,7 +626,7 @@ help_popup() form = XtCreateManagedWidget("form", formWidgetClass, popup, args, n); /* - * text widget in form + * text widget in form */ n = 0; XtSetArg(args[n], XtNborderWidth, 0); @@ -633,16 +634,16 @@ help_popup() XtSetArg(args[n], XtNresize, XawtextResizeBoth); n++; /* - * fallback resources weren't working here on the Sun + * fallback resources weren't working here on the Sun */ XtSetArg(args[n], XtNwidth, 500); n++; - XtSetArg(args[n], XtNheight, 340); + XtSetArg(args[n], XtNheight, 360); n++; text = XtCreateManagedWidget("help", asciiTextWidgetClass, form, args, n); /* - * create text source + * create text source */ n = 0; XtSetArg(args[n], XtNtype, XawAsciiString); @@ -671,14 +672,15 @@ Keyboard Commands\n\ p go down one child\n\ \n\ Mouse Commands\n\ - Left Goto node (goto parent if leftmost box)\n\ - Middle Print path of clicked node\n\ - Right Back to root\n\ + Left Goto node (goto parent if leftmost box)\n\ + Middle Print path of clicked node\n\ + Right Back to root\n\ + DblRight Quit\n\ "); n++; src = XtCreateWidget("textSource", asciiSrcObjectClass, text, args, n); /* - * set text source + * set text source */ XawTextSetSource(text, src, 0); @@ -689,7 +691,7 @@ Mouse Commands\n\ XtAugmentTranslations(form, trans_table); /* - * Set up ICCCM delete window + * Set up ICCCM delete window */ wm_delete_window = XInternAtom(XtDisplay(popup), "WM_DELETE_WINDOW", False); XtOverrideTranslations(popup,