#
/* send1.c */


/* This information is proprietary and is the property of Bell
   Telephone Laboratories, Incorporated.  Its reproduction or
   disclosure to others, either orally or in writing, is pro-
   hibited without written permission of Bell Laboratories. */

/*      mod daa 06/29/81
	1) added nosend support. see additional notes in code.
	2) added #define NAME for ease of use.
*/

#define NAME (gth ? "gath" : "send")
#define IDMOD send1

#define IDSTR "~|^`send1.c 1.20 4/22/76

#include <stdio.h>
#include "send.h"

abt()
{
	if (!abort) {
		abort=1; 
		dotrp();
	};
};

sig1()
{
	signal(1,1); 
	prf("Hang-up. (577)\n"); 
	abt();
};

sig2()
{
	signal(2,1); 
	prf("Interrupt. (578)\n"); 
	abt();
};

sig3()
{
	signal(3,1); 
	prf("Quit signal. (579)\n"); 
	abt();
};

sigx(x)
{
	signal(x,1); 
	prf("Signal #%d. (580)\n",x); 
	abt();
};

struct context icx,zcx;

struct format ifm;

struct trap itr;

FILE *pwfd;

char *pwdbfp, pwdbf[128];

main(ac,av)
char *av[];
{
	extern int errno;
	int idv,ino,getrc,grpid;
	int gth,eof,*aeof,ast,st[18];
	register int t;
	register char *p, *p0, *lgnmep;
	char lgnme[9];
	int i;

	aeof=(&eof);
	ast=st;
	tty=(-1);
	inibf(&obf,1);
	inibf(&dbf,2);
	statb.st_ino = statb.st_dev = statb.st_mode = 0;
	if (fstat(0,&statb)<0) open("/dev/null",0);
	if ((MTY&statb.st_mode)==CTY) {
		tin=1; 
		tty=0;
	};
	if (fstat(1,&statb)<0) dup(0);
	idv = statb.st_dev;
	ino = statb.st_ino;
	if ((MTY&statb.st_mode)==CTY) {
		obf.bb=1; 
		tty=1;
	};
	if (obf.bb==0 && lseek(1,(long)(0),1)<0 && errno==ESPIPE) obf.bb=1;
	if (fstat(2,&statb)<0) dup(1);
	if (statb.st_dev == idv && statb.st_ino == ino) oed = 1;
	if ((MTY&statb.st_mode)==CTY) {
		dbf.bb=1; 
		tty=2;
	};
	code=BCD;
	maxcol=80;
	iniky();
	stfm(&ifm);
	icx.dsp=(&ifm);
	icx.ocx=(&zcx);
	zcx.kw0=icx.kw0=ikw;
	zcx.kw1=icx.kw1=ikw->nx;
	icx.nam=(ac>0? av[0]:"send");
	icx.lvl=(-1);
	p0=p=icx.nam;
	while (t=(*p++))
	{
		if (t=='/') p0=p;
	};
	gth=mtch(4,"gath",p0);
	gth=|mtch(4,"gath",p0+1);

	usage(NAME);
	if (gth)
		icx.flg[F'g']=icx.flg[F'l']=icx.flg[F'q']=1;
	eof=0;
	ac--; 
	av++;
	setrp(&itr);
	if (!signal(1,1)) signal(1,sig1);
	if (!signal(2,1)) signal(2,sig2);
	if (!signal(3,1)) signal(3,sig3);
	for (t=4;t<NSIG;t++) signal(t,sigx);
	while (!eof && !abort)
	{
		if (--ac<0)
		{
			if (!didf) doarg(&icx,1,"-",aeof);
			break;
		};
		p0=(*av++);
		for (p=p0;*p;p++);
		doarg(&icx,p-p0,p0,aeof);
	};
	if (icx.sfd) doarg(&icx,0,0);
	flush(&obf);
	if (tmpf) {
		flush(&qbf); 
		close(qbf.fd);
	};
	if (abort) {
		prf("Aborted. (571)\n"); 
		goto X;
	};
	if (!gth || qat) prf("%D cards.\n",cnt);
	if (!qat) {
		flush(&dbf); 
		exit((error? ~0:0));
	};
	if (!cue) goto X;
	if (error)
	{
		prf("Errors detected. Send anyway? (572)  ");
		ss[0]=0;
		if (read(2,ss,1)==1)
		{
			ss[1]=ss[0];
			while (ss[1]!='\n' && read(2,ss+1,1)==1);
		};
		if ((0137&ss[0])!='Y') goto X;
	};
	flush(&dbf);
	for (t=1;t<NSIG;t++) signal(t,1);
	execl(qer,"qer",tmpf,home,0);
	prf("Cannot execute queuer, %s. (573)\n",qer);
X: 
	flush(&dbf);
	if (tmpf) unlink(tmpf);
	exit(~0);
};

usage(s)
char *s;
{
	int u,*au;
	char *px;
	register int t,d;
	register char *p;
	au=(&u);
	p="/usr/pw/usg";
	t=getuid();
	t=+2; 
	t=<<1;
	if (t < 0)return;
	prf("%q%s/%s",ss,p,s);
	if ((d=open(ss,2))>=0)
	{
		if (lseek(d,(long)(t),0)<0 || read(d,au,2)!=2) u=0;
doit:
		u++;
		if (lseek(d,(long)(t),0)>=0) write(d,au,2);
		close(d); 
		return;
	};
	if ((d=creat(ss,0666))<0) return;
	px=(p=tbf.bf)+512;
	time(p); 
	write(d, p, 4);
	u = 0;
	goto doit;
};

int set14;

sig14()
{
	signal(14,sig14);
	if (set14) alarm(1);
};

getty()
{
	static int x;
	register int t;
	if (x==0)
	{
		x++;
		sig14();
		alarm(set14=10);
		t=open("/dev/tty",2);
		alarm(set14=0);
		signal(14,sigx);
		if (t>=0) tty=t;
	};
	return (tty);
};

colon(ptr)
char *ptr;
{
	char *p;

	p = ptr;
	while (*p++ != ':');
	return(p);
}

getpwline()
{
	register int c;
	int nochar = 0;
	register char *p;

	p = &pwdbf[0];
	while ((c = getc(pwfd)) != 0 && c != '\n') {
		*p++ = c;
		++nochar;
	}

	if (c = '\n') *p++ = 0;
	return (nochar);
}

/*end*/
