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