add opendir alias
[minix.git] / lib / libc / compat / gen / compat_readdir.c
blob72d52437717843ea2f028c54df901f308375d74a
1 /* $NetBSD: compat_readdir.c,v 1.3 2012/02/08 12:10:17 mbalmer Exp $ */
3 #define __LIBC12_SOURCE__
4 #include "namespace.h"
5 #include <sys/param.h>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <limits.h>
10 #include <compat/include/dirent.h>
12 #ifdef __weak_alias
13 __weak_alias(readdir,_readdir)
14 __weak_alias(readdir_r,_readdir_r)
15 #endif
17 #ifdef __warn_references
18 __warn_references(readdir,
19 "warning: reference to compatibility readdir(); include <dirent.h> for correct reference")
20 __warn_references(readdir_r,
21 "warning: reference to compatibility readdir_r(); include <dirent.h> for correct reference")
22 #endif
24 static struct dirent12 *
25 direnttodirent12(struct dirent12 *d12, const struct dirent *d)
27 if (d == NULL)
28 return NULL;
30 if (d->d_fileno > UINT_MAX || d->d_namlen >= sizeof(d12->d_name)) {
31 errno = ERANGE;
32 return NULL;
34 d12->d_fileno = (uint32_t)d->d_fileno;
35 d12->d_reclen = (uint16_t)d->d_reclen;
36 d12->d_namlen = (uint8_t)MIN(d->d_namlen, sizeof(d->d_name) - 1);
37 d12->d_type = (uint8_t)d->d_type;
38 memcpy(d12->d_name, d->d_name, (size_t)d12->d_namlen);
39 d12->d_name[d12->d_namlen] = '\0';
40 return d12;
43 struct dirent12 *
44 readdir(DIR *dirp)
46 static struct dirent12 d12;
47 return direnttodirent12(&d12, __readdir30(dirp));
50 int
51 readdir_r(DIR *dirp, struct dirent12 *entry, struct dirent12 **result)
53 int error;
54 struct dirent e, *ep;
56 if ((error = __readdir_r30(dirp, &e, &ep)) != 0)
57 return error;
59 *result = entry;
60 (void)direnttodirent12(entry, &e);
61 return 0;