在C中使用libdpkg

我查看了“dpkg-query”源代码并尝试获取已安装软件包的列表。

它编译,但是当我尝试运行我得到modstatdb_open()函数的错误。

#define LIBDPKG_VOLATILE_API 1 #include <stdio.h> #include <unistd.h> #include <dpkg/macros.h> #include <dpkg/dpkg.h> #include <dpkg/dpkg-db.h> #include <dpkg/pkg-array.h> #include <dpkg/debug.h> #include <dpkg/pkg-list.h> void main() { struct pkg_array array; modstatdb_open(msdbrw_readonly | msdbrw_available_readonly); pkg_array_init_from_db(&array); printf("%d\n",array.n_pkgs); pkg_array_destroy(&array); modstatdb_shutdown(); } 

分割故障(核心转储)

那里有什么问题?

工作版本。 它必须使用dpkg_program_init(char * progname); 毕竟。

 #define LIBDPKG_VOLATILE_API 1 #include <stdio.h> #include <unistd.h> #include <dpkg/dpkg.h> #include <dpkg/dpkg-db.h> #include <dpkg/pkg-array.h> void main() { struct pkgset *set; struct pkginfo *inf; struct pkg_array array; dpkg_program_init("a.out"); modstatdb_open(msdbrw_available_readonly); pkg_array_init_from_db(&array); printf("Number of packages in local database: %d\n",array.n_pkgs); inf = pkg_db_find_singleton("kate"); printf("status code of package: %d\n", inf->status);// 7 means installed dpkg_program_done(); }