1*2264Sjacobs /*
2*2264Sjacobs * CDDL HEADER START
3*2264Sjacobs *
4*2264Sjacobs * The contents of this file are subject to the terms of the
5*2264Sjacobs * Common Development and Distribution License (the "License").
6*2264Sjacobs * You may not use this file except in compliance with the License.
7*2264Sjacobs *
8*2264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs * See the License for the specific language governing permissions
11*2264Sjacobs * and limitations under the License.
12*2264Sjacobs *
13*2264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs *
19*2264Sjacobs * CDDL HEADER END
20*2264Sjacobs */
21*2264Sjacobs /*
22*2264Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*2264Sjacobs * Use is subject to license terms.
24*2264Sjacobs */
25*2264Sjacobs
26*2264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI"
27*2264Sjacobs
28*2264Sjacobs /*LINTLIBRARY*/
29*2264Sjacobs
30*2264Sjacobs #include <stdio.h>
31*2264Sjacobs #include <stdlib.h>
32*2264Sjacobs #include <unistd.h>
33*2264Sjacobs #include <sys/types.h>
34*2264Sjacobs #include <stdarg.h>
35*2264Sjacobs #include <string.h>
36*2264Sjacobs #include <syslog.h>
37*2264Sjacobs
38*2264Sjacobs #include <ns.h>
39*2264Sjacobs #include <list.h>
40*2264Sjacobs
41*2264Sjacobs extern void ns_kvp_destroy(ns_kvp_t *);
42*2264Sjacobs
43*2264Sjacobs /*
44*2264Sjacobs * Commonly Used routines...
45*2264Sjacobs */
46*2264Sjacobs
47*2264Sjacobs /*
48*2264Sjacobs * FUNCTION:
49*2264Sjacobs * printer_create(char *name, char **aliases, char *source,
50*2264Sjacobs * ns_kvp_t **attributes)
51*2264Sjacobs * INPUT(S):
52*2264Sjacobs * char *name
53*2264Sjacobs * - primary name of printer
54*2264Sjacobs * char **aliases
55*2264Sjacobs * - aliases for printer
56*2264Sjacobs * char *source
57*2264Sjacobs * - name service derived from
58*2264Sjacobs * ks_kvp_t **attributes
59*2264Sjacobs * - key/value pairs
60*2264Sjacobs * OUTPUT(S):
61*2264Sjacobs * ns_printer_t * (return value)
62*2264Sjacobs * - pointer to printer object structure
63*2264Sjacobs * DESCRIPTION:
64*2264Sjacobs */
65*2264Sjacobs ns_printer_t *
ns_printer_create(char * name,char ** aliases,char * source,ns_kvp_t ** attributes)66*2264Sjacobs ns_printer_create(char *name, char **aliases, char *source,
67*2264Sjacobs ns_kvp_t **attributes)
68*2264Sjacobs {
69*2264Sjacobs ns_printer_t *printer;
70*2264Sjacobs
71*2264Sjacobs if ((printer = (ns_printer_t *)calloc(1, sizeof (*printer))) != NULL) {
72*2264Sjacobs printer->name = (char *)name;
73*2264Sjacobs printer->aliases = (char **)aliases;
74*2264Sjacobs printer->source = (char *)source;
75*2264Sjacobs printer->attributes = (ns_kvp_t **)attributes;
76*2264Sjacobs }
77*2264Sjacobs return (printer);
78*2264Sjacobs }
79*2264Sjacobs
80*2264Sjacobs
81*2264Sjacobs static int
ns_strcmp(char * s1,char * s2)82*2264Sjacobs ns_strcmp(char *s1, char *s2)
83*2264Sjacobs {
84*2264Sjacobs return (strcmp(s1, s2) != 0);
85*2264Sjacobs }
86*2264Sjacobs
87*2264Sjacobs
88*2264Sjacobs /*
89*2264Sjacobs * FUNCTION:
90*2264Sjacobs * ns_printer_match_name(const ns_printer_t *printer, const char *name)
91*2264Sjacobs * INPUT(S):
92*2264Sjacobs * const ns_printer_t *printer
93*2264Sjacobs * - key/value pair to check
94*2264Sjacobs * const char *key
95*2264Sjacobs * - key for matching
96*2264Sjacobs * OUTPUT(S):
97*2264Sjacobs * int (return value)
98*2264Sjacobs * - 0 if matched
99*2264Sjacobs * DESCRIPTION:
100*2264Sjacobs */
101*2264Sjacobs int
ns_printer_match_name(ns_printer_t * printer,const char * name)102*2264Sjacobs ns_printer_match_name(ns_printer_t *printer, const char *name)
103*2264Sjacobs {
104*2264Sjacobs if ((printer == NULL) || (printer->name == NULL) || (name == NULL))
105*2264Sjacobs return (-1);
106*2264Sjacobs
107*2264Sjacobs if ((strcmp(printer->name, name) == 0) ||
108*2264Sjacobs (list_locate((void **)printer->aliases,
109*2264Sjacobs (COMP_T)ns_strcmp, (void *)name) != NULL))
110*2264Sjacobs return (0);
111*2264Sjacobs
112*2264Sjacobs return (-1);
113*2264Sjacobs }
114*2264Sjacobs
115*2264Sjacobs
116*2264Sjacobs static void
_ns_append_printer_name(const char * name,va_list ap)117*2264Sjacobs _ns_append_printer_name(const char *name, va_list ap)
118*2264Sjacobs {
119*2264Sjacobs char *buf = va_arg(ap, char *);
120*2264Sjacobs int bufsize = va_arg(ap, int);
121*2264Sjacobs
122*2264Sjacobs if (name == NULL)
123*2264Sjacobs return;
124*2264Sjacobs
125*2264Sjacobs (void) strlcat(buf, name, bufsize);
126*2264Sjacobs (void) strlcat(buf, "|", bufsize);
127*2264Sjacobs }
128*2264Sjacobs
129*2264Sjacobs /*
130*2264Sjacobs * FUNCTION:
131*2264Sjacobs * char *ns_printer_name_list(const ns_printer_t *printer)
132*2264Sjacobs * INPUT:
133*2264Sjacobs * const ns_printer_t *printer - printer object to generate list from
134*2264Sjacobs * OUTPUT:
135*2264Sjacobs * char * (return) - a newly allocated string containing the names of
136*2264Sjacobs * the printer
137*2264Sjacobs */
138*2264Sjacobs char *
ns_printer_name_list(const ns_printer_t * printer)139*2264Sjacobs ns_printer_name_list(const ns_printer_t *printer)
140*2264Sjacobs {
141*2264Sjacobs char buf[BUFSIZ];
142*2264Sjacobs
143*2264Sjacobs if ((printer == NULL) || (printer->name == NULL))
144*2264Sjacobs return (NULL);
145*2264Sjacobs
146*2264Sjacobs if (snprintf(buf, sizeof (buf), "%s|", printer->name) >= sizeof (buf)) {
147*2264Sjacobs syslog(LOG_ERR, "ns_printer_name:buffer overflow");
148*2264Sjacobs return (NULL);
149*2264Sjacobs }
150*2264Sjacobs
151*2264Sjacobs list_iterate((void **)printer->aliases,
152*2264Sjacobs (VFUNC_T)_ns_append_printer_name, buf, sizeof (buf));
153*2264Sjacobs
154*2264Sjacobs buf[strlen(buf) - 1] = (char)NULL;
155*2264Sjacobs
156*2264Sjacobs return (strdup(buf));
157*2264Sjacobs }
158