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