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