sh: Prefer memcpy() to strcpy() in most cases. Remove the scopy macro.

This commit is contained in:
Jilles Tjoelker 2013-11-30 21:27:11 +00:00
parent 9d22cb2e69
commit 670dd3f08f
8 changed files with 34 additions and 27 deletions

View File

@ -182,6 +182,7 @@ cdlogical(char *dest)
struct stat statb; struct stat statb;
int first; int first;
int badstat; int badstat;
size_t len;
/* /*
* Check each component of the path. If we find a symlink or * 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. * next time we get the value of the current directory.
*/ */
badstat = 0; badstat = 0;
cdcomppath = stalloc(strlen(dest) + 1); len = strlen(dest);
scopy(dest, cdcomppath); cdcomppath = stalloc(len + 1);
memcpy(cdcomppath, dest, len + 1);
STARTSTACKSTR(p); STARTSTACKSTR(p);
if (*dest == '/') { if (*dest == '/') {
STPUTC('/', p); STPUTC('/', p);
@ -275,6 +277,7 @@ findcwd(char *dir)
{ {
char *new; char *new;
char *p; char *p;
size_t len;
/* /*
* If our argument is NULL, we don't know the current directory * If our argument is NULL, we don't know the current directory
@ -283,8 +286,9 @@ findcwd(char *dir)
*/ */
if (dir == NULL || curdir == NULL) if (dir == NULL || curdir == NULL)
return getpwd2(); return getpwd2();
cdcomppath = stalloc(strlen(dir) + 1); len = strlen(dir);
scopy(dir, cdcomppath); cdcomppath = stalloc(len + 1);
memcpy(cdcomppath, dir, len + 1);
STARTSTACKSTR(new); STARTSTACKSTR(new);
if (*dir != '/') { if (*dir != '/') {
STPUTS(curdir, new); STPUTS(curdir, new);

View File

@ -187,14 +187,15 @@ padvance(const char **path, const char *name)
{ {
const char *p, *start; const char *p, *start;
char *q; char *q;
size_t len; size_t len, namelen;
if (*path == NULL) if (*path == NULL)
return NULL; return NULL;
start = *path; start = *path;
for (p = start; *p && *p != ':' && *p != '%'; p++) for (p = start; *p && *p != ':' && *p != '%'; p++)
; /* nothing */ ; /* 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); STARTSTACKSTR(q);
CHECKSTRSPACE(len, q); CHECKSTRSPACE(len, q);
if (p != start) { if (p != start) {
@ -202,7 +203,7 @@ padvance(const char **path, const char *name)
q += p - start; q += p - start;
*q++ = '/'; *q++ = '/';
} }
strcpy(q, name); memcpy(q, name, namelen + 1);
pathopt = NULL; pathopt = NULL;
if (*p == '%') { if (*p == '%') {
pathopt = ++p; pathopt = ++p;
@ -527,6 +528,7 @@ cmdlookup(const char *name, int add)
const char *p; const char *p;
struct tblentry *cmdp; struct tblentry *cmdp;
struct tblentry **pp; struct tblentry **pp;
size_t len;
p = name; p = name;
hashval = *p << 4; hashval = *p << 4;
@ -541,11 +543,11 @@ cmdlookup(const char *name, int add)
} }
if (add && cmdp == NULL) { if (add && cmdp == NULL) {
INTOFF; INTOFF;
cmdp = *pp = ckmalloc(sizeof (struct tblentry) len = strlen(name);
+ strlen(name) + 1); cmdp = *pp = ckmalloc(sizeof (struct tblentry) + len + 1);
cmdp->next = NULL; cmdp->next = NULL;
cmdp->cmdtype = CMDUNKNOWN; cmdp->cmdtype = CMDUNKNOWN;
strcpy(cmdp->cmdname, name); memcpy(cmdp->cmdname, name, len + 1);
INTON; INTON;
} }
lastcmdentry = pp; lastcmdentry = pp;

View File

@ -1307,9 +1307,11 @@ addfname(char *name)
{ {
char *p; char *p;
struct strlist *sp; struct strlist *sp;
size_t len;
p = stalloc(strlen(name) + 1); len = strlen(name);
scopy(name, p); p = stalloc(len + 1);
memcpy(p, name, len + 1);
sp = (struct strlist *)stalloc(sizeof *sp); sp = (struct strlist *)stalloc(sizeof *sp);
sp->text = p; sp->text = p;
*exparg.lastp = sp; *exparg.lastp = sp;

View File

@ -98,9 +98,11 @@ char *
savestr(const char *s) savestr(const char *s)
{ {
char *p; char *p;
size_t len;
p = ckmalloc(strlen(s) + 1); len = strlen(s);
scopy(s, p); p = ckmalloc(len + 1);
memcpy(p, s, len + 1);
return p; return p;
} }

View File

@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$");
* String functions. * String functions.
* *
* equal(s1, s2) Return true if strings are equal. * 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. * number(s) Convert a string of digits to an integer.
* is_number(s) Return true if s is a string of digits. * 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 * equal - #defined in mystring.h
*/ */
/*
* scopy - #defined in mystring.h
*/
/* /*
* prefix -- see if pfx is a prefix of string. * prefix -- see if pfx is a prefix of string.

View File

@ -40,4 +40,3 @@ int number(const char *);
int is_number(const char *); int is_number(const char *);
#define equal(s1, s2) (strcmp(s1, s2) == 0) #define equal(s1, s2) (strcmp(s1, s2) == 0)
#define scopy(s1, s2) ((void)strcpy(s2, s1))

View File

@ -390,11 +390,11 @@ opentrace(void)
else else
p = "/tmp"; p = "/tmp";
} }
scopy(p, s); strcpy(s, p);
strcat(s, "/trace"); strcat(s, "/trace");
} }
#else #else
scopy("./trace", s); strcpy(s, "./trace");
#endif /* not_this_way */ #endif /* not_this_way */
if ((tracefile = fopen(s, "a")) == NULL) { if ((tracefile = fopen(s, "a")) == NULL) {
fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno)); fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));

View File

@ -224,8 +224,9 @@ void
setvar(const char *name, const char *val, int flags) setvar(const char *name, const char *val, int flags)
{ {
const char *p; const char *p;
int len; size_t len;
int namelen; size_t namelen;
size_t vallen;
char *nameeq; char *nameeq;
int isbad; int isbad;
@ -244,18 +245,20 @@ setvar(const char *name, const char *val, int flags)
} }
namelen = p - name; namelen = p - name;
if (isbad) 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' */ len = namelen + 2; /* 2 is space for '=' and '\0' */
if (val == NULL) { if (val == NULL) {
flags |= VUNSET; flags |= VUNSET;
vallen = 0;
} else { } else {
len += strlen(val); vallen = strlen(val);
len += vallen;
} }
nameeq = ckmalloc(len); nameeq = ckmalloc(len);
memcpy(nameeq, name, namelen); memcpy(nameeq, name, namelen);
nameeq[namelen] = '='; nameeq[namelen] = '=';
if (val) if (val)
scopy(val, nameeq + namelen + 1); memcpy(nameeq + namelen + 1, val, vallen + 1);
else else
nameeq[namelen + 1] = '\0'; nameeq[namelen + 1] = '\0';
setvareq(nameeq, flags); setvareq(nameeq, flags);