From: Geoff Clare on
Nobody wrote:

> The correct answer for Unix (readlink() isn't in ANSI C) is to use
> pathconf(path, _PC_PATH_MAX) or fpathconf(fd, _PC_PATH_MAX), where path or
> fd identify the filesystem (the values can vary between filesystems, so
> you might get a different result for /usr or /tmp than for the root
> filesystem).

There is a separate _PC_* constant for that: _PC_SYMLINK_MAX
The symlink length limit can be different from the pathname length
limit.

> However:
>
> 1. [f]pathconf can return -1, indicating that there is no pre-determined
> limit.

Still true for _PC_SYMLINK_MAX

> 2. If the buffer isn't large enough, readlink() *doesn't* include a
> terminating NUL byte.

The standard doesn't require readlink() to add a null byte even if
there is room for one.

> So the safest approach is to malloc() a buffer, then call readlink() in a
> loop, realloc()ing the buffer until the value returned from readlink() is
> strictly less than (i.e. "<", *not* "<=") the the buffer size.

Yup. It's crazy that whoever invented readlink() (presumably
someone at UCB) didn't have it return an error if the buffer isn't
big enough.

--
Geoff Clare <netnews(a)gclare.org.uk>


 | 
Pages: 1
Prev: defining after execution
Next: UDP broadcast