Project

General

Profile

Feature #4933

getcwd(3c) could be more useful

Added by Jason King over 5 years ago. Updated over 5 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
lib - userland libraries
Start date:
2014-06-21
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:
needs-triage

Description

Currently, correct use of getcwd(3c) requires one to loop until it a sufficiently large buffer is given to hold the return value or an error other than ERANGE is returned.

I would suggest that invoking getcwd(NULL, 0) should either return a malloc(3c)'d buffer containing the result, or NULL with errno set appropriately (I believe EACCESS and ENOMEM would be the two candidates). This would also match what other platforms (glibc, OS X, FreeBSD at least) do.

As all the standards I can find suggest that passing the first argument as NULL is undefined. As such, I believe that would allow us to ignore the 'EINVAL shall be returned when size is 0' clause (since presumably that only pertains to defined behavior).

History

#1

Updated by Gary Mills over 5 years ago

Doesn't it already do this? The man page says:

     If buf is a null pointer, getcwd()  obtains  size  bytes  of
     space using malloc(3C). The pointer returned by getcwd() can
     be used as the argument in a subsequent call to free().
#2

Updated by Jason King over 5 years ago

Not exactly -- all that does is malloc a buffer of 'size' and tries to use that. If 'size' wasn't large enough, it still fails and you have to call it again with increasingly larger values until it succeeds or returns some other error (which makes it of questionable utility IMO).

I don't see why we can't just do this looping for the caller when requested.

Also available in: Atom PDF