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