8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / print / bsd-sysv-commands / lpmove.c
blob15cb076ef1144513de2e45b4d209c0c31c258c4d
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <libintl.h>
34 #include <papi.h>
35 #include "common.h"
37 static void
38 usage(char *program)
40 char *name;
42 if ((name = strrchr(program, '/')) == NULL)
43 name = program;
44 else
45 name++;
47 fprintf(stdout,
48 gettext("Usage: %s [request-id] (destination)\n"
49 " %s (source) (destination)\n"), name, name);
50 exit(1);
53 static int
54 move_job(papi_service_t svc, char *src, int32_t id, char *dest)
56 int result = 0;
57 papi_status_t status;
58 char *mesg = gettext("moved");
60 status = papiJobMove(svc, src, id, dest);
61 if (status != PAPI_OK) {
62 mesg = (char *)verbose_papi_message(svc, status);
63 result = -1;
65 fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
67 return (result);
70 int
71 main(int ac, char *av[])
73 int exit_code = 0;
74 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
75 char *destination = NULL;
76 int c;
78 (void) setlocale(LC_ALL, "");
79 (void) textdomain("SUNW_OST_OSCMD");
81 while ((c = getopt(ac, av, "E:")) != EOF)
82 switch (c) {
83 case 'E':
84 encryption = PAPI_ENCRYPT_REQUIRED;
85 break;
86 default:
87 usage(av[0]);
90 if (optind >= ac - 1)
91 usage(av[0]);
93 destination = av[--ac];
95 for (c = optind; c < ac; c++) {
96 papi_status_t status;
97 papi_service_t svc = NULL;
98 papi_job_t *jobs = NULL;
99 char *printer = NULL;
100 int32_t id = -1;
102 (void) get_printer_id(av[c], &printer, &id);
104 status = papiServiceCreate(&svc, printer, NULL, NULL,
105 cli_auth_callback, encryption, NULL);
106 if (status != PAPI_OK) {
107 fprintf(stderr, gettext(
108 "Failed to contact service for %s: %s\n"),
109 printer, verbose_papi_message(svc, status));
110 exit(1);
113 if (id != -1) { /* it's a job */
114 if (move_job(svc, printer, id, destination) < 0)
115 exit_code = 1;
116 } else { /* it's a printer */
117 char message[128];
118 int count = 0;
120 snprintf(message, sizeof (message), "moved jobs to %s",
121 destination);
122 status = papiPrinterPause(svc, printer, message);
123 if (status != PAPI_OK) {
125 * If the user is denied the permission
126 * to disable then return appropriate msg
128 char *result = NULL;
130 result = papiServiceGetStatusMessage(svc);
132 if (result != NULL) {
134 * Check if user is denied
135 * the permission
137 if (strstr(result, "permission denied")
138 != NULL) {
140 * user is denied
141 * permission
143 fprintf(stderr, "UX:lpmove: ");
144 fprintf(stderr,
145 gettext("ERROR: "));
146 fprintf(stderr, gettext("You "
147 "aren't allowed to do"
148 " that."));
149 fprintf(stderr, "\n\t");
150 fprintf(stderr,
151 gettext("TO FIX"));
152 fprintf(stderr, ": ");
153 fprintf(stderr, gettext("You "
154 "must be logged in as "
155 "\"lp\" or \"root\"."));
156 fprintf(stderr, "\n");
157 exit_code = 1;
158 } else {
159 fprintf(stderr, gettext(
160 "Reject %s: %s\n"),
161 printer,
162 verbose_papi_message(
163 svc, status));
164 exit_code = 1;
166 } else {
167 fprintf(stderr, gettext(
168 "Reject %s: %s\n"),
169 printer,
170 verbose_papi_message(svc, status));
171 exit_code = 1;
173 } else {
174 printf(gettext(
175 "destination %s is not accepting"\
176 " requests\n"), printer);
178 status = papiPrinterListJobs(svc, printer, NULL,
179 0, 0, &jobs);
180 if (status != PAPI_OK) {
181 fprintf(stderr, gettext("Jobs %s:"\
182 " %s\n"),
183 printer,
184 verbose_papi_message(svc, status));
185 exit_code = 1;
188 printf(gettext("move in progress ...\n"));
189 while ((jobs != NULL) && (*jobs != NULL)) {
190 id = papiJobGetId(*jobs++);
191 if (move_job(svc, printer,
192 id, destination) < 0)
193 exit_code = 1;
194 else
195 count++;
197 printf(gettext(
198 "total of %d requests moved"\
199 " from %s to %s\n"),
200 count, printer, destination);
202 papiJobListFree(jobs);
206 papiServiceDestroy(svc);
209 return (exit_code);