1*f6a7372bSchristos /* $NetBSD: main.c,v 1.5 2006/10/22 21:16:58 christos Exp $ */
27497747cSagc
37497747cSagc /*
47497747cSagc * Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
57497747cSagc *
67497747cSagc * Redistribution and use in source and binary forms, with or without
77497747cSagc * modification, are permitted provided that the following conditions
87497747cSagc * are met:
97497747cSagc * 1. Redistributions of source code must retain the above copyright
107497747cSagc * notice, this list of conditions and the following disclaimer.
117497747cSagc * 2. Redistributions in binary form must reproduce the above copyright
127497747cSagc * notice, this list of conditions and the following disclaimer in the
137497747cSagc * documentation and/or other materials provided with the distribution.
1440a712f2Sagc * 3. The name of the author may not be used to endorse or promote
157497747cSagc * products derived from this software without specific prior written
167497747cSagc * permission.
177497747cSagc *
187497747cSagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
197497747cSagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
207497747cSagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
217497747cSagc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
227497747cSagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237497747cSagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
247497747cSagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
257497747cSagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
267497747cSagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
277497747cSagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
287497747cSagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297497747cSagc */
307497747cSagc #include <err.h>
317497747cSagc #include <stdio.h>
327497747cSagc #include <stdlib.h>
337497747cSagc #include <string.h>
347497747cSagc #include <unistd.h>
357497747cSagc
367497747cSagc #include "usermgmt.h"
377497747cSagc
387497747cSagc enum {
397497747cSagc MaxCmdWords = 2
407497747cSagc };
417497747cSagc
427497747cSagc /* this struct describes a command */
437497747cSagc typedef struct cmd_t {
447497747cSagc int c_wc; /* word count */
45cb35c097Stron const char *c_word[MaxCmdWords]; /* command words */
46cb35c097Stron int (*c_func)(int, char **); /* called function */
477497747cSagc } cmd_t;
487497747cSagc
497497747cSagc /* despatch table for commands */
507497747cSagc static cmd_t cmds[] = {
517497747cSagc { 1, { "useradd", NULL }, useradd },
527497747cSagc { 2, { "user", "add" }, useradd },
537497747cSagc { 1, { "usermod", NULL }, usermod },
547497747cSagc { 2, { "user", "mod" }, usermod },
557497747cSagc { 1, { "userdel", NULL }, userdel },
567497747cSagc { 2, { "user", "del" }, userdel },
577497747cSagc #ifdef EXTENSIONS
587497747cSagc { 1, { "userinfo", NULL }, userinfo },
597497747cSagc { 2, { "user", "info" }, userinfo },
607497747cSagc #endif
617497747cSagc { 1, { "groupadd", NULL }, groupadd },
627497747cSagc { 2, { "group", "add" }, groupadd },
637497747cSagc { 1, { "groupmod", NULL }, groupmod },
647497747cSagc { 2, { "group", "mod" }, groupmod },
657497747cSagc { 1, { "groupdel", NULL }, groupdel },
667497747cSagc { 2, { "group", "del" }, groupdel },
677497747cSagc #ifdef EXTENSIONS
687497747cSagc { 1, { "groupinfo", NULL }, groupinfo },
697497747cSagc { 2, { "group", "info" }, groupinfo },
707497747cSagc #endif
71*f6a7372bSchristos { .c_wc = 0 }
727497747cSagc };
737497747cSagc
747497747cSagc int
main(int argc,char ** argv)757497747cSagc main(int argc, char **argv)
767497747cSagc {
777497747cSagc cmd_t *cmdp;
787497747cSagc int matched;
797497747cSagc int i;
807497747cSagc
817497747cSagc for (cmdp = cmds ; cmdp->c_wc > 0 ; cmdp++) {
827497747cSagc for (matched = i = 0 ; i < cmdp->c_wc && i < MaxCmdWords ; i++) {
837497747cSagc if (argc > i) {
8425bdbb66Scgd if (strcmp((i == 0) ? getprogname() : argv[i],
857497747cSagc cmdp->c_word[i]) == 0) {
867497747cSagc matched += 1;
877497747cSagc } else {
887497747cSagc break;
897497747cSagc }
907497747cSagc }
917497747cSagc }
927497747cSagc if (matched == cmdp->c_wc) {
937497747cSagc return (*cmdp->c_func)(argc - (matched - 1), argv + (matched - 1));
947497747cSagc }
957497747cSagc }
9625bdbb66Scgd usermgmt_usage(getprogname());
9725bdbb66Scgd errx(EXIT_FAILURE, "Program `%s' not recognised", getprogname());
987497747cSagc /* NOTREACHED */
997497747cSagc }
100