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
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=258776
8 changed files with 34 additions and 27 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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.

View File

@ -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))

View File

@ -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));

View File

@ -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);