home *** CD-ROM | disk | FTP | other *** search
- /* Systems Sciences Laboratory, Webster Research Center */
-
- static char *PROGRAM_information[] =
- {
- "Copyright (c) 1988 Xerox Corporation. All rights reserved.",
- "$Header$",
- "$Locker$"
- }
- ;
-
- /*
- * Copyright protection claimed includes all forms and matters of copyrightable
- * material and information now allowed by statutory or judicial lay or
- * herinafter granted, including without limitation, material generated from
- * the software programs which are displayed on the screen such as icons,
- * screen display looks, etc.
- */
-
- #include "xfilebrowser.h"
-
-
- /**************************
- * local data structures *
- **************************/
- extern int getfiles();
- extern int my_alphasort();
- extern int filesizesort();
- extern int datesort();
-
- static Widget listshell = NULL;
- static Widget sortoption; /* option for sorting files */
- static Widget owneroption; /* print owner or group */
- static Widget dottoggle; /* should dot file be printed */
-
- static Widget optioncaller;
-
- static int currentsort = 1;
- static int currentdotfiles = 0; /* don't print dot files */
- int currentid = 1; /* 1 = print owner name; 2 = print group name */
-
- static short direction = 1;
-
- static short disp_option = 0;
- typedef int (*intfunc)();
-
- static intfunc sortfunc[] = {
- my_alphasort, filesizesort, datesort };
-
-
- check_option()
- {
- return disp_option;
- }
-
- XtState GetToggle(w)
- Widget w;
- {
- XtState toggle;
- Arg arglist[1];
-
- XtSetArg(arglist[0], XtNstate, &toggle);
- XtGetValues(w, arglist, (Cardinal)1);
- return(toggle);
- }
-
- void SetToggle(w, value)
- Widget w;
- XtState value;
- {
- Arg arglist[1];
-
- XtSetArg(arglist[0], XtNstate, value);
- XtSetValues(w, arglist, (Cardinal)1);
- }
-
- void DoApply()
- {
- currentsort = XtOptionGetSelection(sortoption);
- currentid = XtOptionGetSelection(owneroption);
- reset_ownercache();
-
- if (GetToggle(dottoggle) == XtToggleOn) currentdotfiles = 1;
- else currentdotfiles = 0;
-
- disp_option = 0;
- change_sensitive(optioncaller, TRUE);
- XtPopdown(listshell);
- }
-
- void DoCancel()
- {
- XtState toggle;
-
- /* reset the option dialog window */
- if ( !(currentsort == XtOptionGetSelection(sortoption)) )
- /* sorting was modified by user */
- XtOptionSetSelection(sortoption, currentsort);
-
- if ( !(currentid == XtOptionGetSelection(owneroption)) )
- /* printing owner/group was modified by user */
- XtOptionSetSelection(owneroption, currentid);
-
- /* reset toggle switch for dot files */
- toggle = GetToggle(dottoggle);
- if (toggle == XtToggleOn && currentdotfiles == 0)
- SetToggle(dottoggle, XtToggleOff);
- else if (toggle == XtToggleOff && currentdotfiles == 1)
- SetToggle(dottoggle, XtToggleOn);
-
- disp_option = 0;
- change_sensitive(optioncaller, TRUE);
- XtPopdown(listshell);
- }
-
-
- build_listoptions()
- {
- Arg popargs[1];
- Widget listpane, listrow1, listrow2, optform;
-
- static Arg paneargs[] = {
- { XtNallowResize, (XtArgVal)True },
- };
- static Arg optformlist[] = {
- {XtNborderWidth, (XtArgVal)0 }
- };
-
- static Arg optionargs[] = {
- { XtNlabel, (XtArgVal)NULL },
- { XtNorientation,(XtArgVal)XtorientVertical },
- { XtNfromHoriz, (XtArgVal) NULL },
- { XtNfromVert, (XtArgVal) NULL },
- { XtNleft, (XtArgVal) XtChainLeft },
- { XtNright, (XtArgVal) XtChainLeft },
- { XtNtop, (XtArgVal) XtChainTop },
- { XtNbottom, (XtArgVal) XtChainTop }
-
- };
- static XtCallbackRec callbackList[] = { {NULL, NULL}, {NULL, NULL} };
- static Arg toggleargs[] = {
- { XtNlabel, (XtArgVal)NULL },
- { XtNstate,(XtArgVal)XtToggleOff },
- { XtNfromHoriz, (XtArgVal) NULL },
- { XtNfromVert, (XtArgVal) NULL },
- { XtNleft, (XtArgVal) XtChainLeft },
- { XtNright, (XtArgVal) XtChainLeft },
- { XtNtop, (XtArgVal) XtChainTop },
- { XtNbottom, (XtArgVal) XtChainTop }
-
- };
-
- XtSetArg( popargs[0], XtNborderWidth, 2 );
-
- listshell = XtCreatePopupShell("listshell",
- overrideShellWidgetClass,
- toplevel, popargs, XtNumber(popargs));
-
- listpane = XtCreateManagedWidget( "listpaned", vPanedWidgetClass,
- listshell, paneargs , XtNumber(paneargs) );
- listrow1 = XtCreateManagedWidget("listrow1", boxWidgetClass,
- listpane, NULL,0);
- listrow2 = XtCreateManagedWidget("listrow2", boxWidgetClass,
- listpane, NULL,0);
- makeCommandButton(listrow1, "Apply", DoApply);
- makeCommandButton(listrow1, "Cancel", DoCancel);
-
- optform = XtCreateManagedWidget("sorting",formWidgetClass,
- listrow2, optformlist, XtNumber(optformlist));
-
- /* define option menu for sorting files */
- optionargs[0].value = (XtArgVal)"Select Sorting Option:";
- sortoption = XtCreateManagedWidget("sorting", optionWidgetClass,
- optform, optionargs, XtNumber(optionargs));
- XtOptionAddOption(sortoption, "File Name", TRUE);
- XtOptionAddOption(sortoption, "File Size", FALSE);
- XtOptionAddOption(sortoption, "Date", FALSE);
-
- /* define option menu for printing owner/group */
- optionargs[0].value = (XtArgVal)"Print Owner/Group Name:";
- optionargs[2].value = (XtArgVal)sortoption;
- owneroption = XtCreateManagedWidget("owner", optionWidgetClass,
- optform, optionargs, XtNumber(optionargs));
- XtOptionAddOption(owneroption, "Owner Name", TRUE);
- XtOptionAddOption(owneroption, "Group name", FALSE);
-
- /* define toggle for printing . files */
- toggleargs[0].value = (XtArgVal)"Print Dot Files";
- toggleargs[3].value = (XtArgVal)sortoption;
- dottoggle = XtCreateManagedWidget("owner", toggleWidgetClass,
- optform, toggleargs, XtNumber(toggleargs));
-
- XtSetMappedWhenManaged(listshell, FALSE);
- XtRealizeWidget(listshell);
- }
-
- display_listoptions(caller)
- Widget caller;
- {
- if (listshell == NULL) {
- build_listoptions();
- optioncaller = caller;
- }
-
- disp_option = 1;
- move_popup(listshell, caller);
- change_sensitive(caller, FALSE);
- XtMapWidget(listshell);
- XtPopup(listshell, XtGrabNonexclusive);
- }
-
- /*********************************
- * routines for scanning the directory *
- ***********************************/
-
- free_direct(dfiles, numdfiles)
- struct afile ***dfiles;
- int *numdfiles;
- {
- register int i;
- register struct afile **listfiles = *dfiles;
-
- if (listfiles != (struct afile **)NULL) {
- for (i = 0; i < *numdfiles; i++) free(listfiles[i]);
- *numdfiles = 0;
- free(listfiles);
- *dfiles = (struct afile **)NULL;
- }
- }
-
-
- /* ARGSUSED */
- int prepare_list(dirname, dir, dfiles, numdfiles, nameprepend)
- char * dirname;
- short dir;
- struct afile ***dfiles;
- int *numdfiles;
- char *nameprepend;
- {
- if (dir != 0) direction = dir;
- if ( (*numdfiles = my_scandir(dirname, dfiles, getfiles,
- sortfunc[currentsort-1], nameprepend) ) < 0) {
- disp_message("\nList: current directory?");
- return (-1);
- }
- return(0);
- }
-
- /* ARGSUSED */
- getfiles(dp)
- register struct direct *dp;
- {
- if (!currentdotfiles && (dp->d_name)[0] == '.') return 0;
- if (! strcmp(dp->d_name, ".") || ! strcmp(dp->d_name, "..")) return 0;
- if (re_exec(dp->d_name)) return 1;
- else return 0;
- }
-
- /* ARGSUSED */
- my_scandir(dirname, namelist, select, dcomp, nameprepend)
- char *dirname;
- struct afile *(*namelist[]);
- int (*select)(), (*dcomp)();
- char *nameprepend;
- {
- register struct direct *d;
- register struct afile *p, **names;
- register int j;
- int nitems, cc;
- register char *cp1, *cp2;
- struct stat stb;
- struct stat fbuf;
- u_short elemlength;
- long arraysz;
- DIR *dirp;
- char path[MAXNAME + 1];
- int prelength = (nameprepend == NULL) ? 0 : (strlen(nameprepend)+1);
-
- if ((dirp = opendir(dirname)) == NULL)
- return(-1);
- if (fstat(dirp->dd_fd, &stb) < 0)
- return(-1);
-
- /*
- * estimate the array size by taking the size of the directory file
- * and dividing it by a multiple of the minimum size entry.
- */
- arraysz = (stb.st_size / 24);
- names = (struct afile **)XtMalloc(arraysz * sizeof(struct afile *));
-
- nitems = 0;
- while ((d = readdir(dirp)) != NULL) {
- if (select != NULL && !(*select)(d))
- continue; /* just selected names */
- /*
- * Make a minimum size copy of the data
- */
- elemlength = MYDIRSIZ(d, prelength);
- p = (struct afile *)XtMalloc(elemlength);
- p->d_marked = 0;
- p->d_ino = d->d_ino;
- p->d_reclen = elemlength;
- p->d_namlen = d->d_namlen + prelength;
- sprintf(path, "%s/%s", dirname, d->d_name);
-
- if (nameprepend != (char *)NULL) {
- for (cp1 = p->d_name, cp2 = nameprepend;
- *cp1++ = *cp2++; );
- *(cp1 - 1) = '/';
- for (cp2 = d->d_name; *cp1++ = *cp2++; );
- if (stat(path, &fbuf) ) return(-1);
- /* no stats file is found */
- }
- else {
- for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
-
- if (lstat(path, &fbuf) ) {
- disp_message("\nList: stats file for %s", d->d_name);
- return(-1);
- }
- }
-
- p->d_size = fbuf.st_size;
- p->d_nlink = fbuf.st_nlink;
-
- switch(fbuf.st_mode & S_IFMT) {
- case S_IFDIR: p->d_type = 'd'; break;
- case S_IFBLK: p->d_type = 'b'; p->d_size = fbuf.st_rdev; break;
- case S_IFCHR: p->d_type = 'c'; p->d_size = fbuf.st_rdev; break;
- case S_IFSOCK: p->d_type = 's'; p->d_size = 0; break;
- case S_IFLNK: p->d_type = 'l'; break;
- default: p->d_type = '-'; break;
- }
-
- j = 0;
- if (fbuf.st_mode & 0400)
- p->d_access[j++] = 'r'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0200)
- p->d_access[j++] = 'w'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0100)
- p->d_access[j++] = 'x'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0040)
- p->d_access[j++] = 'r'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0020)
- p->d_access[j++] = 'w'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0010)
- p->d_access[j++] = 'x'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0004)
- p->d_access[j++] = 'r'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0002)
- p->d_access[j++] = 'w'; else p->d_access[j++] = '-';
- if (fbuf.st_mode & 0001)
- p->d_access[j++] = 'x'; else p->d_access[j++] = '-';
- p->d_access[j] = '\0';
-
- p->d_ctime = fbuf.st_ctime;
- p->d_uid = fbuf.st_uid;
- p->d_gid = fbuf.st_gid;
-
- /*
- * Check to make sure the array has space left and
- * realloc the maximum size.
- */
- if (++nitems >= arraysz) {
- if (fstat(dirp->dd_fd, &stb) < 0)
- return(-1); /* just might have grown */
- arraysz = stb.st_size / 12;
- names = (struct afile **)XtRealloc((char *)names,
- arraysz * sizeof(struct afile *));
- }
- names[nitems-1] = p;
- }
- closedir(dirp);
- if (nitems && dcomp != NULL)
- qsort(names, nitems, sizeof(struct afile *), dcomp);
- *namelist = names;
- return(nitems);
- }
-
- /*
- * Alphabetic order comparison routine
- */
- /* ARGSUSED */
- int my_alphasort(d1, d2)
- struct afile **d1, **d2;
- {
- return(direction * strcmp((*d1)->d_name, (*d2)->d_name));
- }
-
- /*
- * file size order comparison routine
- */
- /* ARGSUSED */
- int filesizesort(d1, d2)
- struct afile **d1, **d2;
- {
- int i;
-
- if ( (*d1)->d_size < (*d2)->d_size ) i = (int)direction * (-1);
- else if ( (*d1)->d_size > (*d2)->d_size ) i = (int)direction;
- else i = 0;
- return(i);
- }
-
- /*
- * file date order comparison routine
- */
- /* ARGSUSED */
- int datesort(d1, d2)
- struct afile **d1, **d2;
- {
- int i;
-
- if ( (*d1)->d_ctime < (*d2)->d_ctime ) i = (int)direction * (-1);
- else if ( (*d1)->d_ctime > (*d2)->d_ctime ) i = (int)direction;
- else i = 0;
- return(i);
- }
-