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

[PATCH] querycmd: only write() once



This way a program reading from dpkg-query through a pipe will get the
output in a single read()
---
 src/querycmd.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/querycmd.c b/src/querycmd.c
index a39ee25..7d0be09 100644
--- a/src/querycmd.c
+++ b/src/querycmd.c
@@ -306,7 +306,8 @@ listpackages(const char *const *argv)
 static int searchoutput(struct filenamenode *namenode) {
   struct filepackages_iterator *iter;
   struct pkginfo *pkg_owner;
-  int found;
+  char *out, *t;
+  int found, q;
 
   if (namenode->divert) {
     const char *name_from = namenode->divert->camefrom ?
@@ -326,16 +327,34 @@ static int searchoutput(struct filenamenode *namenode) {
   }
   found= 0;
 
+  out = malloc(256);
+  if (!out) {
+    notice(_("out of memory"));
+    exit(1);
+  }
+  out[0] = '\0';
+
   iter = filepackages_iter_new(namenode);
   while ((pkg_owner = filepackages_iter_next(iter))) {
-    if (found)
-      fputs(", ", stdout);
-    fputs(pkg_name(pkg_owner, pnaw_nonambig), stdout);
+    if (found) {
+      q = strlen(out) + 2;
+      out = realloc(out, strlen(out) + 2);
+      strcat(out, ", ");
+    }
+    t = (char *) pkg_name(pkg_owner, pnaw_nonambig);
+    out = realloc(out, strlen(out) + strlen(t));
+    strcat(out, t);
     found++;
   }
   filepackages_iter_free(iter);
 
-  if (found) printf(": %s\n",namenode->name);
+  if (found) {
+    out = realloc(out, strlen(out) + strlen(": ") + strlen(namenode->name));
+    strcat(out, ": ");
+    strcat(out, namenode->name);
+    puts(out);
+    free(out);
+  }
   return found + (namenode->divert ? 1 : 0);
 }
 
-- 
1.8.4.rc3


Reply to: