home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / mail-s / mail-s.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.3 KB  |  97 lines

  1. /* mail-s
  2.  *
  3.  *    Send mail (with an optional subject) iff standard input contains
  4.  *    more than a given minimum of lines.
  5.  *
  6.  *    Typical usage from system maintenance scripts:
  7.  *
  8.  *        grep "[Ee]rror" < logfile | mail-s "Something wrong" root
  9.  *
  10.  */
  11.  
  12. static char SCCS_id[] = "@(#)@ mail-s    1.2 - mail-s";
  13. static char C_id[] = "@(#)@ Copyright 1988 Johan Vromans, Multihouse Research";
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17.  
  18. #ifndef MAILER
  19. #define MAILER    "/bin/smail"
  20. #endif
  21.  
  22. main (argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     int c;
  27.     int errflg        = 0;        /* errors seen */
  28.     int f_thresh    = 1;        /* threshold value */
  29.     char *f_subj    = NULL;        /* subject line */
  30.     char buf [BUFSIZ];            /* file copy buffer */
  31.     FILE *tmp;                /* workfile */
  32.     extern char *optarg;        /* getopt interface */
  33.     extern int optind;            /* getopt interface */
  34.  
  35.     /* Phase one: command line options parsing */
  36.  
  37.     while ((c = getopt (argc, argv, "s:n:")) != EOF) {
  38.     switch (c) {
  39.     case 'n':
  40.         /* threshold: xmit to mailer iff stdin contains at least
  41.          * this much lines.
  42.          */
  43.         f_thresh = atoi (optarg);
  44.         if (!isdigit(*optarg)) errflg++;
  45.         break;
  46.     case 's':
  47.         /* subject to pass to mailer */
  48.         f_subj = optarg;
  49.         break;
  50.     default:
  51.         errflg++;
  52.         break;
  53.     }
  54.     }
  55.  
  56.     if (errflg) {
  57.     fprintf (stderr,
  58.         "Usage mail-s [-s subject] [-n threshold] recipients\n");
  59.     exit (2);
  60.     }
  61.  
  62.     /* phase two: copy the file and decrease f_thresh upon each complete
  63.      * line. The work file is passed to the mailer, so put in the
  64.      * subject first.
  65.      */
  66.  
  67.     tmp = tmpfile ();
  68.  
  69.     if (f_subj)
  70.     fprintf (tmp, "Subject: %s\n\n", f_subj);
  71.  
  72.     while (fgets (buf, BUFSIZ, stdin) != NULL) {
  73.     register int len = strlen (buf);
  74.     fputs (buf, tmp);
  75.     if (len >= 1 && buf[len-1] == '\n') f_thresh--;
  76.     }
  77.  
  78.     /* Check for go or no-go */
  79.     if (f_thresh > 0)        /* didn't make it */
  80.     return 0;
  81.  
  82.     /* Phase three: rewind the workfile, attach to standard input,
  83.      * build new arg vector, and exec the mailer.
  84.      */
  85.  
  86.     rewind (tmp);
  87.     close (0);            /* stdin */
  88.     dup (fileno (tmp));        /* attach to standard input */
  89.  
  90.     argv[0] = "rmail";        /* build rmail arg vector */
  91.     for (c = 1; optind < argc; optind++, c++)
  92.     argv[c] = argv[optind];
  93.     argv[c] = NULL;
  94.     return execv (MAILER, argv);    /* and Go */
  95. }
  96.  
  97.