diff options
author | robert <robert@FreeBSD.org> | 2004-06-17 14:07:16 +0000 |
---|---|---|
committer | robert <robert@FreeBSD.org> | 2004-06-17 14:07:16 +0000 |
commit | 0f9ac8040a5e3f5e724e16f7616fb2443b7f1f8c (patch) | |
tree | 272aebdf55e7b5fbdd34d2127d480ff57d520195 | |
parent | cac15e3814fdc7dcd94b48e13ea1e949d2c9e5c1 (diff) | |
download | FreeBSD-src-0f9ac8040a5e3f5e724e16f7616fb2443b7f1f8c.zip FreeBSD-src-0f9ac8040a5e3f5e724e16f7616fb2443b7f1f8c.tar.gz |
Use strlcpy(3) to replace the idiomatic
strncpy(d, s, l);
d[l - 1] = '\0';
statements.
-rw-r--r-- | usr.sbin/pw/psdate.c | 16 | ||||
-rw-r--r-- | usr.sbin/pw/pw_user.c | 26 | ||||
-rw-r--r-- | usr.sbin/pw/pw_vpw.c | 6 |
3 files changed, 18 insertions, 30 deletions
diff --git a/usr.sbin/pw/psdate.c b/usr.sbin/pw/psdate.c index f97253b..3f4c010 100644 --- a/usr.sbin/pw/psdate.c +++ b/usr.sbin/pw/psdate.c @@ -234,8 +234,8 @@ parse_date(time_t dt, char const * str) * Skip past any weekday prefix */ weekday(&str); - str = strncpy(tmp, str, sizeof tmp - 1); - tmp[sizeof tmp - 1] = '\0'; + strlcpy(tmp, str, sizeof(tmp)); + str = tmp; T = localtime(&dt); /* @@ -275,19 +275,15 @@ parse_date(time_t dt, char const * str) if ((q = strpbrk(p, " \t")) != NULL) { /* Time first? */ int l = q - str; - strncpy(timestr, str, l); - timestr[l] = '\0'; - strncpy(datestr, q + 1, sizeof datestr); - datestr[sizeof datestr - 1] = '\0'; + strlcpy(timestr, str, l + 1); + strlcpy(datestr, q + 1, sizeof(datestr)); parse_time(timestr, &T->tm_hour, &T->tm_min, &T->tm_sec); parse_datesub(datestr, &T->tm_mday, &T->tm_mon, &T->tm_year); } else if ((q = strrchr(tmp, ' ')) != NULL) { /* Time last */ int l = q - tmp; - strncpy(timestr, q + 1, sizeof timestr); - timestr[sizeof timestr - 1] = '\0'; - strncpy(datestr, tmp, l); - datestr[l] = '\0'; + strlcpy(timestr, q + 1, sizeof(timestr)); + strlcpy(datestr, tmp, l + 1); } else /* Bail out */ return dt; parse_time(timestr, &T->tm_hour, &T->tm_min, &T->tm_sec); diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c index f7a6c56..5fd3671 100644 --- a/usr.sbin/pw/pw_user.c +++ b/usr.sbin/pw/pw_user.c @@ -187,8 +187,8 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) } /* If this falls, fall back to old method */ } - p = strncpy(dbuf, cnf->home, sizeof dbuf); - dbuf[MAXPATHLEN-1] = '\0'; + strlcpy(dbuf, cnf->home, sizeof(dbuf)); + p = dbuf; if (stat(dbuf, &st) == -1) { while ((p = strchr(++p, '/')) != NULL) { *p = '\0'; @@ -396,8 +396,7 @@ pw_user(struct userconf * cnf, int mode, struct cargs * args) * invalidated by deletion */ sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); - strncpy(home, pwd->pw_dir, sizeof home); - home[sizeof home - 1] = '\0'; + strlcpy(home, pwd->pw_dir, sizeof(home)); rc = delpwent(pwd); if (rc == -1) @@ -978,8 +977,7 @@ shell_path(char const * path, char *shells[], char *sh) /* * We need to search paths */ - strncpy(paths, path, sizeof paths); - paths[sizeof paths - 1] = '\0'; + strlcpy(paths, path, sizeof(paths)); for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { int i; static char shellpath[256]; @@ -1118,8 +1116,7 @@ pw_password(struct userconf * cnf, struct cargs * args, char const * user) return "*"; case 1: /* user's name */ - strncpy(pwbuf, user, sizeof pwbuf); - pwbuf[sizeof pwbuf - 1] = '\0'; + strlcpy(pwbuf, user, sizeof(pwbuf)); break; } return pw_pwcrypt(pwbuf); @@ -1144,17 +1141,14 @@ print_user(struct passwd * pwd, int pretty, int v7) struct tm * tptr; if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { - strncpy(uname, p, sizeof uname); - uname[sizeof uname - 1] = '\0'; + strlcpy(uname, p, sizeof(uname)); if ((p = strtok(NULL, ",")) != NULL) { - strncpy(office, p, sizeof office); - office[sizeof office - 1] = '\0'; + strlcpy(office, p, sizeof(office)); if ((p = strtok(NULL, ",")) != NULL) { - strncpy(wphone, p, sizeof wphone); - wphone[sizeof wphone - 1] = '\0'; + strlcpy(wphone, p, sizeof(wphone)); if ((p = strtok(NULL, "")) != NULL) { - strncpy(hphone, p, sizeof hphone); - hphone[sizeof hphone - 1] = '\0'; + strlcpy(hphone, p, + sizeof(hphone)); } } } diff --git a/usr.sbin/pw/pw_vpw.c b/usr.sbin/pw/pw_vpw.c index bc5713e..473cbb6 100644 --- a/usr.sbin/pw/pw_vpw.c +++ b/usr.sbin/pw/pw_vpw.c @@ -60,8 +60,7 @@ vnextpwent(char const * nam, uid_t uid, int doclose) struct passwd * pw = NULL; static char pwtmp[1024]; - strncpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof pwtmp); - pwtmp[sizeof pwtmp - 1] = '\0'; + strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp)); if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) { int done = 0; @@ -210,8 +209,7 @@ vnextgrent(char const * nam, gid_t gid, int doclose) static int memlen = 0; extendline(&grtmp, &grlen, MAXPATHLEN); - strncpy(grtmp, getgrpath(_GROUP), MAXPATHLEN); - grtmp[MAXPATHLEN - 1] = '\0'; + strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN); if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) { int done = 0; |