xref: /openbsd/usr.sbin/user/main.c (revision 9b7c3dbb)
1 /* $OpenBSD: main.c,v 1.10 2016/03/29 13:32:54 mestre Exp $ */
2 /* $NetBSD: main.c,v 1.3 2002/07/09 10:34:16 tron Exp $ */
3 
4 /*
5  * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Alistair G. Crooks.
18  * 4. The name of the author may not be used to endorse or promote
19  *    products derived from this software without specific prior written
20  *    permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "usermgmt.h"
41 
42 enum {
43 	MaxCmdWords = 2
44 };
45 
46 /* this struct describes a command */
47 typedef struct cmd_t {
48 	int		c_wc;				/* word count */
49 	const char	*c_word[MaxCmdWords];		/* command words */
50 	int		(*c_func)(int, char **);	/* called function */
51 } cmd_t;
52 
53 /* despatch table for commands */
54 static cmd_t	cmds[] = {
55 	{	1,	{ "useradd",	NULL },		useradd		},
56 	{	2,	{ "user",	"add" },	useradd		},
57 	{	1,	{ "usermod",	NULL },		usermod		},
58 	{	2,	{ "user",	"mod" },	usermod		},
59 	{	1,	{ "userdel",	NULL },		userdel		},
60 	{	2,	{ "user",	"del" },	userdel		},
61 	{	1,	{ "userinfo",	NULL },		userinfo	},
62 	{	2,	{ "user",	"info" },	userinfo	},
63 	{	1,	{ "groupadd",	NULL },		groupadd	},
64 	{	2,	{ "group",	"add" },	groupadd	},
65 	{	1,	{ "groupmod",	NULL },		groupmod	},
66 	{	2,	{ "group",	"mod" },	groupmod	},
67 	{	1,	{ "groupdel",	NULL },		groupdel	},
68 	{	2,	{ "group",	"del" },	groupdel	},
69 	{	1,	{ "groupinfo",	NULL },		groupinfo	},
70 	{	2,	{ "group",	"info" },	groupinfo	},
71 	{	0	}
72 };
73 
74 extern char	*__progname;
75 
76 int
77 main(int argc, char **argv)
78 {
79 	cmd_t	*cmdp;
80 	int	matched;
81 	int	i;
82 
83 	for (cmdp = cmds ; cmdp->c_wc > 0 ; cmdp++) {
84 		for (matched = i = 0 ; i < cmdp->c_wc && i < MaxCmdWords ; i++) {
85 			if (argc > i) {
86 				if (strcmp((i == 0) ? __progname : argv[i],
87 				    cmdp->c_word[i]) == 0) {
88 					matched += 1;
89 				} else
90 					break;
91 			}
92 		}
93 		if (matched == cmdp->c_wc && cmdp->c_func != NULL)
94 			return (*cmdp->c_func)(argc - (matched - 1),
95 			    argv + (matched - 1));
96 	}
97 	usermgmt_usage(__progname);
98 }
99