TOC   Previous   Next

Extended Attributes

Example code

List the EAs associated with a file

First, call listxattr() to retrieve list of EA names:

#define XATTR_SIZE 10000      /* A guess at likely upper limit */

int
main(int argc, char *argv[])
{
    char list[XATTR_SIZE], value[XATTR_SIZE];
    int ns, llen, vlen;

    assert(argc == 2);

    llen = listxattr(argv[1], list, XATTR_SIZE);
    if (llen == -1) errExit("listxattr");

Walk through the list of EA names, using getxattr() to retrieve associated value:

    for (ns = 0; ns < llen; ns += strlen(&list[ns]) + 1) {
        printf("%s=", &list[ns]);

        vlen = getxattr(argv[1], &list[ns], value, XATTR_SIZE);
        if (vlen == -1)         /* Maybe name was removed */
            printf("couldn't get value\n");
        else            /* Assumes value is printable string */
            printf("%.*s\n", vlen, value);
    } 

    exit(EXIT_SUCCESS);
}

(C) 2006, Michael Kerrisk