xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/lpd-cancel.c (revision 4bd2082ff2d009263265d7de938de336894b6009)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23*4bd2082fSceastha  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpd-cancel.c 155 2006-04-26 02:34:54Z ktou $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
33355b4669Sjacobs #include <stdio.h>
34355b4669Sjacobs #include <stdlib.h>
35355b4669Sjacobs #include <unistd.h>
36355b4669Sjacobs #include <string.h>
37355b4669Sjacobs #include <papi_impl.h>
38355b4669Sjacobs 
39355b4669Sjacobs papi_status_t
40355b4669Sjacobs lpd_cancel_job(service_t *svc, int id)
41355b4669Sjacobs {
42355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
43355b4669Sjacobs 	int fd;
44752ec50eSjacobs 	char *list[2];
45355b4669Sjacobs 	char buf[128];	/* this should be overkill */
46355b4669Sjacobs 
47355b4669Sjacobs 	if (svc == NULL)
48355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
49355b4669Sjacobs 
50355b4669Sjacobs 	snprintf(buf, sizeof (buf), "%d", id);
51355b4669Sjacobs 	list[0] = buf;
52355b4669Sjacobs 	list[1] = NULL;
53355b4669Sjacobs 
54*4bd2082fSceastha 	if ((fd = lpd_open(svc, 'c', list, 15)) < 0)
55355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
56355b4669Sjacobs 
57355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
58355b4669Sjacobs 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
59355b4669Sjacobs 		if (buf[0] == '\0')
60355b4669Sjacobs 			status = PAPI_NOT_FOUND;
61355b4669Sjacobs 		else if (strstr(buf, "permission denied") != NULL)
62355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
63355b4669Sjacobs 		else if ((strstr(buf, "cancelled") != NULL) ||
64355b4669Sjacobs 			 (strstr(buf, "removed") != NULL))
65355b4669Sjacobs 			status = PAPI_OK;
66355b4669Sjacobs 	} else
67355b4669Sjacobs 		status = PAPI_NOT_FOUND;
68355b4669Sjacobs 
69355b4669Sjacobs 	close(fd);
70355b4669Sjacobs 
71355b4669Sjacobs 	return (status);
72355b4669Sjacobs }
73355b4669Sjacobs 
74355b4669Sjacobs papi_status_t
75355b4669Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
76355b4669Sjacobs {
77355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
78355b4669Sjacobs 	int fd;
79355b4669Sjacobs 	char *queue;
80355b4669Sjacobs 	char buf[256];
81355b4669Sjacobs 
82355b4669Sjacobs 	if (svc == NULL)
83355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
84355b4669Sjacobs 
85*4bd2082fSceastha 	if ((fd = lpd_open(svc, 'c', NULL, 15)) < 0)
86355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
87355b4669Sjacobs 
88355b4669Sjacobs 	queue = queue_name_from_uri(svc->uri);
89355b4669Sjacobs 
90355b4669Sjacobs 	status = PAPI_OK;
91355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
92355b4669Sjacobs 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
93355b4669Sjacobs 		/* if we canceled it, add it to the list */
94355b4669Sjacobs 		if ((strstr(buf, "cancelled") != NULL) ||
95355b4669Sjacobs 		    (strstr(buf, "removed") != NULL)) {
96355b4669Sjacobs 			job_t *job;
97355b4669Sjacobs 			papi_attribute_t **attributes = NULL;
98355b4669Sjacobs 			char *ptr, *iter = NULL;
99355b4669Sjacobs 			int id;
100355b4669Sjacobs 
101355b4669Sjacobs 			ptr = strtok_r(buf, ":", &iter);
102355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
103355b4669Sjacobs 					"job-name", ptr);
104355b4669Sjacobs 			id = atoi(ptr);
105355b4669Sjacobs 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
106355b4669Sjacobs 					"job-id", id);
107355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
108355b4669Sjacobs 					"job-printer", queue);
109355b4669Sjacobs 
110355b4669Sjacobs 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
111355b4669Sjacobs 					!= NULL) {
112355b4669Sjacobs 				job->attributes = attributes;
113355b4669Sjacobs 				list_append(jobs, job);
114355b4669Sjacobs 			} else
115355b4669Sjacobs 				papiAttributeListFree(attributes);
116355b4669Sjacobs 		} else if (strstr(buf, "permission denied") != NULL)
117355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
118355b4669Sjacobs 	}
119355b4669Sjacobs 	close(fd);
120355b4669Sjacobs 
121355b4669Sjacobs 	return (status);
122355b4669Sjacobs }
123