From 86287122ee8c4098f3c6c00dfa262dfcf2a21a35 Mon Sep 17 00:00:00 2001
From: Peter Korsgaard <jacmet@sunsite.dk>
Date: Mon, 22 Sep 2008 14:27:04 +0200
Subject: [PATCH] dpkg: add pattern support to list (-l)

./scripts/bloat-o-meter busybox_unstripped{_old,}
function                                             old     new   delta
dpkg_main                                           3012    3044     +32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 32/0)               Total: 32 bytes

(It's +27 if we get rid of the pattern pointer check, but that might have
speed impact).

Signed-off-by: Peter Korsgaard <jacmet@sunsite.dk>
---
 archival/dpkg.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/archival/dpkg.c b/archival/dpkg.c
index f31a7f0..d853457 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -24,6 +24,7 @@
  *
  */
 
+#include <fnmatch.h>
 #include "libbb.h"
 #include "unarchive.h"
 
@@ -1293,7 +1294,7 @@ static void free_array(char **array)
  * the status_hashtable to retrieve the info. This results in smaller code than
  * scanning the status file. The resulting list, however, is unsorted.
  */
-static void list_packages(void)
+static void list_packages(const char *pattern)
 {
 	int i;
 
@@ -1314,6 +1315,9 @@ static void list_packages(void)
 			name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name];
 			vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version];
 
+			if (pattern && fnmatch(pattern, name_str, 0))
+				continue;
+
 			/* get abbreviation for status field 1 */
 			s1 = stat_str[0] == 'i' ? 'i' : 'r';
 
@@ -1626,7 +1630,7 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
 
 	/* if the list action was given print the installed packages and exit */
 	if (opt & OPT_list_installed) {
-		list_packages();
+		list_packages(argv[0]);
 		return EXIT_SUCCESS;
 	}
 
-- 
1.5.6.3

