#define UMAX 50
#define CLDSIG 18
char	mesg[3000];
int	msize;
struct UTMP
{
	char	name[8];
	char	ln[2];
	int	time[2];
	int	pid;
} utmp[UMAX];

struct {
	char	lobyte;
	char	hibyte;
};

char	who[9]	"???";
char	line[]	"/dev/lnxx";

main(argc, argv)
char *argv[];
{
	int i;
	register struct UTMP *p;
	register f;

	f = open("/etc/utmp", 0);
	if(f < 0) {
		printf("utmp?\n");
		exit(1);
	}
	read(f, utmp, sizeof(utmp));
	close(f);
	i = lnxx(2);
	for(p = utmp; p < &utmp[UMAX]; p++) {
		if (p->ln[0] != i.lobyte || p->ln[1] != i.hibyte)
			continue;
		for (f=0; f<8; f++)
			if ((who[f] = p->name[f]) == ' ')
				break;
			else
				who[f] =& ~0200;
		who[f] = 0;
		break;
	}
	line[7] = i.lobyte;
	line[8] = i.hibyte;
	chmod(line, 0622);
	f = 0;
	if(argc >= 2) {
		f = open(argv[1], 0);
		if(f < 0) {
			printf("%s?\n", argv[1]);
			exit(1);
		}
	}
	while((i = read(f, &mesg[msize], sizeof mesg - msize)) > 0)
		msize =+ i;
	close(f);
	signal(CLDSIG, 1);
	for(p = &utmp[0]; p < &utmp[UMAX]; p++) {
		if (!(p->name[0] & 0200))
			continue;
		p->name[0] =& 0177;
		sleep(1);
		sendmes(p);
	}
}

sendmes(p)
struct UTMP *p;
{
	register i;
	register char *s;
	register int try;

	try = 0;
	while ((i = fork()) == -1) {
		if (++try == 4) {
			printf("Can't fork!\n");
			exit(2);
		}
		sleep(5);
	}

	if(i)
		return;

	s = "/dev/lnxx";
	s[7] = p->ln[0];
	s[8] = p->ln[1];
	i = open(s, 1);
	if(i < 0) {
		printf("Cannot send to %8.8s\n", p->name);
		exit(1);
	}
	close(1);
	dup(i);
	printf("\07\07\07Broadcast Message from %s (%s) ...\n\n",
		 who, &line[5]);
	write(1, mesg, msize);
	exit(0);
}
