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