From 0ec995601bf515abaddadd361f75ddd2bf93a1ca Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sun, 30 Dec 2012 19:40:04 +0100 Subject: [PATCH] shl: misc: add shl_dirent() helper "struct dirent" has always been a mess. As its size may differ between systems, we need to dynamically allocate it. This helper does that for us. Signed-off-by: David Herrmann --- src/shl_misc.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/shl_misc.h b/src/shl_misc.h index e6354bc..954bab3 100644 --- a/src/shl_misc.h +++ b/src/shl_misc.h @@ -31,16 +31,33 @@ #ifndef SHL_MISC_H #define SHL_MISC_H +#include #include #include #include #include #include +#include #include #define SHL_HAS_BITS(_bitmask, _bits) (((_bitmask) & (_bits)) == (_bits)) #define SHL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +static inline int shl_dirent(const char *path, struct dirent **ent) +{ + size_t len; + struct dirent *tmp; + + len = offsetof(struct dirent, d_name) + + pathconf(path, _PC_NAME_MAX) + 1; + tmp = malloc(len); + if (!tmp) + return -ENOMEM; + + *ent = tmp; + return 0; +} + static inline int shl_strtou(const char *input, unsigned int *output) { unsigned long val;