xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 10349)
14632Seric # include "sendmail.h"
24632Seric # include <sys/stat.h>
38348Seric # include <dir.h>
44634Seric # include <signal.h>
54632Seric # include <errno.h>
64632Seric 
75182Seric # ifndef QUEUE
8*10349Seric SCCSID(@(#)queue.c	3.70		01/17/83	(no queueing));
95182Seric # else QUEUE
104632Seric 
11*10349Seric SCCSID(@(#)queue.c	3.70		01/17/83);
125182Seric 
134632Seric /*
149377Seric **  Work queue.
159377Seric */
169377Seric 
179377Seric struct work
189377Seric {
199377Seric 	char		*w_name;	/* name of control file */
209377Seric 	long		w_pri;		/* priority of message, see below */
219377Seric 	struct work	*w_next;	/* next in queue */
229377Seric };
239377Seric 
249377Seric typedef struct work	WORK;
259377Seric 
269377Seric WORK	*WorkQ;			/* queue of things to be done */
279377Seric /*
284632Seric **  QUEUEUP -- queue a message up for future transmission.
294632Seric **
304632Seric **	Parameters:
316980Seric **		e -- the envelope to queue up.
326999Seric **		queueall -- if TRUE, queue all addresses, rather than
336999Seric **			just those with the QQUEUEUP flag set.
349377Seric **		announce -- if TRUE, tell when you are queueing up.
354632Seric **
364632Seric **	Returns:
374632Seric **		none.
384632Seric **
394632Seric **	Side Effects:
409377Seric **		The current request are saved in a control file.
414632Seric */
424632Seric 
439377Seric queueup(e, queueall, announce)
446980Seric 	register ENVELOPE *e;
456999Seric 	bool queueall;
469377Seric 	bool announce;
474632Seric {
487812Seric 	char *tf;
497812Seric 	char *qf;
507763Seric 	char buf[MAXLINE];
517812Seric 	register FILE *tfp;
524632Seric 	register HDR *h;
535007Seric 	register ADDRESS *q;
5410173Seric 	MAILER nullmailer;
554632Seric 
565037Seric 	/*
575037Seric 	**  Create control file.
585037Seric 	*/
594632Seric 
607812Seric 	tf = newstr(queuename(e, 't'));
617812Seric 	tfp = fopen(tf, "w");
627812Seric 	if (tfp == NULL)
634632Seric 	{
647812Seric 		syserr("queueup: cannot create temp file %s", tf);
654632Seric 		return;
664632Seric 	}
679048Seric 	(void) chmod(tf, FileMode);
684632Seric 
694632Seric # ifdef DEBUG
707677Seric 	if (tTd(40, 1))
717812Seric 		printf("queueing in %s\n", tf);
724632Seric # endif DEBUG
734632Seric 
744632Seric 	/*
756980Seric 	**  If there is no data file yet, create one.
766980Seric 	*/
776980Seric 
786980Seric 	if (e->e_df == NULL)
796980Seric 	{
806980Seric 		register FILE *dfp;
819389Seric 		extern putbody();
826980Seric 
837812Seric 		e->e_df = newstr(queuename(e, 'd'));
846980Seric 		dfp = fopen(e->e_df, "w");
856980Seric 		if (dfp == NULL)
866980Seric 		{
876980Seric 			syserr("queueup: cannot create %s", e->e_df);
887812Seric 			(void) fclose(tfp);
896980Seric 			return;
906980Seric 		}
919048Seric 		(void) chmod(e->e_df, FileMode);
9210173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
937009Seric 		(void) fclose(dfp);
949389Seric 		e->e_putbody = putbody;
956980Seric 	}
966980Seric 
976980Seric 	/*
984632Seric 	**  Output future work requests.
999377Seric 	**	Priority should be first, since it is read by orderq.
1004632Seric 	*/
1014632Seric 
1029377Seric 	/* output message priority */
1039377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1049377Seric 
1059630Seric 	/* output creation time */
1069630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1079630Seric 
1084632Seric 	/* output name of data file */
1097812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1104632Seric 
11110108Seric 	/* message from envelope, if it exists */
11210108Seric 	if (e->e_message != NULL)
11310108Seric 		fprintf(tfp, "M%s\n", e->e_message);
11410108Seric 
1154632Seric 	/* output name of sender */
1167812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1174632Seric 
1184632Seric 	/* output list of recipient addresses */
1196980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1204632Seric 	{
1217763Seric 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
1227763Seric 			       bitset(QQUEUEUP, q->q_flags))
1238245Seric 		{
1247812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1259377Seric 			if (announce)
1269377Seric 			{
1279377Seric 				e->e_to = q->q_paddr;
1289377Seric 				message(Arpa_Info, "queued");
1299377Seric 				if (LogLevel > 4)
1309377Seric 					logdelivery("queued");
1319377Seric 				e->e_to = NULL;
1329377Seric 			}
1339387Seric #ifdef DEBUG
1349387Seric 			if (tTd(40, 1))
1359387Seric 			{
1369387Seric 				printf("queueing ");
1379387Seric 				printaddr(q, FALSE);
1389387Seric 			}
1399387Seric #endif DEBUG
1408245Seric 		}
1414632Seric 	}
1424632Seric 
1439377Seric 	/*
1449377Seric 	**  Output headers for this message.
1459377Seric 	**	Expand macros completely here.  Queue run will deal with
1469377Seric 	**	everything as absolute headers.
1479377Seric 	**		All headers that must be relative to the recipient
1489377Seric 	**		can be cracked later.
14910173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
15010173Seric 	**	no effect on the addresses as they are output.
1519377Seric 	*/
1529377Seric 
15310173Seric 	bzero(&nullmailer, sizeof nullmailer);
15410173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
155*10349Seric 	nullmailer.m_eol = "\n";
15610173Seric 
1579377Seric 	define('g', "$f", e);
1586980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
1594632Seric 	{
1604632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
1614632Seric 			continue;
1627812Seric 		fprintf(tfp, "H");
1634632Seric 		if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags))
1647812Seric 			mfdecode(h->h_mflags, tfp);
1657763Seric 		if (bitset(H_DEFAULT, h->h_flags))
1667763Seric 		{
1677763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
1688236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
1697763Seric 		}
1708245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
1719348Seric 		{
1729348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
17310173Seric 				 &nullmailer);
1749348Seric 		}
1757763Seric 		else
1768245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
1774632Seric 	}
1784632Seric 
1794632Seric 	/*
1804632Seric 	**  Clean up.
1814632Seric 	*/
1824632Seric 
1837812Seric 	(void) fclose(tfp);
1847812Seric 	qf = queuename(e, 'q');
1859377Seric 	holdsigs();
1867812Seric 	(void) unlink(qf);
1877812Seric 	if (link(tf, qf) < 0)
1887812Seric 		syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
1896980Seric 	else
1907812Seric 		(void) unlink(tf);
1919377Seric 	rlsesigs();
1927391Seric 
1937677Seric # ifdef LOG
1947677Seric 	/* save log info */
1957878Seric 	if (LogLevel > 15)
1967878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
1977677Seric # endif LOG
1984632Seric }
1994632Seric /*
2004632Seric **  RUNQUEUE -- run the jobs in the queue.
2014632Seric **
2024632Seric **	Gets the stuff out of the queue in some presumably logical
2034632Seric **	order and processes them.
2044632Seric **
2054632Seric **	Parameters:
2064632Seric **		none.
2074632Seric **
2084632Seric **	Returns:
2094632Seric **		none.
2104632Seric **
2114632Seric **	Side Effects:
2124632Seric **		runs things in the mail queue.
2134632Seric */
2144632Seric 
2154639Seric runqueue(forkflag)
2164639Seric 	bool forkflag;
2174632Seric {
2187466Seric 	/*
2197466Seric 	**  See if we want to go off and do other useful work.
2207466Seric 	*/
2214639Seric 
2224639Seric 	if (forkflag)
2234639Seric 	{
2247943Seric 		int pid;
2257943Seric 
2267943Seric 		pid = dofork();
2277943Seric 		if (pid != 0)
2284639Seric 		{
2297943Seric 			/* parent -- pick up intermediate zombie */
2309377Seric 			(void) waitfor(pid);
2317690Seric 			if (QueueIntvl != 0)
2329348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2334639Seric 			return;
2344639Seric 		}
2357943Seric 		/* child -- double fork */
2367943Seric 		if (fork() != 0)
2377943Seric 			exit(EX_OK);
2384639Seric 	}
2397876Seric # ifdef LOG
2407876Seric 	if (LogLevel > 11)
2417943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
2427876Seric # endif LOG
2434639Seric 
2447466Seric 	/*
24510205Seric 	**  Release any resources used by the daemon code.
24610205Seric 	*/
24710205Seric 
24810205Seric # ifdef DAEMON
24910205Seric 	clrdaemon();
25010205Seric # endif DAEMON
25110205Seric 
25210205Seric 	/*
2537466Seric 	**  Start making passes through the queue.
2547466Seric 	**	First, read and sort the entire queue.
2557466Seric 	**	Then, process the work in that order.
2567466Seric 	**		But if you take too long, start over.
2577466Seric 	*/
2587466Seric 
2597943Seric 	/* order the existing work requests */
26010070Seric 	(void) orderq();
2617690Seric 
2627943Seric 	/* process them once at a time */
2637943Seric 	while (WorkQ != NULL)
2644639Seric 	{
2657943Seric 		WORK *w = WorkQ;
2667881Seric 
2677943Seric 		WorkQ = WorkQ->w_next;
2687943Seric 		dowork(w);
2697943Seric 		free(w->w_name);
2707943Seric 		free((char *) w);
2714639Seric 	}
2727943Seric 	finis();
2734634Seric }
2744634Seric /*
2754632Seric **  ORDERQ -- order the work queue.
2764632Seric **
2774632Seric **	Parameters:
2784632Seric **		none.
2794632Seric **
2804632Seric **	Returns:
28110121Seric **		The number of request in the queue (not necessarily
28210121Seric **		the number of requests in WorkQ however).
2834632Seric **
2844632Seric **	Side Effects:
2854632Seric **		Sets WorkQ to the queue of available work, in order.
2864632Seric */
2874632Seric 
2884632Seric # define WLSIZE		120	/* max size of worklist per sort */
2894632Seric 
2904632Seric orderq()
2914632Seric {
2926625Sglickman 	register struct direct *d;
2934632Seric 	register WORK *w;
2944632Seric 	register WORK **wp;		/* parent of w */
2956625Sglickman 	DIR *f;
2964632Seric 	register int i;
29710121Seric 	WORK wlist[WLSIZE+1];
29810070Seric 	int wn = -1;
2994632Seric 	extern workcmpf();
3004632Seric 
3014632Seric 	/* clear out old WorkQ */
3024632Seric 	for (w = WorkQ; w != NULL; )
3034632Seric 	{
3044632Seric 		register WORK *nw = w->w_next;
3054632Seric 
3064632Seric 		WorkQ = nw;
3074632Seric 		free(w->w_name);
3084632Seric 		free((char *) w);
3094632Seric 		w = nw;
3104632Seric 	}
3114632Seric 
3124632Seric 	/* open the queue directory */
3138148Seric 	f = opendir(".");
3144632Seric 	if (f == NULL)
3154632Seric 	{
3168148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
31710070Seric 		return (0);
3184632Seric 	}
3194632Seric 
3204632Seric 	/*
3214632Seric 	**  Read the work directory.
3224632Seric 	*/
3234632Seric 
32410070Seric 	while ((d = readdir(f)) != NULL)
3254632Seric 	{
3269377Seric 		FILE *cf;
3274632Seric 		char lbuf[MAXNAME];
3284632Seric 
3294632Seric 		/* is this an interesting entry? */
3307812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3314632Seric 			continue;
3324632Seric 
33310070Seric 		/* yes -- open control file (if not too many files) */
33410070Seric 		if (++wn >= WLSIZE)
33510070Seric 			continue;
3368148Seric 		cf = fopen(d->d_name, "r");
3374632Seric 		if (cf == NULL)
3384632Seric 		{
3397055Seric 			/* this may be some random person sending hir msgs */
3407055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
34110090Seric #ifdef DEBUG
34210090Seric 			if (tTd(41, 2))
34310090Seric 				printf("orderq: cannot open %s (%d)\n",
34410090Seric 					d->d_name, errno);
34510090Seric #endif DEBUG
3467055Seric 			errno = 0;
34710090Seric 			wn--;
3484632Seric 			continue;
3494632Seric 		}
3508148Seric 		wlist[wn].w_name = newstr(d->d_name);
3514632Seric 
3524632Seric 		/* extract useful information */
3534632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
3544632Seric 		{
3559377Seric 			if (lbuf[0] == 'P')
3564632Seric 			{
3575037Seric 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
3584632Seric 				break;
3594632Seric 			}
3604632Seric 		}
3614632Seric 		(void) fclose(cf);
3624632Seric 	}
3636625Sglickman 	(void) closedir(f);
36410090Seric 	wn++;
3654632Seric 
3664632Seric 	/*
3674632Seric 	**  Sort the work directory.
3684632Seric 	*/
3694632Seric 
37010121Seric 	qsort(wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
3714632Seric 
3724632Seric 	/*
3734632Seric 	**  Convert the work list into canonical form.
3749377Seric 	**	Should be turning it into a list of envelopes here perhaps.
3754632Seric 	*/
3764632Seric 
3774632Seric 	wp = &WorkQ;
37810121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
3794632Seric 	{
3804632Seric 		w = (WORK *) xalloc(sizeof *w);
3814632Seric 		w->w_name = wlist[i].w_name;
3824632Seric 		w->w_pri = wlist[i].w_pri;
3834632Seric 		w->w_next = NULL;
3844632Seric 		*wp = w;
3854632Seric 		wp = &w->w_next;
3864632Seric 	}
3874632Seric 
3884632Seric # ifdef DEBUG
3897677Seric 	if (tTd(40, 1))
3904632Seric 	{
3914632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
3925037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
3934632Seric 	}
3944632Seric # endif DEBUG
39510070Seric 
39610090Seric 	return (wn);
3974632Seric }
3984632Seric /*
3997677Seric **  WORKCMPF -- compare function for ordering work.
4004632Seric **
4014632Seric **	Parameters:
4024632Seric **		a -- the first argument.
4034632Seric **		b -- the second argument.
4044632Seric **
4054632Seric **	Returns:
40610121Seric **		1 if a < b
4074632Seric **		0 if a == b
40810121Seric **		-1 if a > b
4094632Seric **
4104632Seric **	Side Effects:
4114632Seric **		none.
4124632Seric */
4134632Seric 
4144632Seric workcmpf(a, b)
4155037Seric 	register WORK *a;
4165037Seric 	register WORK *b;
4174632Seric {
4185037Seric 	if (a->w_pri == b->w_pri)
4194632Seric 		return (0);
4205037Seric 	else if (a->w_pri > b->w_pri)
42110121Seric 		return (-1);
42210121Seric 	else
4234632Seric 		return (1);
4244632Seric }
4254632Seric /*
4264632Seric **  DOWORK -- do a work request.
4274632Seric **
4284632Seric **	Parameters:
4294632Seric **		w -- the work request to be satisfied.
4304632Seric **
4314632Seric **	Returns:
4324632Seric **		none.
4334632Seric **
4344632Seric **	Side Effects:
4354632Seric **		The work request is satisfied if possible.
4364632Seric */
4374632Seric 
4384632Seric dowork(w)
4394632Seric 	register WORK *w;
4404632Seric {
4414632Seric 	register int i;
4424632Seric 
4434632Seric # ifdef DEBUG
4447677Seric 	if (tTd(40, 1))
4455037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
4464632Seric # endif DEBUG
4474632Seric 
4484632Seric 	/*
4494632Seric 	**  Fork for work.
4504632Seric 	*/
4514632Seric 
4524632Seric 	i = fork();
4534632Seric 	if (i < 0)
4544632Seric 	{
4554632Seric 		syserr("dowork: cannot fork");
4564632Seric 		return;
4574632Seric 	}
4584632Seric 
4594632Seric 	if (i == 0)
4604632Seric 	{
4614632Seric 		/*
4624632Seric 		**  CHILD
4638148Seric 		**	Lock the control file to avoid duplicate deliveries.
4648148Seric 		**		Then run the file as though we had just read it.
4657350Seric 		**	We save an idea of the temporary name so we
4667350Seric 		**		can recover on interrupt.
4674632Seric 		*/
4684632Seric 
4697763Seric 		/* set basic modes, etc. */
4707356Seric 		(void) alarm(0);
47110195Seric 		closexscript(CurEnv);
4729338Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
4734632Seric 		QueueRun = TRUE;
4749377Seric 		ErrorMode = EM_MAIL;
4758148Seric 		CurEnv->e_id = &w->w_name[2];
4767876Seric # ifdef LOG
4777876Seric 		if (LogLevel > 11)
4787881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
4797881Seric 			       getpid());
4807876Seric # endif LOG
4817763Seric 
4827763Seric 		/* don't use the headers from sendmail.cf... */
4837763Seric 		CurEnv->e_header = NULL;
4849348Seric 		(void) chompheader("from: $q", TRUE);
4857763Seric 
4867763Seric 		/* create the link to the control file during processing */
4877812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
4886980Seric 		{
4897812Seric 			/* being processed by another queuer */
4907881Seric # ifdef LOG
4917881Seric 			if (LogLevel > 4)
4927881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
4937881Seric # endif LOG
4946980Seric 			exit(EX_OK);
4956980Seric 		}
4966980Seric 
4976980Seric 		/* do basic system initialization */
4984632Seric 		initsys();
4996980Seric 
5006980Seric 		/* read the queue control file */
5019630Seric 		readqf(CurEnv, TRUE);
5029338Seric 		CurEnv->e_flags |= EF_INQUEUE;
5039377Seric 		eatheader(CurEnv);
5046980Seric 
5056980Seric 		/* do the delivery */
5069338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
5079282Seric 			sendall(CurEnv, SM_DELIVER);
5086980Seric 
5096980Seric 		/* finish up and exit */
5104632Seric 		finis();
5114632Seric 	}
5124632Seric 
5134632Seric 	/*
5144632Seric 	**  Parent -- pick up results.
5154632Seric 	*/
5164632Seric 
5174632Seric 	errno = 0;
5189377Seric 	(void) waitfor(i);
5194632Seric }
5204632Seric /*
5214632Seric **  READQF -- read queue file and set up environment.
5224632Seric **
5234632Seric **	Parameters:
5249377Seric **		e -- the envelope of the job to run.
5259630Seric **		full -- if set, read in all information.  Otherwise just
5269630Seric **			read in info needed for a queue print.
5274632Seric **
5284632Seric **	Returns:
5294632Seric **		none.
5304632Seric **
5314632Seric **	Side Effects:
5324632Seric **		cf is read and created as the current job, as though
5334632Seric **		we had been invoked by argument.
5344632Seric */
5354632Seric 
5369630Seric readqf(e, full)
5379377Seric 	register ENVELOPE *e;
5389630Seric 	bool full;
5394632Seric {
5404632Seric 	register FILE *f;
5417785Seric 	char buf[MAXFIELD];
5429348Seric 	extern char *fgetfolded();
5439377Seric 	register char *p;
5444632Seric 
5454632Seric 	/*
5464632Seric 	**  Open the file created by queueup.
5474632Seric 	*/
5484632Seric 
5499377Seric 	p = queuename(e, 'q');
5509377Seric 	f = fopen(p, "r");
5514632Seric 	if (f == NULL)
5524632Seric 	{
5539377Seric 		syserr("readqf: no control file %s", p);
5544632Seric 		return;
5554632Seric 	}
5569377Seric 	FileName = p;
5579377Seric 	LineNumber = 0;
5584632Seric 
5594632Seric 	/*
5604632Seric 	**  Read and process the file.
5614632Seric 	*/
5624632Seric 
5639630Seric 	if (Verbose && full)
5649377Seric 		printf("\nRunning %s\n", e->e_id);
5657785Seric 	while (fgetfolded(buf, sizeof buf, f) != NULL)
5664632Seric 	{
5674632Seric 		switch (buf[0])
5684632Seric 		{
5694632Seric 		  case 'R':		/* specify recipient */
5709618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
5714632Seric 			break;
5724632Seric 
5734632Seric 		  case 'H':		/* header */
5749630Seric 			if (full)
5759630Seric 				(void) chompheader(&buf[1], FALSE);
5764632Seric 			break;
5774632Seric 
57810108Seric 		  case 'M':		/* message */
57910108Seric 			e->e_message = newstr(&buf[1]);
58010108Seric 			break;
58110108Seric 
5824632Seric 		  case 'S':		/* sender */
5834634Seric 			setsender(newstr(&buf[1]));
5844632Seric 			break;
5854632Seric 
5864632Seric 		  case 'D':		/* data file name */
5879630Seric 			if (!full)
5889630Seric 				break;
5899377Seric 			e->e_df = newstr(&buf[1]);
5909544Seric 			e->e_dfp = fopen(e->e_df, "r");
5919544Seric 			if (e->e_dfp == NULL)
5929377Seric 				syserr("readqf: cannot open %s", e->e_df);
5934632Seric 			break;
5944632Seric 
5957860Seric 		  case 'T':		/* init time */
5969377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
5974632Seric 			break;
5984632Seric 
5994634Seric 		  case 'P':		/* message priority */
6009377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
6015037Seric 
6025037Seric 			/* make sure that big things get sent eventually */
6039377Seric 			e->e_msgpriority -= WKTIMEFACT;
6044634Seric 			break;
6054634Seric 
6064632Seric 		  default:
6079377Seric 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
6084632Seric 			break;
6094632Seric 		}
6104632Seric 	}
6119377Seric 
6129377Seric 	FileName = NULL;
6134632Seric }
6144632Seric /*
6159630Seric **  PRINTQUEUE -- print out a representation of the mail queue
6169630Seric **
6179630Seric **	Parameters:
6189630Seric **		none.
6199630Seric **
6209630Seric **	Returns:
6219630Seric **		none.
6229630Seric **
6239630Seric **	Side Effects:
6249630Seric **		Prints a listing of the mail queue on the standard output.
6259630Seric */
6265182Seric 
6279630Seric printqueue()
6289630Seric {
6299630Seric 	register WORK *w;
6309630Seric 	FILE *f;
63110070Seric 	int nrequests;
6329630Seric 	char buf[MAXLINE];
6339630Seric 
6349630Seric 	/*
6359630Seric 	**  Read and order the queue.
6369630Seric 	*/
6379630Seric 
63810070Seric 	nrequests = orderq();
6399630Seric 
6409630Seric 	/*
6419630Seric 	**  Print the work list that we have read.
6429630Seric 	*/
6439630Seric 
6449630Seric 	/* first see if there is anything */
64510070Seric 	if (nrequests <= 0)
6469630Seric 	{
64710070Seric 		printf("Mail queue is empty\n");
6489630Seric 		return;
6499630Seric 	}
6509630Seric 
65110096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
65210070Seric 	if (nrequests > WLSIZE)
65310070Seric 		printf(", only %d printed", WLSIZE);
65410070Seric 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
6559630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
6569630Seric 	{
6579630Seric 		struct stat st;
65810070Seric 		auto time_t submittime = 0;
65910070Seric 		long dfsize = -1;
66010108Seric 		char lf[20];
66110108Seric 		char message[MAXLINE];
6629630Seric 
6639630Seric 		printf("%7s", w->w_name + 2);
66410070Seric 		strcpy(lf, w->w_name);
66510070Seric 		lf[0] = 'l';
66610070Seric 		if (stat(lf, &st) >= 0)
66710070Seric 			printf("*");
66810070Seric 		else
66910070Seric 			printf(" ");
67010070Seric 		errno = 0;
6719630Seric 		f = fopen(w->w_name, "r");
6729630Seric 		if (f == NULL)
6739630Seric 		{
6749630Seric 			printf(" (finished)\n");
67510070Seric 			errno = 0;
6769630Seric 			continue;
6779630Seric 		}
67810108Seric 		message[0] = '\0';
6799630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
6809630Seric 		{
6819630Seric 			fixcrlf(buf, TRUE);
6829630Seric 			switch (buf[0])
6839630Seric 			{
68410108Seric 			  case 'M':	/* error message */
68510108Seric 				strcpy(message, &buf[1]);
68610108Seric 				break;
68710108Seric 
6889630Seric 			  case 'S':	/* sender name */
68910108Seric 				if (message[0] != '\0')
69010108Seric 				{
69110108Seric 					(void) strcat(buf, " (");
69210108Seric 					(void) strcat(buf, message);
69310108Seric 					(void) strcat(buf, ")");
69410108Seric 				}
69510070Seric 				printf("%8d %.16s %.40s", dfsize,
69610070Seric 					ctime(&submittime), &buf[1]);
6979630Seric 				break;
6989630Seric 
6999630Seric 			  case 'R':	/* recipient name */
70010070Seric 				printf("\n\t\t\t\t  %.40s", &buf[1]);
7019630Seric 				break;
7029630Seric 
7039630Seric 			  case 'T':	/* creation time */
70410070Seric 				sscanf(&buf[1], "%ld", &submittime);
7059630Seric 				break;
70610070Seric 
70710070Seric 			  case 'D':	/* data file name */
70810070Seric 				if (stat(&buf[1], &st) >= 0)
70910070Seric 					dfsize = st.st_size;
71010070Seric 				break;
7119630Seric 			}
7129630Seric 		}
71310070Seric 		if (submittime == (time_t) 0)
71410070Seric 			printf(" (no control file)");
7159630Seric 		printf("\n");
7169630Seric 		fclose(f);
7179630Seric 	}
7189630Seric }
7199630Seric 
7205182Seric # endif QUEUE
721