home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / newmail.c < prev    next >
C/C++ Source or Header  |  1992-03-22  |  21KB  |  792 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: newmail.c,v 4.1.1.3 90/12/05 15:05:39 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.3 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    newmail.c,v $
  17.  * Revision 4.1.1.3  90/12/05  15:05:39  syd
  18.  * Remove unused opterr variable, some getopts dont define it
  19.  * From: Syd via Peter King
  20.  *
  21.  * Revision 4.1.1.2  90/10/07  21:10:35  syd
  22.  * newmail did not correctly present sender name if the source
  23.  * of the mail is local from the system.
  24.  * From: JT McDuffie <guardian!jt@Sun.COM>
  25.  *
  26.  * Revision 4.1.1.1  90/10/07  19:43:44  syd
  27.  * Fixes when newmail detects that the mail folder has grown in size it prints a newline, even
  28.  * if there were no new subjects in the folder.
  29.  * From: Uwe Doering <gemini%geminix.mbx.sub.org@RELAY.CS.NET>
  30.  *
  31.  * Revision 4.1  90/04/28  22:44:48  syd
  32.  * checkin of Elm 2.3 as of Release PL0
  33.  *
  34.  *
  35.  ******************************************************************************/
  36.  
  37. /** This is actually two programs folded into one - 'newmail()' and
  38.     'wnewmail()'.  They perform essentially the same function, to
  39.     monitor the mail arriving in a set of/a mailbox or folder, but
  40.     newmail is designed to run in background on a terminal, and
  41.     wnewmail is designed to have a window of its own to run in.
  42.  
  43.     The main difference is that wnewmail checks for mail more often.
  44.  
  45.     The usage parameters are:
  46.  
  47.     -i <interval>          how often to check for mail
  48.                 (default: 60 secs if newmail,
  49.                       10 secs if wnewmail)
  50.  
  51.     <filename>        name of a folder to monitor
  52.                 (can prefix with '+'/'=', or can
  53.                  default to the incoming mailbox)
  54.  
  55.     <filename>=prefix    file to monitor, output with specified
  56.                 prefix when mail arrives.
  57.  
  58.     If we're monitoring more than one mailbox the program will prefix
  59.     each line output (if 'newmail') or each cluster of mail (if 'wnewmail')
  60.     with the basename of the folder the mail has arrived in.  In the
  61.     interest of exhaustive functionality, you can also use the "=prefix"
  62.     suffix (eh?) to specify your own strings to prefix messages with.
  63.  
  64.     The output format is either:
  65.  
  66.       newmail:
  67.          >> New mail from <user> - <subject>
  68.          >> Priority mail from <user> - <subject>
  69.  
  70.          >> <folder>: from <user> - <subject>
  71.          >> <folder>: Priority from <user> - <subject>
  72.  
  73.       wnewmail:
  74.          <user> - <subject>
  75.          Priority: <user> - <subject>
  76.  
  77.          <folder>: <user> - <subject>
  78.          <folder>: Priority: <user> - <subject>\fR
  79.  
  80. **/
  81.  
  82. #include <stdio.h>
  83. #include <errno.h>
  84. #include <sys/types.h>
  85. #include <sys/stat.h>
  86. #include <pwd.h>
  87.  
  88. #include "defs.h"
  89.  
  90. #include <signal.h>    /* background jobs ignore some signals... */
  91.  
  92. static char ident[] = { WHAT_STRING };
  93.  
  94. #define LINEFEED        (char) 10
  95. #define BEGINNING        0            /* seek fseek(3S) */
  96. #define DEFAULT_INTERVAL    60
  97.  
  98. #define MAX_FOLDERS        25        /* max we can keep track of */
  99.  
  100. #define NO_SUBJECT    "(No Subject Specified)"
  101.  
  102. #define metachar(c)    (c == '+' || c == '=' || c == '%')
  103.  
  104. char  *getusername();
  105. long  bytes();
  106.  
  107. struct folder_struct {
  108.       char        foldername[SLEN];
  109.       char        prefix[NLEN];
  110.       FILE         *fd;
  111.       long        filesize;
  112.        } folders[MAX_FOLDERS];
  113.  
  114. int  interval_time,        /* how long to sleep between checks */
  115.      debug = 0,            /* include verbose debug output?    */
  116.      in_window = 0,        /* are we running as 'wnewmail'?    */
  117.      total_folders = 0,        /* # of folders we're monitoring    */
  118.      current_folder = 0;    /* struct pointer for looping       */
  119.  
  120. #ifdef PIDCHECK
  121. int  parent_pid;        /* See if sucide should be attempt  */
  122. #endif /* PIDCHECK */
  123.  
  124. char hostname[SLEN],            /* name of machine we're on         */
  125.      hostdomain[SLEN];          /* name of domain we're in          */
  126.  
  127. #ifdef BSD
  128. extern int errno;
  129. #endif
  130.  
  131. main(argc, argv)
  132. int argc;
  133. char *argv[];
  134. {
  135.     extern char *optarg;
  136.     extern int   optind;
  137.     char *ptr;
  138.     int c, i, done;
  139.     long lastsize,
  140.          newsize;            /* file size for comparison..      */
  141.  
  142.         initpaths();
  143.  
  144. #ifdef HOSTCOMPILED
  145.     strncpy(hostname, HOSTNAME, sizeof(hostname));
  146. #else
  147.     gethostname(hostname, sizeof(hostname));
  148. #endif
  149.     gethostdomain(hostdomain, sizeof(hostdomain));
  150.  
  151. #ifdef PIDCHECK                /* This will get the pid that         */
  152.     parent_pid = getppid();        /* started the program, ie: /bin/sh   */
  153.                     /* If it dies for some reason (logout)*/
  154. #endif /* PIDCHECK */            /* Then exit the program if PIDCHECK  */
  155.  
  156.     interval_time = DEFAULT_INTERVAL;
  157.  
  158.     /** let's see if the first character of the basename of the
  159.         command invoked is a 'w' (e.g. have we been called as
  160.         'wnewmail' rather than just 'newmail'?)
  161.     **/
  162.  
  163.     for (i=0, ptr=(argv[0] + strlen(argv[0])-1); !i && ptr > argv[0]; ptr--)
  164.       if (*ptr == '/') {
  165.         in_window = (*(ptr+1) == 'w');
  166.         i++;
  167.       }
  168.  
  169.     if (ptr == argv[0] && i == 0 && argv[0][0] == 'w')
  170.       in_window = 1;
  171.  
  172.     while ((c = getopt(argc, argv, "di:wh?")) != EOF) {
  173.       switch (c) {
  174.         case 'd' : debug++;                    break;
  175.         case 'i' : interval_time = atoi(optarg);        break;
  176.         case 'w' : in_window = 1;                break;
  177.         default  : usage(argv[0]);                exit(1);
  178.      }
  179.     }
  180.  
  181.     if (interval_time < 10)
  182.       fprintf(stderr,
  183. "Warning: interval set to %d second%s.  I hope you know what you're doing!\n",
  184.       interval_time, interval_time == 1 ? "" : "s");
  185.  
  186.     /* now let's parse the foldernames, if any are given */
  187.  
  188.     if (optind >= argc) /* get default */
  189.       add_default_folder();
  190.     else {
  191.       while (optind < argc)
  192.         add_folder(argv[optind++]);
  193.       pad_prefixes();            /* for nice output...*/
  194.     }
  195.  
  196. #ifdef AUTO_BACKGROUND
  197.     if (! in_window) {
  198.       if (fork())        /* automatically puts this task in background! */
  199.         exit(0);
  200.  
  201.       (void) signal(SIGINT, SIG_IGN);
  202.       (void) signal(SIGQUIT, SIG_IGN);
  203.     }
  204. #endif
  205. #ifdef SIGHUP
  206.     (void) signal(SIGHUP, SIG_DFL);
  207. #endif
  208.  
  209.     if (in_window && ! debug)
  210.       printf("Incoming mail:\n");
  211.  
  212.     while (1) {
  213.  
  214. #ifndef OS2
  215. #ifdef PIDCHECK
  216.     if ( kill(parent_pid,0))
  217.         exit(0);
  218. #else
  219. #ifndef AUTO_BACKGROUND        /* won't work if we're nested this deep! */
  220.       if (getppid() == 1)     /* we've lost our shell! */
  221.         exit();
  222. #endif /* AUTO_BACKGROUND */
  223. #endif /* PIDCHECK */
  224. #endif
  225.  
  226.       if (! isatty(1))    /* we're not sending output to a tty any more */
  227.          exit();
  228.  
  229.       if (debug) printf("\n----\n");
  230.  
  231.       for (i = 0; i < total_folders; i++) {
  232.  
  233.         if (debug)
  234.           printf("[checking folder #%d: %s]\n", i, folders[i].foldername);
  235.  
  236.         if (folders[i].fd == (FILE *) NULL) {
  237.  
  238.           if ((folders[i].fd = fopen(folders[i].foldername,"r")) == NULL)
  239.             if (errno == EACCES) {
  240.            fprintf(stderr, "\nPermission to monitor %s denied!\n\n",
  241.              folders[i].foldername);
  242.                sleep(5);
  243.                exit(1);
  244.             }
  245.         }
  246.  
  247.         if ((newsize = bytes(folders[i].foldername)) >
  248.             folders[i].filesize) {    /* new mail has arrived! */
  249.  
  250.           if (debug)
  251.             printf(
  252.            "\tnew mail has arrived!  old size = %ld, new size=%ld\n",
  253.            folders[i].filesize, newsize);
  254.  
  255.           /* skip what we've read already... */
  256.  
  257.           if (fseek(folders[i].fd, folders[i].filesize,
  258.             BEGINNING) != 0)
  259.             perror("fseek()");
  260.  
  261.           folders[i].filesize = newsize;
  262.  
  263.           /* read and display new mail! */
  264.           if (read_headers(i) && ! in_window)
  265.             printf("\n\r");
  266.         }
  267.         else if (newsize != folders[i].filesize) {    /* file SHRUNK! */
  268.  
  269.           folders[i].filesize = bytes(folders[i].foldername);
  270.           (void) fclose(folders[i].fd);    /* close it and ...         */
  271.           folders[i].fd = (FILE *) NULL;    /* let's reopen the file    */
  272.  
  273.           lastsize = folders[i].filesize;
  274.           done     = 0;
  275.  
  276.           while (! done) {
  277.             sleep(0);    /* basically gives up our CPU slice */
  278.             newsize = bytes(folders[i].foldername);
  279.             if (newsize != lastsize)
  280.               lastsize = newsize;
  281.         else
  282.               done++;
  283.           }
  284.  
  285.           folders[i].filesize = newsize;
  286.         }
  287.       }
  288.  
  289.       sleep(interval_time);
  290.     }
  291. }
  292.  
  293. int
  294. read_headers(current_folder)
  295. int current_folder;
  296. {
  297.     /** read the headers, output as found given current_folder,
  298.         the prefix of that folder, and whether we're in a window
  299.         or not.
  300.     **/
  301.  
  302.     char buffer[SLEN], from_whom[SLEN], subject[SLEN];
  303.     register int subj = 0, in_header = 0, count = 0, priority=0;
  304. #ifdef MMDF
  305.     int newheader = 0;
  306. #endif /* MMDF */
  307.  
  308.     from_whom[0] = '\0';
  309.  
  310.     while (fgets(buffer, SLEN, folders[current_folder].fd) != NULL) {
  311. #ifdef MMDF
  312.           if (strcmp(buffer, MSG_SEPERATOR) == 0) {
  313.             newheader = 1;
  314.             if (newheader) {
  315. #else
  316.       if (first_word(buffer,"From ")) {
  317.         if (real_from(buffer, from_whom)) {
  318. #endif /* MMDF */
  319.           subj = 0;
  320.           priority = 0;
  321.           in_header = 1;
  322.           subject[0] ='\0';
  323.           if (in_window)
  324.             putc((char) 007, stdout);        /* BEEP!*/
  325.           else
  326.             printf("\n\r");    /* blank lines surrounding message */
  327.  
  328.         }
  329.       }
  330.       else if (in_header) {
  331. #ifdef MMDF
  332.             if (first_word(buffer,"From "))
  333.               real_from(buffer, from_whom);
  334. #endif /* MMDF */
  335.         if (first_word(buffer,">From"))
  336.           forwarded(buffer, from_whom); /* return address */
  337.         else if (first_word(buffer,"Subject:") ||
  338.              first_word(buffer,"Re:")) {
  339.           if (! subj++) {
  340.             remove_first_word(buffer);
  341.         strcpy(subject, buffer);
  342.           }
  343.         }
  344.         else if (first_word(buffer,"Priority:"))
  345.           priority++;
  346.         else if (first_word(buffer,"From:"))
  347.           parse_arpa_from(buffer, from_whom);
  348.         else if (buffer[0] == LINEFEED) {
  349.           in_header = 0;    /* in body of message! */
  350. #ifdef MMDF
  351.               if (*from_whom == '\0')
  352.                 strcpy(from_whom,getusername());
  353. #endif /* MMDF */
  354.           show_header(priority, from_whom, subject, current_folder);
  355.           count++;
  356.           from_whom[0] = '\0';
  357.         }
  358.       }
  359.     }
  360.     return(count);
  361. }
  362.  
  363. add_folder(name)
  364. char *name;
  365. {
  366.     /* add the specified folder to the list of folders...ignore any
  367.        problems we may having finding it (user could be monitoring
  368.        a mailbox that doesn't currently exist, for example)
  369.     */
  370.  
  371.     char *cp, buf[SLEN];
  372.  
  373.     if (current_folder > MAX_FOLDERS) {
  374.       fprintf(stderr,
  375.               "Sorry, but I can only keep track of %d folders.\n", MAX_FOLDERS);
  376.       exit(1);
  377.     }
  378.  
  379.     /* now let's rip off the suffix "=<string>" if it's there... */
  380.  
  381.     for (cp = name + strlen(name); cp > name+1 && *cp != '=' ; cp--)
  382.       /* just keep stepping backwards */ ;
  383.  
  384.     /* if *cp isn't pointing to the first character we'e got something! */
  385.  
  386.     if (cp > name+1) {
  387.  
  388.       *cp++ = '\0';        /* null terminate the filename & get prefix */
  389.  
  390.       if (metachar(*cp)) cp++;
  391.  
  392.       strcpy(folders[current_folder].prefix, cp);
  393.     }
  394.     else {            /* nope, let's get the basename of the file */
  395.       for (cp = name + strlen(name); cp > name && *cp != '/'; cp--)
  396.         /* backing up a bit... */ ;
  397.  
  398.       if (metachar(*cp)) cp++;
  399.       if (*cp == '/') cp++;
  400.  
  401.       strcpy(folders[current_folder].prefix, cp);
  402.     }
  403.  
  404.     /* and next let's see what kind of weird prefix chars this user
  405.        might be testing us with.  We can have '+'|'='|'%' to expand
  406.        or a file located in the incoming mail dir...
  407.     */
  408.  
  409.     if (metachar(name[0]))
  410.       expand_filename(name, folders[current_folder].foldername);
  411.     else if (access(name, 00) == -1) {
  412.       /* let's try it in the mail home directory */
  413.       sprintf(buf, "%s%s", mailhome, name);
  414.       if (access(buf, 00) != -1)         /* aha! */
  415.         strcpy(folders[current_folder].foldername, buf);
  416.       else
  417.         strcpy(folders[current_folder].foldername, name);
  418.     }
  419.     else
  420.       strcpy(folders[current_folder].foldername, name);
  421.  
  422.     /* now let's try to actually open the file descriptor and grab
  423.        a size... */
  424.  
  425.     if ((folders[current_folder].fd =
  426.           fopen(folders[current_folder].foldername, "r")) == NULL)
  427.           if (errno == EACCES) {
  428.         fprintf(stderr, "\nPermission to monitor \"%s\" denied!\n\n",
  429.              folders[current_folder].foldername);
  430.         exit(1);
  431.       }
  432.  
  433.     folders[current_folder].filesize =
  434.           bytes(folders[current_folder].foldername);
  435.  
  436.     /* and finally let's output what we did */
  437.  
  438.     if (debug)
  439.       printf("folder %d: \"%s\" <%s> %s, size = %ld\n",
  440.           current_folder,
  441.           folders[current_folder].foldername,
  442.           folders[current_folder].prefix,
  443.           folders[current_folder].fd == NULL? "not found" : "opened",
  444.           folders[current_folder].filesize);
  445.  
  446.     /* and increment current-folder please! */
  447.  
  448.     current_folder++;
  449.     total_folders++;
  450. }
  451.  
  452. add_default_folder()
  453. {
  454.     char    *cp;
  455.  
  456.     /* this routine will add the users home mailbox as the folder
  457.      * to monitor.  Since there'll only be one folder we'll never
  458.      * prefix it either...
  459.      *    determine mail file from environment variable if found,
  460.      *    else use password entry
  461.      */
  462.     if ((cp = getenv("MAIL")) == NULL)
  463.       sprintf(folders[0].foldername, "%s%s", mailhome, getusername());
  464.     else
  465.       strcpy(folders[0].foldername, cp);
  466.  
  467.     folders[0].fd       = fopen(folders[0].foldername, "r");
  468.     folders[0].filesize = bytes(folders[0].foldername);
  469.  
  470.     if (debug)
  471.       printf("default folder: \"%s\" <%s> %s, size = %ld\n",
  472.           folders[0].foldername,
  473.           folders[0].prefix,
  474.           folders[0].fd == NULL? "not found" : "opened",
  475.           folders[0].filesize);
  476.  
  477.     total_folders = 1;
  478. }
  479.  
  480. int
  481. real_from(buffer, who)
  482. char *buffer, *who;
  483. {
  484.     /***** returns true iff 's' has the seven 'from' fields,
  485.            initializing the who to the sender *****/
  486.  
  487.     char junk[SLEN], who_tmp[SLEN];
  488.  
  489.     junk[0] = '\0';
  490.     who_tmp[0] = '\0';
  491.  
  492.     sscanf(buffer, "%*s %s %*s %*s %*s %*s %s",
  493.             who_tmp, junk);
  494.  
  495.     if (junk[0] != '\0')
  496.         strcpy(who, who_tmp);
  497.  
  498.     return(junk[0] != '\0');
  499. }
  500.  
  501. forwarded(buffer, who)
  502. char *buffer, *who;
  503. {
  504.     /** change 'from' and date fields to reflect the ORIGINATOR of
  505.         the message by iteratively parsing the >From fields... **/
  506.  
  507.     char machine[SLEN], buff[SLEN];
  508.  
  509.     machine[0] = '\0';
  510.     sscanf(buffer, "%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %s",
  511.                 who, machine);
  512.  
  513.     if(machine[0] == '\0')    /* try for address with timezone in date */
  514.     sscanf(buffer, "%*s %s %*s %*s %*s %*s %*s %*s %*s %s",
  515.                 who, machine);
  516.  
  517.     if (machine[0] == '\0') /* try for srm address */
  518.       sscanf(buffer, "%*s %s %*s %*s %*s %*s %*s %*s %s",
  519.                 who, machine);
  520.  
  521.     if (machine[0] == '\0')
  522.       sprintf(buff,"anonymous");
  523.     else
  524.       sprintf(buff,"%s!%s", machine, who);
  525.  
  526.     strncpy(who, buff, SLEN);
  527. }
  528.  
  529.  
  530. remove_first_word(string)
  531. char *string;
  532. {    /** removes first word of string, ie up to first non-white space
  533.         following a white space! **/
  534.  
  535.     register int loc;
  536.  
  537.     for (loc = 0; string[loc] != ' ' && string[loc] != '\0'; loc++)
  538.         ;
  539.  
  540.     while (string[loc] == ' ' || string[loc] == '\t')
  541.       loc++;
  542.  
  543.     move_left(string, loc);
  544. }
  545.  
  546. move_left(string, chars)
  547. char string[];
  548. int  chars;
  549. {
  550.     /** moves string chars characters to the left DESTRUCTIVELY **/
  551.  
  552.     register int i;
  553.  
  554.     chars--; /* index starting at zero! */
  555.  
  556.     for (i=chars; string[i] != '\0' && string[i] != '\n'; i++)
  557.       string[i-chars] = string[i];
  558.  
  559.     string[i-chars] = '\0';
  560. }
  561.  
  562. show_header(priority, from, subject, current_folder)
  563. int   priority;
  564. char *from, *subject;
  565. int   current_folder;
  566. {
  567.     /** output header in clean format, including abbreviation
  568.         of return address if more than one machine name is
  569.         contained within it! **/
  570.     char buffer[SLEN];
  571.     int  loc, i=0, exc=0, len;
  572.  
  573. #ifndef INTERNET
  574.     /* Remove bogus "@host.domain" string. */
  575.  
  576.     sprintf(buffer, "@%s%s", hostname, hostdomain);
  577.  
  578.     if (chloc(from, '!') != -1 && in_string(from, buffer))
  579.       from[strlen(from) - strlen(buffer)] = '\0';
  580. #endif
  581.  
  582.     loc = strlen(from);
  583.  
  584.     while (exc < 2 && loc > 0)
  585.       if (from[--loc] == '!')
  586.         exc++;
  587.  
  588.     if (exc == 2) { /* lots of machine names!  Get last one */
  589.       loc++;
  590.       len = strlen(from);
  591.       while (loc < len && loc < SLEN)
  592.         buffer[i++] = from[loc++];
  593.       buffer[i] = '\0';
  594.       strcpy(from, buffer);
  595.     }
  596.  
  597.     if (strlen(subject) < 2)
  598.       strcpy(subject, NO_SUBJECT);
  599.  
  600.     if (in_window)
  601.       if (total_folders > 1)
  602.         printf("%s: %s%s -- %s\n",
  603.            folders[current_folder].prefix,
  604.            priority? "Priority " : "", from, subject);
  605.       else
  606.         printf("%s%s -- %s\n",
  607.            priority? "Priority " : "", from, subject);
  608.     else
  609.       if (total_folders > 1)
  610.         printf(">> %s: %sail from %s - %s\n\r",
  611.           folders[current_folder].prefix,
  612.           priority? "Priority m" : "M", from, subject);
  613.       else
  614.         printf(">> %sail from %s - %s\n\r",
  615.           priority? "Priority m" : "M", from, subject);
  616. }
  617.  
  618. parse_arpa_from(buffer, newfrom)
  619. char *buffer, *newfrom;
  620. {
  621.     /** try to parse the 'From:' line given... It can be in one of
  622.         three formats:
  623.         From: Dave Taylor <hpcnou!dat>
  624.         or  From: hpcnou!dat (Dave Taylor)
  625.         or  From: hpcnou!dat
  626.         Change 'newfrom' ONLY if sucessfully parsed this entry and
  627.         the resulting name is non-null!
  628.     **/
  629.  
  630.     char temp_buffer[SLEN], *temp;
  631.     register int i, j = 0, in_parens;
  632.  
  633.     temp = (char *) temp_buffer;
  634.     temp[0] = '\0';
  635.  
  636.     no_ret(buffer);        /* blow away '\n' char! */
  637.  
  638.     if (lastch(buffer) == '>') {
  639.       for (i=strlen("From: "); buffer[i] != '\0' && buffer[i] != '<' &&
  640.            buffer[i] != '('; i++)
  641.         temp[j++] = buffer[i];
  642.       temp[j] = '\0';
  643.     }
  644.     else if (lastch(buffer) == ')') {
  645.       in_parens = 1;
  646.       for (i=strlen(buffer)-2; buffer[i] != '\0' && buffer[i] != '<'; i--) {
  647.         switch(buffer[i]) {
  648.         case ')':    in_parens++;
  649.             break;
  650.         case '(':    in_parens--;
  651.             break;
  652.         }
  653.         if(!in_parens) break;
  654.         temp[j++] = buffer[i];
  655.       }
  656.       temp[j] = '\0';
  657.       reverse(temp);
  658.     }
  659.     else
  660.     /* Unusual to have address like -  From: hpcnou!dat
  661.      * but valid */
  662.     {
  663.       for (i=strlen("From: "); buffer[i] != '\0'; i++)
  664.         temp[j++] = buffer[i];
  665.       temp[j] = '\0';
  666.     }
  667.  
  668.     if (strlen(temp) > 0) {        /* mess with buffer... */
  669.  
  670.       /* remove leading spaces... */
  671.  
  672.       while (whitespace(temp[0]))
  673.         temp = (char *) (temp + 1);        /* increment address! */
  674.  
  675.       /* remove trailing spaces... */
  676.  
  677.       i = strlen(temp) - 1;
  678.  
  679.       while (whitespace(temp[i]))
  680.        temp[i--] = '\0';
  681.  
  682.       /* if anything is left, let's change 'from' value! */
  683.  
  684.       if (strlen(temp) > 0)
  685.         strcpy(newfrom, temp);
  686.     }
  687. }
  688.  
  689. reverse(string)
  690. char *string;
  691. {
  692.     /** reverse string... pretty trivial routine, actually! **/
  693.  
  694.     char buffer[SLEN];
  695.     register int i, j = 0;
  696.  
  697.     for (i = strlen(string)-1; i >= 0; i--)
  698.       buffer[j++] = string[i];
  699.  
  700.     buffer[j] = '\0';
  701.  
  702.     strcpy(string, buffer);
  703. }
  704.  
  705. long
  706. bytes(name)
  707. char *name;
  708. {
  709.     /** return the number of bytes in the specified file.  This
  710.         is to check to see if new mail has arrived....  **/
  711.  
  712.     int ok = 1;
  713.     struct stat buffer;
  714.  
  715.     if (stat(name, &buffer) != 0)
  716.       if (errno != 2) {
  717.         fprintf(stderr,"Error %d attempting fstat on %s", errno, name);
  718.         exit(1);
  719.       }
  720.       else
  721.         ok = 0;
  722.  
  723.     return(ok ? buffer.st_size : 0);
  724. }
  725.  
  726. char  *getusername()
  727. {
  728.     /** Getting the username on some systems is a real pain, so...
  729.        This routine is guaranteed to return a usable username **/
  730.  
  731.   struct passwd *password_entry;
  732.   struct passwd *getpwuid();
  733.  
  734.   if (( password_entry = getpwuid(getuid())) != NULL)
  735.     return(password_entry->pw_name);
  736. }
  737.  
  738. usage(name)
  739. char *name;
  740. {
  741.     /* print a nice friendly usage message */
  742.  
  743.     fprintf(stderr,
  744. "\nUsage: %s [-d] [-i interval] [-w] {folders}\n", name);
  745.     fprintf(stderr, "\nWhere:\n");
  746.     fprintf(stderr, "  -d\tturns on debugging output\n");
  747.     fprintf(stderr,
  748. "  -i D\tsets the interval checking time to 'D' seconds\n");
  749.     fprintf(stderr,
  750. "  -w\tforces 'window'-style output, and bypasses auto-background\n\n");
  751.     fprintf(stderr,
  752. "folders can be specified by relative or absolute path names, can be the name\n");
  753.     fprintf(stderr,
  754. "of a mailbox in the incoming mail directory to check, or can have one of the\n");
  755.     fprintf(stderr,
  756. "standard Elm mail directory prefix chars (e.g. '+', '=' or '%').\n");
  757.     fprintf(stderr,
  758. "Furthermore, any folder can have '=string' as a suffix to indicate a folder\n");
  759.     fprintf(stderr,
  760. "identifier other than the basename of the file\n\n");
  761. }
  762.  
  763.  
  764. expand_filename(name, store_space)
  765. char *name, *store_space;
  766. {
  767.     strcpy(store_space, name);
  768.     if (expand(store_space) == 0) {
  769.       fprintf(stderr,"Sorry, but I couldn't expand \"%s\"\n",name);
  770.       exit(1);
  771.     }
  772. }
  773.  
  774. pad_prefixes()
  775. {
  776.     /** This simple routine is to ensure that we have a nice
  777.         output format.  What it does is whip through the different
  778.         prefix strings we've been given, figures out the maximum
  779.         length, then space pads the other prefixes to match.
  780.     **/
  781.  
  782.     register int i, j, len = 0;
  783.  
  784.     for (i=0; i < total_folders; i++)
  785.       if (len < (j=strlen(folders[i].prefix)))
  786.         len = j;
  787.  
  788.     for (i=0; i < total_folders; i++)
  789.       for (j = strlen(folders[i].prefix); j < len; j++)
  790.         strcat(folders[i].prefix, " ");
  791. }
  792.