Przeglądaj źródła

libdpkg: Refactor string format escaping

Guillem Jover 18 lat temu
rodzic
commit
3ec7fa2a57
5 zmienionych plików z 55 dodań i 1 usunięć
  1. 8 0
      ChangeLog
  2. 1 0
      lib/Makefile.am
  3. 4 0
      lib/dpkg-priv.h
  4. 3 1
      lib/parsehelp.c
  5. 39 0
      lib/string.c

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2008-06-29  Guillem Jover  <guillem@debian.org>
+
+	* lib/Makefile.am (libdpkg_a_SOURCES): Add 'string.c'.
+	* lib/dpkg-priv.h (str_escape_fmt): New prototype.
+	* lib/parsehelp.c: Include <dpkg-priv.h>.
+	(parseerr): Refactor string format escaping to ,,,
+	* lib/string.c (str_escape_fmt): ... here. New file.
+
 2008-06-28  Guillem Jover  <guillem@debian.org>
 
 	* lib/parsedump.h (parseerr, parsemustfield): Remove unused 'file'

+ 1 - 0
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 \

+ 4 - 0
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);

+ 3 - 1
lib/parsehelp.c

@@ -26,6 +26,8 @@
 
 #include <dpkg.h>
 #include <dpkg-db.h>
+#include <dpkg-priv.h>
+
 #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);

+ 39 - 0
lib/string.c

@@ -0,0 +1,39 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * string.c - string handling routines
+ *
+ * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
+ * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ *
+ * 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;
+}
+