_getcwd

Description

Gets the current working directory.

#include <direct.h> Required only for function declarations  

char *_getcwd( char *buffer, int maxlen );

buffer Storage location for path name  
maxlen Maximum length of path name  

Remarks

The _getcwd function gets the full path name of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path name. An error occurs if the length of the path name (including the terminating null character) exceeds maxlen.

The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path name. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).

Note that _getcwd returns a string that represents the path name of the current working directory. If the current working directory is set to the root, the string will end with a backslash (\). If the current working directory is set to a directory other than the root, the string will end with the name of the directory and not with a backslash.

Return Value

The _getcwd function returns a pointer to buffer. A NULL return value indicates an error, and errno is set to one of the following values:

Value Meaning

ENOMEM Insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer)
ERANGE Path name longer than maxlen characters

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _getcwd for compatibility with ANSI naming conventions of non-ANSI functions. Use getcwd and link with OLDNAMES.LIB for UNIX compatibility.

See Also

_chdir, _mkdir, _rmdir

Example

/* This program places the name of the current directory in the buffer

* array, then displays the name of the current directory on the screen.

* Specifying a length of _MAX_DIR leaves room for the longest legal

* directory name.

*/

#include <direct.h>

#include <stdlib.h>

#include <stdio.h>

void main( void )

{

char buffer[_MAX_DIR];

/* Get the current working directory: */

if( _getcwd( buffer, _MAX_DIR ) == NULL )

perror( "_getcwd error" );

else

printf( "%s\n", buffer );

}

Output

C:\LIBREF