home *** CD-ROM | disk | FTP | other *** search
- /* mail-s
- *
- * Send mail (with an optional subject) iff standard input contains
- * more than a given minimum of lines.
- *
- * Typical usage from system maintenance scripts:
- *
- * grep "[Ee]rror" < logfile | mail-s "Something wrong" root
- *
- */
-
- static char SCCS_id[] = "@(#)@ mail-s 1.2 - mail-s";
- static char C_id[] = "@(#)@ Copyright 1988 Johan Vromans, Multihouse Research";
-
- #include <stdio.h>
- #include <ctype.h>
-
- #ifndef MAILER
- #define MAILER "/bin/smail"
- #endif
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- int c;
- int errflg = 0; /* errors seen */
- int f_thresh = 1; /* threshold value */
- char *f_subj = NULL; /* subject line */
- char buf [BUFSIZ]; /* file copy buffer */
- FILE *tmp; /* workfile */
- extern char *optarg; /* getopt interface */
- extern int optind; /* getopt interface */
-
- /* Phase one: command line options parsing */
-
- while ((c = getopt (argc, argv, "s:n:")) != EOF) {
- switch (c) {
- case 'n':
- /* threshold: xmit to mailer iff stdin contains at least
- * this much lines.
- */
- f_thresh = atoi (optarg);
- if (!isdigit(*optarg)) errflg++;
- break;
- case 's':
- /* subject to pass to mailer */
- f_subj = optarg;
- break;
- default:
- errflg++;
- break;
- }
- }
-
- if (errflg) {
- fprintf (stderr,
- "Usage mail-s [-s subject] [-n threshold] recipients\n");
- exit (2);
- }
-
- /* phase two: copy the file and decrease f_thresh upon each complete
- * line. The work file is passed to the mailer, so put in the
- * subject first.
- */
-
- tmp = tmpfile ();
-
- if (f_subj)
- fprintf (tmp, "Subject: %s\n\n", f_subj);
-
- while (fgets (buf, BUFSIZ, stdin) != NULL) {
- register int len = strlen (buf);
- fputs (buf, tmp);
- if (len >= 1 && buf[len-1] == '\n') f_thresh--;
- }
-
- /* Check for go or no-go */
- if (f_thresh > 0) /* didn't make it */
- return 0;
-
- /* Phase three: rewind the workfile, attach to standard input,
- * build new arg vector, and exec the mailer.
- */
-
- rewind (tmp);
- close (0); /* stdin */
- dup (fileno (tmp)); /* attach to standard input */
-
- argv[0] = "rmail"; /* build rmail arg vector */
- for (c = 1; optind < argc; optind++, c++)
- argv[c] = argv[optind];
- argv[c] = NULL;
- return execv (MAILER, argv); /* and Go */
- }
-
-