>kevin writes:
>> In /usr/include/sys/dirent.h I see this:
>> 
>> /*
>>  * File-system independent directory entry.
>>  */
>> typedef struct dirent {
>>      ino_t                  d_ino;           /* "inode number" of entry */
>>      off_t                          d_off;           /* offset of disk 
>> directory entry */
>>      unsigned short       d_reclen;          /* length of this record */
>>      char                           d_name[1];       /* name of file */
>> } dirent_t;
>> 
>> Why the *name of file* d_name[1] have only one char?
>
>It's a string of unspecified length -- determined by d_reclen.
>
>(Some compilers will allow either '[]' or '[0]' in a structure for
>this case, but that's not compatible with older compilers.)

Also, you *really* shouldn't allocate one yourself; use readdir().

And don't assume that "you must because your code is threaded"; that is
not true; the only reason to use readdir_r are:


        - you want to save each and every record returned by readdir_r()
          (but you can also copy it)

        - if the DIR stream is used by multiple threads at the same time.


And typically you MUST SHOULD NOT (because it's so hard to use readdir_r
correctly) when your code looks like this:



        dir = opendir(dirname);

        while ((ent = readdir(dir)) != NULL) {
                /* do something with ent */
        }

        closedir(dir);


Even through readdir returns "static allocated memory", it is allocated
with each DIR buffer.  So withing readding from a single directory stream,
you can use readdir() even if multiple threads are reading from their
own directory streams.

Casper
_______________________________________________
opensolaris-code mailing list
opensolaris-code@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to