[Rpm-maint] rpm tag query and headerSprintf

Michael Schroeder mls at suse.de
Fri Dec 14 19:05:15 UTC 2007

On Fri, Dec 14, 2007 at 06:37:18PM +0000, Vento Neicapelli wrote:
> I'm trying to use the librpm in a custom application, I use rpm and I need your help about tag querying...
> 1) Have you got any snip of code to use the headerSprintf probably I'm doing it wrong...
> 2)  given the  prototype:
> char * headerSprintf(Header h, const char * fmt, const struct headerTagTableEntry_s * tags, const struct headerSprintfExtension_s * extensions, /*@null@*/ /*@out@*/ errmsg_t * errmsg)
> if I understand the tag fmt should have the same format of the rpm tool (cli version) with  --queryformat switch....or not?
> Below you find the code I wrote just to get familiar with rpmlib functions: it tries to search a package (by name) and then prints some tags....
>         const char * qfmt = "[%{*:xml}\n]";
>         struct headerTagTableEntry_s hTTE;
>         struct headerSprintfExtension_s hSE;
>         sResult = headerSprintf(h, qfmt, &hTTE, &hSE, &errMsg);

You shouldn't use your own extension table here, just use the ones
from librpm:

          sResult = headerSprintf(h, qfmt, rpmTagTable, rpmHeaderFormats, &errMsg);

>         printf("ALLTAGS:\n %s",sResult);
>         free(sResult); sResult=NULL; errMsg=NULL;
>         const char *qSummary="%{NAME}";
>         sResult = headerSprintf(h, qSummary, &hTTE, &hSE, &errMsg);

see above

>         if(!errMsg){
>             if(sResult){
>                 printf("NAME:\n %s",sResult);
>                 free(sResult); sResult=NULL; errMsg=NULL;
>             }
>         }else printf("ERROR: %s\n",errMsg);
>         const char *qFiles="[%{DIRNAMES}%{BASENAMES}\n]";
>         sResult = headerSprintf(h, qFiles, &hTTE, &hSE, &errMsg);

This can't work as DIRNAMES and BASENAMES have different sizes. Use the
pseudo-tag FILENAMES instead:

          const char *qFiles="[%{FILENAMES}\n]";
          sResult = headerSprintf(h, qFiles, rpmTagTable, rpmHeaderFormats, &errMsg);


Michael Schroeder                                   mls at suse.de
SUSE LINUX Products GmbH, GF Markus Rex, HRB 16746 AG Nuernberg

More information about the Rpm-maint mailing list