From bbd13d34a8e98d3ad7f71fc6d00d4c13de2936c4 Mon Sep 17 00:00:00 2001 From: lukem Date: Tue, 7 Dec 1999 10:42:12 +0000 Subject: [PATCH] * don't dump core if run without args * use strlcpy() instead of homegrown strnncpy() * sort #includes --- usr.sbin/user/user.c | 46 ++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/usr.sbin/user/user.c b/usr.sbin/user/user.c index 2e9de8942a7d..c89e940dbb99 100644 --- a/usr.sbin/user/user.c +++ b/usr.sbin/user/user.c @@ -1,4 +1,4 @@ -/* $NetBSD: user.c,v 1.4 1999/12/07 10:14:03 lukem Exp $ */ +/* $NetBSD: user.c,v 1.5 1999/12/07 10:42:12 lukem Exp $ */ /* * Copyright (c) 1999 Alistair G. Crooks. All rights reserved. @@ -34,19 +34,19 @@ #include #include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include #include -#include -#include -#include #include +#include #include -#include -#include -#include #include "defs.h" #include "usermgmt.h" @@ -92,17 +92,6 @@ asystem(char *fmt, ...) return ret; } -/* bounds checking strncpy */ -static char * -strnncpy(char *to, size_t tosize, char *from, size_t fromsize) -{ - size_t n = MIN(tosize - 1, fromsize); - - (void) memcpy(to, from, n); - to[n] = 0; - return to; -} - /* copy any dot files into the user's home directory */ static int copydotfiles(char *skeldir, int uid, int gid, char *dir) @@ -235,7 +224,7 @@ modify_gid(char *group, char *newent) continue; } else { cc = strlen(newent); - (void) strnncpy(buf, sizeof(buf), newent, (unsigned) cc); + (void) strlcpy(buf, newent, sizeof(buf)); } } if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) { @@ -786,6 +775,10 @@ usage(char *prog) (void) fprintf(stderr, "%s [-v] group\n", prog); } else if (strcmp(prog, "groupmod") == 0) { (void) fprintf(stderr, "%s [-ggid] [-o] [-nnewname] [-v] group\n", prog); + } else if ((strcmp(prog, "user") == 0) || (strcmp(prog, "group") == 0)) { + (void) fprintf(stderr, "%s ( add | del | mod ) ...\n", prog); + } else { + warn("usage() called with unknown prog `%s'", prog); } exit(EXIT_FAILURE); /* NOTREACHED */ @@ -943,7 +936,7 @@ usermod(int argc, char **argv) break; case 'l': have_new_user = 1; - (void) strnncpy(newuser, sizeof(newuser), optarg, strlen(optarg)); + (void) strlcpy(newuser, optarg, sizeof(newuser)); break; case 'm': u.u_mkdir = 1; @@ -1231,9 +1224,12 @@ main(int argc, char **argv) for (cmdp = cmds ; cmdp->c_progname ; cmdp++) { if (strcmp(__progname, cmdp->c_progname) == 0) { return (*cmdp->c_func)(argc, argv); - } else if (strcmp(__progname, cmdp->c_word1) == 0 && - strcmp(argv[1], cmdp->c_word2) == 0) { - return (*cmdp->c_func)(argc - 1, argv + 1); + } else if (strcmp(__progname, cmdp->c_word1) == 0) { + if (argc > 1 && strcmp(argv[1], cmdp->c_word2) == 0) { + return (*cmdp->c_func)(argc - 1, argv + 1); + } else { + usage(__progname); + } } } errx(EXIT_FAILURE, "Program `%s' not recognised", __progname);