10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3125Sjacobs  * Common Development and Distribution License (the "License").
6*3125Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3125Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
30*3125Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "stdio.h"
340Sstevel@tonic-gate #include "string.h"
350Sstevel@tonic-gate #include "errno.h"
360Sstevel@tonic-gate #include "sys/types.h"
370Sstevel@tonic-gate #include "sys/utsname.h"
380Sstevel@tonic-gate #include "stdlib.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "lp.h"
410Sstevel@tonic-gate #include "requests.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate extern struct {
440Sstevel@tonic-gate 	char			*v;
450Sstevel@tonic-gate 	short			len;
460Sstevel@tonic-gate }			reqheadings[];
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /**
490Sstevel@tonic-gate  ** putrequest() - WRITE REQUEST STRUCTURE TO DISK FILE
500Sstevel@tonic-gate  **/
510Sstevel@tonic-gate 
520Sstevel@tonic-gate int
530Sstevel@tonic-gate #if	defined(__STDC__)
540Sstevel@tonic-gate putrequest (
550Sstevel@tonic-gate 	char *			file,
560Sstevel@tonic-gate 	REQUEST *		reqbufp
570Sstevel@tonic-gate )
580Sstevel@tonic-gate #else
590Sstevel@tonic-gate putrequest (file, reqbufp)
600Sstevel@tonic-gate 	char			*file;
610Sstevel@tonic-gate 	REQUEST			*reqbufp;
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	char			**pp,
650Sstevel@tonic-gate 				*path;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	int fd;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	int			fld;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * First go through the structure and see if we have
730Sstevel@tonic-gate 	 * anything strange.
740Sstevel@tonic-gate 	 */
750Sstevel@tonic-gate 	if (
760Sstevel@tonic-gate 		reqbufp->copies <= 0
770Sstevel@tonic-gate 	     || !(reqbufp->destination)
780Sstevel@tonic-gate 	     || !reqbufp->file_list || !*(reqbufp->file_list)
790Sstevel@tonic-gate 	     || (reqbufp->actions & (ACT_MAIL|ACT_WRITE))
800Sstevel@tonic-gate 			&& (reqbufp->alert && *(reqbufp->alert))
810Sstevel@tonic-gate 	     || reqbufp->priority < -1 || 39 < reqbufp->priority
820Sstevel@tonic-gate 	) {
830Sstevel@tonic-gate 		errno = EINVAL;
840Sstevel@tonic-gate 		return (-1);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * Now open the file and write out the request.
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * Full pathname? If so the file must lie in LP's
930Sstevel@tonic-gate 	 * regular temporary directory.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	if (*file == '/') {
960Sstevel@tonic-gate 		if (!STRNEQU(file, Lp_Tmp, strlen(Lp_Tmp))) {
970Sstevel@tonic-gate 			errno = EINVAL;
980Sstevel@tonic-gate 			return (-1);
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 		path = Strdup(file);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	/*
1030Sstevel@tonic-gate 	 * A relative pathname (such as system/name)?
1040Sstevel@tonic-gate 	 * If so we'll locate it under LP's regular temporary
1050Sstevel@tonic-gate 	 * directory.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	} else if (strchr(file, '/')) {
1080Sstevel@tonic-gate 		if (!(path = makepath(Lp_Tmp, file, (char *)0)))
1090Sstevel@tonic-gate 			return (-1);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * If must be a simple name. Locate this under the
1130Sstevel@tonic-gate 	 * special temporary directory that is linked to the
1140Sstevel@tonic-gate 	 * regular place for the local system.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	} else if (!(path = makepath(Lp_Temp, file, (char *)0)))
1170Sstevel@tonic-gate 		return (-1);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
1200Sstevel@tonic-gate 		Free (path);
1210Sstevel@tonic-gate 		return (-1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	Free (path);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	for (fld = 0; fld < RQ_MAX; fld++)  switch (fld) {
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define HEAD	reqheadings[fld].v
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	case RQ_COPIES:
1300Sstevel@tonic-gate 		(void)fdprintf(fd, "%s%d\n", HEAD, reqbufp->copies);
1310Sstevel@tonic-gate 		break;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	case RQ_DEST:
1340Sstevel@tonic-gate 		(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->destination);
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	case RQ_FILE:
1380Sstevel@tonic-gate 		for (pp = reqbufp->file_list; *pp; pp++)
1390Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, *pp);
1400Sstevel@tonic-gate 		break;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	case RQ_FORM:
1430Sstevel@tonic-gate 		if (reqbufp->form)
1440Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->form);
1450Sstevel@tonic-gate 		break;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	case RQ_HANDL:
1480Sstevel@tonic-gate 		if ((reqbufp->actions & ACT_SPECIAL) == ACT_IMMEDIATE)
1490Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_IMMEDIATE);
1500Sstevel@tonic-gate 		else if ((reqbufp->actions & ACT_SPECIAL) == ACT_RESUME)
1510Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_RESUME);
1520Sstevel@tonic-gate 		else if ((reqbufp->actions & ACT_SPECIAL) == ACT_HOLD)
1530Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_HOLD);
1540Sstevel@tonic-gate 		break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	case RQ_NOTIFY:
1570Sstevel@tonic-gate 		if (reqbufp->actions & ACT_MAIL)
1580Sstevel@tonic-gate 			(void)fdprintf(fd, "%sM\n", HEAD);
1590Sstevel@tonic-gate 		else if (reqbufp->actions & ACT_WRITE)
1600Sstevel@tonic-gate 			(void)fdprintf(fd, "%sW\n", HEAD);
1610Sstevel@tonic-gate 		else if (reqbufp->actions & ACT_NOTIFY)
1620Sstevel@tonic-gate 			(void)fdprintf(fd, "%sN\n", HEAD);
1630Sstevel@tonic-gate 		else if (reqbufp->alert && *(reqbufp->alert))
1640Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->alert);
1650Sstevel@tonic-gate 		break;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	case RQ_OPTS:
1680Sstevel@tonic-gate 		if (reqbufp->options)
1690Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->options);
1700Sstevel@tonic-gate 		break;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	case RQ_PRIOR:
1730Sstevel@tonic-gate 		if (reqbufp->priority != -1)
1740Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%d\n", HEAD, reqbufp->priority);
1750Sstevel@tonic-gate 		break;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	case RQ_PAGES:
1780Sstevel@tonic-gate 		if (reqbufp->pages)
1790Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->pages);
1800Sstevel@tonic-gate 		break;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	case RQ_CHARS:
1830Sstevel@tonic-gate 		if (reqbufp->charset)
1840Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->charset);
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	case RQ_TITLE:
1880Sstevel@tonic-gate 		if (reqbufp->title)
1890Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->title);
1900Sstevel@tonic-gate 		break;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	case RQ_MODES:
1930Sstevel@tonic-gate 		if (reqbufp->modes)
1940Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->modes);
1950Sstevel@tonic-gate 		break;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	case RQ_TYPE:
1980Sstevel@tonic-gate 		if (reqbufp->input_type)
1990Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->input_type);
2000Sstevel@tonic-gate 		break;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	case RQ_USER:
2030Sstevel@tonic-gate 		if (reqbufp->user)
2040Sstevel@tonic-gate 			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->user);
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	case RQ_RAW:
2080Sstevel@tonic-gate 		if (reqbufp->actions & ACT_RAW)
2090Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", HEAD);
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	case RQ_FAST:
2130Sstevel@tonic-gate 		if (reqbufp->actions & ACT_FAST)
2140Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", HEAD);
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	case RQ_STAT:
2180Sstevel@tonic-gate 		(void)fdprintf(fd, "%s%#6.4x\n", HEAD, reqbufp->outcome);
2190Sstevel@tonic-gate 		break;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	close(fd);
2240Sstevel@tonic-gate 	return (0);
2250Sstevel@tonic-gate }
226