cgetcwd

Get case-sensitive CWD in C


In this code, when user inputs c:\\temp, getcwd() returns "c:\temp".

Actual path is C:\Temp.

if(chdir(fullpath))
{
    perror("cd");
    return 1;
}
if(!getcwd(path, sizeof(path))
{
    perror("getcwd error");
    getchar();
    exit(1);
}

I expected it to return "C:\\Temp". Is there any way to get case-sensitive current working directory in C? Wherever I searched, there was only getcwd().

Edit: Also, if user inputs c:\tEMp, it returns the same. How can I prevent this?


Solution

  • In the comments, you said you wanted cross platform portability, but that's nearly impossible:

    So what can you do?

    1. Live with it as is.

    2. Write code that recognizes a lowercase drive letter in a path and changes it to uppercase.

    3. Use platform specific calls to get the OS to give the canonical name for that path.

      This is difficult. For a file, you could open the file, and then use GetFinalPathNameByHandle. You cannot open a directory as a file.

      Neither GetFullPathName nor GetLongFileName do what you need either.

      I think you'd have to use FindFirstFile to get the official name of the last segment of the path. Then chop off that segment and "search" again to get the official name of its parent. Repeat until you reach the root.