12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
232264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpd-job.c 157 2006-04-26 15:07:55Z ktou $ */
292264Sjacobs 
302264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
332264Sjacobs #include <stdio.h>
342264Sjacobs #include <stdlib.h>
352264Sjacobs #include <unistd.h>
362264Sjacobs #include <errno.h>
372264Sjacobs #include <limits.h>
382264Sjacobs #include <sys/types.h>
392264Sjacobs #include <sys/stat.h>
402264Sjacobs #include <fcntl.h>
412264Sjacobs #include <string.h>
422264Sjacobs #include <pwd.h>
432264Sjacobs #include <libintl.h>
442264Sjacobs #include <papi_impl.h>
452264Sjacobs 
462264Sjacobs enum { LPD_RFC, LPD_SVR4 };
472264Sjacobs 
482264Sjacobs static char
492264Sjacobs mime_type_to_rfc1179_type(char *mime)
502264Sjacobs {
512264Sjacobs 	static struct { char *mime; char rfc; } cvt[] = {
52*3125Sjacobs 		{ "text/plain", 'f' },
532264Sjacobs 		{ "application/octet-stream", 'l' },
542264Sjacobs 		{ "application/postscript", 'f' }, /* rfc incorrectly has 'o' */
552264Sjacobs 		{ "application/x-pr", 'p' },
562264Sjacobs 		{ "application/x-cif", 'c' },
572264Sjacobs 		{ "application/x-dvi", 'd' },
582264Sjacobs 		{ "application/x-fortran", 'r' },
592264Sjacobs 		{ "application/x-plot", 'g' },
602264Sjacobs 		{ "application/x-ditroff", 'n' },
612264Sjacobs 		{ "application/x-troff", 't' },
622264Sjacobs 		{ "application/x-raster", 'v' },
632264Sjacobs 		{ NULL, 0}
642264Sjacobs 	};
652264Sjacobs 	char result = '\0';
662264Sjacobs 
672264Sjacobs 	if (mime != NULL) {
682264Sjacobs 		int i;
692264Sjacobs 
702264Sjacobs 		for (i = 0; cvt[i].mime != NULL; i++)
712264Sjacobs 			if (strcasecmp(cvt[i].mime, mime) == 0) {
722264Sjacobs 				result = cvt[i].rfc;
732264Sjacobs 				break;
742264Sjacobs 			}
752264Sjacobs 	}
762264Sjacobs 
772264Sjacobs 	return (result);
782264Sjacobs }
792264Sjacobs 
802264Sjacobs static papi_status_t
812264Sjacobs add_lpd_control_line(char **metadata, char code, char *value)
822264Sjacobs {
832264Sjacobs 	size_t size = 0;
842264Sjacobs 	char line[BUFSIZ];
852264Sjacobs 
862264Sjacobs 	if ((metadata == NULL) || (value == NULL))
872264Sjacobs 		return (PAPI_BAD_REQUEST);
882264Sjacobs 
892264Sjacobs 	if (*metadata != NULL)
902264Sjacobs 		size = strlen(*metadata);
912264Sjacobs 	size += strlen(value) + 3;
922264Sjacobs 
932264Sjacobs 	if (*metadata == NULL) {
942264Sjacobs 		*metadata = (char *)calloc(1, size);
952264Sjacobs 	} else {
962264Sjacobs 		void *tmp;
972264Sjacobs 		tmp = realloc(*metadata, size);
982264Sjacobs 		if (tmp)
992264Sjacobs 			*metadata = (char *)tmp;
1002264Sjacobs 		else
1012264Sjacobs 			return (PAPI_TEMPORARY_ERROR);
1022264Sjacobs 	}
1032264Sjacobs 
1042264Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
1052264Sjacobs 	strlcat(*metadata, line, size);
1062264Sjacobs 
1072264Sjacobs 	return (PAPI_OK);
1082264Sjacobs }
1092264Sjacobs 
1102264Sjacobs static papi_status_t
1112264Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
1122264Sjacobs {
1132264Sjacobs 
1142264Sjacobs 	char line[BUFSIZ];
1152264Sjacobs 
1162264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1172264Sjacobs 		return (PAPI_BAD_REQUEST);
1182264Sjacobs 
1192264Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
1202264Sjacobs 
1212264Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
1222264Sjacobs }
1232264Sjacobs 
1242264Sjacobs static papi_status_t
1252264Sjacobs add_hpux_control_line(char **metadata, char *value)
1262264Sjacobs {
1272264Sjacobs 
1282264Sjacobs 	char line[BUFSIZ];
1292264Sjacobs 
1302264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1312264Sjacobs 		return (PAPI_BAD_REQUEST);
1322264Sjacobs 
1332264Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
1342264Sjacobs 
1352264Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
1362264Sjacobs }
1372264Sjacobs 
1382264Sjacobs static papi_status_t
1392264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
1402264Sjacobs {
1412264Sjacobs 	char buf[16];
1422264Sjacobs 
1432264Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
1442264Sjacobs 
1452264Sjacobs 	if (flag == LPD_SVR4)
1462264Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
1472264Sjacobs 	else
1482264Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
1492264Sjacobs }
1502264Sjacobs 
1512264Sjacobs static papi_status_t
1522264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
1532264Sjacobs 		char **metadata, papi_attribute_t ***used)
1542264Sjacobs {
1552264Sjacobs 	papi_status_t status = PAPI_OK;
1562264Sjacobs 	char *s;
1572264Sjacobs 	int integer;
1582264Sjacobs 	char bool;
1592264Sjacobs 	char host[BUFSIZ];
1602264Sjacobs 	char *user = "nobody";
1612264Sjacobs 	uid_t uid = getuid();
1622264Sjacobs 	struct passwd *pw;
1632264Sjacobs 
1642264Sjacobs 	if (svc == NULL)
1652264Sjacobs 		return (PAPI_BAD_REQUEST);
1662264Sjacobs 
1672264Sjacobs 	/* There is nothing to do */
1682264Sjacobs 	if (attributes == NULL)
1692264Sjacobs 		return (PAPI_OK);
1702264Sjacobs 
1712264Sjacobs 	gethostname(host, sizeof (host));
1722264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
1732264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1742264Sjacobs 			"job-originating-host-name", host);
1752264Sjacobs 
1762264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
1772264Sjacobs 		user = pw->pw_name;
1782264Sjacobs 	if (uid == 0)
1792264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
1802264Sjacobs 			&user);
1812264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1822264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1832264Sjacobs 			"job-originating-user-name", user);
1842264Sjacobs 
1852264Sjacobs 	/* Class for Banner Page */
1862264Sjacobs 	s = NULL;
1872264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
1882264Sjacobs 	if (s != NULL) {
1892264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
1902264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1912264Sjacobs 				"rfc-1179-class", s);
1922264Sjacobs 	}
1932264Sjacobs 
1942264Sjacobs 	/* Print Banner Page */
1952264Sjacobs 	s = NULL;
1962264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
1972264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
1982264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
1992264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2002264Sjacobs 				"job-sheets", s);
2012264Sjacobs 	}
2022264Sjacobs 
2032264Sjacobs 	/* Jobname */
2042264Sjacobs 	s = NULL;
2052264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
2062264Sjacobs 	if (s != NULL) {
2072264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
2082264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2092264Sjacobs 				"job-name", s);
2102264Sjacobs 	}
2112264Sjacobs 
2122264Sjacobs 	/* User to mail when job is done - lpr -m */
2132264Sjacobs 	bool = PAPI_FALSE;
2142264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
2152264Sjacobs 	if (bool == PAPI_TRUE) {
2162264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
2172264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
2182264Sjacobs 				"rfc-1179-mail", bool);
2192264Sjacobs 	}
2202264Sjacobs 
2212264Sjacobs 	/* Title for pr */
2222264Sjacobs 	s = NULL;
2232264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
2242264Sjacobs 	if (s != NULL) {
2252264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
2262264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2272264Sjacobs 				"pr-title", s);
2282264Sjacobs 	}
2292264Sjacobs 
2302264Sjacobs 	/* Indent - used with pr filter */
2312264Sjacobs 	integer = 0;
2322264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
2332264Sjacobs 	if (integer >= 1) {
2342264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
2352264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2362264Sjacobs 				"pr-indent", integer);
2372264Sjacobs 	}
2382264Sjacobs 
2392264Sjacobs 	/* Width - used with pr filter */
2402264Sjacobs 	integer = 0;
2412264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
2422264Sjacobs 	if (integer >= 1) {
2432264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
2442264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2452264Sjacobs 				"pr-width", integer);
2462264Sjacobs 	}
2472264Sjacobs 
2482264Sjacobs 	/* file with Times Roman font lpr -1	*/
2492264Sjacobs 	s = NULL;
2502264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
2512264Sjacobs 	if (s != NULL) {
2522264Sjacobs 		add_lpd_control_line(metadata, '1', s);
2532264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2542264Sjacobs 				"rfc-1179-font-r", s);
2552264Sjacobs 	}
2562264Sjacobs 
2572264Sjacobs 	/* file with Times Roman font lpr -2	*/
2582264Sjacobs 	s = NULL;
2592264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
2602264Sjacobs 	if (s != NULL) {
2612264Sjacobs 		add_lpd_control_line(metadata, '2', s);
2622264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2632264Sjacobs 				"rfc-1179-font-i", s);
2642264Sjacobs 	}
2652264Sjacobs 
2662264Sjacobs 	/* file with Times Roman font lpr -3	*/
2672264Sjacobs 	s = NULL;
2682264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
2692264Sjacobs 	if (s != NULL) {
2702264Sjacobs 		add_lpd_control_line(metadata, '3', s);
2712264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2722264Sjacobs 				"rfc-1179-font-b", s);
2732264Sjacobs 	}
2742264Sjacobs 
2752264Sjacobs 	/* file with Times Roman font lpr -4	*/
2762264Sjacobs 	s = NULL;
2772264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
2782264Sjacobs 	if (s != NULL) {
2792264Sjacobs 		add_lpd_control_line(metadata, '4', s);
2802264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2812264Sjacobs 				"rfc-1179-font-s", s);
2822264Sjacobs 	}
2832264Sjacobs 
2842264Sjacobs 	return (status);
2852264Sjacobs }
2862264Sjacobs 
2872264Sjacobs /*
2882264Sjacobs  * lpd_add_svr4_attributes
2892264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
2902264Sjacobs  */
2912264Sjacobs static papi_status_t
2922264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
2932264Sjacobs 		char **metadata, papi_attribute_t ***used)
2942264Sjacobs {
2952264Sjacobs 	char *s;
2962264Sjacobs 	int integer;
2972264Sjacobs 
2982264Sjacobs 	if (svc == NULL)
2992264Sjacobs 		return (PAPI_BAD_REQUEST);
3002264Sjacobs 
3012264Sjacobs 	/* media */
3022264Sjacobs 	s = NULL;
3032264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3042264Sjacobs 	if (s != NULL) {
3052264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3062264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3072264Sjacobs 				"media", s);
3082264Sjacobs 	}
3092264Sjacobs 
3102264Sjacobs 	/* Handling */
3112264Sjacobs 	s = NULL;
3122264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job_hold_until", &s);
3132264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
3142264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3152264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3162264Sjacobs 				"media", "hold");
3172264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
3182264Sjacobs 		add_svr4_control_line(metadata, 'H', "release");
3192264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3202264Sjacobs 				"media", "release");
3212264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "immediate"))) {
3222264Sjacobs 		add_int_control_line(metadata, 'q', 0, LPD_SVR4);
3232264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3242264Sjacobs 				"media", "immediate");
3252264Sjacobs 	}
3262264Sjacobs 
3272264Sjacobs 	/* Pages */
3282264Sjacobs 	s = NULL;
3292264Sjacobs 	papiAttributeListGetString(attributes, NULL, "page-ranges", &s);
3302264Sjacobs 	if (s != NULL) {
3312264Sjacobs 		add_svr4_control_line(metadata, 'P', s);
3322264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3332264Sjacobs 				"page-ranges", s);
3342264Sjacobs 	}
3352264Sjacobs 
3362264Sjacobs 	/* Priority : lp -q */
3372264Sjacobs 	integer = -1;
3382264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "priority", &integer);
3392264Sjacobs 	if (integer != -1) {
3402264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
3412264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
3422264Sjacobs 				"priority", integer);
3432264Sjacobs 	}
3442264Sjacobs 
3452264Sjacobs 	/* Charset : lp -S */
3462264Sjacobs 	s = NULL;
3472264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
3482264Sjacobs 	if (s != NULL) {
3492264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
3502264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3512264Sjacobs 				"lp-charset", s);
3522264Sjacobs 	}
3532264Sjacobs 
3542264Sjacobs 	/* Type : done when adding file  */
3552264Sjacobs 
3562264Sjacobs 	/* Mode : lp -y */
3572264Sjacobs 	s = NULL;
3582264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
3592264Sjacobs 	if (s != NULL) {
3602264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
3612264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3622264Sjacobs 				"lp-modes", s);
3632264Sjacobs 	}
3642264Sjacobs 
3652264Sjacobs 	/* Options lp -o */
3662264Sjacobs 	s = NULL;
3672264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-options", &s);
3682264Sjacobs 	if (s != NULL) {
3692264Sjacobs 		add_svr4_control_line(metadata, 'o', s);
3702264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3712264Sjacobs 				"lp-options", s);
3722264Sjacobs 	}
3732264Sjacobs 
3742264Sjacobs 	return (PAPI_OK);
3752264Sjacobs }
3762264Sjacobs 
3772264Sjacobs papi_status_t
3782264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
3792264Sjacobs 		char **metadata, papi_attribute_t ***used)
3802264Sjacobs {
3812264Sjacobs 	char *s = NULL;
3822264Sjacobs 
3832264Sjacobs 	/* Options lp -o */
3842264Sjacobs 	s = NULL;
3852264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-options", &s);
3862264Sjacobs 	if (s != NULL) {
3872264Sjacobs 		add_hpux_control_line(metadata, s);
3882264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3892264Sjacobs 				"lp-options", s);
3902264Sjacobs 	}
3912264Sjacobs 
3922264Sjacobs 	return (PAPI_OK);
3932264Sjacobs }
3942264Sjacobs 
3952264Sjacobs papi_status_t
3962264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
3972264Sjacobs 		char **metadata, papi_attribute_t ***used)
3982264Sjacobs {
3992264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4002264Sjacobs 		return (PAPI_BAD_REQUEST);
4012264Sjacobs 
4022264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4032264Sjacobs 
4042264Sjacobs 	if (svc->uri->fragment != NULL) {
4052264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4062264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4072264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
4082264Sjacobs 					used);
4092264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4102264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
4112264Sjacobs 						used);
4122264Sjacobs 		/*
4132264Sjacobs 		 * others could be added here:
4142264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4152264Sjacobs 		 */
4162264Sjacobs 	}
4172264Sjacobs 
4182264Sjacobs 	return (PAPI_OK);
4192264Sjacobs }
4202264Sjacobs 
4212264Sjacobs papi_status_t
4222264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4232264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4242264Sjacobs {
425*3125Sjacobs 	char *format = "text/plain";
4262264Sjacobs 	char rfc_fmt = 'l';
4272264Sjacobs 	int copies = 1;
4282264Sjacobs 	char host[BUFSIZ];
4292264Sjacobs 	int i;
4302264Sjacobs 
4312264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4322264Sjacobs 	    (metadata == NULL))
4332264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4342264Sjacobs 
4352264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
4362264Sjacobs 			&format);
4372264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4382264Sjacobs 			"document-format", format);
4392264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
4402264Sjacobs 		if ((svc->uri->fragment != NULL) &&
4412264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4422264Sjacobs 		     (strcasecmp(svc->uri->fragment, "svr4") == 0)))
4432264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
4442264Sjacobs 		rfc_fmt = 'l';
4452264Sjacobs 	}
4462264Sjacobs 
4472264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
4482264Sjacobs 	if (copies < 1)
4492264Sjacobs 		copies = 1;
4502264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
4512264Sjacobs 
4522264Sjacobs 	gethostname(host, sizeof (host));
4532264Sjacobs 
4542264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
4552264Sjacobs 		char name[BUFSIZ];
4562264Sjacobs 		char key;
4572264Sjacobs 		int j;
4582264Sjacobs 
4592264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
4602264Sjacobs 		    (access(files[i], R_OK) < 0)) {
4612264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
4622264Sjacobs 				files[i], strerror(errno));
4632264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
4642264Sjacobs 		}
4652264Sjacobs 
4662264Sjacobs 		if (i < 26)
4672264Sjacobs 			key = 'A' + i;
4682264Sjacobs 		else if (i < 52)
4692264Sjacobs 			key = 'a' + (i - 26);
4702264Sjacobs 		else if (i < 62)
4712264Sjacobs 			key = '0' + (i - 52);
4722264Sjacobs 		else {
4732264Sjacobs 			detailed_error(svc,
4742264Sjacobs 				gettext("too many files, truncated at 62"));
4752264Sjacobs 			return (PAPI_OK_SUBST);
4762264Sjacobs 		}
4772264Sjacobs 
4782264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
4792264Sjacobs 
4802264Sjacobs 		for (j = 0; j < copies; j++)
4812264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
4822264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
4832264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
4842264Sjacobs 	}
4852264Sjacobs 
4862264Sjacobs 	return (PAPI_OK);
4872264Sjacobs }
4882264Sjacobs 
4892264Sjacobs papi_status_t
4902264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
4912264Sjacobs 		int *ofd)
4922264Sjacobs {
4932264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
4942264Sjacobs 	int fd;
4952264Sjacobs 	char path[32];
4962264Sjacobs 	char *list[2];
4972264Sjacobs 
4982264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4992264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5002264Sjacobs 
5012264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5022264Sjacobs 	fd = mkstemp(path);
5032264Sjacobs 	write(fd, metadata, strlen(metadata));
5042264Sjacobs 	close(fd);
5052264Sjacobs 
5062264Sjacobs 	list[0] = path;
5072264Sjacobs 	list[1] = NULL;
5082264Sjacobs 
5092264Sjacobs 	if (((fd = lpd_open(svc, 's', list, 3)) < 0) && (errno != EBADMSG)) {
5102264Sjacobs 		switch (errno) {
5112264Sjacobs 		case ENOSPC:
5122264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5132264Sjacobs 			break;
5142264Sjacobs 		case EIO:
5152264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5162264Sjacobs 			break;
5172264Sjacobs 		case ECONNREFUSED:
5182264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5192264Sjacobs 			break;
5202264Sjacobs 		case ENOENT:
5212264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5222264Sjacobs 			break;
5232264Sjacobs 		case EBADMSG:
5242264Sjacobs 		case EBADF:
5252264Sjacobs 			status = PAPI_OK;
5262264Sjacobs 			break;
5272264Sjacobs 		default:
5282264Sjacobs 			status = PAPI_TIMEOUT;
5292264Sjacobs 			break;
5302264Sjacobs 		}
5312264Sjacobs 	} else
5322264Sjacobs 		status = PAPI_OK;
5332264Sjacobs 
5342264Sjacobs 	if (ofd != NULL)
5352264Sjacobs 		*ofd = fd;
5362264Sjacobs 	else
5372264Sjacobs 		close(fd);
5382264Sjacobs 
5392264Sjacobs 	/* read the ID and add it to to the job */
5402264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
5412264Sjacobs 		int job_id = 0;
5422264Sjacobs 		read(fd, &job_id, sizeof (job_id));
5432264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
5442264Sjacobs 				"job-id", job_id);
5452264Sjacobs 		close(fd);
5462264Sjacobs 	}
5472264Sjacobs 
5482264Sjacobs 	unlink(path);
5492264Sjacobs 
5502264Sjacobs 	return (status);
5512264Sjacobs }
552