While RPM packages have a robust ecosystem around them for programmatically retrieving information about package metadata, the Debian package management system is sorely lacking in this respect.
Here is a simple C example which demonstrates how to programmatically list all installed Debian packages.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <dpkg/dpkg.h> #include <dpkg/dpkg-db.h> #include <dpkg/pkg-array.h> #include "filesdb.h" const char thisname[] = "example1"; int main(int argc, const char *const *argv) { struct pkg_array array; struct pkginfo *pkg; int i; enum modstatdb_rw msdb_status; standard_startup(); filesdbinit(); msdb_status = modstatdb_open(msdbrw_readonly); pkg_infodb_init(msdb_status); pkg_array_init_from_db(&array); pkg_array_sort(&array, pkg_sorter_by_name); for (i = 0; i < array.n_pkgs; i++) { pkg = array.pkgs[i]; if (pkg->status == stat_notinstalled) continue; printf("Package --> %sn", pkg->set->name); } pkg_array_destroy(&array); modstatdb_shutdown(); standard_shutdown(); }
You also need the following files from the dpkg source code:
config.h strnlen.h ehandle.h filesdb.h infodb.h main.h filesdb.c infodb.c libdpkg.a
Here is how to build the example:
gcc -c -DLIBDPKG_VOLATILE_API -I. example.c infodb.c filesdb.c gcc -DLIBDPKG_VOLATILE_API -o example example.o filesdb.o infodb.o ./libdpkg.a
In case you were wondering, there is no dynamic version of libdpkg and neither is there a published API for this library.
If you need a complete solution to programmatically interacting with the Debian package management system using C or C++, please contact me for details.