home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / mq / from.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-05  |  6.6 KB  |  297 lines

  1. /*
  2.  * usage:
  3.  *      from [-f] [file] [-d] [-w] [-s sender] [-u user]
  4.  *
  5.  * caveats/comments:
  6.  *    no checking is done w.r.t. what other programs are doing to the 
  7.  *    mailboxes.
  8.  *
  9.  * author:
  10.  *    Kevin Sweet ---------------------------------- sweet@scubed.arpa
  11.  *    S-CUBED, 3398 Carmel Mountain,  San Diego, CA 92121-1095
  12.  *    (home)  12205 Carmel Vista 242, San Diego, CA 92130-2237
  13.  *    ------- [ames,att,harvard,rutgers,ucsd,uunet]scubed!sweet ------
  14.  *
  15.  * This work is copyright 1988 by Kevin Sweet. Permission is granted 
  16.  * to modify and distribute this work under the provision that the 
  17.  * author is informed of any non-cosmetic changes to the code and that 
  18.  * the author's name remain part of the documentaion. 
  19.  */
  20.  
  21. #ifdef SYSV
  22. # define MAXPATHLEN    1024
  23. # include <string.h>
  24. # define index        strchr
  25. # define rindex        strrchr
  26. #else
  27. # include <sys/param.h>
  28. # include <strings.h>
  29. #endif
  30. #include <stdio.h>
  31.  
  32. extern char *getenv();
  33. static char *getmbox();
  34. static int gethelp();
  35.  
  36. #define FORWARD        0
  37. #define LSIZE        256
  38. #define NUMQ        256
  39. #define SWITCHAR    '-'
  40.  
  41. struct mbox {
  42.     char from[LSIZE];
  43.     char subject[LSIZE];
  44. };
  45.  
  46. main(argc, argv)
  47. int argc;
  48. char *argv[];
  49. {
  50.     FILE *fp;
  51.     char line[LSIZE], mbox[MAXPATHLEN], path[LSIZE], sender[LSIZE];
  52.     register char *bang, *cp;
  53.     register int DATE, SENDER, WIDE, 
  54.                  from, i, j, left, mcnt, subject;
  55.     struct mbox mail[NUMQ];
  56.  
  57.     /* find the mail file to list along with any options
  58.      */
  59.     strcpy(mbox, "\0");
  60.     DATE = SENDER = WIDE = 0;
  61.     for (i = 1, cp = argv[1]; i < argc; i++, cp = argv[i]) {
  62.  
  63.         if (cp[0] == SWITCHAR) 
  64.         switch (cp[1]) {
  65.         case 'h': case 'H': 
  66.             gethelp(argv[0], 0);
  67.         case 'd': case 'D': 
  68.             DATE = 1;
  69.             break;
  70.         case 'w': case 'W': 
  71.             WIDE ++;
  72.             if (cp[2] == 'w' || cp[2] == 'W') WIDE++;
  73.             break;
  74.         case 'f': case 'F': 
  75.             if (cp[2]) 
  76.                 strncpy(mbox, &argv[i][2], MAXPATHLEN);
  77.             else 
  78.             if (i != argc-1 && argv[i+1][0] != SWITCHAR) {
  79.                 strncpy(mbox, argv[i+1], MAXPATHLEN);
  80.                 i++;
  81.             }
  82.             else 
  83.             strncpy(mbox, getmbox(), MAXPATHLEN);
  84.             break;
  85.         case 's': case 'S': 
  86.             if (cp[2]) 
  87.                 strncpy(sender, &argv[i][2], LSIZE);
  88.             else 
  89.             if (i != argc-1 && argv[i+1][0] != SWITCHAR) {
  90.                 strncpy(sender, argv[i+1], LSIZE);
  91.                 i++;
  92.             }
  93.             else 
  94. #ifdef SYSV
  95.             strncpy(sender, getenv("LOGNAME"), LSIZE);
  96. #else
  97.             strncpy(sender, getenv("USER"), LSIZE);
  98. #endif
  99.             SENDER = strlen(sender);
  100.             break;
  101.         case 'u': case 'U': 
  102.             if (cp[2]) {
  103.                 strncpy(mbox, MAILDIR, MAXPATHLEN);
  104.                 strncat(mbox, &argv[i][2], 
  105.                         MAXPATHLEN - strlen(mbox));
  106.             }
  107.             else 
  108.             if (i != argc-1 || argv[i+1][0] != SWITCHAR) {
  109.                 strncpy(mbox, MAILDIR, MAXPATHLEN);
  110.                 strncat(mbox, argv[i+1], 
  111.                         MAXPATHLEN - strlen(mbox));
  112.                 i++;
  113.             }
  114.             else
  115.             strcpy(mbox, "\0");
  116.             break;
  117.         default: 
  118.             printf("invalid option '%c': ", cp[1]);
  119.             gethelp(argv[0], -1);
  120.         }    /* end if/switch */
  121.         else 
  122.         strncpy(mbox, argv[i], MAXPATHLEN);
  123.  
  124.     }    /* end for */
  125.  
  126.     /* use the system mailbox if there are no arguments or if 
  127.      * there are any (fatal) errors
  128.      */
  129.     if (!strlen(mbox)) {
  130.         strncpy(mbox, MAILDIR, MAXPATHLEN);
  131. #ifdef SYSV
  132.         strncat(mbox, getenv("LOGNAME"), 
  133. #else
  134.         strncat(mbox, getenv("USER"), 
  135. #endif
  136.                 MAXPATHLEN - strlen(mbox));
  137.     } 
  138.  
  139.     /* if we can't open the mailbox, exit quitely
  140.      */
  141.     fp = fopen(mbox, "r");
  142.     if (!fp) exit(1);
  143.  
  144.     /* read in the sender and subject of each letter
  145.      */
  146.     mcnt = -1;
  147.     while ( cp = fgets(line, LSIZE, fp) ) {
  148.         cp[strlen(line)-1] = '\0';
  149.         if (!strncmp(line, "From ", 5)) {
  150.             if (mcnt >= 0 && !strlen(mail[mcnt].from)) 
  151.                strcpy(mail[mcnt].from, path);
  152.             strcpy(path, index(line, ' ')+1);
  153.             if (!DATE) {
  154.                 cp = index(path, ' ');
  155.                 if (cp) cp[0] = '\0';
  156.             }
  157.             if (SENDER) {
  158.                 from = 1;
  159.                 subject = 1;
  160.                 cp = path;
  161.                 j = 0;
  162.                 for (; *cp; *cp++) 
  163.                 if (!strncmp(cp, sender, SENDER)) j++;
  164.             } else 
  165.             j = 1;
  166.             if (j) {
  167.                 mcnt++;
  168.                 from = 0;
  169.                 subject = 0;
  170.             }
  171.         } else 
  172.         if (!from && ( !strncmp(line, "From: ", 6) || 
  173.             !strncmp(line+1, "From: ", 6) )) {
  174.             if (!FORWARD) from = 1;
  175.             if (DATE || WIDE > 1) {
  176.                 strcpy(mail[mcnt].from, path);
  177.                 continue;
  178.             }
  179.             cp = index(line, '<');
  180.             if (cp) *cp++;
  181.             else cp = index(line, ' ')+1;
  182.             strcpy(mail[mcnt].from, cp);
  183.             cp = index(mail[mcnt].from, '>');
  184.             if (!cp) cp = index(mail[mcnt].from, ' ');
  185.             if (cp) cp[0] = '\0';
  186.         } else 
  187.         if (!subject && !strncmp(line, "Subject: ", 9)) {
  188.             if (!FORWARD) subject = 1;
  189.             strcpy(mail[mcnt].subject, index(line, ' ')+1);
  190.             if (DATE && !WIDE) 
  191.                strcpy(mail[mcnt].subject, "\0");
  192.         } 
  193.     }
  194.     if (!strlen(mail[mcnt].from)) strcpy(mail[mcnt].from, path);
  195.  
  196.     /* display each entry: 
  197.      * if this isn't a wide list, use the shortest available name 
  198.      * for the sender and make sure that the subject does not 
  199.      * wrap 80 columns 
  200.      */
  201.     for (i = 0; i <= mcnt; i++) {
  202.         cp = index(mail[i].from, '!');
  203.         bang = rindex(mail[i].from, '!');
  204.         if (!DATE && WIDE < 2) {
  205.             if (!bang || !strcmp(cp, bang)) 
  206.                cp = mail[i].from;
  207.             else 
  208.             {
  209.                 cp++;
  210.                 while (strcmp(index(cp, '!'), bang)) {
  211.                     cp = index(cp, '!');
  212.                     cp++;
  213.                 }    /* end while */
  214.             }
  215.         } else 
  216.                cp = mail[i].from;
  217.         left = 71 - (int) strlen(cp);
  218.         printf("From %s", cp);
  219.         if (DATE && !WIDE) putchar('\n');
  220.         else 
  221.         if (strlen(mail[i].subject)) {
  222.             printf(", ");
  223.             if (!WIDE) {
  224.                 putchar('"');
  225.                 cp = mail[i].subject;
  226.                 for (j = 0; j < left; j++) 
  227.                     if (*cp) putchar(*cp++);
  228.                 printf("\"\n");
  229.             } else 
  230.             printf("\"%s\"\n", mail[i].subject);
  231.         } 
  232.         else
  233.         printf(", (no subject)\n");
  234.     } 
  235.  
  236.     fclose(fp);
  237. }
  238.  
  239. static 
  240. char *
  241. getmbox()
  242. {
  243.     FILE *fp;
  244.     char line[LSIZE], mailrc[MAXPATHLEN], mbox[MAXPATHLEN];
  245.     register char *cp;
  246.  
  247.     /* find out where to look for mail startup file
  248.      */
  249.     strncpy(mailrc, getenv("HOME"), MAXPATHLEN);
  250.     strncat(mailrc, "/.mailrc", MAXPATHLEN - strlen(mailrc));
  251.     if (getenv("MAILRC")) 
  252.         strncpy(mailrc, getenv("MAILRC"), MAXPATHLEN);
  253.  
  254.     /* find out where to look for the default mailbox
  255.      */
  256.     strncpy(mbox, getenv("HOME"), MAXPATHLEN);
  257.     strncat(mbox, "/mbox", MAXPATHLEN - strlen(mbox));
  258.     fp = fopen(mailrc, "r");
  259.     if (fp) {
  260.         while ( cp = fgets(line, LSIZE, fp) ) {
  261.             cp[strlen(line)-1] = '\0';
  262.             if (strncmp(line, "set MBOX=", 9)) continue;
  263.             cp += 9;
  264.             strncpy(mbox, cp, MAXPATHLEN);
  265.             break;
  266.         }
  267.         fclose(fp);
  268.     }
  269.     if (getenv("MBOX")) 
  270.         strncpy(mbox, getenv("MBOX"), MAXPATHLEN);
  271.  
  272.     return(mbox);
  273. }
  274.  
  275. /* the help/usage message 
  276.  */
  277. static 
  278. int 
  279. gethelp(program, exit_status)
  280. char *program;
  281. int exit_status;
  282. {
  283.     register char *name;
  284.  
  285.     name = rindex(program, '/');
  286.     if (name) *name++;
  287.     else {
  288.         name = rindex(program, '\\');
  289.         if (name) *name++;
  290.         else name = program;
  291.     }
  292.     printf("usage: %s [-f] [file] ", name);
  293.     printf("[-d] [-w] [-s sender] [-u user]\n");
  294.     exit(exit_status);
  295. }
  296.  
  297.