1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6 #pragma ident "$Id: add-modify-printer.c,v 1.3 2005/11/10 17:50:58 njacobs Exp $"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <locale.h>
13 #include <libintl.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <papi.h>
17
18 static void
usage(char * program)19 usage(char *program)
20 {
21 char *name;
22
23 if ((name = strrchr(program, '/')) == NULL)
24 name = program;
25 else
26 name++;
27
28 fprintf(stdout, gettext("Usage: %s -[a|m] (destination) ...\n"), name);
29 exit(1);
30 }
31
32 static void
print_attributes(FILE * fp,papi_attribute_t ** attributes,char * delimiter)33 print_attributes(FILE *fp, papi_attribute_t **attributes, char *delimiter)
34 {
35 char buffer[20480];
36
37 papiAttributeListToString(attributes, delimiter,
38 buffer, sizeof (buffer));
39 fprintf(fp,"%s%s\n", delimiter, buffer);
40 fflush(fp);
41 }
42
43 int
main(int ac,char * av[])44 main(int ac, char *av[])
45 {
46 papi_status_t status;
47 papi_service_t svc = NULL;
48 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
49 papi_printer_t printer = NULL;
50 papi_attribute_t **attributes = NULL;
51 char *destination = NULL;
52 enum {ADD, MODIFY} type;
53 int c;
54
55 (void) setlocale(LC_ALL, "");
56 (void) textdomain("SUNW_OST_OSCMD");
57
58 while ((c = getopt(ac, av, "a:m:")) != EOF)
59 switch (c) {
60 case 'a':
61 type = ADD;
62 destination = optarg;
63 case 'm':
64 type = MODIFY;
65 destination = optarg;
66 }
67
68 if ((destination == NULL) || (optind == ac))
69 usage(av[0]);
70
71 for (c = optind; c < ac; c++)
72 papiAttributeListFromString(&attributes, PAPI_ATTR_APPEND,
73 av[c]);
74
75 status = papiServiceCreate(&svc, NULL, NULL, NULL, NULL,
76 encryption, NULL);
77 if (status != PAPI_OK) {
78 fprintf(stderr, gettext(
79 "Failed to contact service for %s: %s\n"), destination,
80 papiStatusString(status));
81 }
82
83 if (type == ADD)
84 status = papiPrinterAdd(svc, destination, attributes, &printer);
85 else
86 status = papiPrinterModify(svc, destination, attributes,
87 &printer);
88 papiAttributeListFree(attributes);
89 if (status == PAPI_OK) {
90 printf(gettext("destination: %s\n"), destination);
91 attributes = papiPrinterGetAttributeList(printer);
92 if (attributes != NULL)
93 print_attributes(stdout, attributes, "\nRESULT ");
94 papiPrinterFree(printer);
95 } else
96 fprintf(stderr, gettext("jobPrinter(Add|Modify): %s: %s\n"),
97 destination, papiStatusString(status));
98
99 papiServiceDestroy(svc);
100
101 exit(0);
102 }
103