1 /* $NetBSD: compat_readdir.c,v 1.3 2012/02/08 12:10:17 mbalmer Exp $ */ 2 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> 11 12 #ifdef __weak_alias 13 __weak_alias(readdir,_readdir) 14 __weak_alias(readdir_r,_readdir_r) 15 #endif 16 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 23 24 static struct dirent12 * 25 direnttodirent12(struct dirent12 *d12, const struct dirent *d) 26 { 27 if (d == NULL) 28 return NULL; 29 30 if (d->d_fileno > UINT_MAX || d->d_namlen >= sizeof(d12->d_name)) { 31 errno = ERANGE; 32 return NULL; 33 } 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; 41 } 42 43 struct dirent12 * 44 readdir(DIR *dirp) 45 { 46 static struct dirent12 d12; 47 return direnttodirent12(&d12, __readdir30(dirp)); 48 } 49 50 int 51 readdir_r(DIR *dirp, struct dirent12 *entry, struct dirent12 **result) 52 { 53 int error; 54 struct dirent e, *ep; 55 56 if ((error = __readdir_r30(dirp, &e, &ep)) != 0) 57 return error; 58 59 *result = entry; 60 (void)direnttodirent12(entry, &e); 61 return 0; 62 } 63