xref: /titanic_50/usr/src/lib/print/libpapi-lpd/common/lpd-cancel.c (revision 494f7e12a62129ef191a15f9dfde6b7abe3bf510)
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 2009 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 #define	__EXTENSIONS__	/* for strtok_r() */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <papi_impl.h>
36 
37 papi_status_t
38 lpd_cancel_job(service_t *svc, int id)
39 {
40 	papi_status_t status = PAPI_INTERNAL_ERROR;
41 	int fd;
42 	char *list[2];
43 	char buf[128];	/* this should be overkill */
44 
45 	if (svc == NULL)
46 		return (PAPI_BAD_ARGUMENT);
47 
48 	snprintf(buf, sizeof (buf), "%d", id);
49 	list[0] = buf;
50 	list[1] = NULL;
51 
52 	if ((fd = lpd_open(svc, 'c', list, 15)) < 0)
53 		return (PAPI_INTERNAL_ERROR);
54 
55 	memset(buf, 0, sizeof (buf));
56 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
57 		if (buf[0] == '\0')
58 			status = PAPI_NOT_FOUND;
59 		else if ((strstr(buf, "permission denied") != NULL) ||
60 		    (strstr(buf, "not-authorized") != NULL))
61 			status = PAPI_NOT_AUTHORIZED;
62 		else if ((strstr(buf, "cancelled") != NULL) ||
63 		    (strstr(buf, "removed") != NULL))
64 			status = PAPI_OK;
65 	} else
66 		status = PAPI_NOT_FOUND;
67 
68 	close(fd);
69 
70 	return (status);
71 }
72 
73 papi_status_t
74 lpd_purge_jobs(service_t *svc, job_t ***jobs)
75 {
76 	papi_status_t status = PAPI_INTERNAL_ERROR;
77 	int fd;
78 	char *queue;
79 	char buf[256];
80 
81 	if (svc == NULL)
82 		return (PAPI_BAD_ARGUMENT);
83 
84 	if ((fd = lpd_open(svc, 'c', NULL, 15)) < 0)
85 		return (PAPI_INTERNAL_ERROR);
86 
87 	queue = queue_name_from_uri(svc->uri);
88 
89 	status = PAPI_OK;
90 	memset(buf, 0, sizeof (buf));
91 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
92 		/* if we canceled it, add it to the list */
93 		if ((strstr(buf, "cancelled") != NULL) ||
94 		    (strstr(buf, "removed") != NULL)) {
95 			job_t *job;
96 			papi_attribute_t **attributes = NULL;
97 			char *ptr, *iter = NULL;
98 			int id;
99 
100 			ptr = strtok_r(buf, ":", &iter);
101 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
102 			    "job-name", ptr);
103 			id = atoi(ptr);
104 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
105 			    "job-id", id);
106 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
107 			    "job-printer", queue);
108 
109 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
110 			    != NULL) {
111 				job->attributes = attributes;
112 				list_append(jobs, job);
113 			} else
114 				papiAttributeListFree(attributes);
115 		} else if (strstr(buf, "permission denied") != NULL)
116 			status = PAPI_NOT_AUTHORIZED;
117 	}
118 	close(fd);
119 
120 	return (status);
121 }
122