home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / FOLDERS.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  6KB  |  234 lines

  1. /*
  2.    folders.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Derived from bm.
  5.  
  6.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  7.    Permission granted for non-commercial copying and use, provided
  8.    this notice is retained.
  9.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  10.    Permission granted for non-commercial copying and use, provided
  11.    this notice is retained.
  12.  
  13.    911222 : Added this header.
  14.    920601 : Added test program to display info on folders - define FOLDER_PROG
  15.    920604 : folders.exe, release v0.1
  16.    920610 : release v0.2, stars public areas
  17.    920616 : release v0.3, simpler date, flag locked folders
  18.    920708 : v0.5, cleanup for release
  19.    920715 : Cleanup bmh v0.1 release, fixed month display
  20.    920719 : Added recursive search of mail directory for news files
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <io.h>
  27. #include <time.h>
  28. #include "pc.h"
  29. #include "rc.h"
  30. #include "lock.h"
  31. #include "misc.h"
  32. #include "help.h"
  33. #include "pager.h"
  34.  
  35. #ifdef BMH
  36. #define main folders_main
  37. #endif
  38.  
  39. /* undef this to make dates appear in the form <Month>/<Day>/<Year>
  40.    This should go into bm.rc
  41. */
  42. #define DAY_FIRST
  43.  
  44. static const char PublicMark = '*';
  45. static const int MaxAreas = 25;
  46. static const int AreaLength = 20;
  47.  
  48. static char *areas = NULL;
  49. static int numareas = 0;
  50.  
  51. static int listfolders_long(char *maildir, char *prefix);
  52.  
  53. static int
  54. readareas(char *dir)
  55. {
  56.    char s[256], *p, *cp;
  57.    FILE *fp;
  58.  
  59.    if ( (dir == NULL) || (areas != NULL) )
  60.       return -1;
  61.  
  62.    if ( (areas = (char *) malloc(sizeof(char)*AreaLength*MaxAreas)) == NULL) {
  63.       fprintf(stderr, "folders: warning no room to hold public areas\n");
  64.       return -1;
  65.       }
  66.  
  67.    sprintf(s, "%s/areas", dir);
  68.    if ((fp=fopen(s, "r")) == NULL) {
  69.       free (areas);
  70.       areas = NULL;
  71.       return -1;
  72.       }
  73.       
  74.    numareas = 0;
  75.    p = areas;
  76.    while (fgets(s, sizeof(s), fp) != NULL) {
  77.       if ( (*s=='\0') || (*s==' ') || (*s=='\n') || (*s=='\t') )
  78.          continue;
  79.       s[9] = '\0';
  80.       cp = s;
  81.  
  82.       while (*cp)
  83.          if ( (*cp == ' ') || (*cp == '\t') )
  84.             *cp = '\0';
  85.          else
  86.             cp++;
  87.  
  88.       strncpy(p, s, AreaLength);
  89.       p[AreaLength-1] = '\0';
  90.       numareas++;
  91.       p+=AreaLength;
  92.       }
  93.    fclose(fp);
  94.    qsort(areas, numareas, AreaLength, strcmp);
  95.    return 0;
  96. }
  97.  
  98. static int
  99. ispublic(char *s)
  100. {
  101.    return bsearch(s, areas, numareas, AreaLength, strcmp) != 0;
  102. }
  103.  
  104. static void
  105. listfolders_short(char *maildir)
  106. {
  107.    char buf[256], *cp, *cp1;
  108.    Directory d;
  109.  
  110.    sprintf(buf, "%s/*%s", maildir, EXT);
  111.  
  112.    initdir(&d);
  113.  
  114.    while( ( cp1 = readdir(&d, buf) ) != NULL ) {
  115.       cp = strchr(cp1,'.');
  116.       *cp = '\0';
  117.       printf("%10s", cp1);
  118.    }
  119. }
  120.  
  121. static char *
  122. bmtime(const time_t *t, char *s)
  123. {
  124.    struct tm *tmp = localtime(t);
  125.  
  126.    sprintf(s, "%2d/%02d/%2d",
  127. #ifdef DAY_FIRST
  128.               tmp->tm_mday, tmp->tm_mon+1,
  129. #else
  130.               tmp->tm_mon+1,  tmp->tm_mday,
  131. #endif
  132.               tmp->tm_year);
  133.    return s;
  134. }
  135.  
  136. static int
  137. listnews(char *maildir, char *prefix)
  138. {
  139.    char buf[256], *cp1, newmaildir[256], newprefix[256];
  140.    Directory d;
  141.  
  142.    if ( listfolders_long(maildir, prefix) == -1)
  143.       return -1;
  144.  
  145.    sprintf(buf, "%s/*.  ", maildir);
  146.  
  147.    initdir(&d);
  148.  
  149.    while( ( cp1 = readdir(&d, buf) ) != NULL ) {
  150.       struct stat sbuf;
  151.  
  152.       sprintf(buf, "%s/%s", maildir, cp1);
  153.       if ( stat(buf, &sbuf) == -1) {
  154.          fprintf(stderr, "\nfolders: warning can't stat %s\n", buf);
  155.          return -1;
  156.          }
  157.       if (!(sbuf.st_mode & S_IFDIR) || (*cp1 == '.'))
  158.          continue;
  159.  
  160.       sprintf(newmaildir, "%s/%s", maildir, cp1);
  161.       sprintf(newprefix, "%s%s.", prefix, cp1);
  162.  
  163.       if ( listnews(newmaildir, newprefix) == -1)
  164.          return -1;
  165.    }
  166.    return 0;
  167. }
  168.  
  169. static int
  170. listfolders_long(char *maildir, char *prefix)
  171. {
  172.    char buf[256], *cp, *cp1, date[10], line[256], name[256];
  173.    Directory d;
  174.  
  175.    sprintf(buf, "%s/*%s", maildir, EXT);
  176.  
  177.    page_setup();
  178.  
  179.    initdir(&d);
  180.  
  181.    while( ( cp1 = readdir(&d, buf) ) != NULL ) {
  182.       struct stat sbuf;
  183.  
  184.       sprintf(buf, "%s/%s", maildir, cp1);
  185.       if ( stat(buf, &sbuf) == -1) {
  186.          fprintf(stderr, "\nfolders: warning can't stat %s\n", buf);
  187.          return -1;
  188.          }
  189.  
  190.       if ( (cp = strchr(cp1, '.')) != NULL)
  191.          *cp = '\0';
  192.  
  193.       if (prefix[0] == '\0')
  194.          strcpy(name, cp1);
  195.       else 
  196.          sprintf(name, "%s%s", prefix, cp1);
  197.  
  198.       sprintf(buf, "%s/%s", maildir, cp1);
  199.       sprintf(line, " %c %s%-*s%8ld   %s  %c",
  200.          ispublic(name) == 0 ? ' ' : PublicMark,
  201.          prefix, 20 - strlen(prefix), cp1,
  202.          sbuf.st_size, bmtime(&sbuf.st_atime, date),
  203.          islocked(buf) ? 'L' : ' ');
  204.  
  205.       if (page_puts(line) == -1)
  206.          return -1;
  207.    }
  208.    return 0;
  209. }
  210.  
  211. int
  212. main(int argc, char *argv[])
  213. {
  214.    dohelp(argc, argv, "folders [-short - mail]");
  215.  
  216.    if (setupbm()==-1)
  217.       return -1;
  218.  
  219.    (void) readareas(getrc(spooldir));
  220.  
  221.    if (argc > 1) {
  222.       if (strcmp(argv[1], "-short") == 0)
  223.          listfolders_short(getrc(maildir));
  224.       else if (strcmp(argv[1], "-mail") == 0)
  225.          (void) listfolders_long(getrc(maildir), "");
  226.       else
  227.          fprintf(stderr, "folders: unknown option %s\n", argv[1]);
  228.       }
  229.    else 
  230.       (void) listnews(getrc(maildir), "");
  231.  
  232.    return 0;
  233. }
  234.