PRB: getcwd() and _getdcwd Return Backslash for Root Requests

ID Number: Q65642

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

docerr

Summary:

The Microsoft C version 6.0, 6.0a, and 6.0ax library functions are

documented incorrectly. C/C++ version 7.0 correctly states that

getcwd() and _getdcwd() will append a backslash to the drive

specification for requests made from a drive's root directory.

More Information:

General requests made to the getcwd() or _getdcwd() function will not

place a final backslash after the directory name. For example, when

called from c:\dos\loc\ the return value of getcwd() will be

c:\dos\loc. However, when invoked from a drive's root directory, the

backslash character is appended (for example, when called from d:\ the

return value is d:\). This behavior may cause problems when programs

append a backslash followed by a filename to the return of getcwd() in

order to create data files in the current working directories. In

these cases, it is important to check for the root directory

condition, in which case a backslash need not be appended.

The following code demonstrates the possible conflict; it will fail

when the default directory is a drive's root directory:

Sample Code

-----------

/* Compile options needed: none

*/

#include<stdio.h>

#include<stdlib.h>

#include<direct.h>

#include<string.h>

void main(void)

{

FILE *fp;

char buffer[_MAX_DIR+13];

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

perror("getcwd error");

else

{

strcat(buffer,"\\test.dat");

if((fp=fopen(buffer,"w"))!=NULL)

fputs("hello",fp);

else

puts("failure in writing to current drive\n");

}

}

Additional reference words: 6.00 6.00a 6.00ax