1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs 
22*2264Sjacobs /*
23*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2264Sjacobs  * Use is subject to license terms.
25*2264Sjacobs  *
26*2264Sjacobs  */
27*2264Sjacobs 
28*2264Sjacobs /* $Id: lpd-cancel.c 155 2006-04-26 02:34:54Z ktou $ */
29*2264Sjacobs 
30*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*2264Sjacobs 
32*2264Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
33*2264Sjacobs #include <stdio.h>
34*2264Sjacobs #include <stdlib.h>
35*2264Sjacobs #include <unistd.h>
36*2264Sjacobs #include <string.h>
37*2264Sjacobs #include <papi_impl.h>
38*2264Sjacobs 
39*2264Sjacobs papi_status_t
40*2264Sjacobs lpd_cancel_job(service_t *svc, int id)
41*2264Sjacobs {
42*2264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
43*2264Sjacobs 	int fd;
44*2264Sjacobs 	char **list;
45*2264Sjacobs 	char buf[128];	/* this should be overkill */
46*2264Sjacobs 
47*2264Sjacobs 	if (svc == NULL)
48*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
49*2264Sjacobs 
50*2264Sjacobs 	snprintf(buf, sizeof (buf), "%d", id);
51*2264Sjacobs 	list[0] = buf;
52*2264Sjacobs 	list[1] = NULL;
53*2264Sjacobs 
54*2264Sjacobs 	if ((fd = lpd_open(svc, 'c', list, 3)) < 0)
55*2264Sjacobs 		return (PAPI_INTERNAL_ERROR);
56*2264Sjacobs 
57*2264Sjacobs 	memset(buf, 0, sizeof (buf));
58*2264Sjacobs 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
59*2264Sjacobs 		if (buf[0] == '\0')
60*2264Sjacobs 			status = PAPI_NOT_FOUND;
61*2264Sjacobs 		else if (strstr(buf, "permission denied") != NULL)
62*2264Sjacobs 			status = PAPI_NOT_AUTHORIZED;
63*2264Sjacobs 		else if ((strstr(buf, "cancelled") != NULL) ||
64*2264Sjacobs 			 (strstr(buf, "removed") != NULL))
65*2264Sjacobs 			status = PAPI_OK;
66*2264Sjacobs 	} else
67*2264Sjacobs 		status = PAPI_NOT_FOUND;
68*2264Sjacobs 
69*2264Sjacobs 	close(fd);
70*2264Sjacobs 
71*2264Sjacobs 	return (status);
72*2264Sjacobs }
73*2264Sjacobs 
74*2264Sjacobs papi_status_t
75*2264Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
76*2264Sjacobs {
77*2264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
78*2264Sjacobs 	int fd;
79*2264Sjacobs 	char *queue;
80*2264Sjacobs 	char buf[256];
81*2264Sjacobs 
82*2264Sjacobs 	if (svc == NULL)
83*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
84*2264Sjacobs 
85*2264Sjacobs 	if ((fd = lpd_open(svc, 'c', NULL, 3)) < 0)
86*2264Sjacobs 		return (PAPI_INTERNAL_ERROR);
87*2264Sjacobs 
88*2264Sjacobs 	queue = queue_name_from_uri(svc->uri);
89*2264Sjacobs 
90*2264Sjacobs 	status = PAPI_OK;
91*2264Sjacobs 	memset(buf, 0, sizeof (buf));
92*2264Sjacobs 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
93*2264Sjacobs 		/* if we canceled it, add it to the list */
94*2264Sjacobs 		if ((strstr(buf, "cancelled") != NULL) ||
95*2264Sjacobs 		    (strstr(buf, "removed") != NULL)) {
96*2264Sjacobs 			job_t *job;
97*2264Sjacobs 			papi_attribute_t **attributes = NULL;
98*2264Sjacobs 			char *ptr, *iter = NULL;
99*2264Sjacobs 			int id;
100*2264Sjacobs 
101*2264Sjacobs 			ptr = strtok_r(buf, ":", &iter);
102*2264Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
103*2264Sjacobs 					"job-name", ptr);
104*2264Sjacobs 			id = atoi(ptr);
105*2264Sjacobs 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
106*2264Sjacobs 					"job-id", id);
107*2264Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
108*2264Sjacobs 					"job-printer", queue);
109*2264Sjacobs 
110*2264Sjacobs 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
111*2264Sjacobs 					!= NULL) {
112*2264Sjacobs 				job->attributes = attributes;
113*2264Sjacobs 				list_append(jobs, job);
114*2264Sjacobs 			} else
115*2264Sjacobs 				papiAttributeListFree(attributes);
116*2264Sjacobs 		} else if (strstr(buf, "permission denied") != NULL)
117*2264Sjacobs 			status = PAPI_NOT_AUTHORIZED;
118*2264Sjacobs 	}
119*2264Sjacobs 	close(fd);
120*2264Sjacobs 
121*2264Sjacobs 	return (status);
122*2264Sjacobs }
123