home *** CD-ROM | disk | FTP | other *** search
- /* $Author: reggers $
- * $Date: 1992/04/21 14:31:46 $
- * $Id: sendopr.c,v 1.5 1992/04/21 14:31:46 reggers Exp $
- * $Source: /usr/src/usr.local/utilities/sendopr/RCS/sendopr.c,v $
- * $State: Exp $
- *
- * Original work done by Brian Borowski on April 10 1992
- * done during a placement here at UWO
- * This program sends messages to the operator in one of two modes.
- * These are immediate where the message on the command line is sent via
- * xmessage. In this mode the program will pause until the operator
- * clicks on the button at the bottom of the message window, at which
- * point the sender will see "okay" and be returned to the prompt.
- * In the second mode queued mode the message is sent via a host
- * socket pair using tcp and the message is queued for future reading
- * by the operator. Immediate and queued can be controlled from the
- * command line by the -i and -q options. If the controlling device
- * is not a tty then queued mode is assumed.
- */
-
- static char *rcsid="$Id: sendopr.c,v 1.5 1992/04/21 14:31:46 reggers Exp $";
-
- #ifndef SENDOPRHOST
- #define SENDOPRHOST "sendoprhost"
- #endif
-
- #ifndef SENDOPRPORT
- #define SENDOPRPORT "sendopr"
- #endif
-
- #define CFG "/usr/local/share/etc/operator"
-
- #include <stdio.h>
- #include <sysexits.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <errno.h>
- #include "sendoprd.h"
-
- char display_buf[BUFSIZ];
- char msg_buf[BUFSIZ];
- char *usage="sendopr [-i|-q] [-d display] [-s subject] message";
- char *display = DISPLAY;
- char *title = TITLE;
- char *name = NAME;
- char *subject=NAME, *logname, *date;
- char myhost[BUFSIZ]; /* name of host */
- int from_stdin=0; /* assume input is from args rather than stdin */
- int debug=0;
-
- /* msg_print: feeds the users message into the file pointer.
- Returns no value. Called by main. */
- void msg_print(argv, argc, start, fp)
- char *argv[];
- int argc, start;
- FILE *fp;
- { int i;
-
- fprintf(fp, "From: %s@%s\n", logname, myhost);
- fprintf(fp, "Subject: %s\n", subject);
- fprintf(fp, "Date: %s\n", date);
- if (from_stdin) /* get message from stdin and feed it out */
- while (fgets(msg_buf, BUFSIZ, stdin) != NULL)
- fputs(msg_buf, fp);
- else /* else get message from argv */
- { for (i = start; argv[i]; i++) /* feed rest of argv into pipe */
- { fprintf(fp, "%s", argv[i]);
- if (i < argc-1) putc(' ', fp);
- } /* end for args */
- putc('\n', fp);
- } /* end else from args */
- fflush(fp);
- } /* end msg_print */
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- { int unit, i;
- time_t secs;
- char *getlogin(), *ctime();
- extern int optind;
- extern char *optarg;
- extern int tcpopen();
- int immediate, c;
- struct passwd *pw_info;
- FILE *fp_out, *fp1;
-
- if (isatty(fileno(stdin)))
- immediate = 1; /* set immediate to true if tty */
- else immediate = 0;
- /* parse command line arguments: */
- if (argc == 1) /* assume input from stdin */
- from_stdin=1;
- else /* check the argv */
- while ((c = getopt(argc, argv, "iqd:s:")) != EOF)
- switch(c)
- { case 'i' : if (optind == 2)
- immediate=1;
- break;
- case 'q' : if (optind == 2)
- immediate=0;
- break;
- case 'd' : display=optarg;
- break;
- case 's' : subject = (optarg)? optarg: NAME;
- break;
- default : fprintf(stderr, usage);
- exit(EX_USAGE);
- } /* end case */
-
- if (optind == argc) from_stdin=1; /* message from stdin */
- if (display == DISPLAY) /* then hasn't been changed by command line */
- if ((fp1=fopen(CFG, "r")) != NULL) /* check for config file */
- { fgets(display_buf,BUFSIZ,fp1);
- display = display_buf;
- fclose(fp1);
- } /* end get display info */
- time(&secs);
- date = ctime(&secs);
- if ((logname = getlogin()) == NULL)
- { pw_info = getpwuid(getuid()); /* not attached to a tty */
- logname = pw_info->pw_name;
- }
- if (gethostname(myhost, BUFSIZ) == -1)
- { fprintf(stderr, "***-cannot get name of host");
- exit(127);
- }
- if (debug) fprintf(stderr,"got user, date and host\n");
- if (immediate) /* send using xmessage */
- { sprintf(msg_buf, "%s -display %s -title %s -name %s -",
- XMESSAGE,display,title,name);
- if ((fp_out=popen(msg_buf, "w")) == NULL)
- { fprintf(stderr, "cannot pipe to xmessage");
- exit(127);
- }
- msg_print(argv, argc, optind, fp_out);
- pclose(fp_out);
- } /* end if immediate */
- else /* use the sendopr host */
- { if ((unit=tcpopen(SENDOPRHOST, SENDOPRPORT)) < 0)
- { fprintf(stderr, "**cannot create a socket\n");
- exit(127);
- }
- fp_out=fdopen(unit, "w");
- /* feed in the message */
- msg_print(argv, argc, optind, fp_out);
- fclose(fp_out);
- } /* end else use the sendopr host */
- exit(0);
- } /* end main */
-