xref: /illumos-gate/usr/src/cmd/getent/getent.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <locale.h>
32 #include <unistd.h>
33 #include "getent.h"
34 
35 static const char *cmdname;
36 
37 struct table {
38 	char	*name;			/* name of the table */
39 	int	(*func)(const char **);	/* function to do the lookup */
40 };
41 
42 static struct table t[] = {
43 	{ "passwd",	dogetpw },
44 	{ "group",	dogetgr },
45 	{ "hosts",	dogethost },
46 	{ "ipnodes",	dogetipnodes },
47 	{ "services",	dogetserv },
48 	{ "protocols",	dogetproto },
49 	{ "ethers",	dogetethers },
50 	{ "networks",	dogetnet },
51 	{ "netmasks",	dogetnetmask },
52 	{ "project",	dogetproject },
53 	{ NULL,		NULL }
54 };
55 
56 static	void usage(void);
57 
58 main(int argc, const char **argv)
59 {
60 	struct table *p;
61 
62 	(void) setlocale(LC_ALL, "");
63 
64 #if !defined(TEXT_DOMAIN)
65 #define	TEXT_DOMAIN	"SYS_TEXT"
66 #endif
67 
68 	(void) textdomain(TEXT_DOMAIN);
69 
70 	cmdname = argv[0];
71 
72 	if (argc < 2)
73 		usage();
74 
75 	for (p = t; p->name != NULL; p++) {
76 		if (strcmp(argv[1], p->name) == 0) {
77 			int rc;
78 
79 			rc = (*p->func)(&argv[2]);
80 			switch (rc) {
81 			case EXC_SYNTAX:
82 				(void) fprintf(stderr,
83 					gettext("Syntax error\n"));
84 				break;
85 			case EXC_ENUM_NOT_SUPPORTED:
86 				(void) fprintf(stderr,
87 	gettext("Enumeration not supported on %s\n"), argv[1]);
88 				break;
89 			case EXC_NAME_NOT_FOUND:
90 				break;
91 			}
92 			exit(rc);
93 		}
94 	}
95 	(void) fprintf(stderr, gettext("Unknown database: %s\n"), argv[1]);
96 	usage();
97 	/* NOTREACHED */
98 }
99 
100 static void
101 usage(void)
102 {
103 	(void) fprintf(stderr,
104 	    gettext("usage: %s database [ key ... ]\n"), cmdname);
105 	exit(EXC_SYNTAX);
106 }
107