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 <ctype.h>
- #include <pwd.h>
- #include <grp.h>
-
- #include <sys/file.h>
-
- #include "xfilebrowser.h"
-
- extern char *getenv();
- extern struct passwd *getpwuid();
- extern struct passwd *getpwnam();
- extern struct group *getgrgid();
- extern struct tm *localtime();
-
- #define HALFYEAR 15000000L
-
- static char *months[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
- "Nov", "Dec", (char *)NULL };
-
- static struct _pwcache {
- u_short id; /* owner or group id */
- char name[32];
- } ownercache;
-
- reset_ownercache()
- {
- ownercache.name[0] = '\0';
- }
-
- /* ARGSUSED */
- int format_file(i, s, curtime)
- int i;
- char *s;
- long curtime;
- {
- char tmp[64];
- struct passwd *passname;
- struct group *groupname;
- struct tm *actime;
- struct afile *fp = files[i];
-
- switch (currentid) {
- case 2:
- if (ownercache.name[0] == '\0' || fp->d_gid != ownercache.id) {
- if ( (groupname = getgrgid(fp->d_gid)) == NULL) {
- disp_message("\nList: Group of %s not registered",
- fp->d_name);
- return(-1);
- }
- strcpy(ownercache.name, groupname->gr_name);
- ownercache.id = fp->d_gid;
- }
- break;
-
- default:
- if (ownercache.name[0] == '\0' || fp->d_uid != ownercache.id) {
- if ( (passname = getpwuid(fp->d_uid)) == NULL) {
- disp_message("\nList: Owner of %s not registered",
- fp->d_name);
- return(-1);
- }
- strcpy(ownercache.name, passname->pw_name);
- ownercache.id = fp->d_uid;
- }
- break;
- }
-
- sprintf(s, "%c%s %2d %-8s %8d",
- fp->d_type, fp->d_access, fp->d_nlink, ownercache.name,
- fp->d_size);
- actime = localtime(&(fp->d_ctime));
- if ( (curtime - fp->d_ctime) > HALFYEAR)
- sprintf(tmp, " %-3s %-2d %5d %s\n",
- months[actime->tm_mon], actime->tm_mday,
- 1900+actime->tm_year, fp->d_name);
- else sprintf(tmp, " %-3s %-2d %02d:%02d %s\n",
- months[actime->tm_mon], actime->tm_mday,
- actime->tm_hour, actime->tm_min,
- fp->d_name);
- strcat(s, tmp);
- return 0;
- }
-
- #define BUFSIZE 64 /* assume this as normal length of a file format */
-
- int display_files(position)
- int position;
- {
- int i, length, buflength, curlength = 0;
- long curtime;
- XtTextBlock text;
- XtTextPosition start, end, oldend;
- char *buf;
- char tmpbuf[2*MAXNAME];
-
- if (numfiles == 0) {
- disp_message("\nList: no files found");
- clear_widget(listwidget, listsource);
- return(-1);
- }
-
- curtime = time(0);
- oldend =
- (*listsource->Scan)(listsource, 0, XtstAll, XtsdRight, 1, 0);
- XtTextUnsetSelection(listwidget);
-
- start = 0;
- buflength = BUFSIZE * numfiles;
- buf = XtMalloc(buflength);
- *buf = '\0';
-
- for (i = 0; i < numfiles; i++) {
- format_file(i, tmpbuf, curtime);
- curlength += length = strlen(tmpbuf);
- if ( (curlength) >= buflength) {
- buflength += 1096;
- buf = XtRealloc(buf, buflength);
- }
-
- strcat(buf, tmpbuf);
- files[i]->d_pos1 = start;
- start = end = start + length;
- files[i]->d_pos2 = end;
- }
-
- allowedit = 1;
- XtTextDisableRedisplay(listwidget);
-
- text.length = curlength;
- text.ptr = buf;
- text.firstPos = 0;
- XtTextReplace(listwidget, 0, oldend, &text);
- allowedit = 0;
-
- XtFree(buf);
- XtTextSetInsertionPoint(listwidget, position);
- XtTextEnableRedisplay(listwidget);
- return(0);
- }
-
-
- /* ARGSUSED */
- char *get_userdir(user)
- char *user;
- {
- struct passwd *object;
-
- if ( (object = getpwnam(user)) == NULL) {
- disp_message("\n: user name not found");
- return(NULL);
- }
- else
- return(object->pw_dir);
- }
-
- /* ARGSUSED */
- int separate_dirpat(s, dir, pattern)
- char *s, *dir, *pattern;
- {
- /* if no pattern is found a "*" pattern is returned */
- int cc;
- char *p;
- struct stat statbuf;
-
- cc = stat(s, &statbuf);
- if (cc != -1 && ( (statbuf.st_mode & S_IFMT) == S_IFDIR) )
- strcpy(pattern, "*");
- else {
- p = rindex(s, '/');
- strcpy(pattern, p+1);
- /* check if we are at the root directory */
- if (p != s) *p = '\0'; else *(p+1) = '\0';
- }
- if (access(s, X_OK)) { *pattern = '\0'; return(-1); }
- else { strcpy(dir, s); return(0); }
- }
-
-
- /* ARGSUSED */
- int get_dirpat(s, dir, pattern)
- char *s, *dir, *pattern;
- {
- char *cwd, *p;
- char directory[255];
-
- if (s == NULL || !(*s) ) {
- if ( (cwd = getcwd((char *)NULL, 255)) == (char *)NULL) {
- disp_message("\ncannot get working directory");
- return(-1);
- }
- else { strcpy(dir, cwd); strcpy(pattern, "*"); return(0); }
- }
-
- if (s[0] == '~') {
- /* expand tilde with the user directory as defined in password
- file */
- if ( (p = expand_tilde(s)) == (char *) NULL) return(-1);
- strcpy(directory, p);
- XtFree(p);
- }
- else if (s[0] == '/') {
- /* full pathname specified */
- strcpy(directory, s);
- }
- else {
- /* use the current directory setting */
- if (strlen(curdirectory) == 1)
- sprintf(directory, "/%s", s);
- else sprintf(directory, "%s/%s", curdirectory, s);
- }
- return(separate_dirpat(directory, dir, pattern));
- }
-
-
- /* ARGSUSED */
- int select_file(start, end)
- int start, end;
- {
- int i = 0;
-
- while (i < numfiles && files[i]->d_pos1 <= start) i++;
- if ( (files[i-1]->d_pos2 + 1) < end) return(-1);
- else return(i-1);
- }
-
- /* ARGSUSED */
- int select_files(start, end, fstart, fend)
- int start, end;
- int *fstart, *fend;
- {
- int i = 0;
-
- while (i < numfiles && files[i]->d_pos1 < start) i++;
- if (i == numfiles) return(-1); else *fstart = i;
-
- while ( (i < numfiles - 1) && (files[i]->d_pos2 + 1 < end) ) i++;
-
- *fend = i;
- return(0);
- }
-