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