DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

getcwd(S)


getcwd -- get pathname of current working directory

Syntax

cc . . . -lc

#include <unistd.h>

char *getcwd(char *buf, size_t size);

Description

The getcwd function returns a pointer to the current directory pathname. The value of size must be at least one (1) greater than the length of the pathname to be returned.

If buf is not NULL, the pathname will be stored in the space pointed to by buf.

If buf is a NULL pointer, getcwd obtains size bytes of space using malloc(S). In this case, the pointer returned by getcwd may be used as the argument in a subsequent call to free.

Return values

The getcwd function returns a pointer to the current directory pathname; it returns NULL with errno set if size is not large enough, or if an error occurs in a lower-level function.

Diagnostics

getcwd() will fail if one or more of the following are true:

[EACCES]
A parent directory cannot be read to get its name.

[EINVAL]
size is equal to 0.

[ERANGE]
size is less than 0 or is greater than 0 and less than the length of the pathname plus 1.

Examples

Here is a program that prints the current working directory:
   #include <unistd.h>
   #include <stdio.h>
   

main() { char *cwd; if ((cwd = getcwd(NULL, 64)) == NULL) { perror("pwd"); exit(2); } (void)printf("%s\n", cwd); return(0); }

See also

malloc(S)

Standards conformance

getcwd is conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003