图书馆在C查询安装(Debian的)包? [英] Library for querying installed (Debian) packages in C?

查看:118
本文介绍了图书馆在C查询安装(Debian的)包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查某些Debian软件包是否安装在我的C程序的系统上。我可以用外部的shell脚本里面做的apt-缓存输出的grep魔术体系(),但它似乎有点不雅和hackish的,还有一个事实,即如果用户安装的语言不同,它不会起作用。是否有一个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 won't work if the user's installed language is different. Is there a C library I can hook into to query package installations?

推荐答案

我不认为你会发现,满足您的条件的出货库,然而,的dpkg 程序在内部做了你所描述的到底是什么,不链接对C ++库(或的libstdc ++全部):

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)

如果您 apt-get的源的dpkg ,我想你会发现在code您在的lib / dpkg的<需要/ code>源代码树中,特别是 database.c parse.c

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.

这一招,当然只是提取你从它需要的东西。此外,GPL可能会或可能不会与手头的项目达成一致,但至少这是学习的实现。

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.

示例的dpkg 读取数据库可以通过中可以看出的dpkg -l | grep的二,例如,看到所有安装的软件包。这听起来像你只需要得到这些信息纳入一个优雅的数组或列表,我想你会的只是如何做到这一点在的dpkg 寻找灵感。

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.

如果你最终(dpkg的中位左右或包装)编写自己的库,请把它的地方,其他人能够找到它。你需要的是一个反复出现的一个多人共享。

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.

这篇关于图书馆在C查询安装(Debian的)包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆