Send.c improved

Karl Denninger karl at ddsw1.UUCP
Wed May 11 10:28:06 AEST 1988


Here's a small utility which has been floating around to send single-line
messages to another user's terminal.  This version searches for the first
writable terminal if there is more than one.

It's small and handy -- ideal qualities.

Should compile and run on either Microport or SCO Xenix (was last tested on
SCO Xenix V/386 2.2.1).

/*
 *	send.c - send a one-line message to another user
 *	Bill Wisner, billw at killer.UUCP
 *	Modified to send to the first writable terminal by karl at ddsw1
 */

#ifndef lint
static char *SccsId = "@(#)send.c	1.1	05/10/88";
#endif /* lint */

#include <stdio.h>
#include <sys/types.h>
#include <utmp.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

main(argc,argv)
int argc;
char *argv[];
{
	char *to, *message, *tty = "/dev/null", *devname, *malloc();
	int device, logins = 0;
	register int i;
	struct utmp *utent, *getutent();

	if (argc < 3) {
		(void)printf("Usage: %s user message\n",argv[0]);
		exit(1);
	}

	to = malloc(9);
	sprintf(to,"%s",argv[1]);

	do {	/* See how many times victim is logged in, if any */
		utent = getutent();
		if (strcmp(utent->ut_user,to) == 0)
			logins++;
	} while (utent != NULL);

	if (logins == 0) {
		(void)printf("%s: %s is not logged in.\n",argv[0],to);
		exit(1);
	}
/*
 * Now look at all logins, and send to the first write-enabled terminal
 * we find.  Ignore the rest.  Complain only if there is no place to put
 * the user's message.  This is better than just blindly trying the first
 * port in the hopes it is writable.	- KSD 5/10/88
 *
 */
	setutent();			/* Rewind utmp file */
	devname = malloc(20);
	message = malloc(512);		/* Please gimme memory :-) */
	sprintf(message,"\007\n;;%s:",getlogin());
	for(i=2;i<argc;i++) {
		(void)sprintf(message,"%s %s",message,argv[i]);
	}

	sprintf(message,"%s\n",message);/* Message is built, find a line */
	do {
		utent = getutent();
		if (strcmp(utent->ut_user, to) == 0) {	/* Check this line */
			sprintf(devname, "/dev/%s", utent->ut_line);
			device = open(devname,O_WRONLY);
			if (device == -1) {
				if (errno != EACCES) {
					printf("%s: Cannot open %s!\n", argv[0], devname);
					continue;
				}
			} else {
				if (write(device,message,strlen(message)) == -1) {
					close(device);
					continue;
				} else
					if (logins > 1)
						printf("To %s (%d logins).\n", utent->ut_line, logins);
					exit(0);		/* Get out, delivered */
			}
		}
	} while (utent != NULL);
	printf("%s: Cannot write to user %s\n", argv[0], to);
	exit(1);
}


----
Karl Denninger                 |  Data: +1 312 566-8912
Macro Computer Solutions, Inc. | Voice: +1 312 566-8910
...ihnp4!ddsw1!karl            | "Quality solutions for work or play"



More information about the Comp.unix.microport mailing list