Skip to content

Instantly share code, notes, and snippets.

@bdash
Created November 26, 2021 04:25
Show Gist options
  • Save bdash/a6e7dfe920596c10e8498d9b19c241b4 to your computer and use it in GitHub Desktop.
Save bdash/a6e7dfe920596c10e8498d9b19c241b4 to your computer and use it in GitHub Desktop.
getattrlist / ATTR_DIR_ENTRYCOUNT
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/attr.h>
#include <sys/errno.h>
#include <unistd.h>
#include <sys/vnode.h>
struct DirEntryCount {
u_int32_t length;
u_int32_t entryCount;
} __attribute__((aligned(4), packed));
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s [directory]\n", argv[0]);
return 1;
}
struct attrlist attrList = { .bitmapcount = ATTR_BIT_MAP_COUNT, .dirattr = ATTR_DIR_ENTRYCOUNT };
struct DirEntryCount dirEntryCount = {0};
int err = getattrlist(argv[1], &attrList, &dirEntryCount, sizeof(dirEntryCount), 0);
if (err != 0) {
perror("getattrlist");
return 2;
}
assert(dirEntryCount.length == sizeof(dirEntryCount));
printf("Directory %s contains %u items\n", argv[1], dirEntryCount.entryCount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment