[PATCH v2] Use latest FreeBSD definition for DIRSIZ

Sebastian Huber sebastian.huber@embedded-brains.de
Thu Mar 28 14:21:00 GMT 2013


On 03/28/2013 03:15 PM, Sebastian Huber wrote:
> newlib/ChangeLog
> 2013-03-28  Sebastian Huber <sebastian.huber@embedded-brains.de>
>
> 	* libc/posix/scandir.c (DIRENT_NAMELEN): New define.
> 	(DIRSIZ): Use latest FreeBSD definition.  Use DIRENT_NAMELEN.
> ---
>   newlib/libc/posix/scandir.c |   25 ++++++++++++++-----------
>   1 files changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/newlib/libc/posix/scandir.c b/newlib/libc/posix/scandir.c
> index 96a66de..4fddfb5 100644
> --- a/newlib/libc/posix/scandir.c
> +++ b/newlib/libc/posix/scandir.c
> @@ -42,26 +42,29 @@ static char sccsid[] = "@(#)scandir.c	5.10 (Berkeley) 2/23/91";
>
>   #include <sys/types.h>
>   #include <sys/stat.h>
> +#include <stddef.h>

Sorry, this shouldn't be there.

>   #include <dirent.h>
>   #include <stdlib.h>
>   #include <string.h>
>   #include <sys/lock.h>
>
> -/*
> - * The DIRSIZ macro gives the minimum record length which will hold
> - * the directory entry.  This requires the amount of space in struct dirent
> - * without the d_name field, plus enough space for the name with a terminating
> - * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
> - */
> -#undef DIRSIZ
>   #ifdef _DIRENT_HAVE_D_NAMLEN
> -#define DIRSIZ(dp) \
> -    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
> +#define DIRENT_NAMELEN(dp) ((dp)->d_namlen)
>   #else
> -#define DIRSIZ(dp) \
> -    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + ((strlen((dp)->d_name)+1 + 3) &~ 3))
> +#define DIRENT_NAMELEN(dp) (strlen((dp)->d_name))
>   #endif
>
> +/*
> + * The DIRSIZ macro is the minimum record length which will hold the directory
> + * entry.  This requires the amount of space in struct dirent without the
> + * d_name field, plus enough space for the name and a terminating nul byte
> + * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
> + */
> +#undef DIRSIZ
> +#define DIRSIZ(dp)							\
> +	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
> +	    ((DIRENT_NAMELEN(dp) + 1 + 3) &~ 3))
> +
>   #ifndef __P
>   #define __P(args) ()
>   #endif
>

This is a direct copy of DIRSIZ() from the FreeBSD sources:

http://svnweb.freebsd.org/base/head/lib/libc/gen/scandir.c?revision=202693&view=markup

The "sizeof(struct dirent) - sizeof(dp)->d_name" is not identical to "offsetof 
(struct dirent, d_name)".  For example on PowerPC with the RTEMS definition for 
struct dirent we have an off_t field which is 64-bit.  This results in 64-bit 
aligned size of struct dirent per PowerPC ABI.  If I compare patch v1 with v2 
we have:

--- 1.txt       2013-03-28 15:08:05.742963772 +0100
+++ 2.txt       2013-03-28 15:08:16.034922388 +0100
@@ -1,5 +1,5 @@

-./scandir.o-v1:     file format elf32-powerpc
+./scandir.o-v2:     file format elf32-powerpc


  Disassembly of section .text:
@@ -79,7 +79,7 @@
   11c:  48 00 00 01     bl      11c <scandir+0x11c>
   120:  38 63 00 04     addi    r3,r3,4
   124:  54 63 00 3a     rlwinm  r3,r3,0,0,29
- 128:  38 63 00 14     addi    r3,r3,20
+ 128:  38 63 00 18     addi    r3,r3,24
   12c:  48 00 00 01     bl      12c <scandir+0x12c>
   130:  7f e4 fb 78     mr      r4,r31
   134:  7c 7f 1b 79     mr.     r31,r3

-- 
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.



More information about the Newlib mailing list