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