Use new log_format_has() function instead of strstr().
[rsync/rsync.git] / log.c
diff --git a/log.c b/log.c
index c6d2fc8..c8a433d 100644 (file)
--- a/log.c
+++ b/log.c
@@ -364,13 +364,15 @@ static void log_formatted(enum logcode code, char *format, char *op,
         * rather keep going until we reach the nul of the format. */
        total = strlcpy(buf, format, sizeof buf);
        
-       for (p = buf; (p = strchr(p, '%')) != NULL && p[1]; ) {
+       for (p = buf; (p = strchr(p, '%')) != NULL; ) {
                s = p++;
                n = fmt + 1;
                if (*p == '-')
                        *n++ = *p++;
                while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
                        *n++ = *p++;
+               if (!*p)
+                       break;
                *n = '\0';
                n = NULL;
 
@@ -497,9 +499,6 @@ static void log_formatted(enum logcode code, char *format, char *op,
                        break;
                }
 
-               /* Subtract the length of the escape from the string's size. */
-               total -= p - s;
-
                /* "n" is the string to be inserted in place of this % code. */
                if (!n)
                        continue;
@@ -510,6 +509,9 @@ static void log_formatted(enum logcode code, char *format, char *op,
                }
                len = strlen(n);
 
+               /* Subtract the length of the escape from the string's size. */
+               total -= p - s;
+
                if (len + total >= sizeof buf) {
                        rprintf(FERROR,
                                "buffer overflow expanding %%%c -- exiting\n",
@@ -533,6 +535,26 @@ static void log_formatted(enum logcode code, char *format, char *op,
        rprintf(code, "%s\n", buf);
 }
 
+int log_format_has(const char *format, char esc)
+{
+       const char *p;
+
+       if (!format)
+               return 0;
+
+       for (p = format; (p = strchr(p, '%')) != NULL; ) {
+               if (*++p == '-')
+                       p++;
+               while (isdigit(*(uchar*)p))
+                       p++;
+               if (!*p)
+                       break;
+               if (*p == esc)
+                       return 1;
+       }
+       return 0;
+}
+
 /* log the transfer of a file */
 void log_item(struct file_struct *file, struct stats *initial_stats,
              int iflags, char *hlink)