]> granicus.if.org Git - strace/commitdiff
Optimization: eliminate some usages of strcat()
authorDenys Vlasenko <dvlasenk@redhat.com>
Wed, 31 Aug 2011 10:07:38 +0000 (12:07 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Wed, 31 Aug 2011 10:07:38 +0000 (12:07 +0200)
* 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 <dvlasenk@redhat.com>
defs.h
file.c
util.c

diff --git a/defs.h b/defs.h
index 878cfdd6474c067938a30ade45f84799d6e8dc05..0b6525798c24c210eae2d2d17ce71d896c0e4dab 100644 (file)
--- 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 5d5a0feb1935a46d1eabd33c0577431982d9e2ab..c40fde2b7b8b41c9aebccaad6f45c54491420e2a 100644 (file)
--- 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 ec0b3cbc7fff688fbc40167f17f5907ad5ee0f6e..b2bf3e36bd451cdb4cf6fd725445e8d8f5f16463 100644 (file)
--- 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