[Apt-Rpm] tools/pkglist-query.cc: new program, obsoletes countpkglist

Alexey Tourbin at at altlinux.ru
Sun Nov 16 16:20:09 PST 2008


Please tell me if you can think of a better name for the program.

Sample usage:
$ bzcat .../pkglist.classic.bz2 | ./tools/pkglist-query '%{NAME} - %{SUMMARY}\n' - | head -10
3proxy - Proxy server
4th - Basic framework for creating application specific scripting languages
7colors - Little addicting game, take over the gaming area with your color
AutoScan - AutoScan - a utility for network exploration
C - C - a pseudo interpreter of the C programming language
ConsoleKit - System daemon for tracking users, sessions and seats
ConsoleKit-x11 - X11-requiring add-ons for ConsoleKit
Cultivation - Cultivation is a game about the interactions within a gardening community
FSViewer.app - FSViewer is a NeXT FileViewer lookalike for Window Maker
FlightGear - FlightGear - best opensource fly simulation engine
$
---
 tools/Makefile.am      |    4 ++--
 tools/countpkglist.cc  |   29 -----------------------------
 tools/pkglist-query.cc |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 31 deletions(-)
 delete mode 100644 tools/countpkglist.cc
 create mode 100644 tools/pkglist-query.cc

diff --git a/tools/Makefile.am b/tools/Makefile.am
index 5ac52c2..75d004c 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -1,5 +1,5 @@
 
-bin_PROGRAMS = genpkglist gensrclist countpkglist
+bin_PROGRAMS = genpkglist gensrclist pkglist-query
 bin_SCRIPTS = genbasedir
 
 EXTRA_DIST = genbasedir
@@ -8,4 +8,4 @@ LDADD = ../apt-pkg/libapt-pkg.la $(RPM_LIBS)
 
 genpkglist_SOURCES = genpkglist.cc cached_md5.cc cached_md5.h genutil.h
 gensrclist_SOURCES = gensrclist.cc cached_md5.cc cached_md5.h genutil.h
-countpkglist_SOURCES = countpkglist.cc
+pkglist_query_SOURCES = pkglist-query.cc
diff --git a/tools/countpkglist.cc b/tools/countpkglist.cc
deleted file mode 100644
index 6eb80ba..0000000
--- a/tools/countpkglist.cc
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-
-#include <rpm/rpmlib.h>
-
-int main(int argc, char *argv[])
-{
-	Header header = NULL;
-	long count = 0;
-	FD_t fd;
-	if (argc != 2) {
-		fprintf(stderr, "usage: countpkglist <pkglist>\n");
-		exit(1);
-	}
-	fd = Fopen(argv[1], "r");
-	if (fd == NULL) {
-		fprintf(stderr, "error: can't open %s: %s",
-			argv[1], strerror(errno));
-		exit(1);
-	}
-	while ((header = headerRead(fd, HEADER_MAGIC_YES)) != NULL) {
-		headerFree(header);
-		count++;
-	}
-	Fclose(fd);
-	printf("%ld\n", count);
-	return 0;
-}
diff --git a/tools/pkglist-query.cc b/tools/pkglist-query.cc
new file mode 100644
index 0000000..4a08ee1
--- /dev/null
+++ b/tools/pkglist-query.cc
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <rpm/rpmlib.h>
+#include "config.h"
+
+int main(int argc, char *argv[])
+{
+    const char *progname = argv[0];
+    if (argc < 3) {
+	fprintf(stderr, "Usage: %s <format> <pkglist>...", progname);
+	return 2;
+    }
+    const char *format = argv[1];
+    int rc = 0;
+    const char *pkglist;
+    int ix = 2;
+    while ((pkglist = argv[ix++]) != NULL) {
+	FD_t Fd = Fopen(pkglist, "r");
+	if (Ferror(Fd)) {
+	    fprintf(stderr, "%s: %s: %s\n", progname, pkglist, Fstrerror(Fd));
+	    rc = 1;
+	    continue;
+	}
+	Header h;
+	while ((h = headerRead(Fd, HEADER_MAGIC_YES)) != NULL) {
+	    const char *err = "unknown error";
+#ifdef RPM_HAVE_HEADERFORMAT
+	    char *str = headerFormat(h, format, &err);
+#else
+	    char *str = headerSprintf(h, format, rpmTagTable, rpmHeaderFormats, &err);
+#endif
+	    if (str == NULL) {
+		rc = 1;
+		fprintf(stderr, "%s: %s: %s\n", progname, pkglist, err);
+	    }
+	    else {
+		fputs(str, stdout);
+		free(str);
+	    }
+	    headerFree(h);
+	}
+	Fclose(Fd);
+    }
+    return rc;
+}
+
+// ex:set ts=8 sts=4 sw=4 noet:
-- 
1.6.0.2.GIT



More information about the Apt-Rpm mailing list