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-cancel.c 155 2006-04-26 02:34:54Z 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 <string.h>
372264Sjacobs #include <papi_impl.h>
382264Sjacobs 
392264Sjacobs papi_status_t
402264Sjacobs lpd_cancel_job(service_t *svc, int id)
412264Sjacobs {
422264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
432264Sjacobs 	int fd;
44*2582Sjacobs 	char *list[2];
452264Sjacobs 	char buf[128];	/* this should be overkill */
462264Sjacobs 
472264Sjacobs 	if (svc == NULL)
482264Sjacobs 		return (PAPI_BAD_ARGUMENT);
492264Sjacobs 
502264Sjacobs 	snprintf(buf, sizeof (buf), "%d", id);
512264Sjacobs 	list[0] = buf;
522264Sjacobs 	list[1] = NULL;
532264Sjacobs 
542264Sjacobs 	if ((fd = lpd_open(svc, 'c', list, 3)) < 0)
552264Sjacobs 		return (PAPI_INTERNAL_ERROR);
562264Sjacobs 
572264Sjacobs 	memset(buf, 0, sizeof (buf));
582264Sjacobs 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
592264Sjacobs 		if (buf[0] == '\0')
602264Sjacobs 			status = PAPI_NOT_FOUND;
612264Sjacobs 		else if (strstr(buf, "permission denied") != NULL)
622264Sjacobs 			status = PAPI_NOT_AUTHORIZED;
632264Sjacobs 		else if ((strstr(buf, "cancelled") != NULL) ||
642264Sjacobs 			 (strstr(buf, "removed") != NULL))
652264Sjacobs 			status = PAPI_OK;
662264Sjacobs 	} else
672264Sjacobs 		status = PAPI_NOT_FOUND;
682264Sjacobs 
692264Sjacobs 	close(fd);
702264Sjacobs 
712264Sjacobs 	return (status);
722264Sjacobs }
732264Sjacobs 
742264Sjacobs papi_status_t
752264Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
762264Sjacobs {
772264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
782264Sjacobs 	int fd;
792264Sjacobs 	char *queue;
802264Sjacobs 	char buf[256];
812264Sjacobs 
822264Sjacobs 	if (svc == NULL)
832264Sjacobs 		return (PAPI_BAD_ARGUMENT);
842264Sjacobs 
852264Sjacobs 	if ((fd = lpd_open(svc, 'c', NULL, 3)) < 0)
862264Sjacobs 		return (PAPI_INTERNAL_ERROR);
872264Sjacobs 
882264Sjacobs 	queue = queue_name_from_uri(svc->uri);
892264Sjacobs 
902264Sjacobs 	status = PAPI_OK;
912264Sjacobs 	memset(buf, 0, sizeof (buf));
922264Sjacobs 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
932264Sjacobs 		/* if we canceled it, add it to the list */
942264Sjacobs 		if ((strstr(buf, "cancelled") != NULL) ||
952264Sjacobs 		    (strstr(buf, "removed") != NULL)) {
962264Sjacobs 			job_t *job;
972264Sjacobs 			papi_attribute_t **attributes = NULL;
982264Sjacobs 			char *ptr, *iter = NULL;
992264Sjacobs 			int id;
1002264Sjacobs 
1012264Sjacobs 			ptr = strtok_r(buf, ":", &iter);
1022264Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
1032264Sjacobs 					"job-name", ptr);
1042264Sjacobs 			id = atoi(ptr);
1052264Sjacobs 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
1062264Sjacobs 					"job-id", id);
1072264Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
1082264Sjacobs 					"job-printer", queue);
1092264Sjacobs 
1102264Sjacobs 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
1112264Sjacobs 					!= NULL) {
1122264Sjacobs 				job->attributes = attributes;
1132264Sjacobs 				list_append(jobs, job);
1142264Sjacobs 			} else
1152264Sjacobs 				papiAttributeListFree(attributes);
1162264Sjacobs 		} else if (strstr(buf, "permission denied") != NULL)
1172264Sjacobs 			status = PAPI_NOT_AUTHORIZED;
1182264Sjacobs 	}
1192264Sjacobs 	close(fd);
1202264Sjacobs 
1212264Sjacobs 	return (status);
1222264Sjacobs }
123