xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/lpd-cancel.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: lpd-cancel.c 155 2006-04-26 02:34:54Z ktou $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
33*355b4669Sjacobs #include <stdio.h>
34*355b4669Sjacobs #include <stdlib.h>
35*355b4669Sjacobs #include <unistd.h>
36*355b4669Sjacobs #include <string.h>
37*355b4669Sjacobs #include <papi_impl.h>
38*355b4669Sjacobs 
39*355b4669Sjacobs papi_status_t
40*355b4669Sjacobs lpd_cancel_job(service_t *svc, int id)
41*355b4669Sjacobs {
42*355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
43*355b4669Sjacobs 	int fd;
44*355b4669Sjacobs 	char **list;
45*355b4669Sjacobs 	char buf[128];	/* this should be overkill */
46*355b4669Sjacobs 
47*355b4669Sjacobs 	if (svc == NULL)
48*355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
49*355b4669Sjacobs 
50*355b4669Sjacobs 	snprintf(buf, sizeof (buf), "%d", id);
51*355b4669Sjacobs 	list[0] = buf;
52*355b4669Sjacobs 	list[1] = NULL;
53*355b4669Sjacobs 
54*355b4669Sjacobs 	if ((fd = lpd_open(svc, 'c', list, 3)) < 0)
55*355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
56*355b4669Sjacobs 
57*355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
58*355b4669Sjacobs 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
59*355b4669Sjacobs 		if (buf[0] == '\0')
60*355b4669Sjacobs 			status = PAPI_NOT_FOUND;
61*355b4669Sjacobs 		else if (strstr(buf, "permission denied") != NULL)
62*355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
63*355b4669Sjacobs 		else if ((strstr(buf, "cancelled") != NULL) ||
64*355b4669Sjacobs 			 (strstr(buf, "removed") != NULL))
65*355b4669Sjacobs 			status = PAPI_OK;
66*355b4669Sjacobs 	} else
67*355b4669Sjacobs 		status = PAPI_NOT_FOUND;
68*355b4669Sjacobs 
69*355b4669Sjacobs 	close(fd);
70*355b4669Sjacobs 
71*355b4669Sjacobs 	return (status);
72*355b4669Sjacobs }
73*355b4669Sjacobs 
74*355b4669Sjacobs papi_status_t
75*355b4669Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
76*355b4669Sjacobs {
77*355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
78*355b4669Sjacobs 	int fd;
79*355b4669Sjacobs 	char *queue;
80*355b4669Sjacobs 	char buf[256];
81*355b4669Sjacobs 
82*355b4669Sjacobs 	if (svc == NULL)
83*355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
84*355b4669Sjacobs 
85*355b4669Sjacobs 	if ((fd = lpd_open(svc, 'c', NULL, 3)) < 0)
86*355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
87*355b4669Sjacobs 
88*355b4669Sjacobs 	queue = queue_name_from_uri(svc->uri);
89*355b4669Sjacobs 
90*355b4669Sjacobs 	status = PAPI_OK;
91*355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
92*355b4669Sjacobs 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
93*355b4669Sjacobs 		/* if we canceled it, add it to the list */
94*355b4669Sjacobs 		if ((strstr(buf, "cancelled") != NULL) ||
95*355b4669Sjacobs 		    (strstr(buf, "removed") != NULL)) {
96*355b4669Sjacobs 			job_t *job;
97*355b4669Sjacobs 			papi_attribute_t **attributes = NULL;
98*355b4669Sjacobs 			char *ptr, *iter = NULL;
99*355b4669Sjacobs 			int id;
100*355b4669Sjacobs 
101*355b4669Sjacobs 			ptr = strtok_r(buf, ":", &iter);
102*355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
103*355b4669Sjacobs 					"job-name", ptr);
104*355b4669Sjacobs 			id = atoi(ptr);
105*355b4669Sjacobs 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
106*355b4669Sjacobs 					"job-id", id);
107*355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
108*355b4669Sjacobs 					"job-printer", queue);
109*355b4669Sjacobs 
110*355b4669Sjacobs 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
111*355b4669Sjacobs 					!= NULL) {
112*355b4669Sjacobs 				job->attributes = attributes;
113*355b4669Sjacobs 				list_append(jobs, job);
114*355b4669Sjacobs 			} else
115*355b4669Sjacobs 				papiAttributeListFree(attributes);
116*355b4669Sjacobs 		} else if (strstr(buf, "permission denied") != NULL)
117*355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
118*355b4669Sjacobs 	}
119*355b4669Sjacobs 	close(fd);
120*355b4669Sjacobs 
121*355b4669Sjacobs 	return (status);
122*355b4669Sjacobs }
123