xref: /onnv-gate/usr/src/lib/print/libprint/common/ns_bsd_addr.c (revision 7253:39650c3caeca)
12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs /*
22*7253Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
232264Sjacobs  * Use is subject to license terms.
242264Sjacobs  */
252264Sjacobs 
262264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
272264Sjacobs 
282264Sjacobs /*LINTLIBRARY*/
292264Sjacobs 
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
332264Sjacobs #include <sys/types.h>
342264Sjacobs #include <stdarg.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <syslog.h>
372264Sjacobs 
382264Sjacobs #include <ns.h>
392264Sjacobs #include <list.h>
40*7253Sjacobs 
41*7253Sjacobs static char **
strsplit(char * string,char * seperators)42*7253Sjacobs strsplit(char *string, char *seperators)
43*7253Sjacobs {
44*7253Sjacobs 	char **list = NULL;
45*7253Sjacobs 	char *where = NULL;
46*7253Sjacobs 	char *element;
47*7253Sjacobs 
48*7253Sjacobs 	for (element = strtok_r(string, seperators, &where); element != NULL;
49*7253Sjacobs 	    element = strtok_r(NULL, seperators, &where))
50*7253Sjacobs 		list = (char **)list_append((void **)list, element);
51*7253Sjacobs 
52*7253Sjacobs 	return (list);
53*7253Sjacobs }
542264Sjacobs 
552264Sjacobs /*
562264Sjacobs  *	Manipulate bsd_addr structures
572264Sjacobs  */
582264Sjacobs ns_bsd_addr_t *
bsd_addr_create(const char * server,const char * printer,const char * extension)592264Sjacobs bsd_addr_create(const char *server, const char *printer, const char *extension)
602264Sjacobs {
612264Sjacobs 	ns_bsd_addr_t *addr = NULL;
622264Sjacobs 
632264Sjacobs 	if ((server != NULL) &&
642264Sjacobs 	    ((addr = calloc(1, sizeof (*addr))) != NULL)) {
652264Sjacobs 		addr->printer = (char *)printer;
662264Sjacobs 		addr->server = (char *)server;
672264Sjacobs 		addr->extension = (char *)extension;
682264Sjacobs 	}
692264Sjacobs 
702264Sjacobs 	return (addr);
712264Sjacobs }
722264Sjacobs 
732264Sjacobs static char *
bsd_addr_to_string(const ns_bsd_addr_t * addr)742264Sjacobs bsd_addr_to_string(const ns_bsd_addr_t *addr)
752264Sjacobs {
762264Sjacobs 	char buf[BUFSIZ];
772264Sjacobs 
782264Sjacobs 	if ((addr == NULL) || (addr->server == NULL))
792264Sjacobs 		return (NULL);
802264Sjacobs 
812264Sjacobs 	if (snprintf(buf, sizeof (buf), "%s", addr->server) >= sizeof (buf)) {
822264Sjacobs 		syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
832264Sjacobs 		return (NULL);
842264Sjacobs 	}
852264Sjacobs 
862264Sjacobs 	if ((addr->printer != NULL) || (addr->extension != NULL))
872264Sjacobs 		(void) strlcat(buf, ",", sizeof (buf));
882264Sjacobs 	if (addr->printer != NULL)
892264Sjacobs 		if (strlcat(buf, addr->printer, sizeof (buf)) >= sizeof (buf)) {
902264Sjacobs 			syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
912264Sjacobs 			return (NULL);
922264Sjacobs 		}
932264Sjacobs 	if (addr->extension != NULL) {
942264Sjacobs 		(void) strlcat(buf, ",", sizeof (buf));
952264Sjacobs 		if (strlcat(buf, addr->extension, sizeof (buf))
962264Sjacobs 						>= sizeof (buf)) {
972264Sjacobs 			syslog(LOG_ERR, "bsd_addr_to_string: buffer overflow");
982264Sjacobs 			return (NULL);
992264Sjacobs 		}
1002264Sjacobs 	}
1012264Sjacobs 
1022264Sjacobs 	return (strdup(buf));
1032264Sjacobs }
1042264Sjacobs 
1052264Sjacobs ns_bsd_addr_t *
string_to_bsd_addr(const char * string)1062264Sjacobs string_to_bsd_addr(const char *string)
1072264Sjacobs {
1082264Sjacobs 	char **list, *tmp, *printer = NULL, *extension = NULL;
1092264Sjacobs 
1102264Sjacobs 	if (string == NULL)
1112264Sjacobs 		return (NULL);
1122264Sjacobs 
1132264Sjacobs 	tmp = strdup(string);
1142264Sjacobs 	list = strsplit(tmp, ",");
1152264Sjacobs 
1162264Sjacobs 	if (list[1] != NULL) {
1172264Sjacobs 		printer = list[1];
1182264Sjacobs 		if (list[2] != NULL)
1192264Sjacobs 			extension = list[2];
1202264Sjacobs 	}
1212264Sjacobs 
1222264Sjacobs 	return (bsd_addr_create(list[0], printer, extension));
1232264Sjacobs }
1242264Sjacobs 
1252264Sjacobs static char *
list_to_string(const char ** list)1262264Sjacobs list_to_string(const char **list)
1272264Sjacobs {
1282264Sjacobs 	char buf[BUFSIZ];
1292264Sjacobs 
1302264Sjacobs 	if ((list == NULL) || (*list == NULL))
1312264Sjacobs 		return (NULL);
1322264Sjacobs 
1332264Sjacobs 	if (snprintf(buf, sizeof (buf), "%s", *list) >= sizeof (buf)) {
1342264Sjacobs 		syslog(LOG_ERR, "list_to_string: buffer overflow");
1352264Sjacobs 		return (NULL);
1362264Sjacobs 	}
1372264Sjacobs 
1382264Sjacobs 	while (*++list != NULL) {
1392264Sjacobs 		(void) strlcat(buf, ",", sizeof (buf));
1402264Sjacobs 		if (strlcat(buf, *list, sizeof (buf)) >= sizeof (buf)) {
1412264Sjacobs 			syslog(LOG_ERR, "list_to_string: buffer overflow");
1422264Sjacobs 			return (NULL);
1432264Sjacobs 		}
1442264Sjacobs 	}
1452264Sjacobs 
1462264Sjacobs 	return (strdup(buf));
1472264Sjacobs }
1482264Sjacobs 
1492264Sjacobs static char *
internal_list_to_string(const ns_printer_t ** list)1502264Sjacobs internal_list_to_string(const ns_printer_t **list)
1512264Sjacobs {
1522264Sjacobs 	char buf[BUFSIZ];
1532264Sjacobs 
1542264Sjacobs 	if ((list == NULL) || (*list == NULL))
1552264Sjacobs 		return (NULL);
1562264Sjacobs 
1572264Sjacobs 	if (snprintf(buf, sizeof (buf), "%s", (*list)->name) >= sizeof (buf)) {
1582264Sjacobs 		syslog(LOG_ERR, "internal_list_to_string:buffer overflow");
1592264Sjacobs 		return (NULL);
1602264Sjacobs 	}
1612264Sjacobs 
1622264Sjacobs 	while (*++list != NULL) {
1632264Sjacobs 		(void) strlcat(buf, ",", sizeof (buf));
1642264Sjacobs 		if (strlcat(buf, (*list)->name, sizeof (buf)) >= sizeof (buf)) {
1652264Sjacobs 			syslog(LOG_ERR,
1662264Sjacobs 				"internal_list_to_string:buffer overflow");
1672264Sjacobs 			return (NULL);
1682264Sjacobs 		}
1692264Sjacobs 	}
1702264Sjacobs 
1712264Sjacobs 	return (strdup(buf));
1722264Sjacobs }
1732264Sjacobs 
1742264Sjacobs 
1752264Sjacobs char *
value_to_string(const char * key,void * value)1762264Sjacobs value_to_string(const char *key, void *value)
1772264Sjacobs {
1782264Sjacobs 	char *string = NULL;
1792264Sjacobs 
1802264Sjacobs 	if ((key != NULL) && (value != NULL)) {
1812264Sjacobs 		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
1822264Sjacobs 			string = bsd_addr_to_string(value);
1832264Sjacobs 		} else if ((strcmp(key, NS_KEY_ALL) == 0) ||
1842264Sjacobs 			    (strcmp(key, NS_KEY_GROUP) == 0)) {
1852264Sjacobs 			string = list_to_string(value);
1862264Sjacobs 		} else if (strcmp(key, NS_KEY_LIST) == 0) {
1872264Sjacobs 			string = internal_list_to_string(value);
1882264Sjacobs 		} else {
1892264Sjacobs 			string = strdup((char *)value);
1902264Sjacobs 		}
1912264Sjacobs 	}
1922264Sjacobs 
1932264Sjacobs 	return (string);
1942264Sjacobs }
1952264Sjacobs 
1962264Sjacobs 
1972264Sjacobs void *
string_to_value(const char * key,char * string)1982264Sjacobs string_to_value(const char *key, char *string)
1992264Sjacobs {
2002264Sjacobs 	void *value = NULL;
2012264Sjacobs 
2022264Sjacobs 	if ((key != NULL) && (string != NULL) && (string[0] != NULL)) {
2032264Sjacobs 		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
2042264Sjacobs 			value = (void *)string_to_bsd_addr(string);
2052264Sjacobs 		} else if ((strcmp(key, NS_KEY_ALL) == 0) ||
2062264Sjacobs 			    (strcmp(key, NS_KEY_GROUP) == 0)) {
2072264Sjacobs 			value = (void *)strsplit(string, ",");
2082264Sjacobs 		} else {
2092264Sjacobs 			value = (void *)string;
2102264Sjacobs 		}
2112264Sjacobs 	}
2122264Sjacobs 
2132264Sjacobs 	return (value);
2142264Sjacobs }
2152264Sjacobs 
2162264Sjacobs static void
split_name(char * name,const char * delimiter,char ** p1,char ** p2,char ** p3)2172264Sjacobs split_name(char *name, const char *delimiter, char **p1, char **p2, char **p3)
2182264Sjacobs {
2192264Sjacobs 	char *tmp, *junk = NULL;
2202264Sjacobs 
2212264Sjacobs 	if (p1 != NULL)
2222264Sjacobs 		*p1 = NULL;
2232264Sjacobs 	if (p2 != NULL)
2242264Sjacobs 		*p2 = NULL;
2252264Sjacobs 	if (p3 != NULL)
2262264Sjacobs 		*p3 = NULL;
2272264Sjacobs 
2282264Sjacobs 	if ((name == NULL) || (delimiter == NULL)) {
2292264Sjacobs 		syslog(LOG_DEBUG, "split_name(): name/delimter invalid\n");
2302264Sjacobs 		return;
2312264Sjacobs 	}
2322264Sjacobs 
2332264Sjacobs 	for (tmp = (char *)strtok_r(name, delimiter, &junk); tmp != NULL;
2342264Sjacobs 	    tmp = (char *)strtok_r(NULL, delimiter, &junk))
2352264Sjacobs 		if ((p1 != NULL) && (*p1 == NULL))
2362264Sjacobs 			*p1 = tmp;
2372264Sjacobs 		else if ((p2 != NULL) && (*p2 == NULL)) {
2382264Sjacobs 			*p2 = tmp;
2392264Sjacobs 			if (p3 == NULL)
2402264Sjacobs 				break;
2412264Sjacobs 		} else if ((p3 != NULL) && (*p3 == NULL)) {
2422264Sjacobs 			*p3 = tmp;
2432264Sjacobs 			break;
2442264Sjacobs 		}
2452264Sjacobs }
2462264Sjacobs 
2472264Sjacobs /*
2482264Sjacobs  * This implements support for printer names that are fully resolvable
2492264Sjacobs  * on their own.  These "complete" names are converted into a ns_printer_t
2502264Sjacobs  * structure containing an appropriate "bsdaddr" attribute.  The supported
2512264Sjacobs  * formats are as follows:
2522264Sjacobs  *	POSIX style (server:printer[:conformance]).
2532264Sjacobs  *		This format is an adaptation of the format originally
2542264Sjacobs  *		described in POSIX 1387.4.  The POSIX draft has since been
2552264Sjacobs  *		squashed, but this particular component lives on.  The
2562264Sjacobs  *		conformace field has been added to allow further identification
2572264Sjacobs  *		of the the server.
2582264Sjacobs  */
2592264Sjacobs ns_printer_t *
posix_name(const char * name)2602264Sjacobs posix_name(const char *name)
2612264Sjacobs {
2622264Sjacobs 	ns_printer_t *printer = NULL;
2632264Sjacobs 	char *tmp = NULL;
2642264Sjacobs 
2652264Sjacobs 	if ((name != NULL) && ((tmp = strpbrk(name, ":")) != NULL)) {
2662264Sjacobs 		char *server = NULL;
2672264Sjacobs 		char *queue = NULL;
2682264Sjacobs 		char *extension = NULL;
2692264Sjacobs 		char *addr = strdup(name);
2702264Sjacobs 		char buf[BUFSIZ];
2712264Sjacobs 
2722264Sjacobs 		if (*tmp == ':')
2732264Sjacobs 			split_name(addr, ": \t", &server, &queue, &extension);
2742264Sjacobs 
2752264Sjacobs 		memset(buf, 0, sizeof (buf));
2762264Sjacobs 		if ((server != NULL) && (queue != NULL))
2772264Sjacobs 			snprintf(buf, sizeof (buf), "%s,%s%s%s", server,
2782264Sjacobs 				queue, (extension != NULL ? "," : ""),
2792264Sjacobs 				(extension != NULL ? extension : ""));
2802264Sjacobs 
2812264Sjacobs 		/* build the structure here */
2822264Sjacobs 		if (buf[0] != NULL) {
2832264Sjacobs 			ns_kvp_t **list, *kvp;
2842264Sjacobs 
2852264Sjacobs 			kvp = ns_kvp_create(NS_KEY_BSDADDR, buf);
2862264Sjacobs 			list = (ns_kvp_t **)list_append(NULL, kvp);
2872264Sjacobs 			if (list != NULL)
2882264Sjacobs 				printer = ns_printer_create(strdup(name), NULL,
2892264Sjacobs 							"posix", list);
2902264Sjacobs 		}
2912264Sjacobs 	}
2922264Sjacobs 
2932264Sjacobs 	return (printer);
2942264Sjacobs }
2952264Sjacobs 
2962264Sjacobs /*
2972264Sjacobs  * FUNCTION:
2982264Sjacobs  *	int ns_bsd_addr_cmp(ns_bsd_addr_t *at, ns_bsd_addr_t *a2)
2992264Sjacobs  * INPUTS:
3002264Sjacobs  *	ns_bsd_addr_t *a1 - a bsd addr
3012264Sjacobs  *	ns_bsd_addr_t *21 - another bsd addr
3022264Sjacobs  * DESCRIPTION:
3032264Sjacobs  *	This functions compare 2 bsd_addr structures to determine if the
3042264Sjacobs  *	information in them is the same.
3052264Sjacobs  */
3062264Sjacobs static int
ns_bsd_addr_cmp(ns_bsd_addr_t * a1,ns_bsd_addr_t * a2)3072264Sjacobs ns_bsd_addr_cmp(ns_bsd_addr_t *a1, ns_bsd_addr_t *a2)
3082264Sjacobs {
3092264Sjacobs 	int rc;
3102264Sjacobs 
3112264Sjacobs 	if ((a1 == NULL) || (a2 == NULL))
3122264Sjacobs 		return (1);
3132264Sjacobs 
3142264Sjacobs 	if ((rc = strcmp(a1->server, a2->server)) != 0)
3152264Sjacobs 		return (rc);
3162264Sjacobs 
3172264Sjacobs 	if ((a1->printer == NULL) || (a2->printer == NULL))
3182264Sjacobs 		return (a1->printer != a2->printer);
3192264Sjacobs 
3202264Sjacobs 	return (strcmp(a1->printer, a2->printer));
3212264Sjacobs }
3222264Sjacobs 
3232264Sjacobs 
3242264Sjacobs 
3252264Sjacobs 
3262264Sjacobs /*
3272264Sjacobs  * FUNCTION:    ns_bsd_addr_cmp_local()
3282264Sjacobs  *
3292264Sjacobs  * DESCRIPTION: This function compares 2 bsd_addr structures to determine if
3302264Sjacobs  *              the information in them is the same. It destinquishes between
3312264Sjacobs  *              real printer names and alias names while doing the compare.
3322264Sjacobs  *
3332264Sjacobs  * INPUTS:      ns_bsd_addr_t *a1 - a bsd addr
3342264Sjacobs  *              ns_bsd_addr_t *21 - another bsd addr
3352264Sjacobs  */
3362264Sjacobs 
3372264Sjacobs static int
ns_bsd_addr_cmp_local(ns_bsd_addr_t * a1,ns_bsd_addr_t * a2)3382264Sjacobs ns_bsd_addr_cmp_local(ns_bsd_addr_t *a1, ns_bsd_addr_t *a2)
3392264Sjacobs 
3402264Sjacobs {
3412264Sjacobs 	int rc;
3422264Sjacobs 
3432264Sjacobs 	if ((a1 == NULL) || (a2 == NULL))
3442264Sjacobs 	{
3452264Sjacobs 		return (1);
3462264Sjacobs 	}
3472264Sjacobs 
3482264Sjacobs 	if ((rc = strcmp(a1->server, a2->server)) != 0)
3492264Sjacobs 	{
3502264Sjacobs 		return (rc);
3512264Sjacobs 	}
3522264Sjacobs 
3532264Sjacobs 	if ((a1->printer == NULL) || (a2->printer == NULL))
3542264Sjacobs 	{
3552264Sjacobs 		return (a1->printer != a2->printer);
3562264Sjacobs 	}
3572264Sjacobs 
3582264Sjacobs 	rc = strcmp(a1->printer, a2->printer);
3592264Sjacobs 	if (rc == 0)
3602264Sjacobs 	{
3612264Sjacobs 		/*
3622264Sjacobs 		 * The printer's real names are the same, but now check if
3632264Sjacobs 		 * their local names (alias) are the same.
3642264Sjacobs 		 */
3652264Sjacobs 		rc = strcmp(a1->pname, a2->pname);
3662264Sjacobs 	}
3672264Sjacobs 
3682264Sjacobs 	return (rc);
3692264Sjacobs } /* ns_bsd_addr_cmp_local */
3702264Sjacobs 
3712264Sjacobs 
3722264Sjacobs 
3732264Sjacobs /*
3742264Sjacobs  * FUNCTION:
3752264Sjacobs  *	ns_bsd_addr_t *ns_bsd_addr_get_name(char *name)
3762264Sjacobs  * INPUTS:
3772264Sjacobs  *	char *name - name of printer to get address for
3782264Sjacobs  * OUTPUTS:
3792264Sjacobs  *	ns_bsd_addr_t *(return) - the address of the printer
3802264Sjacobs  * DESCRIPTION:
3812264Sjacobs  *	This function will get the BSD address of the printer specified.
3822264Sjacobs  *	it fills in the printer name if none is specified in the "name service"
3832264Sjacobs  *	as a convenience to calling functions.
3842264Sjacobs  */
3852264Sjacobs ns_bsd_addr_t *
ns_bsd_addr_get_name(char * name)3862264Sjacobs ns_bsd_addr_get_name(char *name)
3872264Sjacobs {
3882264Sjacobs 	ns_printer_t *printer;
3892264Sjacobs 	ns_bsd_addr_t *addr = NULL;
3902264Sjacobs 
3912264Sjacobs 	endprinterentry();
3922264Sjacobs 	if ((printer = ns_printer_get_name(name, NULL)) != NULL) {
3932264Sjacobs 		addr = ns_get_value(NS_KEY_BSDADDR, printer);
3942264Sjacobs 
3952264Sjacobs 		if (addr != NULL && addr->printer == NULL)
3962264Sjacobs 			addr->printer = strdup(printer->name);
3972264Sjacobs 		if (addr != NULL) {
3982264Sjacobs 			/*
3992264Sjacobs 			 * if the name given is not the same as that in the
4002264Sjacobs 			 * this is an alias/remote name so put that into the
4012264Sjacobs 			 * pname field otherwise duplicate the real printer
4022264Sjacobs 			 * name
4032264Sjacobs 			 */
4042264Sjacobs 			if (strcmp(name, printer->name) != 0) {
4052264Sjacobs 				addr->pname = strdup(name);
4062264Sjacobs 			} else {
4072264Sjacobs 				addr->pname = strdup(printer->name);
4082264Sjacobs 			}
4092264Sjacobs 		}
4102264Sjacobs 	}
4112264Sjacobs 
4122264Sjacobs 	return (addr);
4132264Sjacobs }
4142264Sjacobs 
4152264Sjacobs 
4162264Sjacobs /*
4172264Sjacobs  * FUNCTION:
4182264Sjacobs  *	ns_bsd_addr_t **ns_bsd_addr_get_list()
4192264Sjacobs  * OUTPUT:
4202264Sjacobs  *	ns_bsd_addr_t **(return) - a list of bsd addresses for all printers
4212264Sjacobs  *				   in all "name services"
4222264Sjacobs  * DESCRIPTION:
4232264Sjacobs  *	This function will gather a list of all printer addresses in all
4242264Sjacobs  *	of the "name services".  All redundancy is removed.
4252264Sjacobs  */
4262264Sjacobs ns_bsd_addr_t **
ns_bsd_addr_get_list(int unique)4272264Sjacobs ns_bsd_addr_get_list(int unique)
4282264Sjacobs 
4292264Sjacobs {
4302264Sjacobs 	ns_printer_t **printers;
4312264Sjacobs 	ns_bsd_addr_t **list = NULL;
4322264Sjacobs 	char **aliases = NULL;
4332264Sjacobs 
4342264Sjacobs 	for (printers = ns_printer_get_list(NULL);
4352264Sjacobs 			printers != NULL && *printers != NULL; printers++) {
4362264Sjacobs 		ns_bsd_addr_t *addr;
4372264Sjacobs 
4382264Sjacobs 		if (strcmp(NS_NAME_ALL, (*printers)->name) == 0)
4392264Sjacobs 			continue;
4402264Sjacobs 
4412264Sjacobs 		if ((addr = ns_get_value(NS_KEY_BSDADDR, *printers)) != NULL) {
4422264Sjacobs 			if (addr->printer == NULL)
4432264Sjacobs 				addr->printer = strdup((*printers)->name);
4442264Sjacobs 			addr->pname = strdup((*printers)->name);
4452264Sjacobs 		}
4462264Sjacobs 
4472264Sjacobs 		if (unique == UNIQUE)
4482264Sjacobs 			list =
4492264Sjacobs 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
4502264Sjacobs 				(void *)addr, (COMP_T)ns_bsd_addr_cmp);
4512264Sjacobs 		else
4522264Sjacobs 		if (unique == LOCAL_UNIQUE)
4532264Sjacobs 			list =
4542264Sjacobs 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
4552264Sjacobs 				(void *)addr, (COMP_T)ns_bsd_addr_cmp_local);
4562264Sjacobs 		else
4572264Sjacobs 			list = (ns_bsd_addr_t **)list_append((void **)list,
4582264Sjacobs 					(void *)addr);
4592264Sjacobs 
4602264Sjacobs 		for (aliases = (*printers)->aliases;
4612264Sjacobs 			(aliases != NULL) && (*aliases != NULL); aliases++)
4622264Sjacobs 		{
4632264Sjacobs 			/*
4642264Sjacobs 			 * Include any alias names that belong to the printer
4652264Sjacobs 			 */
4662264Sjacobs 
4672264Sjacobs 			if ((addr =
4682264Sjacobs 			    ns_get_value(NS_KEY_BSDADDR, *printers)) != NULL)
4692264Sjacobs 			{
4702264Sjacobs 				if (addr->printer == NULL)
4712264Sjacobs 				{
4722264Sjacobs 					addr->printer = strdup(*aliases);
4732264Sjacobs 				}
4742264Sjacobs 				addr->pname = strdup(*aliases);
4752264Sjacobs 			}
4762264Sjacobs 
4772264Sjacobs 			if (unique == UNIQUE)
4782264Sjacobs 			{
4792264Sjacobs 				list = (ns_bsd_addr_t **)
4802264Sjacobs 					list_append_unique((void **)list,
4812264Sjacobs 					(void *)addr, (COMP_T)ns_bsd_addr_cmp);
4822264Sjacobs 			}
4832264Sjacobs 			else
4842264Sjacobs 			if (unique == LOCAL_UNIQUE)
4852264Sjacobs 			{
4862264Sjacobs 				list = (ns_bsd_addr_t **)
4872264Sjacobs 					list_append_unique((void **)list,
4882264Sjacobs 						(void *)addr,
4892264Sjacobs 						(COMP_T)ns_bsd_addr_cmp_local);
4902264Sjacobs 			}
4912264Sjacobs 			else
4922264Sjacobs 			{
4932264Sjacobs 				list = (ns_bsd_addr_t **)
4942264Sjacobs 				    list_append((void **)list, (void *)addr);
4952264Sjacobs 			}
4962264Sjacobs 		}
4972264Sjacobs 	}
4982264Sjacobs 
4992264Sjacobs 	return (list);
5002264Sjacobs }
5012264Sjacobs 
5022264Sjacobs 
5032264Sjacobs 
5042264Sjacobs 
5052264Sjacobs /*
5062264Sjacobs  * FUNCTION:
5072264Sjacobs  *	ns_bsd_addr_t **ns_bsd_addr_get_list()
5082264Sjacobs  * OUTPUT:
5092264Sjacobs  *	ns_bsd_addr_t **(return) - a list of bsd addresses for "_all" printers
5102264Sjacobs  *				   in the "name service"
5112264Sjacobs  * DESCRIPTION:
5122264Sjacobs  *	This function will use the "_all" entry to find a list of printers and
5132264Sjacobs  *	addresses. The "default" printer is also added to the list.
5142264Sjacobs  *	All redundancy is removed.
5152264Sjacobs  */
5162264Sjacobs ns_bsd_addr_t **
ns_bsd_addr_get_all(int unique)5172264Sjacobs ns_bsd_addr_get_all(int unique)
5182264Sjacobs {
5192264Sjacobs 	ns_printer_t *printer;
5202264Sjacobs 	ns_bsd_addr_t **list = NULL;
5212264Sjacobs 	char **printers;
5222264Sjacobs 	char *def = NULL;
5232264Sjacobs 
5242264Sjacobs 	if (((def = (char *)getenv("PRINTER")) == NULL) &&
5252264Sjacobs 	    ((def = (char *)getenv("LPDEST")) == NULL))
5262264Sjacobs 		def = NS_NAME_DEFAULT;
5272264Sjacobs 
5282264Sjacobs 	list = (ns_bsd_addr_t **)list_append((void **)list,
5292264Sjacobs 			(void *)ns_bsd_addr_get_name(def));
5302264Sjacobs 
5312264Sjacobs 	endprinterentry();
5322264Sjacobs 	if ((printer = ns_printer_get_name(NS_NAME_ALL, NULL)) == NULL)
5332264Sjacobs 		return (ns_bsd_addr_get_list(unique));
5342264Sjacobs 
5352264Sjacobs 	for (printers = (char **)ns_get_value(NS_KEY_ALL, printer);
5362264Sjacobs 			printers != NULL && *printers != NULL; printers++) {
5372264Sjacobs 		ns_bsd_addr_t *addr;
5382264Sjacobs 
5392264Sjacobs 		addr = ns_bsd_addr_get_name(*printers);
5402264Sjacobs 		if (addr != NULL)
5412264Sjacobs 			addr->pname = *printers;
5422264Sjacobs 		if (unique == UNIQUE)
5432264Sjacobs 			list =
5442264Sjacobs 			    (ns_bsd_addr_t **)list_append_unique((void **)list,
5452264Sjacobs 				(void *)addr, (COMP_T)ns_bsd_addr_cmp);
5462264Sjacobs 		else
5472264Sjacobs 			list = (ns_bsd_addr_t **)list_append((void **)list,
5482264Sjacobs 					(void *)addr);
5492264Sjacobs 	}
5502264Sjacobs 
5512264Sjacobs 	return (list);
5522264Sjacobs }
5532264Sjacobs 
5542264Sjacobs ns_bsd_addr_t *
ns_bsd_addr_get_default()5552264Sjacobs ns_bsd_addr_get_default()
5562264Sjacobs {
5572264Sjacobs 	char *def = NULL;
5582264Sjacobs 	ns_bsd_addr_t *addr;
5592264Sjacobs 
5602264Sjacobs 	if (((def = (char *)getenv("PRINTER")) == NULL) &&
5612264Sjacobs 	    ((def = (char *)getenv("LPDEST")) == NULL)) {
5622264Sjacobs 		def = NS_NAME_DEFAULT;
5632264Sjacobs 		addr = ns_bsd_addr_get_name(def);
5642264Sjacobs 		if (addr != NULL) {
5652264Sjacobs 			addr->pname = def;
5662264Sjacobs 			return (addr);
5672264Sjacobs 		}
5682264Sjacobs 	}
5692264Sjacobs 
5702264Sjacobs 	return (NULL);
5712264Sjacobs }
572