[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH 07/15] libdpkg: simplify code to calculate compression options



Rather than making the reader track down the meaning of indices
into a fixed-size buffer as it is modified, calculate the
compression options all at once.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 lib/dpkg/compression.c |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/lib/dpkg/compression.c b/lib/dpkg/compression.c
index d751150..ab39ea9 100644
--- a/lib/dpkg/compression.c
+++ b/lib/dpkg/compression.c
@@ -21,8 +21,16 @@
 
 static void
 fd_fd_filter(int fd_in, int fd_out, const char *desc,
-             const char *file, const char *cmd, const char *args)
+             const char *file, const char *cmd,
+             const char *argfmt, ...)
 {
+  struct varbuf argbuf = VARBUF_INIT;
+  va_list ap;
+
+  va_start(ap, argfmt);
+  varbufvprintf(&argbuf, argfmt, ap);
+  va_end(ap);
+
   if (fd_in != 0) {
     m_dup2(fd_in, 0);
     close(fd_in);
@@ -31,8 +39,11 @@ fd_fd_filter(int fd_in, int fd_out, const char *desc,
     m_dup2(fd_out, 1);
     close(fd_out);
   }
-  execlp(file, cmd, args, NULL);
-  ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, args);
+
+  execlp(file, cmd, argbuf.buf, NULL);
+  ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, argbuf.buf);
+
+  varbuffree(&argbuf);
 }
 
 #define DECOMPRESS(format, zFile, zdopen, zread, zerror, ERR_ERRNO, \
@@ -62,13 +73,11 @@ fd_fd_filter(int fd_in, int fd_out, const char *desc,
 #define COMPRESS(format, zFile, zdopen, zwrite, zclose, zerror, ERR_ERRNO, \
                  fd_in, fd_out, compression, desc) do \
 { \
-  char combuf[6]; \
+  char combuf[] = {'w', compression, '\0'}; \
   int actualread, actualwrite; \
   char buffer[4096]; \
   zFile zfile; \
   \
-  strncpy(combuf, "w9", sizeof(combuf)); \
-  combuf[1] = compression; \
   zfile = zdopen(fd_out, combuf); \
   while ((actualread = read(fd_in, buffer, sizeof(buffer))) > 0) { \
     if (actualread < 0) { \
@@ -99,10 +108,7 @@ compress_cmd(int fd_in, int fd_out, const char *path,
              const char *cmd, char compression,
              const char *desc)
 {
-  char combuf[6];
-  strncpy(combuf, "-9c", sizeof(combuf));
-  combuf[1] = compression;
-  fd_fd_filter(fd_in, fd_out, desc, path, cmd, combuf);
+  fd_fd_filter(fd_in, fd_out, desc, path, cmd, "-c%c", compression);
 }
 
 void decompress_cat(enum compress_type type, int fd_in, int fd_out, char *desc, ...) {
-- 
1.6.5.rc1.199.g596ec


Reply to: