Translate

Archives

List packages in RPM Database using C

Here is one way of listing installed RPM packages. It works with all versions of CentOS 5, RHEL5 and up to Fedora 14.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#include <rpm/rpmlib.h>
#include <rpm/header.h>
#include <rpm/rpmdb.h>

int
main(int argc, char *argv[])
{
    rpmdbMatchIterator mi;
    int type, count;
    char *name;
    rpmdb db;
    Header h;

    rpmReadConfigFiles( NULL, NULL );
    if (rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0) {
        fprintf( stderr, "ERROR: Cannot open RPM databasen");
        exit(1);
    }

    mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, NULL, 0);
    while ((h = rpmdbNextIterator(mi))) {
        headerGetEntry(h, RPMTAG_NAME, &type, (void **) &name, &count);
        printf("%sn", name);
    }

    rpmdbFreeIterator(mi);
    rpmdbClose(db);

    exit(0);
}


RHEL6 and Fedora 15 have a new version of RPM. The rpmdbOpen and rpmdbClose APIs are no longer supported in that version of RPM.

If you need a complete solution to programmatically interacting with the RPM package management system using C or C++, please contact me for details.

Comments are closed.