xref: /illumos-gate/usr/src/lib/print/libprint/common/ns_cmn_printer.c (revision 2d6125aab2c7deca41a7689dae14eb32ec909f49)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*LINTLIBRARY*/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <syslog.h>
37 
38 #include <ns.h>
39 #include <list.h>
40 
41 /*
42  *	Commonly Used routines...
43  */
44 
45 /*
46  * FUNCTION:
47  *	printer_create(char *name, char **aliases, char *source,
48  *			ns_kvp_t **attributes)
49  * INPUT(S):
50  *	char *name
51  *		- primary name of printer
52  *	char **aliases
53  *		- aliases for printer
54  *	char *source
55  *		- name service derived from
56  *	ks_kvp_t **attributes
57  *		- key/value pairs
58  * OUTPUT(S):
59  *	ns_printer_t * (return value)
60  *		- pointer to printer object structure
61  * DESCRIPTION:
62  */
63 ns_printer_t *
64 ns_printer_create(char *name, char **aliases, char *source,
65 			ns_kvp_t **attributes)
66 {
67 	ns_printer_t *printer;
68 
69 	if ((printer = (ns_printer_t *)calloc(1, sizeof (*printer))) != NULL) {
70 		printer->name = (char *)name;
71 		printer->aliases = (char **)aliases;
72 		printer->source = (char *)source;
73 		printer->attributes = (ns_kvp_t **)attributes;
74 	}
75 	return (printer);
76 }
77 
78 
79 static int
80 ns_strcmp(char *s1, char *s2)
81 {
82 	return (strcmp(s1, s2) != 0);
83 }
84 
85 
86 /*
87  * FUNCTION:
88  *	ns_printer_match_name(const ns_printer_t *printer, const char *name)
89  * INPUT(S):
90  *	const ns_printer_t *printer
91  *		- key/value pair to check
92  *	const char *key
93  *		- key for matching
94  * OUTPUT(S):
95  *	int (return value)
96  *		- 0 if matched
97  * DESCRIPTION:
98  */
99 int
100 ns_printer_match_name(ns_printer_t *printer, const char *name)
101 {
102 	if ((printer == NULL) || (printer->name == NULL) || (name == NULL))
103 		return (-1);
104 
105 	if ((strcmp(printer->name, name) == 0) ||
106 	    (list_locate((void **)printer->aliases,
107 			(COMP_T)ns_strcmp, (void *)name) != NULL))
108 		return (0);
109 
110 	return (-1);
111 }
112 
113 
114 static void
115 _ns_append_printer_name(const char *name, va_list ap)
116 {
117 	char *buf = va_arg(ap, char *);
118 	int bufsize = va_arg(ap, int);
119 
120 	if (name == NULL)
121 		return;
122 
123 	(void) strlcat(buf, name, bufsize);
124 	(void) strlcat(buf, "|", bufsize);
125 }
126 
127 /*
128  * FUNCTION:
129  *	char *ns_printer_name_list(const ns_printer_t *printer)
130  * INPUT:
131  *	const ns_printer_t *printer - printer object to generate list from
132  * OUTPUT:
133  *	char * (return) - a newly allocated string containing the names of
134  *			  the printer
135  */
136 char *
137 ns_printer_name_list(const ns_printer_t *printer)
138 {
139 	char buf[BUFSIZ];
140 
141 	if ((printer == NULL) || (printer->name == NULL))
142 		return (NULL);
143 
144 	if (snprintf(buf, sizeof (buf), "%s|", printer->name) >= sizeof (buf)) {
145 		syslog(LOG_ERR, "ns_printer_name:buffer overflow");
146 		return (NULL);
147 	}
148 
149 	list_iterate((void **)printer->aliases,
150 		(VFUNC_T)_ns_append_printer_name, buf, sizeof (buf));
151 
152 	buf[strlen(buf) - 1] = (char)NULL;
153 
154 	return (strdup(buf));
155 }
156