Mercurial > dovecot > original-hg > dovecot-1.2
changeset 5679:74e06273985b HEAD
Renamed printf_string_fix_format() to printf_format_fix() and moved it to
its own file.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 11 Jun 2007 01:25:04 +0300 |
parents | 2cde91fb7283 |
children | 7577fb89916a |
files | src/lib/Makefile.am src/lib/failures.c src/lib/printf-format-fix.c src/lib/printf-format-fix.h src/lib/printf-upper-bound.c src/lib/strfuncs.c src/lib/strfuncs.h |
diffstat | 7 files changed, 90 insertions(+), 73 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ 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 @@ ostream-crlf.h \ ostream-internal.h \ primes.h \ + printf-format-fix.h \ printf-upper-bound.h \ process-title.h \ randgen.h \
--- 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 @@ 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 @@ 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 @@ 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 @@ /* 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;
--- /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; +} +
--- /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
--- 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 @@ } /* while (*format) */ if (fix_format) - *format_p = printf_string_fix_format(*format_p); + (void)printf_format_fix(format_p); return len; }
--- 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 @@ -12,71 +12,6 @@ #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, ...) { #ifndef HAVE_VSNPRINTF
--- 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 @@ -6,8 +6,6 @@ #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, ...) __attr_format__(3, 4);