diff options
author | jilles <jilles@FreeBSD.org> | 2013-11-30 21:27:11 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2013-11-30 21:27:11 +0000 |
commit | 1d244d8c4507dc5abd9737693f6701563f3d84cf (patch) | |
tree | c17a94fbfcf3043c6d204ab64d6206f30a61de7c | |
parent | bcd91960e57f31a95cde119b0a51024463a4b4cc (diff) | |
download | FreeBSD-src-1d244d8c4507dc5abd9737693f6701563f3d84cf.zip FreeBSD-src-1d244d8c4507dc5abd9737693f6701563f3d84cf.tar.gz |
sh: Prefer memcpy() to strcpy() in most cases. Remove the scopy macro.
-rw-r--r-- | bin/sh/cd.c | 12 | ||||
-rw-r--r-- | bin/sh/exec.c | 14 | ||||
-rw-r--r-- | bin/sh/expand.c | 6 | ||||
-rw-r--r-- | bin/sh/memalloc.c | 6 | ||||
-rw-r--r-- | bin/sh/mystring.c | 5 | ||||
-rw-r--r-- | bin/sh/mystring.h | 1 | ||||
-rw-r--r-- | bin/sh/show.c | 4 | ||||
-rw-r--r-- | bin/sh/var.c | 13 |
8 files changed, 34 insertions, 27 deletions
diff --git a/bin/sh/cd.c b/bin/sh/cd.c index fa6f492..7720fad 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -182,6 +182,7 @@ cdlogical(char *dest) struct stat statb; int first; int badstat; + size_t len; /* * Check each component of the path. If we find a symlink or @@ -189,8 +190,9 @@ cdlogical(char *dest) * next time we get the value of the current directory. */ badstat = 0; - cdcomppath = stalloc(strlen(dest) + 1); - scopy(dest, cdcomppath); + len = strlen(dest); + cdcomppath = stalloc(len + 1); + memcpy(cdcomppath, dest, len + 1); STARTSTACKSTR(p); if (*dest == '/') { STPUTC('/', p); @@ -275,6 +277,7 @@ findcwd(char *dir) { char *new; char *p; + size_t len; /* * If our argument is NULL, we don't know the current directory @@ -283,8 +286,9 @@ findcwd(char *dir) */ if (dir == NULL || curdir == NULL) return getpwd2(); - cdcomppath = stalloc(strlen(dir) + 1); - scopy(dir, cdcomppath); + len = strlen(dir); + cdcomppath = stalloc(len + 1); + memcpy(cdcomppath, dir, len + 1); STARTSTACKSTR(new); if (*dir != '/') { STPUTS(curdir, new); diff --git a/bin/sh/exec.c b/bin/sh/exec.c index e2dd053..5f30ec6 100644 --- a/bin/sh/exec.c +++ b/bin/sh/exec.c @@ -187,14 +187,15 @@ padvance(const char **path, const char *name) { const char *p, *start; char *q; - size_t len; + size_t len, namelen; if (*path == NULL) return NULL; start = *path; for (p = start; *p && *p != ':' && *p != '%'; p++) ; /* nothing */ - len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ + namelen = strlen(name); + len = p - start + namelen + 2; /* "2" is for '/' and '\0' */ STARTSTACKSTR(q); CHECKSTRSPACE(len, q); if (p != start) { @@ -202,7 +203,7 @@ padvance(const char **path, const char *name) q += p - start; *q++ = '/'; } - strcpy(q, name); + memcpy(q, name, namelen + 1); pathopt = NULL; if (*p == '%') { pathopt = ++p; @@ -527,6 +528,7 @@ cmdlookup(const char *name, int add) const char *p; struct tblentry *cmdp; struct tblentry **pp; + size_t len; p = name; hashval = *p << 4; @@ -541,11 +543,11 @@ cmdlookup(const char *name, int add) } if (add && cmdp == NULL) { INTOFF; - cmdp = *pp = ckmalloc(sizeof (struct tblentry) - + strlen(name) + 1); + len = strlen(name); + cmdp = *pp = ckmalloc(sizeof (struct tblentry) + len + 1); cmdp->next = NULL; cmdp->cmdtype = CMDUNKNOWN; - strcpy(cmdp->cmdname, name); + memcpy(cmdp->cmdname, name, len + 1); INTON; } lastcmdentry = pp; diff --git a/bin/sh/expand.c b/bin/sh/expand.c index 414bfa7..5224f37 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -1307,9 +1307,11 @@ addfname(char *name) { char *p; struct strlist *sp; + size_t len; - p = stalloc(strlen(name) + 1); - scopy(name, p); + len = strlen(name); + p = stalloc(len + 1); + memcpy(p, name, len + 1); sp = (struct strlist *)stalloc(sizeof *sp); sp->text = p; *exparg.lastp = sp; diff --git a/bin/sh/memalloc.c b/bin/sh/memalloc.c index cb330d0..119f12e 100644 --- a/bin/sh/memalloc.c +++ b/bin/sh/memalloc.c @@ -98,9 +98,11 @@ char * savestr(const char *s) { char *p; + size_t len; - p = ckmalloc(strlen(s) + 1); - scopy(s, p); + len = strlen(s); + p = ckmalloc(len + 1); + memcpy(p, s, len + 1); return p; } diff --git a/bin/sh/mystring.c b/bin/sh/mystring.c index 67617ec..de7e9b6 100644 --- a/bin/sh/mystring.c +++ b/bin/sh/mystring.c @@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$"); * String functions. * * equal(s1, s2) Return true if strings are equal. - * scopy(from, to) Copy a string. * number(s) Convert a string of digits to an integer. * is_number(s) Return true if s is a string of digits. */ @@ -60,10 +59,6 @@ char nullstr[1]; /* zero length string */ * equal - #defined in mystring.h */ -/* - * scopy - #defined in mystring.h - */ - /* * prefix -- see if pfx is a prefix of string. diff --git a/bin/sh/mystring.h b/bin/sh/mystring.h index 5a6e3d8..87b76c8 100644 --- a/bin/sh/mystring.h +++ b/bin/sh/mystring.h @@ -40,4 +40,3 @@ int number(const char *); int is_number(const char *); #define equal(s1, s2) (strcmp(s1, s2) == 0) -#define scopy(s1, s2) ((void)strcpy(s2, s1)) diff --git a/bin/sh/show.c b/bin/sh/show.c index 7cb3547..db18ae9 100644 --- a/bin/sh/show.c +++ b/bin/sh/show.c @@ -390,11 +390,11 @@ opentrace(void) else p = "/tmp"; } - scopy(p, s); + strcpy(s, p); strcat(s, "/trace"); } #else - scopy("./trace", s); + strcpy(s, "./trace"); #endif /* not_this_way */ if ((tracefile = fopen(s, "a")) == NULL) { fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno)); diff --git a/bin/sh/var.c b/bin/sh/var.c index c20d032..df3ce43 100644 --- a/bin/sh/var.c +++ b/bin/sh/var.c @@ -224,8 +224,9 @@ void setvar(const char *name, const char *val, int flags) { const char *p; - int len; - int namelen; + size_t len; + size_t namelen; + size_t vallen; char *nameeq; int isbad; @@ -244,18 +245,20 @@ setvar(const char *name, const char *val, int flags) } namelen = p - name; if (isbad) - error("%.*s: bad variable name", namelen, name); + error("%.*s: bad variable name", (int)namelen, name); len = namelen + 2; /* 2 is space for '=' and '\0' */ if (val == NULL) { flags |= VUNSET; + vallen = 0; } else { - len += strlen(val); + vallen = strlen(val); + len += vallen; } nameeq = ckmalloc(len); memcpy(nameeq, name, namelen); nameeq[namelen] = '='; if (val) - scopy(val, nameeq + namelen + 1); + memcpy(nameeq + namelen + 1, val, vallen + 1); else nameeq[namelen + 1] = '\0'; setvareq(nameeq, flags); |