Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - readdir() sets wrong dirent->d_namelen value?: (4 Items)
   
readdir() sets wrong dirent->d_namelen value?  
Hi everybody,

as written in the QNX documentation on "struct dirent", the "d_namelen" member holds the number of characters of the 
directory entry name, stored in the "d_name" member (not regarding the terminating '\0').

I'm using QNX 6.4.1 on an embedded PPC target. My problem is, that the string length of "d_name" is actually 1 character
 less than "d_namelen" indicates (e.g. a directory entry named "bitmaps" has a "d_namelen" value of 8 instead of 7).

Is this a flaw in QNX 6.4.1, or is there a reasonable explanation to this? One could be, that "d_namelen" in fact 
regards the terminating '\0', or it accounts for a preceeding '/' character (which is however not stored in the "d_name"
 member of the dirent struct)...

I would appreciate any comments on this matter.
Many thanks in advance!
RE: readdir() sets wrong dirent->d_namelen value?  
I think the behavior depends on the process that populates the dirent
fields for the given path you are querying.

For example, try out the following test. The d_namelen values are
correct for "/bin" but incorrect for "/proc/boot" (like what you are
seeing).

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

int printdir(char* path)
{
    DIR* dirp;
    struct dirent* direntp;

    printf("\ncontents of %s\n", path);
    dirp = opendir( path );
    if( dirp != NULL ) {
        for(;;) {
            direntp = readdir( dirp );
            if( direntp == NULL ) break;

            printf( "\"%s\", %d, %d\n", direntp->d_name,
direntp->d_namelen, strlen(direntp->d_name) );
        }
        closedir( dirp );
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}

int main( void )
{
	printdir("/proc/boot");
	return printdir("/bin");
}

Running on an x86 6.4.1 installation in vmware:

contents of /proc/boot
"startup-script", 15, 14
"unlink_list", 12, 11
"procnto-smp-instr", 18, 17
"libc.so.3", 10, 9
"libc.so", 8, 7

etc.

contents of /bin
".", 1, 1
"..", 2, 2
"ap", 2, 2
"aps", 3, 3
"asa", 3, 3
"cat", 3, 3
"chgrp", 5, 5
"chmod", 5, 5
"chown", 5, 5
"confstr", 7, 7

etc.

Note, however, when I try the same test on a 6.5.0 x86 installation, the
direntp->d_namelen are ok for /proc/boot.

Regards,
asherk


-----Original Message-----
From: Markus Schmoelzer [mailto:community-noreply@qnx.com] 
Sent: Friday, July 01, 2011 8:37 AM
To: general-filesystems
Subject: readdir() sets wrong dirent->d_namelen value?

Hi everybody,

as written in the QNX documentation on "struct dirent", the "d_namelen"
member holds the number of characters of the directory entry name,
stored in the "d_name" member (not regarding the terminating '\0').

I'm using QNX 6.4.1 on an embedded PPC target. My problem is, that the
string length of "d_name" is actually 1 character less than "d_namelen"
indicates (e.g. a directory entry named "bitmaps" has a "d_namelen"
value of 8 instead of 7).

Is this a flaw in QNX 6.4.1, or is there a reasonable explanation to
this? One could be, that "d_namelen" in fact regards the terminating
'\0', or it accounts for a preceeding '/' character (which is however
not stored in the "d_name" member of the dirent struct)...

I would appreciate any comments on this matter.
Many thanks in advance!




_______________________________________________

General
http://community.qnx.com/sf/go/post87017
Re: RE: readdir() sets wrong dirent->d_namelen value?  
Thanks for the hint.
Using your program, I made the similar observations on my target: Some of the directories are processed properly, others
 aren't. Some results differ from yours, but that was to expect.

Unfortunately I don't have the opportunity to check whether it works fine on 6.5 - let alone upgrading to that version 
right now.

Anyway, I'll try to get around using 'd_namelen' wherever possible. Using 'strlen(d_name)' instead seems safe, of course
. Yet, there is a small performance price to pay which is why this solution is not entirely satisfying for me.

Nevertheless, thanks for your support.
RE: RE: readdir() sets wrong dirent->d_namelen value?  
If you have a support plan with QNX and would like a fix for 6.4.1
procnto, you could open a case for this issue through the support plan.
In which case, please include a link to this posting so the person who
gets the case can sync up with me.
http://community.qnx.com/sf/go/post87113

Best regards,
asherk


-----Original Message-----
From: Markus Schmoelzer [mailto:community-noreply@qnx.com] 
Sent: Wednesday, July 06, 2011 4:09 AM
To: general-filesystems
Subject: Re: RE: readdir() sets wrong dirent->d_namelen value?

Thanks for the hint.
Using your program, I made the similar observations on my target: Some
of the directories are processed properly, others aren't. Some results
differ from yours, but that was to expect.

Unfortunately I don't have the opportunity to check whether it works
fine on 6.5 - let alone upgrading to that version right now.

Anyway, I'll try to get around using 'd_namelen' wherever possible.
Using 'strlen(d_name)' instead seems safe, of course. Yet, there is a
small performance price to pay which is why this solution is not
entirely satisfying for me.

Nevertheless, thanks for your support.



_______________________________________________

General
http://community.qnx.com/sf/go/post87113