I need to check whether certain Debian packages are installed on a system in my C program. I could use external shell scripts which do grep magic of apt-cache output with system(), but it seems a bit inelegant and hackish, as well as the fact that it won’t work if the user’s installed language is different. Is there a C library I can hook into to query package installations?
Share
I don’t think you’ll find a shipped library that meets your criteria, however, the
dpkgprogram internally does exactly what you are describing, and does not link against the C++ libs (or libstdc++ at all):If you
apt-get source dpkg, I think you’ll find the code that you need inlib/dpkgwithin the source tree, in particulardatabase.candparse.c.The trick, of course is extracting just what you need from it. Also, the GPL may or may not agree with your project at hand, but at least it is an implementation to study.
Example of
dpkgreading the database can be seen viadpkg -l | grep ii, for instance, to see all installed packages. It sounds like you just need to get that information into an elegant array or list, and I think you’ll find inspiration withindpkgon just how to do that.If you end up writing your own library (or wrapper around the bits in dpkg) please put it somewhere that other people can find it. The need you have is a recurring one that many people share.