xref: /netbsd-src/usr.bin/netgroup/netgroup.c (revision eb640195f592fa1a49decd9993cd8f6bd6d63795)
1*eb640195Sdholland /*	$NetBSD: netgroup.c,v 1.9 2016/09/03 05:58:30 dholland Exp $	*/
27a3f6c58Schristos 
37a3f6c58Schristos /*-
47a3f6c58Schristos  * Copyright (c) 1999 The NetBSD Foundation, Inc.
57a3f6c58Schristos  * All rights reserved.
67a3f6c58Schristos  *
77a3f6c58Schristos  * This code is derived from software contributed to The NetBSD Foundation
87a3f6c58Schristos  * by Christos Zoulas.
97a3f6c58Schristos  *
107a3f6c58Schristos  * Redistribution and use in source and binary forms, with or without
117a3f6c58Schristos  * modification, are permitted provided that the following conditions
127a3f6c58Schristos  * are met:
137a3f6c58Schristos  * 1. Redistributions of source code must retain the above copyright
147a3f6c58Schristos  *    notice, this list of conditions and the following disclaimer.
157a3f6c58Schristos  * 2. Redistributions in binary form must reproduce the above copyright
167a3f6c58Schristos  *    notice, this list of conditions and the following disclaimer in the
177a3f6c58Schristos  *    documentation and/or other materials provided with the distribution.
187a3f6c58Schristos  *
197a3f6c58Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207a3f6c58Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217a3f6c58Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227a3f6c58Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237a3f6c58Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247a3f6c58Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257a3f6c58Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267a3f6c58Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277a3f6c58Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287a3f6c58Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297a3f6c58Schristos  * POSSIBILITY OF SUCH DAMAGE.
307a3f6c58Schristos  */
317a3f6c58Schristos 
327a3f6c58Schristos #include <sys/cdefs.h>
33*eb640195Sdholland __RCSID("$NetBSD: netgroup.c,v 1.9 2016/09/03 05:58:30 dholland Exp $");
347a3f6c58Schristos 
357a3f6c58Schristos #include <stdio.h>
36fcd0fb11Smatt #include <stdlib.h>
377a3f6c58Schristos #include <unistd.h>
387a3f6c58Schristos #include <netgroup.h>
397a3f6c58Schristos 
40*eb640195Sdholland static __dead void
usage(void)41d34c2845Smatt usage(void)
427a3f6c58Schristos {
437a3f6c58Schristos 
44b635f565Sjmmv 	(void)fprintf(stderr, "usage: %s [-hud] <netgroup>\n", getprogname());
457a3f6c58Schristos 	exit(1);
467a3f6c58Schristos }
477a3f6c58Schristos 
487a3f6c58Schristos int
main(int argc,char * argv[])49d34c2845Smatt main(int argc, char *argv[])
507a3f6c58Schristos {
517a3f6c58Schristos 	int c, i = 0;
527a3f6c58Schristos 	const char *p[3];
537a3f6c58Schristos 
547a3f6c58Schristos 	while ((c = getopt(argc, argv, "hud")) != -1)
557a3f6c58Schristos 		switch (c) {
567a3f6c58Schristos 		case 'h':
577a3f6c58Schristos 			i = 0;
587a3f6c58Schristos 			break;
597a3f6c58Schristos 		case 'u':
607a3f6c58Schristos 			i = 1;
617a3f6c58Schristos 			break;
627a3f6c58Schristos 		case 'd':
637a3f6c58Schristos 			i = 2;
647a3f6c58Schristos 			break;
657a3f6c58Schristos 		default:
667a3f6c58Schristos 			usage();
677a3f6c58Schristos 		}
687a3f6c58Schristos 
697a3f6c58Schristos 	if (optind >= argc)
707a3f6c58Schristos 		usage();
717a3f6c58Schristos 
727a3f6c58Schristos 	for(; optind < argc; optind++) {
737a3f6c58Schristos 		setnetgrent(argv[optind]);
747a3f6c58Schristos 		while (getnetgrent(&p[0], &p[1], &p[2]))
757a3f6c58Schristos 			if (p[i])
767a3f6c58Schristos 				printf("%s\n", p[i]);
777a3f6c58Schristos 		endnetgrent();
787a3f6c58Schristos 	}
797a3f6c58Schristos 	return 0;
807a3f6c58Schristos }
81