From: Denys Vlasenko Date: Wed, 31 Aug 2011 10:07:38 +0000 (+0200) Subject: Optimization: eliminate some usages of strcat() X-Git-Tag: v4.7~274 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5284557bfad96f12dd5798539008e1644f1dc094;p=strace Optimization: eliminate some usages of strcat() * 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 --- diff --git a/defs.h b/defs.h index 878cfdd6..0b652579 100644 --- a/defs.h +++ b/defs.h @@ -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 diff --git a/file.c b/file.c index 5d5a0feb..c40fde2b 100644 --- a/file.c +++ b/file.c @@ -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. @@ -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; @@ -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; diff --git a/util.c b/util.c index ec0b3cbc..b2bf3e36 100644 --- a/util.c +++ b/util.c @@ -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. @@ -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; @@ -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); } } @@ -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"); @@ -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; @@ -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