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