17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate 
36355b4669Sjacobs #include <ns.h>
37355b4669Sjacobs #include <list.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  *	Commonly Used routines...
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * FUNCTION:
457c478bd9Sstevel@tonic-gate  *	printer_create(char *name, char **aliases, char *source,
467c478bd9Sstevel@tonic-gate  *			ns_kvp_t **attributes)
477c478bd9Sstevel@tonic-gate  * INPUT(S):
487c478bd9Sstevel@tonic-gate  *	char *name
497c478bd9Sstevel@tonic-gate  *		- primary name of printer
507c478bd9Sstevel@tonic-gate  *	char **aliases
517c478bd9Sstevel@tonic-gate  *		- aliases for printer
527c478bd9Sstevel@tonic-gate  *	char *source
537c478bd9Sstevel@tonic-gate  *		- name service derived from
547c478bd9Sstevel@tonic-gate  *	ks_kvp_t **attributes
557c478bd9Sstevel@tonic-gate  *		- key/value pairs
567c478bd9Sstevel@tonic-gate  * OUTPUT(S):
577c478bd9Sstevel@tonic-gate  *	ns_printer_t * (return value)
587c478bd9Sstevel@tonic-gate  *		- pointer to printer object structure
597c478bd9Sstevel@tonic-gate  * DESCRIPTION:
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate ns_printer_t *
ns_printer_create(char * name,char ** aliases,char * source,ns_kvp_t ** attributes)627c478bd9Sstevel@tonic-gate ns_printer_create(char *name, char **aliases, char *source,
63f4593de7SToomas Soome     ns_kvp_t **attributes)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	ns_printer_t *printer;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if ((printer = (ns_printer_t *)calloc(1, sizeof (*printer))) != NULL) {
687c478bd9Sstevel@tonic-gate 		printer->name = (char *)name;
697c478bd9Sstevel@tonic-gate 		printer->aliases = (char **)aliases;
707c478bd9Sstevel@tonic-gate 		printer->source = (char *)source;
717c478bd9Sstevel@tonic-gate 		printer->attributes = (ns_kvp_t **)attributes;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	return (printer);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int
ns_strcmp(char * s1,char * s2)787c478bd9Sstevel@tonic-gate ns_strcmp(char *s1, char *s2)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	return (strcmp(s1, s2) != 0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * FUNCTION:
867c478bd9Sstevel@tonic-gate  *	ns_printer_match_name(const ns_printer_t *printer, const char *name)
877c478bd9Sstevel@tonic-gate  * INPUT(S):
887c478bd9Sstevel@tonic-gate  *	const ns_printer_t *printer
897c478bd9Sstevel@tonic-gate  *		- key/value pair to check
907c478bd9Sstevel@tonic-gate  *	const char *key
917c478bd9Sstevel@tonic-gate  *		- key for matching
927c478bd9Sstevel@tonic-gate  * OUTPUT(S):
937c478bd9Sstevel@tonic-gate  *	int (return value)
947c478bd9Sstevel@tonic-gate  *		- 0 if matched
957c478bd9Sstevel@tonic-gate  * DESCRIPTION:
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int
ns_printer_match_name(ns_printer_t * printer,const char * name)987c478bd9Sstevel@tonic-gate ns_printer_match_name(ns_printer_t *printer, const char *name)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	if ((printer == NULL) || (printer->name == NULL) || (name == NULL))
1017c478bd9Sstevel@tonic-gate 		return (-1);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((strcmp(printer->name, name) == 0) ||
1047c478bd9Sstevel@tonic-gate 	    (list_locate((void **)printer->aliases,
105f4593de7SToomas Soome 	    (COMP_T)ns_strcmp, (void *)name) != NULL))
1067c478bd9Sstevel@tonic-gate 		return (0);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (-1);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
112*ef2333d1SToomas Soome static int
_ns_append_printer_name(void * arg,va_list ap)113*ef2333d1SToomas Soome _ns_append_printer_name(void *arg, va_list ap)
1147c478bd9Sstevel@tonic-gate {
115*ef2333d1SToomas Soome 	const char *name = arg;
1167c478bd9Sstevel@tonic-gate 	char *buf = va_arg(ap, char *);
1177c478bd9Sstevel@tonic-gate 	int bufsize = va_arg(ap, int);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (name == NULL)
120*ef2333d1SToomas Soome 		return (0);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) strlcat(buf, name, bufsize);
1237c478bd9Sstevel@tonic-gate 	(void) strlcat(buf, "|", bufsize);
124*ef2333d1SToomas Soome 	return (0);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * FUNCTION:
1297c478bd9Sstevel@tonic-gate  *	char *ns_printer_name_list(const ns_printer_t *printer)
1307c478bd9Sstevel@tonic-gate  * INPUT:
1317c478bd9Sstevel@tonic-gate  *	const ns_printer_t *printer - printer object to generate list from
1327c478bd9Sstevel@tonic-gate  * OUTPUT:
1337c478bd9Sstevel@tonic-gate  *	char * (return) - a newly allocated string containing the names of
1347c478bd9Sstevel@tonic-gate  *			  the printer
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate char *
ns_printer_name_list(const ns_printer_t * printer)1377c478bd9Sstevel@tonic-gate ns_printer_name_list(const ns_printer_t *printer)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if ((printer == NULL) || (printer->name == NULL))
1427c478bd9Sstevel@tonic-gate 		return (NULL);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s|", printer->name) >= sizeof (buf)) {
1457c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ns_printer_name:buffer overflow");
1467c478bd9Sstevel@tonic-gate 		return (NULL);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	list_iterate((void **)printer->aliases,
150*ef2333d1SToomas Soome 	    _ns_append_printer_name, buf, sizeof (buf));
1517c478bd9Sstevel@tonic-gate 
152f4593de7SToomas Soome 	buf[strlen(buf) - 1] = '\0';
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	return (strdup(buf));
1557c478bd9Sstevel@tonic-gate }
156