xref: /titanic_54/usr/src/cmd/print/bsd-sysv-commands/common.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: common.c 162 2006-05-08 14:17:44Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <alloca.h>
36*355b4669Sjacobs #include <string.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <ctype.h>
39*355b4669Sjacobs #include <pwd.h>
40*355b4669Sjacobs #include <papi.h>
41*355b4669Sjacobs #include "common.h"
42*355b4669Sjacobs 
43*355b4669Sjacobs #ifndef HAVE_GETPASSPHRASE	/* some systems don't have getpassphrase() */
44*355b4669Sjacobs #define	getpassphrase getpass
45*355b4669Sjacobs #endif
46*355b4669Sjacobs 
47*355b4669Sjacobs /* give the most verbose error message to the caller */
48*355b4669Sjacobs char *
49*355b4669Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status)
50*355b4669Sjacobs {
51*355b4669Sjacobs 	char *mesg;
52*355b4669Sjacobs 
53*355b4669Sjacobs 	mesg = papiServiceGetStatusMessage(svc);
54*355b4669Sjacobs 
55*355b4669Sjacobs 	if (mesg == NULL)
56*355b4669Sjacobs 		mesg = papiStatusString(status);
57*355b4669Sjacobs 
58*355b4669Sjacobs 	return (mesg);
59*355b4669Sjacobs }
60*355b4669Sjacobs 
61*355b4669Sjacobs static int
62*355b4669Sjacobs match_job(int id, char *user, int ac, char *av[])
63*355b4669Sjacobs {
64*355b4669Sjacobs 	int i;
65*355b4669Sjacobs 
66*355b4669Sjacobs 	for (i = 0; i < ac; i++)
67*355b4669Sjacobs 		if (strcmp("-", av[i]) == 0)
68*355b4669Sjacobs 			return (0);	/* "current" user match */
69*355b4669Sjacobs 		else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i])))
70*355b4669Sjacobs 			return (0);	/* job-id match */
71*355b4669Sjacobs 		else if (strcmp(user, av[i]) == 0)
72*355b4669Sjacobs 			return (0);	/* user match */
73*355b4669Sjacobs 
74*355b4669Sjacobs 	return (-1);
75*355b4669Sjacobs }
76*355b4669Sjacobs 
77*355b4669Sjacobs /*
78*355b4669Sjacobs  * to support job/printer status
79*355b4669Sjacobs  */
80*355b4669Sjacobs static char *
81*355b4669Sjacobs state_string(int state)
82*355b4669Sjacobs {
83*355b4669Sjacobs 	switch (state) {
84*355b4669Sjacobs 	case 3:
85*355b4669Sjacobs 		return (gettext("idle"));
86*355b4669Sjacobs 	case 4:
87*355b4669Sjacobs 		return (gettext("processing"));
88*355b4669Sjacobs 	case 5:
89*355b4669Sjacobs 		return (gettext("stopped"));
90*355b4669Sjacobs 	default:
91*355b4669Sjacobs 		return (gettext("unknown"));
92*355b4669Sjacobs 	}
93*355b4669Sjacobs }
94*355b4669Sjacobs 
95*355b4669Sjacobs static char *_rank_suffixes[] = {
96*355b4669Sjacobs 	"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
97*355b4669Sjacobs };
98*355b4669Sjacobs 
99*355b4669Sjacobs static char *
100*355b4669Sjacobs rank_string(const int rank)
101*355b4669Sjacobs {
102*355b4669Sjacobs 	static char buf[12];
103*355b4669Sjacobs 
104*355b4669Sjacobs 	if (rank < 0)
105*355b4669Sjacobs 		snprintf(buf, sizeof (buf), gettext("invalid"));
106*355b4669Sjacobs 	else if (rank == 0)
107*355b4669Sjacobs 		snprintf(buf, sizeof (buf), gettext("active"));
108*355b4669Sjacobs 	else if ((rank > 10) && (rank < 14))
109*355b4669Sjacobs 		sprintf(buf, "%dth", rank);
110*355b4669Sjacobs 	else
111*355b4669Sjacobs 		sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
112*355b4669Sjacobs 
113*355b4669Sjacobs 	return (buf);
114*355b4669Sjacobs }
115*355b4669Sjacobs 
116*355b4669Sjacobs static void
117*355b4669Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
118*355b4669Sjacobs {
119*355b4669Sjacobs 	papi_attribute_t **list = papiPrinterGetAttributeList(p);
120*355b4669Sjacobs 	int state = 0;
121*355b4669Sjacobs 	char *reason = "";
122*355b4669Sjacobs 
123*355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
124*355b4669Sjacobs 				"printer-state", &state);
125*355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
126*355b4669Sjacobs 				"printer-state-reasons", &reason);
127*355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
128*355b4669Sjacobs 				"printer-name", &name);
129*355b4669Sjacobs 
130*355b4669Sjacobs 	if ((state != 0x03) || (num_jobs != 0)) {
131*355b4669Sjacobs 		fprintf(fp, "%s: %s", name, state_string(state));
132*355b4669Sjacobs 		if (state == 0x05) /* stopped */
133*355b4669Sjacobs 			fprintf(fp, ": %s\n", reason);
134*355b4669Sjacobs 		else
135*355b4669Sjacobs 			fprintf(fp, "\n");
136*355b4669Sjacobs 	} else
137*355b4669Sjacobs 		fprintf(fp, "no entries\n");
138*355b4669Sjacobs }
139*355b4669Sjacobs 
140*355b4669Sjacobs static void
141*355b4669Sjacobs print_header(FILE *fp)
142*355b4669Sjacobs {
143*355b4669Sjacobs 	fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
144*355b4669Sjacobs }
145*355b4669Sjacobs 
146*355b4669Sjacobs static void
147*355b4669Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
148*355b4669Sjacobs {
149*355b4669Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
150*355b4669Sjacobs 	int copies = 1, id = 0, rank = count, size = 0;
151*355b4669Sjacobs 	char *name = "print job";
152*355b4669Sjacobs 	char *user = "nobody";
153*355b4669Sjacobs 	char *host = "localhost";
154*355b4669Sjacobs 	char *suffix = "k";
155*355b4669Sjacobs 
156*355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
157*355b4669Sjacobs 					"job-id", &id);
158*355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
159*355b4669Sjacobs 					"job-originating-user-name", &user);
160*355b4669Sjacobs 
161*355b4669Sjacobs 	/* if we are looking and it doesn't match, return early */
162*355b4669Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
163*355b4669Sjacobs 		return;
164*355b4669Sjacobs 
165*355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
166*355b4669Sjacobs 					"copies", &copies);
167*355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
168*355b4669Sjacobs 					"number-of-intervening-jobs", &rank);
169*355b4669Sjacobs 
170*355b4669Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
171*355b4669Sjacobs 			== PAPI_OK)
172*355b4669Sjacobs 		suffix = "bytes";
173*355b4669Sjacobs 	else
174*355b4669Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
175*355b4669Sjacobs 					"job-k-octets", &size);
176*355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
177*355b4669Sjacobs 					"job-name", &name);
178*355b4669Sjacobs 
179*355b4669Sjacobs 	size *= copies;
180*355b4669Sjacobs 
181*355b4669Sjacobs 	if (fmt == 3) {
182*355b4669Sjacobs 		fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
183*355b4669Sjacobs 			rank_string(++rank), user, id, name, size, suffix);
184*355b4669Sjacobs 	} else
185*355b4669Sjacobs 		fprintf(fp, gettext(
186*355b4669Sjacobs 			"\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
187*355b4669Sjacobs 			user, rank_string(++rank), id, host, name, size,
188*355b4669Sjacobs 			suffix);
189*355b4669Sjacobs }
190*355b4669Sjacobs 
191*355b4669Sjacobs /*
192*355b4669Sjacobs  * to support job cancelation
193*355b4669Sjacobs  */
194*355b4669Sjacobs static void
195*355b4669Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
196*355b4669Sjacobs 		int ac, char *av[])
197*355b4669Sjacobs {
198*355b4669Sjacobs 	papi_status_t status;
199*355b4669Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
200*355b4669Sjacobs 	int id = 0;
201*355b4669Sjacobs 	char *user = "";
202*355b4669Sjacobs 	char *mesg = gettext("cancelled");
203*355b4669Sjacobs 
204*355b4669Sjacobs 	papiAttributeListGetInteger(list, NULL,
205*355b4669Sjacobs 					"job-id", &id);
206*355b4669Sjacobs 	papiAttributeListGetString(list, NULL,
207*355b4669Sjacobs 					"job-originating-user-name", &user);
208*355b4669Sjacobs 
209*355b4669Sjacobs 	/* if we are looking and it doesn't match, return early */
210*355b4669Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
211*355b4669Sjacobs 		return;
212*355b4669Sjacobs 
213*355b4669Sjacobs 	status = papiJobCancel(svc, printer, id);
214*355b4669Sjacobs 	if (status != PAPI_OK)
215*355b4669Sjacobs 		mesg = papiStatusString(status);
216*355b4669Sjacobs 
217*355b4669Sjacobs 	fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
218*355b4669Sjacobs }
219*355b4669Sjacobs 
220*355b4669Sjacobs int
221*355b4669Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
222*355b4669Sjacobs 		int ac, char *av[])
223*355b4669Sjacobs {
224*355b4669Sjacobs 	papi_status_t status;
225*355b4669Sjacobs 	papi_printer_t p = NULL;
226*355b4669Sjacobs 	papi_job_t *jobs = NULL;
227*355b4669Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
228*355b4669Sjacobs 			"printer-state-reasons", NULL };
229*355b4669Sjacobs 	char *jattrs[] = {
230*355b4669Sjacobs 			"job-name", "job-octets", "job-k-octets",
231*355b4669Sjacobs 			"job-originating-user-name", "job-id",
232*355b4669Sjacobs 			"number-of-intervening-jobs", NULL };
233*355b4669Sjacobs 	int num_jobs = 0;
234*355b4669Sjacobs 
235*355b4669Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
236*355b4669Sjacobs 	if (status != PAPI_OK) {
237*355b4669Sjacobs 		fprintf(fp, gettext(
238*355b4669Sjacobs 			"Failed to query service for state of %s: %s\n"),
239*355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
240*355b4669Sjacobs 		return (-1);
241*355b4669Sjacobs 	}
242*355b4669Sjacobs 
243*355b4669Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
244*355b4669Sjacobs 					0, &jobs);
245*355b4669Sjacobs 	if (status != PAPI_OK) {
246*355b4669Sjacobs 		fprintf(fp, gettext(
247*355b4669Sjacobs 			"Failed to query service for jobs on %s: %s\n"),
248*355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
249*355b4669Sjacobs 		return (-1);
250*355b4669Sjacobs 	}
251*355b4669Sjacobs 	if (jobs != NULL) {
252*355b4669Sjacobs 		while (jobs[num_jobs] != NULL)
253*355b4669Sjacobs 			num_jobs++;
254*355b4669Sjacobs 	}
255*355b4669Sjacobs 
256*355b4669Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
257*355b4669Sjacobs 	if (num_jobs > 0) {
258*355b4669Sjacobs 		int i;
259*355b4669Sjacobs 
260*355b4669Sjacobs 		if (fmt == 3)
261*355b4669Sjacobs 			print_header(fp);
262*355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
263*355b4669Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
264*355b4669Sjacobs 	}
265*355b4669Sjacobs 
266*355b4669Sjacobs 	papiPrinterFree(p);
267*355b4669Sjacobs 	papiJobListFree(jobs);
268*355b4669Sjacobs 
269*355b4669Sjacobs 	return (num_jobs);
270*355b4669Sjacobs }
271*355b4669Sjacobs 
272*355b4669Sjacobs int
273*355b4669Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
274*355b4669Sjacobs 		int ac, char *av[])
275*355b4669Sjacobs {
276*355b4669Sjacobs 	papi_status_t status;
277*355b4669Sjacobs 	papi_job_t *jobs = NULL;
278*355b4669Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id", NULL };
279*355b4669Sjacobs 
280*355b4669Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
281*355b4669Sjacobs 					0, &jobs);
282*355b4669Sjacobs 
283*355b4669Sjacobs 	if (status != PAPI_OK) {
284*355b4669Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
285*355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
286*355b4669Sjacobs 		return (-1);
287*355b4669Sjacobs 	}
288*355b4669Sjacobs 
289*355b4669Sjacobs 	/* cancel the job(s) */
290*355b4669Sjacobs 	if (jobs != NULL) {
291*355b4669Sjacobs 		int i;
292*355b4669Sjacobs 
293*355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
294*355b4669Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
295*355b4669Sjacobs 	}
296*355b4669Sjacobs 
297*355b4669Sjacobs 	papiJobListFree(jobs);
298*355b4669Sjacobs 
299*355b4669Sjacobs 	return (0);
300*355b4669Sjacobs }
301*355b4669Sjacobs 
302*355b4669Sjacobs int
303*355b4669Sjacobs get_printer_id(char *name, char **printer, int *id)
304*355b4669Sjacobs {
305*355b4669Sjacobs 	int result = -1;
306*355b4669Sjacobs 
307*355b4669Sjacobs 	if (name != NULL) {
308*355b4669Sjacobs 		char *p = strrchr(name, '-');
309*355b4669Sjacobs 
310*355b4669Sjacobs 		*printer = name;
311*355b4669Sjacobs 		if (p != NULL) {
312*355b4669Sjacobs 			char *s = NULL;
313*355b4669Sjacobs 
314*355b4669Sjacobs 			*id = strtol(p + 1, &s, 10);
315*355b4669Sjacobs 			if (s[0] != '\0')
316*355b4669Sjacobs 				*id = -1;
317*355b4669Sjacobs 			else
318*355b4669Sjacobs 				*p = '\0';
319*355b4669Sjacobs 			result = 0;
320*355b4669Sjacobs 		} else
321*355b4669Sjacobs 			*id = -1;
322*355b4669Sjacobs 	}
323*355b4669Sjacobs 
324*355b4669Sjacobs 	return (result);
325*355b4669Sjacobs }
326*355b4669Sjacobs 
327*355b4669Sjacobs /*
328*355b4669Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
329*355b4669Sjacobs  * determined by a seperator.  The original string is modified, and newly
330*355b4669Sjacobs  * allocated space is only returned for the array itself.  If more than
331*355b4669Sjacobs  * 1024 substrings exist, they will be ignored.
332*355b4669Sjacobs  */
333*355b4669Sjacobs char **
334*355b4669Sjacobs strsplit(char *string, const char *seperators)
335*355b4669Sjacobs {
336*355b4669Sjacobs 	char	*list[BUFSIZ],
337*355b4669Sjacobs 		**result;
338*355b4669Sjacobs 	int	length = 0;
339*355b4669Sjacobs 
340*355b4669Sjacobs 	if ((string == NULL) || (seperators == NULL))
341*355b4669Sjacobs 		return (NULL);
342*355b4669Sjacobs 
343*355b4669Sjacobs 	(void) memset(list, 0, sizeof (list));
344*355b4669Sjacobs 	for (list[length] = strtok(string, seperators);
345*355b4669Sjacobs 		(list[length] != NULL) && (length < (BUFSIZ - 2));
346*355b4669Sjacobs 		list[length] = strtok(NULL, seperators))
347*355b4669Sjacobs 			length++;
348*355b4669Sjacobs 
349*355b4669Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
350*355b4669Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
351*355b4669Sjacobs 
352*355b4669Sjacobs 	return (result);
353*355b4669Sjacobs }
354*355b4669Sjacobs 
355*355b4669Sjacobs papi_status_t
356*355b4669Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, papi_attribute_t **list,
357*355b4669Sjacobs 		papi_job_t *job)
358*355b4669Sjacobs {
359*355b4669Sjacobs 	papi_status_t status;
360*355b4669Sjacobs 	papi_stream_t stream = NULL;
361*355b4669Sjacobs 	int rc;
362*355b4669Sjacobs 	char buf[BUFSIZ];
363*355b4669Sjacobs 
364*355b4669Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
365*355b4669Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
366*355b4669Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
367*355b4669Sjacobs 
368*355b4669Sjacobs 	if (status == PAPI_OK)
369*355b4669Sjacobs 		status = papiJobStreamClose(svc, stream, job);
370*355b4669Sjacobs 
371*355b4669Sjacobs 	return (status);
372*355b4669Sjacobs }
373*355b4669Sjacobs 
374*355b4669Sjacobs static char **
375*355b4669Sjacobs all_list(papi_service_t svc)
376*355b4669Sjacobs {
377*355b4669Sjacobs 	papi_status_t status;
378*355b4669Sjacobs 	papi_printer_t printer = NULL;
379*355b4669Sjacobs 	char *list[] = { "member-names", NULL };
380*355b4669Sjacobs 	char **result = NULL;
381*355b4669Sjacobs 
382*355b4669Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
383*355b4669Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
384*355b4669Sjacobs 		papi_attribute_t **attributes =
385*355b4669Sjacobs 					papiPrinterGetAttributeList(printer);
386*355b4669Sjacobs 		if (attributes != NULL) {
387*355b4669Sjacobs 			void *iter = NULL;
388*355b4669Sjacobs 			char *value = NULL;
389*355b4669Sjacobs 
390*355b4669Sjacobs 			for (status = papiAttributeListGetString(attributes,
391*355b4669Sjacobs 						&iter, "member-names", &value);
392*355b4669Sjacobs 				status == PAPI_OK;
393*355b4669Sjacobs 				status = papiAttributeListGetString(attributes,
394*355b4669Sjacobs 						&iter, NULL, &value))
395*355b4669Sjacobs 					list_append(&result, strdup(value));
396*355b4669Sjacobs 		}
397*355b4669Sjacobs 		papiPrinterFree(printer);
398*355b4669Sjacobs 	}
399*355b4669Sjacobs 
400*355b4669Sjacobs 	return (result);
401*355b4669Sjacobs }
402*355b4669Sjacobs 
403*355b4669Sjacobs static char **
404*355b4669Sjacobs printers_list(papi_service_t svc)
405*355b4669Sjacobs {
406*355b4669Sjacobs 	papi_status_t status;
407*355b4669Sjacobs 	papi_printer_t *printers = NULL;
408*355b4669Sjacobs 	char *keys[] = { "printer-name", NULL };
409*355b4669Sjacobs 	char **result = NULL;
410*355b4669Sjacobs 
411*355b4669Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
412*355b4669Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
413*355b4669Sjacobs 		int i;
414*355b4669Sjacobs 
415*355b4669Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
416*355b4669Sjacobs 			papi_attribute_t **attributes =
417*355b4669Sjacobs 				papiPrinterGetAttributeList(printers[i]);
418*355b4669Sjacobs 			char *name = NULL;
419*355b4669Sjacobs 
420*355b4669Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
421*355b4669Sjacobs 						"printer-name", &name);
422*355b4669Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
423*355b4669Sjacobs 				list_append(&result, strdup(name));
424*355b4669Sjacobs 		}
425*355b4669Sjacobs 		papiPrinterListFree(printers);
426*355b4669Sjacobs 	}
427*355b4669Sjacobs 
428*355b4669Sjacobs 	return (result);
429*355b4669Sjacobs }
430*355b4669Sjacobs 
431*355b4669Sjacobs char **
432*355b4669Sjacobs interest_list(papi_service_t svc)
433*355b4669Sjacobs {
434*355b4669Sjacobs 	static char been_here;
435*355b4669Sjacobs 	static char **result;
436*355b4669Sjacobs 
437*355b4669Sjacobs 	if (been_here == 0) {	/* only do this once */
438*355b4669Sjacobs 		been_here = 1;
439*355b4669Sjacobs 
440*355b4669Sjacobs 		if ((result = all_list(svc)) == NULL)
441*355b4669Sjacobs 			result = printers_list(svc);
442*355b4669Sjacobs 	}
443*355b4669Sjacobs 
444*355b4669Sjacobs 	return (result);
445*355b4669Sjacobs }
446*355b4669Sjacobs 
447*355b4669Sjacobs char *
448*355b4669Sjacobs localhostname()
449*355b4669Sjacobs {
450*355b4669Sjacobs 	static char *result;
451*355b4669Sjacobs 
452*355b4669Sjacobs 	if (result == NULL) {
453*355b4669Sjacobs 		static char buf[256];
454*355b4669Sjacobs 
455*355b4669Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
456*355b4669Sjacobs 			result = buf;
457*355b4669Sjacobs 	}
458*355b4669Sjacobs 
459*355b4669Sjacobs 	return (result);
460*355b4669Sjacobs }
461*355b4669Sjacobs 
462*355b4669Sjacobs int
463*355b4669Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
464*355b4669Sjacobs {
465*355b4669Sjacobs 	char prompt[BUFSIZ];
466*355b4669Sjacobs 	char *user, *svc_name, *passphrase;
467*355b4669Sjacobs 
468*355b4669Sjacobs 	/* get the name of the service we are contacting */
469*355b4669Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
470*355b4669Sjacobs 		return (-1);
471*355b4669Sjacobs 
472*355b4669Sjacobs 	/* find our who we are supposed to be */
473*355b4669Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
474*355b4669Sjacobs 		struct passwd *pw;
475*355b4669Sjacobs 
476*355b4669Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
477*355b4669Sjacobs 			user = pw->pw_name;
478*355b4669Sjacobs 		else
479*355b4669Sjacobs 			user = "nobody";
480*355b4669Sjacobs 	}
481*355b4669Sjacobs 
482*355b4669Sjacobs 	/* build the prompt string */
483*355b4669Sjacobs 	snprintf(prompt, sizeof (prompt),
484*355b4669Sjacobs 		gettext("passphrase for %s to access %s: "), user, svc_name);
485*355b4669Sjacobs 
486*355b4669Sjacobs 	/* ask for the passphrase */
487*355b4669Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
488*355b4669Sjacobs 		papiServiceSetPassword(svc, passphrase);
489*355b4669Sjacobs 
490*355b4669Sjacobs 	return (0);
491*355b4669Sjacobs }
492