home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume2 / xbrowser / part03 / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-03  |  5.7 KB  |  260 lines

  1. /* Systems Sciences Laboratory, Webster Research Center */
  2.  
  3. static char *PROGRAM_information[] =
  4. {
  5.     "Copyright (c) 1988 Xerox Corporation.  All rights reserved.",
  6.     "$Header$",
  7.     "$Locker$"
  8. }
  9. ;
  10.  
  11. /*
  12.  * Copyright protection claimed includes all forms and matters of copyrightable
  13.  * material and information now allowed by statutory or judicial lay or
  14.  * herinafter granted, including without limitation, material generated from
  15.  * the software programs which are displayed on the screen such as icons,
  16.  * screen display looks, etc.
  17.  */
  18.  
  19. #include <ctype.h>
  20. #include <pwd.h>
  21. #include <grp.h>
  22.  
  23. #include <sys/file.h>
  24.  
  25. #include "xfilebrowser.h"
  26.  
  27. extern char *getenv();
  28. extern struct passwd *getpwuid();
  29. extern struct passwd *getpwnam();
  30. extern struct group *getgrgid();
  31. extern struct tm *localtime();
  32.  
  33. #define HALFYEAR     15000000L
  34.  
  35. static char *months[] = {
  36.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
  37.     "Nov", "Dec", (char *)NULL };
  38.  
  39. static struct _pwcache {
  40.     u_short id;        /* owner or group id */
  41.     char name[32];
  42. } ownercache;
  43.  
  44. reset_ownercache()
  45. {
  46.     ownercache.name[0] = '\0';
  47. }
  48.  
  49. /* ARGSUSED */
  50. int format_file(i, s, curtime)
  51. int i;
  52. char *s;
  53. long curtime;
  54. {
  55.     char tmp[64];
  56.     struct passwd *passname;
  57.     struct group *groupname;
  58.     struct tm *actime;
  59.     struct afile *fp = files[i];
  60.  
  61.     switch (currentid) {
  62.     case 2:
  63.         if (ownercache.name[0] == '\0' || fp->d_gid != ownercache.id) {
  64.            if ( (groupname = getgrgid(fp->d_gid)) == NULL) {
  65.                disp_message("\nList: Group of %s not registered",
  66.                fp->d_name);
  67.                return(-1);
  68.            }
  69.            strcpy(ownercache.name, groupname->gr_name);
  70.            ownercache.id = fp->d_gid;
  71.         }
  72.         break;
  73.  
  74.     default:
  75.         if (ownercache.name[0] == '\0' || fp->d_uid != ownercache.id) {
  76.            if ( (passname = getpwuid(fp->d_uid)) == NULL) {
  77.                   disp_message("\nList: Owner of %s not registered",
  78.                fp->d_name);
  79.                   return(-1);
  80.            }
  81.            strcpy(ownercache.name, passname->pw_name);
  82.            ownercache.id = fp->d_uid;
  83.         }
  84.         break;
  85.     }
  86.  
  87.     sprintf(s, "%c%s %2d %-8s %8d", 
  88.         fp->d_type, fp->d_access, fp->d_nlink, ownercache.name,
  89.         fp->d_size);
  90.     actime = localtime(&(fp->d_ctime));
  91.     if ( (curtime - fp->d_ctime) > HALFYEAR)
  92.         sprintf(tmp, " %-3s %-2d %5d %s\n",
  93.             months[actime->tm_mon], actime->tm_mday,
  94.             1900+actime->tm_year, fp->d_name);
  95.     else     sprintf(tmp, " %-3s %-2d %02d:%02d %s\n",
  96.             months[actime->tm_mon], actime->tm_mday,
  97.             actime->tm_hour, actime->tm_min,
  98.             fp->d_name);
  99.     strcat(s, tmp);
  100.     return 0;
  101. }
  102.  
  103. #define BUFSIZE 64    /* assume this as normal length of a file format */
  104.  
  105. int display_files(position)
  106. int position;
  107. {
  108.     int i, length, buflength, curlength = 0;
  109.     long curtime;
  110.     XtTextBlock text;
  111.     XtTextPosition start, end, oldend;
  112.     char *buf;
  113.     char tmpbuf[2*MAXNAME];
  114.  
  115.     if (numfiles == 0) {
  116.        disp_message("\nList: no files found");
  117.        clear_widget(listwidget, listsource);
  118.        return(-1);
  119.     }
  120.  
  121.     curtime = time(0);
  122.     oldend = 
  123.        (*listsource->Scan)(listsource, 0, XtstAll, XtsdRight, 1, 0);
  124.     XtTextUnsetSelection(listwidget);
  125.  
  126.     start = 0;
  127.     buflength = BUFSIZE * numfiles;
  128.     buf = XtMalloc(buflength);
  129.     *buf = '\0';
  130.  
  131.     for (i = 0; i < numfiles; i++) {
  132.        format_file(i, tmpbuf, curtime);
  133.        curlength += length = strlen(tmpbuf);
  134.        if ( (curlength) >= buflength) {
  135.            buflength += 1096;
  136.            buf = XtRealloc(buf, buflength);
  137.        }
  138.  
  139.        strcat(buf, tmpbuf);
  140.        files[i]->d_pos1 = start;
  141.        start = end = start + length;
  142.        files[i]->d_pos2 = end;
  143.     }
  144.  
  145.     allowedit = 1;
  146.     XtTextDisableRedisplay(listwidget);
  147.  
  148.     text.length = curlength;
  149.     text.ptr = buf;
  150.     text.firstPos = 0;
  151.     XtTextReplace(listwidget, 0, oldend, &text);
  152.     allowedit = 0;
  153.  
  154.     XtFree(buf);
  155.     XtTextSetInsertionPoint(listwidget, position);
  156.     XtTextEnableRedisplay(listwidget);
  157.     return(0);
  158. }
  159.  
  160.  
  161. /* ARGSUSED */
  162. char *get_userdir(user)
  163. char *user;
  164. {
  165.     struct passwd *object;
  166.  
  167.     if ( (object = getpwnam(user)) == NULL) {
  168.        disp_message("\n: user name not found");
  169.        return(NULL);
  170.     }
  171.     else 
  172.        return(object->pw_dir);
  173. }
  174.  
  175. /* ARGSUSED */
  176. int separate_dirpat(s, dir, pattern)
  177. char *s, *dir, *pattern;
  178. {
  179.         /* if no pattern is found a "*" pattern is returned */
  180.     int cc;
  181.     char *p;
  182.     struct stat statbuf;
  183.  
  184.     cc = stat(s, &statbuf);
  185.     if (cc != -1 && ( (statbuf.st_mode & S_IFMT) == S_IFDIR) )
  186.        strcpy(pattern, "*");
  187.     else {
  188.        p = rindex(s, '/');
  189.        strcpy(pattern, p+1);
  190.         /* check if we are at the root directory */
  191.        if (p != s) *p = '\0'; else *(p+1) = '\0';
  192.     }
  193.     if (access(s, X_OK)) { *pattern = '\0'; return(-1); }
  194.     else { strcpy(dir, s); return(0); }
  195. }
  196.  
  197.  
  198. /* ARGSUSED */
  199. int get_dirpat(s, dir, pattern)
  200. char *s, *dir, *pattern;
  201. {
  202.     char *cwd, *p;
  203.     char directory[255];
  204.  
  205.     if (s == NULL || !(*s) ) {
  206.        if ( (cwd = getcwd((char *)NULL, 255)) == (char *)NULL) {
  207.              disp_message("\ncannot get working directory");
  208.              return(-1);
  209.        }
  210.        else { strcpy(dir, cwd); strcpy(pattern, "*"); return(0); }
  211.     }
  212.  
  213.     if (s[0] == '~') {
  214.         /* expand tilde with the user directory as defined in password
  215.             file */
  216.        if ( (p = expand_tilde(s)) == (char *) NULL) return(-1);
  217.        strcpy(directory, p);
  218.        XtFree(p);
  219.     }
  220.     else if (s[0] == '/') {
  221.         /* full pathname specified */
  222.        strcpy(directory, s);
  223.     }
  224.     else {
  225.         /* use the current directory setting */
  226.        if (strlen(curdirectory) == 1)
  227.            sprintf(directory, "/%s", s);
  228.        else sprintf(directory, "%s/%s", curdirectory, s);
  229.     }
  230.     return(separate_dirpat(directory, dir, pattern));
  231. }
  232.  
  233.  
  234. /* ARGSUSED */
  235. int select_file(start, end)
  236. int start, end;
  237. {
  238.     int i = 0;
  239.  
  240.     while (i < numfiles && files[i]->d_pos1 <= start) i++; 
  241.     if ( (files[i-1]->d_pos2 + 1) < end) return(-1);
  242.     else return(i-1);
  243. }
  244.  
  245. /* ARGSUSED */
  246. int select_files(start, end, fstart, fend)
  247. int start, end;
  248. int *fstart, *fend;
  249. {
  250.     int i = 0;
  251.  
  252.     while (i < numfiles && files[i]->d_pos1 < start) i++; 
  253.     if (i == numfiles) return(-1); else *fstart = i;
  254.  
  255.     while ( (i < numfiles - 1) && (files[i]->d_pos2 + 1 < end) ) i++;
  256.  
  257.     *fend = i;
  258.     return(0);
  259. }
  260.