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 <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-12-30 19:40:04 +01:00
parent 7304985892
commit 0ec995601b

View File

@ -31,16 +31,33 @@
#ifndef SHL_MISC_H
#define SHL_MISC_H
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon.h>
#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;