xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 10195)
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*10195Seric SCCSID(@(#)queue.c	3.68		01/08/83	(no queueing));
95182Seric # else QUEUE
104632Seric 
11*10195Seric SCCSID(@(#)queue.c	3.68		01/08/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;
15510173Seric 
1569377Seric 	define('g', "$f", e);
1576980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
1584632Seric 	{
1594632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
1604632Seric 			continue;
1617812Seric 		fprintf(tfp, "H");
1624632Seric 		if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags))
1637812Seric 			mfdecode(h->h_mflags, tfp);
1647763Seric 		if (bitset(H_DEFAULT, h->h_flags))
1657763Seric 		{
1667763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
1678236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
1687763Seric 		}
1698245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
1709348Seric 		{
1719348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
17210173Seric 				 &nullmailer);
1739348Seric 		}
1747763Seric 		else
1758245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
1764632Seric 	}
1774632Seric 
1784632Seric 	/*
1794632Seric 	**  Clean up.
1804632Seric 	*/
1814632Seric 
1827812Seric 	(void) fclose(tfp);
1837812Seric 	qf = queuename(e, 'q');
1849377Seric 	holdsigs();
1857812Seric 	(void) unlink(qf);
1867812Seric 	if (link(tf, qf) < 0)
1877812Seric 		syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
1886980Seric 	else
1897812Seric 		(void) unlink(tf);
1909377Seric 	rlsesigs();
1917391Seric 
1927677Seric # ifdef LOG
1937677Seric 	/* save log info */
1947878Seric 	if (LogLevel > 15)
1957878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
1967677Seric # endif LOG
1974632Seric }
1984632Seric /*
1994632Seric **  RUNQUEUE -- run the jobs in the queue.
2004632Seric **
2014632Seric **	Gets the stuff out of the queue in some presumably logical
2024632Seric **	order and processes them.
2034632Seric **
2044632Seric **	Parameters:
2054632Seric **		none.
2064632Seric **
2074632Seric **	Returns:
2084632Seric **		none.
2094632Seric **
2104632Seric **	Side Effects:
2114632Seric **		runs things in the mail queue.
2124632Seric */
2134632Seric 
2144639Seric runqueue(forkflag)
2154639Seric 	bool forkflag;
2164632Seric {
2177466Seric 	/*
2187466Seric 	**  See if we want to go off and do other useful work.
2197466Seric 	*/
2204639Seric 
2214639Seric 	if (forkflag)
2224639Seric 	{
2237943Seric 		int pid;
2247943Seric 
2257943Seric 		pid = dofork();
2267943Seric 		if (pid != 0)
2274639Seric 		{
2287943Seric 			/* parent -- pick up intermediate zombie */
2299377Seric 			(void) waitfor(pid);
2307690Seric 			if (QueueIntvl != 0)
2319348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
2324639Seric 			return;
2334639Seric 		}
2347943Seric 		/* child -- double fork */
2357943Seric 		if (fork() != 0)
2367943Seric 			exit(EX_OK);
2374639Seric 	}
2387876Seric # ifdef LOG
2397876Seric 	if (LogLevel > 11)
2407943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
2417876Seric # endif LOG
2424639Seric 
2437466Seric 	/*
2447466Seric 	**  Start making passes through the queue.
2457466Seric 	**	First, read and sort the entire queue.
2467466Seric 	**	Then, process the work in that order.
2477466Seric 	**		But if you take too long, start over.
2487466Seric 	*/
2497466Seric 
2507943Seric 	/* order the existing work requests */
25110070Seric 	(void) orderq();
2527690Seric 
2537943Seric 	/* process them once at a time */
2547943Seric 	while (WorkQ != NULL)
2554639Seric 	{
2567943Seric 		WORK *w = WorkQ;
2577881Seric 
2587943Seric 		WorkQ = WorkQ->w_next;
2597943Seric 		dowork(w);
2607943Seric 		free(w->w_name);
2617943Seric 		free((char *) w);
2624639Seric 	}
2637943Seric 	finis();
2644634Seric }
2654634Seric /*
2664632Seric **  ORDERQ -- order the work queue.
2674632Seric **
2684632Seric **	Parameters:
2694632Seric **		none.
2704632Seric **
2714632Seric **	Returns:
27210121Seric **		The number of request in the queue (not necessarily
27310121Seric **		the number of requests in WorkQ however).
2744632Seric **
2754632Seric **	Side Effects:
2764632Seric **		Sets WorkQ to the queue of available work, in order.
2774632Seric */
2784632Seric 
2794632Seric # define WLSIZE		120	/* max size of worklist per sort */
2804632Seric 
2814632Seric orderq()
2824632Seric {
2836625Sglickman 	register struct direct *d;
2844632Seric 	register WORK *w;
2854632Seric 	register WORK **wp;		/* parent of w */
2866625Sglickman 	DIR *f;
2874632Seric 	register int i;
28810121Seric 	WORK wlist[WLSIZE+1];
28910070Seric 	int wn = -1;
2904632Seric 	extern workcmpf();
2914632Seric 
2924632Seric 	/* clear out old WorkQ */
2934632Seric 	for (w = WorkQ; w != NULL; )
2944632Seric 	{
2954632Seric 		register WORK *nw = w->w_next;
2964632Seric 
2974632Seric 		WorkQ = nw;
2984632Seric 		free(w->w_name);
2994632Seric 		free((char *) w);
3004632Seric 		w = nw;
3014632Seric 	}
3024632Seric 
3034632Seric 	/* open the queue directory */
3048148Seric 	f = opendir(".");
3054632Seric 	if (f == NULL)
3064632Seric 	{
3078148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
30810070Seric 		return (0);
3094632Seric 	}
3104632Seric 
3114632Seric 	/*
3124632Seric 	**  Read the work directory.
3134632Seric 	*/
3144632Seric 
31510070Seric 	while ((d = readdir(f)) != NULL)
3164632Seric 	{
3179377Seric 		FILE *cf;
3184632Seric 		char lbuf[MAXNAME];
3194632Seric 
3204632Seric 		/* is this an interesting entry? */
3217812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
3224632Seric 			continue;
3234632Seric 
32410070Seric 		/* yes -- open control file (if not too many files) */
32510070Seric 		if (++wn >= WLSIZE)
32610070Seric 			continue;
3278148Seric 		cf = fopen(d->d_name, "r");
3284632Seric 		if (cf == NULL)
3294632Seric 		{
3307055Seric 			/* this may be some random person sending hir msgs */
3317055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
33210090Seric #ifdef DEBUG
33310090Seric 			if (tTd(41, 2))
33410090Seric 				printf("orderq: cannot open %s (%d)\n",
33510090Seric 					d->d_name, errno);
33610090Seric #endif DEBUG
3377055Seric 			errno = 0;
33810090Seric 			wn--;
3394632Seric 			continue;
3404632Seric 		}
3418148Seric 		wlist[wn].w_name = newstr(d->d_name);
3424632Seric 
3434632Seric 		/* extract useful information */
3444632Seric 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
3454632Seric 		{
3469377Seric 			if (lbuf[0] == 'P')
3474632Seric 			{
3485037Seric 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
3494632Seric 				break;
3504632Seric 			}
3514632Seric 		}
3524632Seric 		(void) fclose(cf);
3534632Seric 	}
3546625Sglickman 	(void) closedir(f);
35510090Seric 	wn++;
3564632Seric 
3574632Seric 	/*
3584632Seric 	**  Sort the work directory.
3594632Seric 	*/
3604632Seric 
36110121Seric 	qsort(wlist, min(wn, WLSIZE), sizeof *wlist, workcmpf);
3624632Seric 
3634632Seric 	/*
3644632Seric 	**  Convert the work list into canonical form.
3659377Seric 	**	Should be turning it into a list of envelopes here perhaps.
3664632Seric 	*/
3674632Seric 
3684632Seric 	wp = &WorkQ;
36910121Seric 	for (i = min(wn, WLSIZE); --i >= 0; )
3704632Seric 	{
3714632Seric 		w = (WORK *) xalloc(sizeof *w);
3724632Seric 		w->w_name = wlist[i].w_name;
3734632Seric 		w->w_pri = wlist[i].w_pri;
3744632Seric 		w->w_next = NULL;
3754632Seric 		*wp = w;
3764632Seric 		wp = &w->w_next;
3774632Seric 	}
3784632Seric 
3794632Seric # ifdef DEBUG
3807677Seric 	if (tTd(40, 1))
3814632Seric 	{
3824632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
3835037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
3844632Seric 	}
3854632Seric # endif DEBUG
38610070Seric 
38710090Seric 	return (wn);
3884632Seric }
3894632Seric /*
3907677Seric **  WORKCMPF -- compare function for ordering work.
3914632Seric **
3924632Seric **	Parameters:
3934632Seric **		a -- the first argument.
3944632Seric **		b -- the second argument.
3954632Seric **
3964632Seric **	Returns:
39710121Seric **		1 if a < b
3984632Seric **		0 if a == b
39910121Seric **		-1 if a > b
4004632Seric **
4014632Seric **	Side Effects:
4024632Seric **		none.
4034632Seric */
4044632Seric 
4054632Seric workcmpf(a, b)
4065037Seric 	register WORK *a;
4075037Seric 	register WORK *b;
4084632Seric {
4095037Seric 	if (a->w_pri == b->w_pri)
4104632Seric 		return (0);
4115037Seric 	else if (a->w_pri > b->w_pri)
41210121Seric 		return (-1);
41310121Seric 	else
4144632Seric 		return (1);
4154632Seric }
4164632Seric /*
4174632Seric **  DOWORK -- do a work request.
4184632Seric **
4194632Seric **	Parameters:
4204632Seric **		w -- the work request to be satisfied.
4214632Seric **
4224632Seric **	Returns:
4234632Seric **		none.
4244632Seric **
4254632Seric **	Side Effects:
4264632Seric **		The work request is satisfied if possible.
4274632Seric */
4284632Seric 
4294632Seric dowork(w)
4304632Seric 	register WORK *w;
4314632Seric {
4324632Seric 	register int i;
4334632Seric 
4344632Seric # ifdef DEBUG
4357677Seric 	if (tTd(40, 1))
4365037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
4374632Seric # endif DEBUG
4384632Seric 
4394632Seric 	/*
4404632Seric 	**  Fork for work.
4414632Seric 	*/
4424632Seric 
4434632Seric 	i = fork();
4444632Seric 	if (i < 0)
4454632Seric 	{
4464632Seric 		syserr("dowork: cannot fork");
4474632Seric 		return;
4484632Seric 	}
4494632Seric 
4504632Seric 	if (i == 0)
4514632Seric 	{
4524632Seric 		/*
4534632Seric 		**  CHILD
4548148Seric 		**	Lock the control file to avoid duplicate deliveries.
4558148Seric 		**		Then run the file as though we had just read it.
4567350Seric 		**	We save an idea of the temporary name so we
4577350Seric 		**		can recover on interrupt.
4584632Seric 		*/
4594632Seric 
4607763Seric 		/* set basic modes, etc. */
4617356Seric 		(void) alarm(0);
462*10195Seric 		closexscript(CurEnv);
4639338Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
4644632Seric 		QueueRun = TRUE;
4659377Seric 		ErrorMode = EM_MAIL;
4668148Seric 		CurEnv->e_id = &w->w_name[2];
4677876Seric # ifdef LOG
4687876Seric 		if (LogLevel > 11)
4697881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
4707881Seric 			       getpid());
4717876Seric # endif LOG
4727763Seric 
4737763Seric 		/* don't use the headers from sendmail.cf... */
4747763Seric 		CurEnv->e_header = NULL;
4759348Seric 		(void) chompheader("from: $q", TRUE);
4767763Seric 
4777763Seric 		/* create the link to the control file during processing */
4787812Seric 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
4796980Seric 		{
4807812Seric 			/* being processed by another queuer */
4817881Seric # ifdef LOG
4827881Seric 			if (LogLevel > 4)
4837881Seric 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
4847881Seric # endif LOG
4856980Seric 			exit(EX_OK);
4866980Seric 		}
4876980Seric 
4886980Seric 		/* do basic system initialization */
4894632Seric 		initsys();
4906980Seric 
4916980Seric 		/* read the queue control file */
4929630Seric 		readqf(CurEnv, TRUE);
4939338Seric 		CurEnv->e_flags |= EF_INQUEUE;
4949377Seric 		eatheader(CurEnv);
4956980Seric 
4966980Seric 		/* do the delivery */
4979338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
4989282Seric 			sendall(CurEnv, SM_DELIVER);
4996980Seric 
5006980Seric 		/* finish up and exit */
5014632Seric 		finis();
5024632Seric 	}
5034632Seric 
5044632Seric 	/*
5054632Seric 	**  Parent -- pick up results.
5064632Seric 	*/
5074632Seric 
5084632Seric 	errno = 0;
5099377Seric 	(void) waitfor(i);
5104632Seric }
5114632Seric /*
5124632Seric **  READQF -- read queue file and set up environment.
5134632Seric **
5144632Seric **	Parameters:
5159377Seric **		e -- the envelope of the job to run.
5169630Seric **		full -- if set, read in all information.  Otherwise just
5179630Seric **			read in info needed for a queue print.
5184632Seric **
5194632Seric **	Returns:
5204632Seric **		none.
5214632Seric **
5224632Seric **	Side Effects:
5234632Seric **		cf is read and created as the current job, as though
5244632Seric **		we had been invoked by argument.
5254632Seric */
5264632Seric 
5279630Seric readqf(e, full)
5289377Seric 	register ENVELOPE *e;
5299630Seric 	bool full;
5304632Seric {
5314632Seric 	register FILE *f;
5327785Seric 	char buf[MAXFIELD];
5339348Seric 	extern char *fgetfolded();
5349377Seric 	register char *p;
5354632Seric 
5364632Seric 	/*
5374632Seric 	**  Open the file created by queueup.
5384632Seric 	*/
5394632Seric 
5409377Seric 	p = queuename(e, 'q');
5419377Seric 	f = fopen(p, "r");
5424632Seric 	if (f == NULL)
5434632Seric 	{
5449377Seric 		syserr("readqf: no control file %s", p);
5454632Seric 		return;
5464632Seric 	}
5479377Seric 	FileName = p;
5489377Seric 	LineNumber = 0;
5494632Seric 
5504632Seric 	/*
5514632Seric 	**  Read and process the file.
5524632Seric 	*/
5534632Seric 
5549630Seric 	if (Verbose && full)
5559377Seric 		printf("\nRunning %s\n", e->e_id);
5567785Seric 	while (fgetfolded(buf, sizeof buf, f) != NULL)
5574632Seric 	{
5584632Seric 		switch (buf[0])
5594632Seric 		{
5604632Seric 		  case 'R':		/* specify recipient */
5619618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
5624632Seric 			break;
5634632Seric 
5644632Seric 		  case 'H':		/* header */
5659630Seric 			if (full)
5669630Seric 				(void) chompheader(&buf[1], FALSE);
5674632Seric 			break;
5684632Seric 
56910108Seric 		  case 'M':		/* message */
57010108Seric 			e->e_message = newstr(&buf[1]);
57110108Seric 			break;
57210108Seric 
5734632Seric 		  case 'S':		/* sender */
5744634Seric 			setsender(newstr(&buf[1]));
5754632Seric 			break;
5764632Seric 
5774632Seric 		  case 'D':		/* data file name */
5789630Seric 			if (!full)
5799630Seric 				break;
5809377Seric 			e->e_df = newstr(&buf[1]);
5819544Seric 			e->e_dfp = fopen(e->e_df, "r");
5829544Seric 			if (e->e_dfp == NULL)
5839377Seric 				syserr("readqf: cannot open %s", e->e_df);
5844632Seric 			break;
5854632Seric 
5867860Seric 		  case 'T':		/* init time */
5879377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
5884632Seric 			break;
5894632Seric 
5904634Seric 		  case 'P':		/* message priority */
5919377Seric 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
5925037Seric 
5935037Seric 			/* make sure that big things get sent eventually */
5949377Seric 			e->e_msgpriority -= WKTIMEFACT;
5954634Seric 			break;
5964634Seric 
5974632Seric 		  default:
5989377Seric 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
5994632Seric 			break;
6004632Seric 		}
6014632Seric 	}
6029377Seric 
6039377Seric 	FileName = NULL;
6044632Seric }
6054632Seric /*
6069630Seric **  PRINTQUEUE -- print out a representation of the mail queue
6079630Seric **
6089630Seric **	Parameters:
6099630Seric **		none.
6109630Seric **
6119630Seric **	Returns:
6129630Seric **		none.
6139630Seric **
6149630Seric **	Side Effects:
6159630Seric **		Prints a listing of the mail queue on the standard output.
6169630Seric */
6175182Seric 
6189630Seric printqueue()
6199630Seric {
6209630Seric 	register WORK *w;
6219630Seric 	FILE *f;
62210070Seric 	int nrequests;
6239630Seric 	char buf[MAXLINE];
6249630Seric 
6259630Seric 	/*
6269630Seric 	**  Read and order the queue.
6279630Seric 	*/
6289630Seric 
62910070Seric 	nrequests = orderq();
6309630Seric 
6319630Seric 	/*
6329630Seric 	**  Print the work list that we have read.
6339630Seric 	*/
6349630Seric 
6359630Seric 	/* first see if there is anything */
63610070Seric 	if (nrequests <= 0)
6379630Seric 	{
63810070Seric 		printf("Mail queue is empty\n");
6399630Seric 		return;
6409630Seric 	}
6419630Seric 
64210096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
64310070Seric 	if (nrequests > WLSIZE)
64410070Seric 		printf(", only %d printed", WLSIZE);
64510070Seric 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
6469630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
6479630Seric 	{
6489630Seric 		struct stat st;
64910070Seric 		auto time_t submittime = 0;
65010070Seric 		long dfsize = -1;
65110108Seric 		char lf[20];
65210108Seric 		char message[MAXLINE];
6539630Seric 
6549630Seric 		printf("%7s", w->w_name + 2);
65510070Seric 		strcpy(lf, w->w_name);
65610070Seric 		lf[0] = 'l';
65710070Seric 		if (stat(lf, &st) >= 0)
65810070Seric 			printf("*");
65910070Seric 		else
66010070Seric 			printf(" ");
66110070Seric 		errno = 0;
6629630Seric 		f = fopen(w->w_name, "r");
6639630Seric 		if (f == NULL)
6649630Seric 		{
6659630Seric 			printf(" (finished)\n");
66610070Seric 			errno = 0;
6679630Seric 			continue;
6689630Seric 		}
66910108Seric 		message[0] = '\0';
6709630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
6719630Seric 		{
6729630Seric 			fixcrlf(buf, TRUE);
6739630Seric 			switch (buf[0])
6749630Seric 			{
67510108Seric 			  case 'M':	/* error message */
67610108Seric 				strcpy(message, &buf[1]);
67710108Seric 				break;
67810108Seric 
6799630Seric 			  case 'S':	/* sender name */
68010108Seric 				if (message[0] != '\0')
68110108Seric 				{
68210108Seric 					(void) strcat(buf, " (");
68310108Seric 					(void) strcat(buf, message);
68410108Seric 					(void) strcat(buf, ")");
68510108Seric 				}
68610070Seric 				printf("%8d %.16s %.40s", dfsize,
68710070Seric 					ctime(&submittime), &buf[1]);
6889630Seric 				break;
6899630Seric 
6909630Seric 			  case 'R':	/* recipient name */
69110070Seric 				printf("\n\t\t\t\t  %.40s", &buf[1]);
6929630Seric 				break;
6939630Seric 
6949630Seric 			  case 'T':	/* creation time */
69510070Seric 				sscanf(&buf[1], "%ld", &submittime);
6969630Seric 				break;
69710070Seric 
69810070Seric 			  case 'D':	/* data file name */
69910070Seric 				if (stat(&buf[1], &st) >= 0)
70010070Seric 					dfsize = st.st_size;
70110070Seric 				break;
7029630Seric 			}
7039630Seric 		}
70410070Seric 		if (submittime == (time_t) 0)
70510070Seric 			printf(" (no control file)");
7069630Seric 		printf("\n");
7079630Seric 		fclose(f);
7089630Seric 	}
7099630Seric }
7109630Seric 
7115182Seric # endif QUEUE
712