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.
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.
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]
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
27 /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
44 if ((name
= strrchr(program
, '/')) == NULL
)
49 fprintf(stdout
, "Usage: %s [-u user] (printer|request-id ...)\n", name
);
54 get_job_id_requested(papi_job_t job
) {
57 papi_attribute_t
**list
= papiJobGetAttributeList(job
);
58 papiAttributeListGetInteger(list
, NULL
,
59 "job-id-requested", &rid
);
65 cancel_jobs_for_user(char *user
, papi_encryption_t encryption
, char *pname
) {
68 papi_service_t svc
= NULL
;
69 char **printers
= NULL
;
73 status
= papiServiceCreate(&svc
, NULL
, NULL
, NULL
,
74 cli_auth_callback
, encryption
, NULL
);
75 printers
= interest_list(svc
);
76 papiServiceDestroy(svc
);
78 list_append(&printers
, strdup(pname
));
84 for (i
= 0; printers
[i
] != NULL
; i
++) {
85 char *printer
= printers
[i
];
87 status
= papiServiceCreate(&svc
, printer
, NULL
, NULL
,
88 cli_auth_callback
, encryption
, NULL
);
90 if (status
!= PAPI_OK
) {
91 fprintf(stderr
, gettext(
92 "Failed to contact service for %s: %s\n"),
93 printer
, verbose_papi_message(svc
, status
));
96 exit_code
= berkeley_cancel_request(svc
, stdout
, printer
, 1,
99 papiServiceDestroy(svc
);
108 main(int ac
, char *av
[])
112 papi_encryption_t encryption
= PAPI_ENCRYPT_NEVER
;
118 (void) setlocale(LC_ALL
, "");
119 (void) textdomain("SUNW_OST_OSCMD");
124 while ((c
= getopt(ac
, av
, "Eu:")) != EOF
)
127 encryption
= PAPI_ENCRYPT_REQUIRED
;
136 for (c
= optind
; c
< ac
; c
++) {
137 papi_status_t status
;
138 papi_service_t svc
= NULL
;
139 papi_job_t
*jobs
= NULL
;
140 char *printer
= NULL
;
143 status
= papiServiceCreate(&svc
, av
[c
], NULL
, NULL
,
144 cli_auth_callback
, encryption
, NULL
);
145 if (status
!= PAPI_OK
) {
146 if (first_dest
== 0) {
147 (void) get_printer_id(av
[c
], &printer
, &id
);
148 status
= papiServiceCreate(&svc
, printer
, NULL
,
149 NULL
, cli_auth_callback
, encryption
, NULL
);
151 if (status
!= PAPI_OK
) {
152 fprintf(stderr
, gettext(
153 "Failed to contact service for %s: %s\n"),
155 verbose_papi_message(svc
, status
));
163 #define OUT ((status == PAPI_OK) ? stdout : stderr)
165 if (id
!= -1) { /* it's a job */
166 char *mesg
= gettext("cancelled");
169 * Check if the job-id is job-id-requested
170 * or job-id. If it is job-id-requested then find
171 * corresponding job-id and send it to cancel
173 rid
= job_to_be_queried(svc
, printer
, id
);
176 * Either it is a remote job which cannot be
177 * cancelled based on job-id or job-id is
181 fprintf(OUT
, "%s-%d: %s\n",
182 printer
, id
, gettext("not-found"));
184 status
= papiJobCancel(svc
, printer
, rid
);
185 if (status
== PAPI_NOT_AUTHORIZED
) {
186 mesg
= papiStatusString(status
);
188 } else if (status
!= PAPI_OK
) {
190 verbose_papi_message(
194 fprintf(OUT
, "%s-%d: %s\n", printer
, id
, mesg
);
197 } else { /* it's a printer */
200 /* Remove first job from printer */
202 status
= papiPrinterListJobs(svc
, printer
,
203 NULL
, NULL
, 0, &jobs
);
205 if (status
!= PAPI_OK
) {
206 fprintf(stderr
, gettext(
207 "ListJobs %s: %s\n"), printer
,
208 verbose_papi_message(svc
, status
));
212 if (jobs
!= NULL
&& *jobs
!= NULL
) {
213 char *mesg
= gettext("cancelled");
214 id
= papiJobGetId(*jobs
);
216 status
= papiJobCancel(svc
,
219 if (status
== PAPI_NOT_AUTHORIZED
) {
220 mesg
= papiStatusString(status
);
222 } else if (status
!= PAPI_OK
) {
224 verbose_papi_message(
229 * If job-id-requested exists for this
230 * job-id then that should be displayed
232 rid
= get_job_id_requested(*jobs
);
234 fprintf(OUT
, "%s-%d: %s\n",
237 fprintf(OUT
, "%s-%d: %s\n",
240 papiJobListFree(jobs
);
243 /* Purging user's print jobs */
244 exit_code
= cancel_jobs_for_user(user
,
245 encryption
, printer
);
248 papiServiceDestroy(svc
);
252 exit_code
= cancel_jobs_for_user(user
, encryption
, NULL
);