PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/cmd/print/bsd-sysv-commands/disable.c

https://github.com/richlowe/illumos-gate
C | 163 lines | 112 code | 24 blank | 27 comment | 29 complexity | 52fe52f264df34f82d7dde9337b5875b MD5 | raw file
  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. * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. *
  25. */
  26. /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <locale.h>
  32. #include <libintl.h>
  33. #include <papi.h>
  34. #include "common.h"
  35. static void
  36. usage(char *program)
  37. {
  38. char *name;
  39. if ((name = strrchr(program, '/')) == NULL)
  40. name = program;
  41. else
  42. name++;
  43. fprintf(stdout,
  44. gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
  45. name);
  46. exit(1);
  47. }
  48. static void
  49. cancel_active_job(papi_service_t svc, char *dest)
  50. {
  51. papi_status_t status;
  52. papi_job_t *j = NULL;
  53. char *req_attrs[] = { "job-state", "job-id", NULL };
  54. status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
  55. if ((status == PAPI_OK) && (j != NULL)) {
  56. int i;
  57. for (i = 0; j[i] != NULL; j++) {
  58. papi_attribute_t **a = papiJobGetAttributeList(j[i]);
  59. int state = 0;
  60. if (a == NULL)
  61. continue;
  62. (void) papiAttributeListGetInteger(a, NULL,
  63. "job-state", &state);
  64. if (state & 0x082A) { /* If state is RS_ACTIVE */
  65. int32_t id = papiJobGetId(j[i]);
  66. (void) papiJobCancel(svc, dest, id);
  67. }
  68. }
  69. papiJobListFree(j);
  70. }
  71. }
  72. int
  73. main(int ac, char *av[])
  74. {
  75. papi_status_t status;
  76. papi_service_t svc = NULL;
  77. papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
  78. int exit_status = 0;
  79. int cancel = 0;
  80. int pending = 0; /* not implemented */
  81. char *reason = NULL;
  82. int c;
  83. (void) setlocale(LC_ALL, "");
  84. (void) textdomain("SUNW_OST_OSCMD");
  85. while ((c = getopt(ac, av, "EcWr:")) != EOF)
  86. switch (c) {
  87. case 'c': /* cancel active job first */
  88. cancel = 1;
  89. break;
  90. case 'W': /* wait for active request, not implemented */
  91. pending = 1;
  92. break;
  93. case 'r': /* reason */
  94. reason = optarg;
  95. break;
  96. case 'E':
  97. encryption = PAPI_ENCRYPT_NEVER;
  98. break;
  99. default:
  100. usage(av[0]);
  101. }
  102. if (ac <= optind)
  103. usage(av[0]);
  104. while (optind < ac) {
  105. char *printer = av[optind++];
  106. status = papiServiceCreate(&svc, printer, NULL, NULL,
  107. cli_auth_callback, encryption, NULL);
  108. if (status != PAPI_OK) {
  109. fprintf(stderr, gettext(
  110. "Failed to contact service for %s: %s\n"),
  111. printer, verbose_papi_message(svc, status));
  112. exit_status = 1;
  113. }
  114. status = papiPrinterDisable(svc, printer, reason);
  115. if (status == PAPI_OK) {
  116. printf(gettext("printer \"%s\" now disabled\n"),
  117. printer);
  118. } else if (status == PAPI_NOT_ACCEPTING) {
  119. fprintf(stderr, gettext(
  120. "Destination \"%s\" was already disabled.\n"),
  121. printer);
  122. exit_status = 1;
  123. } else {
  124. /* The operation is not supported in lpd protocol */
  125. if (status == PAPI_OPERATION_NOT_SUPPORTED) {
  126. fprintf(stderr,
  127. verbose_papi_message(svc, status));
  128. } else {
  129. fprintf(stderr, gettext("disable: %s: %s\n"),
  130. printer, verbose_papi_message(svc, status));
  131. }
  132. exit_status = 1;
  133. }
  134. if (cancel != 0)
  135. cancel_active_job(svc, printer);
  136. papiServiceDestroy(svc);
  137. }
  138. return (exit_status);
  139. }