17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
50a44ef6jacobs * Common Development and Distribution License (the "License").
60a44ef6jacobs * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
220a44ef6jacobs * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate
300a44ef6jacobs#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate/* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include "stdio.h"
347c478bdstevel@tonic-gate#include "string.h"
357c478bdstevel@tonic-gate#include "errno.h"
367c478bdstevel@tonic-gate#include "sys/types.h"
377c478bdstevel@tonic-gate#include "sys/utsname.h"
387c478bdstevel@tonic-gate#include "stdlib.h"
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#include "lp.h"
417c478bdstevel@tonic-gate#include "requests.h"
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gateextern struct {
447c478bdstevel@tonic-gate	char			*v;
457c478bdstevel@tonic-gate	short			len;
467c478bdstevel@tonic-gate}			reqheadings[];
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate/**
497c478bdstevel@tonic-gate ** putrequest() - WRITE REQUEST STRUCTURE TO DISK FILE
507c478bdstevel@tonic-gate **/
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gateint
537c478bdstevel@tonic-gate#if	defined(__STDC__)
547c478bdstevel@tonic-gateputrequest (
557c478bdstevel@tonic-gate	char *			file,
567c478bdstevel@tonic-gate	REQUEST *		reqbufp
577c478bdstevel@tonic-gate)
587c478bdstevel@tonic-gate#else
597c478bdstevel@tonic-gateputrequest (file, reqbufp)
607c478bdstevel@tonic-gate	char			*file;
617c478bdstevel@tonic-gate	REQUEST			*reqbufp;
627c478bdstevel@tonic-gate#endif
637c478bdstevel@tonic-gate{
647c478bdstevel@tonic-gate	char			**pp,
657c478bdstevel@tonic-gate				*path;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	int fd;
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate	int			fld;
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate	/*
727c478bdstevel@tonic-gate	 * First go through the structure and see if we have
737c478bdstevel@tonic-gate	 * anything strange.
747c478bdstevel@tonic-gate	 */
757c478bdstevel@tonic-gate	if (
767c478bdstevel@tonic-gate		reqbufp->copies <= 0
777c478bdstevel@tonic-gate	     || !(reqbufp->destination)
787c478bdstevel@tonic-gate	     || !reqbufp->file_list || !*(reqbufp->file_list)
797c478bdstevel@tonic-gate	     || (reqbufp->actions & (ACT_MAIL|ACT_WRITE))
807c478bdstevel@tonic-gate			&& (reqbufp->alert && *(reqbufp->alert))
817c478bdstevel@tonic-gate	     || reqbufp->priority < -1 || 39 < reqbufp->priority
827c478bdstevel@tonic-gate	) {
837c478bdstevel@tonic-gate		errno = EINVAL;
847c478bdstevel@tonic-gate		return (-1);
857c478bdstevel@tonic-gate	}
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	/*
887c478bdstevel@tonic-gate	 * Now open the file and write out the request.
897c478bdstevel@tonic-gate	 */
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate	/*
927c478bdstevel@tonic-gate	 * Full pathname? If so the file must lie in LP's
937c478bdstevel@tonic-gate	 * regular temporary directory.
947c478bdstevel@tonic-gate	 */
957c478bdstevel@tonic-gate	if (*file == '/') {
967c478bdstevel@tonic-gate		if (!STRNEQU(file, Lp_Tmp, strlen(Lp_Tmp))) {
977c478bdstevel@tonic-gate			errno = EINVAL;
987c478bdstevel@tonic-gate			return (-1);
997c478bdstevel@tonic-gate		}
1007c478bdstevel@tonic-gate		path = Strdup(file);
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	/*
1037c478bdstevel@tonic-gate	 * A relative pathname (such as system/name)?
1047c478bdstevel@tonic-gate	 * If so we'll locate it under LP's regular temporary
1057c478bdstevel@tonic-gate	 * directory.
1067c478bdstevel@tonic-gate	 */
1077c478bdstevel@tonic-gate	} else if (strchr(file, '/')) {
1087c478bdstevel@tonic-gate		if (!(path = makepath(Lp_Tmp, file, (char *)0)))
1097c478bdstevel@tonic-gate			return (-1);
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate	/*
1127c478bdstevel@tonic-gate	 * If must be a simple name. Locate this under the
1137c478bdstevel@tonic-gate	 * special temporary directory that is linked to the
1147c478bdstevel@tonic-gate	 * regular place for the local system.
1157c478bdstevel@tonic-gate	 */
1167c478bdstevel@tonic-gate	} else if (!(path = makepath(Lp_Temp, file, (char *)0)))
1177c478bdstevel@tonic-gate		return (-1);
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
1207c478bdstevel@tonic-gate		Free (path);
1217c478bdstevel@tonic-gate		return (-1);
1227c478bdstevel@tonic-gate	}
1237c478bdstevel@tonic-gate	Free (path);
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate	for (fld = 0; fld < RQ_MAX; fld++)  switch (fld) {
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate#define HEAD	reqheadings[fld].v
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	case RQ_COPIES:
1307c478bdstevel@tonic-gate		(void)fdprintf(fd, "%s%d\n", HEAD, reqbufp->copies);
1317c478bdstevel@tonic-gate		break;
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	case RQ_DEST:
1347c478bdstevel@tonic-gate		(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->destination);
1357c478bdstevel@tonic-gate		break;
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate	case RQ_FILE:
1387c478bdstevel@tonic-gate		for (pp = reqbufp->file_list; *pp; pp++)
1397c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, *pp);
1407c478bdstevel@tonic-gate		break;
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate	case RQ_FORM:
1437c478bdstevel@tonic-gate		if (reqbufp->form)
1447c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->form);
1457c478bdstevel@tonic-gate		break;
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate	case RQ_HANDL:
1487c478bdstevel@tonic-gate		if ((reqbufp->actions & ACT_SPECIAL) == ACT_IMMEDIATE)
1497c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_IMMEDIATE);
1507c478bdstevel@tonic-gate		else if ((reqbufp->actions & ACT_SPECIAL) == ACT_RESUME)
1517c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_RESUME);
1527c478bdstevel@tonic-gate		else if ((reqbufp->actions & ACT_SPECIAL) == ACT_HOLD)
1537c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, NAME_HOLD);
1547c478bdstevel@tonic-gate		break;
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate	case RQ_NOTIFY:
1577c478bdstevel@tonic-gate		if (reqbufp->actions & ACT_MAIL)
1587c478bdstevel@tonic-gate			(void)fdprintf(fd, "%sM\n", HEAD);
1597c478bdstevel@tonic-gate		else if (reqbufp->actions & ACT_WRITE)
1607c478bdstevel@tonic-gate			(void)fdprintf(fd, "%sW\n", HEAD);
1617c478bdstevel@tonic-gate		else if (reqbufp->actions & ACT_NOTIFY)
1627c478bdstevel@tonic-gate			(void)fdprintf(fd, "%sN\n", HEAD);
1637c478bdstevel@tonic-gate		else if (reqbufp->alert && *(reqbufp->alert))
1647c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->alert);
1657c478bdstevel@tonic-gate		break;
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	case RQ_OPTS:
1687c478bdstevel@tonic-gate		if (reqbufp->options)
1697c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->options);
1707c478bdstevel@tonic-gate		break;
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate	case RQ_PRIOR:
1737c478bdstevel@tonic-gate		if (reqbufp->priority != -1)
1747c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%d\n", HEAD, reqbufp->priority);
1757c478bdstevel@tonic-gate		break;
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate	case RQ_PAGES:
1787c478bdstevel@tonic-gate		if (reqbufp->pages)
1797c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->pages);
1807c478bdstevel@tonic-gate		break;
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate	case RQ_CHARS:
1837c478bdstevel@tonic-gate		if (reqbufp->charset)
1847c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->charset);
1857c478bdstevel@tonic-gate		break;
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gate	case RQ_TITLE:
1887c478bdstevel@tonic-gate		if (reqbufp->title)
1897c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->title);
1907c478bdstevel@tonic-gate		break;
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate	case RQ_MODES:
1937c478bdstevel@tonic-gate		if (reqbufp->modes)
1947c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->modes);
1957c478bdstevel@tonic-gate		break;
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	case RQ_TYPE:
1987c478bdstevel@tonic-gate		if (reqbufp->input_type)
1997c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->input_type);
2007c478bdstevel@tonic-gate		break;
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	case RQ_USER:
2037c478bdstevel@tonic-gate		if (reqbufp->user)
2047c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s%s\n", HEAD, reqbufp->user);
2057c478bdstevel@tonic-gate		break;
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate	case RQ_RAW:
2087c478bdstevel@tonic-gate		if (reqbufp->actions & ACT_RAW)
2097c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s\n", HEAD);
2107c478bdstevel@tonic-gate		break;
2117c478bdstevel@tonic-gate
2127c478bdstevel@tonic-gate	case RQ_FAST:
2137c478bdstevel@tonic-gate		if (reqbufp->actions & ACT_FAST)
2147c478bdstevel@tonic-gate			(void)fdprintf(fd, "%s\n", HEAD);
2157c478bdstevel@tonic-gate		break;
2167c478bdstevel@tonic-gate
2177c478bdstevel@tonic-gate	case RQ_STAT:
2187c478bdstevel@tonic-gate		(void)fdprintf(fd, "%s%#6.4x\n", HEAD, reqbufp->outcome);
2197c478bdstevel@tonic-gate		break;
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate	}
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate	close(fd);
2247c478bdstevel@tonic-gate	return (0);
2257c478bdstevel@tonic-gate}
226