[dovecot-cvs] dovecot: Renamed printf_string_fix_format() to printf_format_fix...
dovecot at dovecot.org
dovecot at dovecot.org
Mon Jun 11 01:25:09 EEST 2007
details: http://hg.dovecot.org/dovecot/rev/74e06273985b
changeset: 5679:74e06273985b
user: Timo Sirainen <tss at iki.fi>
date: Mon Jun 11 01:25:04 2007 +0300
description:
Renamed printf_string_fix_format() to printf_format_fix() and moved it to
its own file.
diffstat:
7 files changed, 90 insertions(+), 73 deletions(-)
src/lib/Makefile.am | 2 +
src/lib/failures.c | 13 ++++---
src/lib/printf-format-fix.c | 70 ++++++++++++++++++++++++++++++++++++++++++
src/lib/printf-format-fix.h | 8 ++++
src/lib/printf-upper-bound.c | 3 +
src/lib/strfuncs.c | 65 ---------------------------------------
src/lib/strfuncs.h | 2 -
diffs (262 lines):
diff -r 2cde91fb7283 -r 74e06273985b src/lib/Makefile.am
--- a/src/lib/Makefile.am Mon Jun 11 01:00:56 2007 +0300
+++ b/src/lib/Makefile.am Mon Jun 11 01:25:04 2007 +0300
@@ -66,6 +66,7 @@ liblib_a_SOURCES = \
ostream-file.c \
ostream-crlf.c \
primes.c \
+ printf-format-fix.c \
printf-upper-bound.c \
process-title.c \
randgen.c \
@@ -146,6 +147,7 @@ headers = \
ostream-crlf.h \
ostream-internal.h \
primes.h \
+ printf-format-fix.h \
printf-upper-bound.h \
process-title.h \
randgen.h \
diff -r 2cde91fb7283 -r 74e06273985b src/lib/failures.c
--- a/src/lib/failures.c Mon Jun 11 01:00:56 2007 +0300
+++ b/src/lib/failures.c Mon Jun 11 01:25:04 2007 +0300
@@ -3,6 +3,7 @@
#include "lib.h"
#include "str.h"
#include "backtrace-string.h"
+#include "printf-format-fix.h"
#include "write-full.h"
#include "fd-close-on-exec.h"
@@ -82,7 +83,7 @@ default_handler(const char *prefix, FILE
if (recursed == 2) {
/* we're being called from some signal handler, or
- printf_string_fix_format() killed us again */
+ printf_format_fix() killed us again */
return -1;
}
@@ -99,8 +100,8 @@ default_handler(const char *prefix, FILE
t_push();
if (recursed == 2) {
- /* printf_string_fix_format() probably killed us last time,
- just write the format now. */
+ /* printf_format_fix() probably killed us last time,
+ just write the format now. */
fputs("recursed: ", f);
fputs(format, f);
@@ -113,7 +114,8 @@ default_handler(const char *prefix, FILE
errno = old_errno;
/* make sure there's no %n in there and fix %m */
- vfprintf(f, printf_string_fix_format(format), args2);
+ (void)printf_format_fix(&format);
+ vfprintf(f, format, args2);
}
fputc('\n', f);
@@ -288,7 +290,8 @@ syslog_handler(int level, const char *fo
/* make sure there's no %n in there. vsyslog() supports %m, but since
we'll convert it ourself anyway, we might as well it */
- vsyslog(level, printf_string_fix_format(format), args);
+ (void)printf_format_fix(&format);
+ vsyslog(level, format, args);
recursed--;
return 0;
diff -r 2cde91fb7283 -r 74e06273985b src/lib/printf-format-fix.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/printf-format-fix.c Mon Jun 11 01:25:04 2007 +0300
@@ -0,0 +1,70 @@
+/* Copyright (c) 2002-2007 Timo Sirainen */
+
+#include "lib.h"
+#include "printf-format-fix.h"
+
+static const char *fix_format_real(const char *fmt, const char *p)
+{
+ const char *errstr;
+ char *buf;
+ size_t pos, alloc, errlen;
+
+ errstr = strerror(errno);
+ errlen = strlen(errstr);
+
+ pos = (size_t) (p-fmt);
+ i_assert(pos < SSIZE_T_MAX);
+
+ alloc = pos + errlen + 128;
+ buf = t_buffer_get(alloc);
+
+ memcpy(buf, fmt, pos);
+
+ while (*p != '\0') {
+ if (*p == '%' && p[1] == 'm') {
+ if (pos+errlen+1 > alloc) {
+ alloc += errlen+1 + 128;
+ buf = t_buffer_get(alloc);
+ }
+
+ memcpy(buf+pos, errstr, errlen);
+ pos += errlen;
+ p += 2;
+ } else {
+ /* p + \0 */
+ if (pos+2 > alloc) {
+ alloc += 128;
+ buf = t_buffer_get(alloc);
+ }
+
+ buf[pos++] = *p;
+ p++;
+ }
+ }
+
+ buf[pos++] = '\0';
+ t_buffer_alloc(pos);
+ return buf;
+}
+
+bool printf_format_fix(const char **format)
+{
+ const char *p;
+
+ for (p = *format; *p != '\0'; p++) {
+ if (*p++ == '%') {
+ switch (*p) {
+ case 'n':
+ i_panic("%%n modifier used");
+ case 'm':
+ *format = fix_format_real(*format, p-1);
+ return TRUE;
+ case '\0':
+ i_panic("%% modifier missing");
+ }
+ }
+ }
+
+ return FALSE;
+}
+
diff -r 2cde91fb7283 -r 74e06273985b src/lib/printf-format-fix.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/printf-format-fix.h Mon Jun 11 01:25:04 2007 +0300
@@ -0,0 +1,8 @@
+#ifndef __PRINTF_FORMAT_FIX_H
+#define __PRINTF_FORMAT_FIX_H
+
+/* Replaces %m in format with strerror(errno) and panics if %n modifier is
+ used. Returns TRUE if format was modified. */
+bool printf_format_fix(const char **format);
+
+#endif
diff -r 2cde91fb7283 -r 74e06273985b src/lib/printf-upper-bound.c
--- a/src/lib/printf-upper-bound.c Mon Jun 11 01:00:56 2007 +0300
+++ b/src/lib/printf-upper-bound.c Mon Jun 11 01:25:04 2007 +0300
@@ -19,6 +19,7 @@
*/
#include "lib.h"
+#include "printf-format-fix.h"
#include "printf-upper-bound.h"
typedef union _GDoubleIEEE754 GDoubleIEEE754;
@@ -320,6 +321,6 @@ size_t printf_string_upper_bound(const c
} /* while (*format) */
if (fix_format)
- *format_p = printf_string_fix_format(*format_p);
+ (void)printf_format_fix(format_p);
return len;
}
diff -r 2cde91fb7283 -r 74e06273985b src/lib/strfuncs.c
--- a/src/lib/strfuncs.c Mon Jun 11 01:00:56 2007 +0300
+++ b/src/lib/strfuncs.c Mon Jun 11 01:25:04 2007 +0300
@@ -11,71 +11,6 @@
#include <ctype.h>
#define STRCONCAT_BUFSIZE 512
-
-static const char *fix_format_real(const char *fmt, const char *p)
-{
- const char *errstr;
- char *buf;
- size_t pos, alloc, errlen;
-
- errstr = strerror(errno);
- errlen = strlen(errstr);
-
- pos = (size_t) (p-fmt);
- i_assert(pos < SSIZE_T_MAX);
-
- alloc = pos + errlen + 128;
- buf = t_buffer_get(alloc);
-
- memcpy(buf, fmt, pos);
-
- while (*p != '\0') {
- if (*p == '%' && p[1] == 'm') {
- if (pos+errlen+1 > alloc) {
- alloc += errlen+1 + 128;
- buf = t_buffer_get(alloc);
- }
-
- memcpy(buf+pos, errstr, errlen);
- pos += errlen;
- p += 2;
- } else {
- /* p + \0 */
- if (pos+2 > alloc) {
- alloc += 128;
- buf = t_buffer_get(alloc);
- }
-
- buf[pos++] = *p;
- p++;
- }
- }
-
- buf[pos++] = '\0';
- t_buffer_alloc(pos);
- return buf;
-}
-
-/* replace %m with strerror() */
-const char *printf_string_fix_format(const char *fmt)
-{
- const char *p;
-
- for (p = fmt; *p != '\0'; p++) {
- if (*p++ == '%') {
- switch (*p) {
- case 'n':
- i_panic("%%n modifier used");
- case 'm':
- return fix_format_real(fmt, p-1);
- case '\0':
- i_panic("%% modifier missing");
- }
- }
- }
-
- return fmt;
-}
int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
{
diff -r 2cde91fb7283 -r 74e06273985b src/lib/strfuncs.h
--- a/src/lib/strfuncs.h Mon Jun 11 01:00:56 2007 +0300
+++ b/src/lib/strfuncs.h Mon Jun 11 01:25:04 2007 +0300
@@ -5,8 +5,6 @@
((str) == NULL || (str)[0] == '\0')
#define MAX_INT_STRLEN ((sizeof(uintmax_t) * CHAR_BIT + 2) / 3 + 1)
-
-const char *printf_string_fix_format(const char *fmt) __attr_format_arg__(1);
/* Returns -1 if dest wasn't large enough, 0 if not. */
int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
More information about the dovecot-cvs
mailing list