Skip to content

Commit

Permalink
Optimization: eliminate some usages of strcat()
Browse files Browse the repository at this point in the history
* defs.h: Declare stpcpy().
* util.c: Define stpcpy().
* file.c: Remove static str_append().
(sprint_open_modes): Use stpcpy() instead of str_append().
(sprintflags): Use stpcpy() instead of strcat().
(printpathn): Eliminate usage of strcat().
(printstr): Eliminate usage of strcat().

Signed-off-by: Denys Vlasenko <[email protected]>
  • Loading branch information
dvlasenk committed Aug 31, 2011
1 parent b5b2589 commit 5284557
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
7 changes: 7 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ extern void tv_sub(struct timeval *, struct timeval *, struct timeval *);
extern void tv_mul(struct timeval *, struct timeval *, int);
extern void tv_div(struct timeval *, struct timeval *, int);

/* Some libc have stpcpy, some don't. Sigh...
* Roll our private implementation...
*/
#undef stpcpy
#define stpcpy strace_stpcpy
extern char *stpcpy(char *dst, const char *src);

#ifdef SUNOS4
extern int fixvfork(struct tcb *);
#endif
Expand Down
17 changes: 3 additions & 14 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,6 @@ print_dirfd(struct tcb *tcp, int fd)
}
#endif

/*
* Pity stpcpy() is not standardized...
*/
static char *
str_append(char *dst, const char *src)
{
while ((*dst = *src++) != '\0')
dst++;
return dst;
}

/*
* low bits of the open(2) flags define access mode,
* other bits are real flags.
Expand All @@ -366,10 +355,10 @@ sprint_open_modes(mode_t flags)
const char *str;
const struct xlat *x;

p = str_append(outstr, "flags ");
p = stpcpy(outstr, "flags ");
str = xlookup(open_access_modes, flags & 3);
if (str) {
p = str_append(p, str);
p = stpcpy(p, str);
flags &= ~3;
if (!flags)
return outstr;
Expand All @@ -380,7 +369,7 @@ sprint_open_modes(mode_t flags)
if ((flags & x->val) == x->val) {
if (sep)
*p++ = sep;
p = str_append(p, x->str);
p = stpcpy(p, x->str);
flags &= ~x->val;
if (!flags)
return outstr;
Expand Down
34 changes: 24 additions & 10 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ xlookup(const struct xlat *xlat, int val)
return NULL;
}

char *
stpcpy(char *dst, const char *src)
{
while ((*dst = *src++) != '\0')
dst++;
return dst;
}

/*
* Generic ptrace wrapper which tracks ESRCH errors
* by setting tcp->ptrace_errno to ESRCH.
Expand Down Expand Up @@ -302,23 +310,24 @@ const char *
sprintflags(const char *prefix, const struct xlat *xlat, int flags)
{
static char outstr[1024];
char *outptr;
int found = 0;

strcpy(outstr, prefix);
outptr = stpcpy(outstr, prefix);

for (; xlat->str; xlat++) {
if ((flags & xlat->val) == xlat->val) {
if (found)
strcat(outstr, "|");
strcat(outstr, xlat->str);
*outptr++ = '|';
outptr = stpcpy(outptr, xlat->str);
flags &= ~xlat->val;
found = 1;
}
}
if (flags) {
if (found)
strcat(outstr, "|");
sprintf(outstr + strlen(outstr), "%#x", flags);
*outptr++ = '|';
outptr += sprintf(outptr, "%#x", flags);
}

return outstr;
Expand Down Expand Up @@ -554,14 +563,16 @@ printpathn(struct tcb *tcp, long addr, int n)
tprintf("%#lx", addr);
else {
static char outstr[4*(sizeof path - 1) + sizeof "\"...\""];
const char *fmt;
int trunc = (path[n] != '\0');

if (trunc)
path[n] = '\0';
(void) string_quote(path, outstr, -1, n + 1);
string_quote(path, outstr, -1, n + 1);
fmt = "%s";
if (trunc)
strcat(outstr, "...");
tprintf("%s", outstr);
fmt = "%s...";
tprintf(fmt, outstr);
}
}

Expand All @@ -582,6 +593,7 @@ printstr(struct tcb *tcp, long addr, int len)
static char *str = NULL;
static char *outstr;
int size;
const char *fmt;

if (!addr) {
tprintf("NULL");
Expand All @@ -605,6 +617,7 @@ printstr(struct tcb *tcp, long addr, int len)
*/
size = max_strlen + 1;
str[max_strlen] = '\0';
/* FIXME! umovestr can overwrite the '\0' stored above??? */
if (umovestr(tcp, addr, size, str) < 0) {
tprintf("%#lx", addr);
return;
Expand All @@ -618,11 +631,12 @@ printstr(struct tcb *tcp, long addr, int len)
}
}

fmt = "%s";
if (string_quote(str, outstr, len, size) &&
(len < 0 || len > max_strlen))
strcat(outstr, "...");
fmt = "%s...";

tprintf("%s", outstr);
tprintf(fmt, outstr);
}

#if HAVE_SYS_UIO_H
Expand Down

0 comments on commit 5284557

Please sign in to comment.