_getdcwd

Description

Gets full path name of current working directory on the specified drive.

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

char *_getdcwd( int drive, char *buffer, int maxlen );

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

Remarks

The _getdcwd function gets the full path name of the current working directory on the specified drive and stores it at buffer. The 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 drive argument specifies the drive (0 = default drive, 1=A, 2=B, etc.). 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 _getdcwd return value (a pointer to the allocated buffer).

Note that _getdcwd 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 _getdcwd function returns 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:None

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

32-Bit:DOS32X

See Also

_chdir, _getcwd, _getdrive, _mkdir, _rmdir

Example

/* GETDRIVE.C illustrates drive functions including:

* _getdrive _chdrive _getdcwd

*/

#include <stdio.h>

#include <conio.h>

#include <direct.h>

#include <stdlib.h>

void main( void )

{

int ch, drive, curdrive;

static char path[_MAX_PATH];

/* Save current drive. */

curdrive = _getdrive();

printf( "Available drives are: \n" );

/* If we can switch to the drive, it exists. */

for( drive = 1; drive <= 26; drive++ )

if( !_chdrive( drive ) )

printf( "%c: ", drive + 'A' - 1 );

while( 1 )

{

printf( "\nType drive letter to check or ESC to quit: " );

ch = _getch();

if( ch == 27 )

break;

if( isalpha( ch ) )

_putch( ch );

if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )

printf( "\nCurrent directory on that drive is %s\n", path );

}

/* Restore original drive. This is only necessary for DOS.*/

_chdrive( curdrive );

printf( "\n" );

}

Output

Available drives are:

A: B: C:

Type drive letter to check or ESC to quit: q

Type drive letter to check or ESC to quit: a

Current directory on that drive is A:\

Type drive letter to check or ESC to quit: c

Current directory on that drive is C:\LIBREF

Type drive letter to check or ESC to quit: