From 3ec7fa2a57ac687ed9bf222be3ea1bb94fa19f9f Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Sun, 29 Jun 2008 00:08:59 +0300 Subject: [PATCH] libdpkg: Refactor string format escaping --- ChangeLog | 8 ++++++++ lib/Makefile.am | 1 + lib/dpkg-priv.h | 4 ++++ lib/parsehelp.c | 4 +++- lib/string.c | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/string.c diff --git a/ChangeLog b/ChangeLog index 191f69b9..95b97800 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-06-29 Guillem Jover + + * lib/Makefile.am (libdpkg_a_SOURCES): Add 'string.c'. + * lib/dpkg-priv.h (str_escape_fmt): New prototype. + * lib/parsehelp.c: Include . + (parseerr): Refactor string format escaping to ,,, + * lib/string.c (str_escape_fmt): ... here. New file. + 2008-06-28 Guillem Jover * lib/parsedump.h (parseerr, parsemustfield): Remove unused 'file' diff --git a/lib/Makefile.am b/lib/Makefile.am index 2a78ba4e..b452e1e3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -38,6 +38,7 @@ libdpkg_a_SOURCES = \ parsedump.h \ path.c \ showpkg.c \ + string.c \ subproc.c \ tarfn.c tarfn.h \ triglib.c \ diff --git a/lib/dpkg-priv.h b/lib/dpkg-priv.h index 9e690f15..51af10c9 100644 --- a/lib/dpkg-priv.h +++ b/lib/dpkg-priv.h @@ -32,6 +32,10 @@ extern "C" { #define sizeof_array(a) (sizeof(a) / sizeof((a)[0])) #endif +/* String handling. */ + +char *str_escape_fmt(char *dest, const char *src); + /* Path handling. */ size_t rtrim_slash_slashdot(char *path); diff --git a/lib/parsehelp.c b/lib/parsehelp.c index 0623dbd7..a311f73b 100644 --- a/lib/parsehelp.c +++ b/lib/parsehelp.c @@ -26,6 +26,8 @@ #include #include +#include + #include "parsedump.h" void parseerr @@ -47,7 +49,7 @@ void parseerr sprintf(buf2, _(" package `%.255s'"), pigp->name); strcat(buf1,buf2); } - for (p=buf1, q=buf2; *p; *q++= *p++) if (*p == '%') *q++= '%'; + q = str_escape_fmt(buf2, buf1); strcpy(q,":\n "); strcat(q,fmt); va_start(al,fmt); if (!warnonly) ohshitv(buf2,al); diff --git a/lib/string.c b/lib/string.c new file mode 100644 index 00000000..dea49acb --- /dev/null +++ b/lib/string.c @@ -0,0 +1,39 @@ +/* + * libdpkg - Debian packaging suite library routines + * string.c - string handling routines + * + * Copyright © 1995 Ian Jackson + * Copyright © 2008 Guillem Jover + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with dpkg; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +char * +str_escape_fmt(char *dst, const char *src) +{ + char *d = dst; + const char *s = src; + + while (*s) { + if (*s == '%') + *d++ = '%'; + *d++ = *s++; + } + + *d = '\0'; + + return d; +} + -- 2.39.5