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