xref: /illumos-gate/usr/src/lib/print/libprint/common/ns_cmn_printer.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
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 int
113 _ns_append_printer_name(void *arg, va_list ap)
114 {
115 	const char *name = arg;
116 	char *buf = va_arg(ap, char *);
117 	int bufsize = va_arg(ap, int);
118 
119 	if (name == NULL)
120 		return (0);
121 
122 	(void) strlcat(buf, name, bufsize);
123 	(void) strlcat(buf, "|", bufsize);
124 	return (0);
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 	    _ns_append_printer_name, buf, sizeof (buf));
151 
152 	buf[strlen(buf) - 1] = '\0';
153 
154 	return (strdup(buf));
155 }
156