🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.unix
Article #122

Re: Query about <dirent.h>

#122
From: gwyn
Date: Mon, 27 Nov 1989 21:38
44 lines
2540 bytes
From: gwyn@BRL.MIL

In article <444@longway.TIC.COM> Andy Tanenbaum <uunet!cs.vu.nl!ast> writes:
>In article <442@longway.TIC.COM> gwyn@brl.arpa (Doug Gwyn) writes:
>>That's what happens when programmers assume things that are not promised
>>by the standards.
>I don't follow.  What is it that that the standards don't promise.

>From IEEE Std 1003.1-1988: "The character array d_name is of unspecified size".

>Surely a programmer may declare a struct dirent, ...

Sure, but that doesn't mean that it will be particularly useful to do so.

>Furthermore, a programmer may assume that d_name is an array of characters
>that can hold a file name.

That is the important thing that is NOT promised by the standard.
d_name must have type char[] and the filename that it represents
must be null terminated, with no more than NAME_MAX bytes before
the null terminator.

>I don't see how you can put a file name in 1 character.  I don't see any
>alternative than to allocate NAME_MAX+1 characters there.

Because it is wasteful to allocate so much storage for what are typically
short strings, many (maybe even most) implementations allocate just as
much as is actually needed for each individual struct dirent (including
possibly a few bytes for alignment).  Practically all C compilers support
this kind of "type punning", but the programmer need to be careful not to
make unwarranted assumptions.

The reason the standard does not specify char d_name[NAME_MAX+1] is
precisely to allow this particular implementation technique.

>Why doesn't the standard require <dirent.h> to have <limits.h> as a
>prerequisite, so that NAME_MAX is at least known.

IEEE Std 1003.1-1988 explains how a programmer who wished to obtain
that information may do so.  Since the implementation of <dirent.h>
does not require the macro NAME_MAX, it would have been pretty silly
to have required <limits.h> to be included before <dirent.h>.

Volume-Number: Volume 17, Number 74
Technical Details
Message-ID: <446@longway.TIC.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!longway!std-unix
References: <437@longway.TIC.COM> <438@longway.TIC.COM> <441@longway.TIC.COM> <442@longway.TIC.COM> <444@longway.TIC.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!longway!std-unix
From: gwyn@BRL.MIL
Newsgroups: comp.std.unix
Subject: Re: Query about <dirent.h>
Message-ID: <446@longway.TIC.COM>
Date: 27 Nov 89 21:38:14 GMT
References: <437@longway.TIC.COM> <438@longway.TIC.COM> <441@longway.TIC.COM> <442@longway.TIC.COM> <444@longway.TIC.COM>
Sender: std-unix@longway.TIC.COM
Reply-To: gwyn@brl.arpa (Doug Gwyn)
Organization: Ballistic Research Lab (BRL), APG, MD.
Lines: 44
Approved: jsq@longway.tic.com (Moderator, John S. Quarterman)