/*
 * This program , written by Henry Yen does the writing and mailing
 * using the system "write" and the system "mail" as opposed to
 * opening a tty or .mail directly as hdisp used to. 
 * It gets compiled by doing     cc -O -s hnfy.hy.c -lPW
 * it must run as group root and the mode of the file should be 2711
 * owned by hasp and group root.        J.B.B.
 */

/* arg sequence is: who,what,lv,l,tty;
   lv, l, and tty are byted into third arg string */
main(argc, argv)
int argc;
char **argv;
{
	int lv;
	int l;
	char *ptty;

	lv = argv[3][0];
	l = argv[3][1];
	ptty = &argv[3][2];
	lv &= 0377;
	l &= 0377;
	if (l >= (lv%10))
		if (hywrite(argv[1],argv[2],ptty)==0)
			exit(0);
	if (l >= (lv/10))
		hymail(argv[1],argv[2]);
	exit(0);
}
tfork()	/* copied from haspdisp tfork */
{
	int t, fk;

	fk = -1;
	for (t = 0;t < 10;t++) {
		if ((fk = fork()) != -1)
			break;
		sleep(1);
	}
	return(fk);
}
hywrite(who,what,pref)
char *who;
char *what;
char *pref;
{
	int stat;

	if (pref == 0)
		goto nopref;
	if (tfork() == 0) {
		execl("/bin/write","-",who,pref,"-",what,0);
		exit(-1);
	}
	if (wait(&stat) == -1)
		return(-1);
	else if (stat == 0)
		return(0);
	else {
nopref:
		if (tfork() == 0) {
			execl("/bin/write","-",who,"-",what,0);
			exit(-1);
		}
		if (wait(&stat) == -1)
			return(-1);
		else
		    return(stat);
	}
}
hymail(who,what)
char *who;
char *what;
{
	register int y;
	register char *z;
	int x[2];

	z = what;
	y = 0;
	while (*z++)
		y++;
	*--z = '\n';
	close(0);
	close(1);
	close(2);
	pipe(x);
	write(1,what,y+1);
	close(1);
	execl("/bin/mail","mail",who,0);
	exit(0);
}
copy(from,to)
register char *from;
register char *to;
{
	while (*to++ = *from++);
}
