jv@mh.nl.UUCP (Johan Vromans) (05/16/88)
comp.sources.misc: Volume 3, Issue 15 Submitted-By: "Johan Vromans" <jv@mh.nl.UUCP> Archive-Name: mail-s This is a trivial program. It reads standard input, and passes it to a mail program only if there are more than a specified number of lines in the message to be sent. In the default case, empty mail messages are suppressed. Optionally, a subject can be passed to the program, which will be added to the message. It's main purpose is to cut down the number of mails sent to the system manager from system daemons - only if there is something important to tell. There are a few examples in the manual page. #!/bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create: # README # mail-s.1 # mail-s.c # This archive created: Mon May 16 16:34:17 1988 export PATH; PATH=/bin:/usr/bin:$PATH if test -f 'README' then echo shar: "will not over-write existing file 'README'" else sed 's/^X//' << \SHAR_EOF > 'README' XThis is a trivial program. X XIt reads standard input, and passes it to a mail program only if there are Xmore than a specified number of lines in the message to be sent. X XIn the default case, empty mail messages are suppressed. X XOptionally, a subject can be passed to the program, which will be added to Xthe message. X XIt's main purpose is to cut down the number of mails sent to the system Xmanager from system daemons - only if there is something important to tell. X XThere are a few examples in the manual page. X XNo makefile - just compile and run. The only compile-time option is the Xname of the mailer to be used. This defaults to "/bin/smail", but can be Xchanged with X X -DMAILER=\"/usr/local/bin/mymailer\" X XPut into public domain by the author on May 16, 1988. XJohan Vromans, Multihouse Research. SHAR_EOF if test 799 -ne "`wc -c < 'README'`" then echo shar: "error transmitting 'README'" '(should have been 799 characters)' fi fi if test -f 'mail-s.1' then echo shar: "will not over-write existing file 'mail-s.1'" else sed 's/^X//' << \SHAR_EOF > 'mail-s.1' X.TH MAIL-S 1 "Contributed" X.SH SYNOPSIS X.B mail-s X.RB "[ " -n X.IR "threshold" " ]" X.RB "[ " -s X.IR "subject" " ]" X.IR "recipient" " [...]" X.SH DESCRPITION X.B Mail-s Xtakes a mail message on standard input, and passes it to the mailer program Xonly if the mail message contains more than a specified number of lines. X.PP X.SH OPTIONS X.IP -n\ \fIthreshold\fP XThe minimum number of lines the message must contain. Default value is 1. X.IP -s\ \fIsubject\fP XSupply the given subject to the mail message. Default is no subject. X.ne 15v X.SH EXAMPLE XThis program is intended for use in system maintenance procedures, to Xminimize the amount of mails sent. X.sp X.in +5 Xgrep "[Ee]rror" < logfile \e X.br X| mail-s -s "Something wrong" root X.in X.sp XThis command will cause a mail to be sent to the super user if there were Xlines in the logfile with the text "Error" or "error" in it. X.sp X.in +5 Xls -l /applic/queuefile \e X.br X| mail-s -n 10 -s "More than 10 entries waiting" root X.in X.sp XOnly if there are 10 or more files in this directory, the super user will Xbe notified. X.SH AUTHOR XJohan Vromans, Multihouse Research <jv@mh.nl> X.br XThis program is in the public domain. X.sp XComments, bug fixes welcomed - but I don't guarantee to fix anything. SHAR_EOF if test 1230 -ne "`wc -c < 'mail-s.1'`" then echo shar: "error transmitting 'mail-s.1'" '(should have been 1230 characters)' fi fi if test -f 'mail-s.c' then echo shar: "will not over-write existing file 'mail-s.c'" else sed 's/^X//' << \SHAR_EOF > 'mail-s.c' X/* mail-s X * X * Send mail (with an optional subject) iff standard input contains X * more than a given minimum of lines. X * X * Typical usage from system maintenance scripts: X * X * grep "[Ee]rror" < logfile | mail-s "Something wrong" root X * X */ X Xstatic char SCCS_id[] = "@(#)@ mail-s 1.2 - mail-s"; Xstatic char C_id[] = "@(#)@ Copyright 1988 Johan Vromans, Multihouse Research"; X X#include <stdio.h> X#include <ctype.h> X X#ifndef MAILER X#define MAILER "/bin/smail" X#endif X Xmain (argc, argv) Xint argc; Xchar *argv[]; X{ X int c; X int errflg = 0; /* errors seen */ X int f_thresh = 1; /* threshold value */ X char *f_subj = NULL; /* subject line */ X char buf [BUFSIZ]; /* file copy buffer */ X FILE *tmp; /* workfile */ X extern char *optarg; /* getopt interface */ X extern int optind; /* getopt interface */ X X /* Phase one: command line options parsing */ X X while ((c = getopt (argc, argv, "s:n:")) != EOF) { X switch (c) { X case 'n': X /* threshold: xmit to mailer iff stdin contains at least X * this much lines. X */ X f_thresh = atoi (optarg); X if (!isdigit(*optarg)) errflg++; X break; X case 's': X /* subject to pass to mailer */ X f_subj = optarg; X break; X default: X errflg++; X break; X } X } X X if (errflg) { X fprintf (stderr, X "Usage mail-s [-s subject] [-n threshold] recipients\n"); X exit (2); X } X X /* phase two: copy the file and decrease f_thresh upon each complete X * line. The work file is passed to the mailer, so put in the X * subject first. X */ X X tmp = tmpfile (); X X if (f_subj) X fprintf (tmp, "Subject: %s\n\n", f_subj); X X while (fgets (buf, BUFSIZ, stdin) != NULL) { X register int len = strlen (buf); X fputs (buf, tmp); X if (len >= 1 && buf[len-1] == '\n') f_thresh--; X } X X /* Check for go or no-go */ X if (f_thresh > 0) /* didn't make it */ X return 0; X X /* Phase three: rewind the workfile, attach to standard input, X * build new arg vector, and exec the mailer. X */ X X rewind (tmp); X close (0); /* stdin */ X dup (fileno (tmp)); /* attach to standard input */ X X argv[0] = "rmail"; /* build rmail arg vector */ X for (c = 1; optind < argc; optind++, c++) X argv[c] = argv[optind]; X argv[c] = NULL; X return execv (MAILER, argv); /* and Go */ X} X SHAR_EOF if test 2310 -ne "`wc -c < 'mail-s.c'`" then echo shar: "error transmitting 'mail-s.c'" '(should have been 2310 characters)' fi fi exit 0 # End of shell archive -- postmaster on node mhres (currently: Johan Vromans)