开发者

Library for querying installed (Debian) packages in C?

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 wo开发者_StackOverflown't work if the user's installed language is different. Is there a C library I can hook into to query package installations?


I don't think you'll find a shipped library that meets your criteria, however, the dpkg program internally does exactly what you are describing, and does not link against the C++ libs (or libstdc++ at all):

tpost@tpost-desktop:~$ ldd -v /usr/bin/dpkg
        linux-gate.so.1 =>  (0x00f33000)
        libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0x00dfb000)
        libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x0077a000)
        /lib/ld-linux.so.2 (0x006a6000)

If you apt-get source dpkg , I think you'll find the code that you need in lib/dpkg within the source tree, in particular database.c and parse.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 dpkg reading the database can be seen via dpkg -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 within dpkg on 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜