In log_formatted(), add the newline to the format string so
[rsync/rsync.git] / log.c
diff --git a/log.c b/log.c
index f91f7a5..9278c42 100644 (file)
--- a/log.c
+++ b/log.c
@@ -360,17 +360,25 @@ static void log_formatted(enum logcode code, char *format, char *op,
        *fmt = '%';
 
        /* We expand % codes one by one in place in buf.  We don't
-        * copy in the terminating nul of the inserted strings, but
-        * rather keep going until we reach the nul of the format. */
+        * copy in the terminating null of the inserted strings, but
+        * rather keep going until we reach the null of the format. */
        total = strlcpy(buf, format, sizeof buf);
+       if (total > MAXPATHLEN) {
+               rprintf(FERROR, "log-format string is WAY too long!\n");
+               exit_cleanup(RERR_MESSAGEIO);
+       }
+       buf[total++] = '\n';
+       buf[total] = '\0';
        
-       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;
 
@@ -384,9 +392,9 @@ static void log_formatted(enum logcode code, char *format, char *op,
                        n = buf2;
                        break;
                case 'p':
-                       strlcat(fmt, "d", sizeof fmt);
+                       strlcat(fmt, "ld", sizeof fmt);
                        snprintf(buf2, sizeof buf2, fmt,
-                                (int)getpid());
+                                (long)getpid());
                        n = buf2;
                        break;
                case 'o': n = op; break;
@@ -397,7 +405,10 @@ static void log_formatted(enum logcode code, char *format, char *op,
                                         file->dir.root, n);
                                /* The buffer from safe_fname() has more
                                 * room than MAXPATHLEN, so this is safe. */
-                               strcpy(n, buf2);
+                               if (fmt[1])
+                                       strcpy(n, buf2);
+                               else
+                                       n = buf2;
                        }
                        clean_fname(n, 0);
                        if (*n == '/')
@@ -413,15 +424,20 @@ static void log_formatted(enum logcode code, char *format, char *op,
                        break;
                case 'L':
                        if (hlink && *hlink) {
-                               snprintf(buf2, sizeof buf2, " => %s",
-                                        safe_fname(hlink));
-                               n = buf2;
+                               n = safe_fname(hlink);
+                               strcpy(buf2, " => ");
                        } else if (S_ISLNK(file->mode) && file->u.link) {
-                               snprintf(buf2, sizeof buf2, " -> %s",
-                                        safe_fname(file->u.link));
-                               n = buf2;
-                       } else
+                               n = safe_fname(file->u.link);
+                               strcpy(buf2, " -> ");
+                       } else {
                                n = "";
+                               if (!fmt[1])
+                                       break;
+                               strcpy(buf2, "    ");
+                       }
+                       strlcat(fmt, "s", sizeof fmt);
+                       snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
+                       n = buf2;
                        break;
                case 'm': n = lp_name(module_id); break;
                case 't': n = timestring(time(NULL)); break;
@@ -494,9 +510,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;
@@ -507,7 +520,10 @@ static void log_formatted(enum logcode code, char *format, char *op,
                }
                len = strlen(n);
 
-               if (len + total >= sizeof buf) {
+               /* Subtract the length of the escape from the string's size. */
+               total -= p - s;
+
+               if (len + total >= (size_t)sizeof buf) {
                        rprintf(FERROR,
                                "buffer overflow expanding %%%c -- exiting\n",
                                p[-1]);
@@ -527,7 +543,27 @@ static void log_formatted(enum logcode code, char *format, char *op,
                p = s + len;
        }
 
-       rprintf(code, "%s\n", buf);
+       rwrite(code, buf, total);
+}
+
+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 */