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