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 /*
23*8966SSonam.Gupta@Sun.COM * Copyright 2009 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 #define __EXTENSIONS__ /* for strtok_r() */
312264Sjacobs #include <stdio.h>
322264Sjacobs #include <stdlib.h>
332264Sjacobs #include <unistd.h>
342264Sjacobs #include <string.h>
352264Sjacobs #include <papi_impl.h>
362264Sjacobs
372264Sjacobs papi_status_t
lpd_cancel_job(service_t * svc,int id)382264Sjacobs lpd_cancel_job(service_t *svc, int id)
392264Sjacobs {
402264Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR;
412264Sjacobs int fd;
422582Sjacobs char *list[2];
432264Sjacobs char buf[128]; /* this should be overkill */
442264Sjacobs
452264Sjacobs if (svc == NULL)
462264Sjacobs return (PAPI_BAD_ARGUMENT);
472264Sjacobs
482264Sjacobs snprintf(buf, sizeof (buf), "%d", id);
492264Sjacobs list[0] = buf;
502264Sjacobs list[1] = NULL;
512264Sjacobs
523915Sceastha if ((fd = lpd_open(svc, 'c', list, 15)) < 0)
532264Sjacobs return (PAPI_INTERNAL_ERROR);
542264Sjacobs
552264Sjacobs memset(buf, 0, sizeof (buf));
562264Sjacobs if (fdgets(buf, sizeof (buf), fd) != NULL) {
572264Sjacobs if (buf[0] == '\0')
582264Sjacobs status = PAPI_NOT_FOUND;
59*8966SSonam.Gupta@Sun.COM else if ((strstr(buf, "permission denied") != NULL) ||
60*8966SSonam.Gupta@Sun.COM (strstr(buf, "not-authorized") != NULL))
612264Sjacobs status = PAPI_NOT_AUTHORIZED;
622264Sjacobs else if ((strstr(buf, "cancelled") != NULL) ||
63*8966SSonam.Gupta@Sun.COM (strstr(buf, "removed") != NULL))
642264Sjacobs status = PAPI_OK;
652264Sjacobs } else
662264Sjacobs status = PAPI_NOT_FOUND;
672264Sjacobs
682264Sjacobs close(fd);
692264Sjacobs
702264Sjacobs return (status);
712264Sjacobs }
722264Sjacobs
732264Sjacobs papi_status_t
lpd_purge_jobs(service_t * svc,job_t *** jobs)742264Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
752264Sjacobs {
762264Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR;
772264Sjacobs int fd;
782264Sjacobs char *queue;
792264Sjacobs char buf[256];
802264Sjacobs
812264Sjacobs if (svc == NULL)
822264Sjacobs return (PAPI_BAD_ARGUMENT);
832264Sjacobs
843915Sceastha if ((fd = lpd_open(svc, 'c', NULL, 15)) < 0)
852264Sjacobs return (PAPI_INTERNAL_ERROR);
862264Sjacobs
872264Sjacobs queue = queue_name_from_uri(svc->uri);
882264Sjacobs
892264Sjacobs status = PAPI_OK;
902264Sjacobs memset(buf, 0, sizeof (buf));
912264Sjacobs while (fdgets(buf, sizeof (buf), fd) != NULL) {
922264Sjacobs /* if we canceled it, add it to the list */
932264Sjacobs if ((strstr(buf, "cancelled") != NULL) ||
942264Sjacobs (strstr(buf, "removed") != NULL)) {
952264Sjacobs job_t *job;
962264Sjacobs papi_attribute_t **attributes = NULL;
972264Sjacobs char *ptr, *iter = NULL;
982264Sjacobs int id;
992264Sjacobs
1002264Sjacobs ptr = strtok_r(buf, ":", &iter);
1012264Sjacobs papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
102*8966SSonam.Gupta@Sun.COM "job-name", ptr);
1032264Sjacobs id = atoi(ptr);
1042264Sjacobs papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
105*8966SSonam.Gupta@Sun.COM "job-id", id);
1062264Sjacobs papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
107*8966SSonam.Gupta@Sun.COM "job-printer", queue);
1082264Sjacobs
1092264Sjacobs if ((job = (job_t *)calloc(1, (sizeof (*job))))
110*8966SSonam.Gupta@Sun.COM != NULL) {
1112264Sjacobs job->attributes = attributes;
1122264Sjacobs list_append(jobs, job);
1132264Sjacobs } else
1142264Sjacobs papiAttributeListFree(attributes);
1152264Sjacobs } else if (strstr(buf, "permission denied") != NULL)
1162264Sjacobs status = PAPI_NOT_AUTHORIZED;
1172264Sjacobs }
1182264Sjacobs close(fd);
1192264Sjacobs
1202264Sjacobs return (status);
1212264Sjacobs }
122