In GNU libc 2.3.5, the header file /usr/lib/stdlib.h reads: #ifdef __USE_GNU /* Return a malloc'd string containing the canonical absolute name of the named file. The last file name component need not exist, and may be a symlink to a nonexistent file. */ extern char *canonicalize_file_name (__const char *__name) __THROW __nonnull ((1)); #endif However, canonicalize_file_name() returns NULL when passed a path where the last component does not exist. // file: can-test.c #define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> int main(void) { char *p = canonicalize_file_name("no-such-file"); if (!p) perror(NULL); return 0; } The same applies to realpath() and its description. As of version 2.3.5, either the documentation or the implementation is wrong. Best Juergen Lemke
__nonnull ((1)) means that the first argument must not be NULL, not that the function can't return NULL.
Hello, jakub at redhat dot com wrote: > __nonnull ((1)) means that the first argument must > not be NULL, not that > the function can't return NULL. That is not what I was referring to. Just above the declaration of canonicalize_file_name() you can read: "The last file name component need not exist [...]" However, as of GNU libc 2.3.5, this function always fails (returning NULL) when passed a path to a inexistent file. Best, Juergen
As of glibc HEAD, stdlib/stdlib.h really contains wrong comments about canonicalize_file_name() and realpath(). The comment in stdlib/canonicalize.c is correct, though.
I adjusted the comment.