xref: /illumos-gate/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision bea83d026ee1bd1b2a2419e1d0232f107a5d7d9b)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <locale.h>
37 #include <libintl.h>
38 #include <papi.h>
39 #include "common.h"
40 
41 static void
42 usage(char *program)
43 {
44 	char *name;
45 
46 	if ((name = strrchr(program, '/')) == NULL)
47 		name = program;
48 	else
49 		name++;
50 
51 	fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
52 	exit(1);
53 }
54 
55 int
56 main(int ac, char *av[])
57 {
58 	int exit_code = 0;
59 	char *user = NULL;
60 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
61 	int c;
62 
63 	(void) setlocale(LC_ALL, "");
64 	(void) textdomain("SUNW_OST_OSCMD");
65 
66 	while ((c = getopt(ac, av, "Eu:")) != EOF)
67 		switch (c) {
68 		case 'E':
69 			encryption = PAPI_ENCRYPT_REQUIRED;
70 			break;
71 		case 'u':
72 			user = optarg;
73 			break;
74 		default:
75 			usage(av[0]);
76 		}
77 
78 	for (c = optind; c < ac; c++) {
79 		papi_status_t status;
80 		papi_service_t svc = NULL;
81 		papi_job_t *jobs = NULL;
82 		char *printer = NULL;
83 		int32_t id = -1;
84 
85 		(void) get_printer_id(av[c], &printer, &id);
86 
87 		status = papiServiceCreate(&svc, printer, user, NULL,
88 					cli_auth_callback, encryption, NULL);
89 		if (status != PAPI_OK) {
90 			fprintf(stderr, gettext(
91 				"Failed to contact service for %s: %s\n"),
92 				printer, verbose_papi_message(svc, status));
93 			exit(1);
94 		}
95 
96 #define	OUT	((status == PAPI_OK) ? stdout : stderr)
97 
98 		if (id != -1) {	/* it's a job */
99 			char *mesg = "cancelled";
100 
101 			status = papiJobCancel(svc, printer, id);
102 			if (status != PAPI_OK) {
103 				mesg = verbose_papi_message(svc, status);
104 				exit_code = 1;
105 			}
106 			fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
107 		} else {	/* it's a printer */
108 			status = papiPrinterPurgeJobs(svc, printer, &jobs);
109 			if (status != PAPI_OK) {
110 				fprintf(stderr, gettext("PurgeJobs %s: %s\n"),
111 					printer,
112 					verbose_papi_message(svc, status));
113 				exit_code = 1;
114 			}
115 
116 			while ((jobs != NULL) && (*jobs != NULL))
117 				fprintf(OUT, "%s-%d: %s\n", printer,
118 					papiJobGetId(*jobs++), "cancelled");
119 
120 			papiJobListFree(jobs);
121 		}
122 
123 		papiServiceDestroy(svc);
124 	}
125 
126 	return (exit_code);
127 }
128