How to deal with wide-character directory entries in C?

145 Views Asked by At

I have looked up various methods of converting wide strings to single-byte characters, but am unsure how to approach the char d_name[260] field of a struct dirent entry pertaining to a file with a wide character name.

Is the d_name field completely invalid or is there any way to obtain a valid wide string or single-byte string from it?

Any help would be much appreciated, thank you.

Updates:

Platform: Windows 10

Compiler: MinGW

Problematic section of code (minimum reproducible example):

struct dirent *entry;
DIR *dir = opendir(srcDir);
time_t mtime = 0;
size_t PDFlen = 260 + HPlen + 12 + 1;
char *PDF = malloc(PDFlen);
char finalPDF[264] = {0};
memset_c(PDF, 0, PDFlen);
while ((entry = readdir(dir)) != NULL) {
    if (endswith(entry->d_name, ".pdf")) {
        strcpy_c(PDF, srcDir); strcat_c(PDF, "\\"); strcat_c(PDF, entry->d_name);
        stat64(PDF, &buffer);
        if (buffer.st_mtime > mtime) {
            mtime = buffer.st_mtime;
#ifdef _WIN32
            strcpy_c(finalPDF, "\\");
#else
            strcpy_c(finalPDF, "/");
#endif
            strcat_c(finalPDF, entry->d_name);
        }
    }
}

... then later on I am unable to open finalPDF with fopen(), hence my question (finalPDF is a PDF file in Spanish whose filename contains two non-ASCII characters).

P.S. srcDir is my Downloads folder and HPlen is the number of characters in my home path.

0

There are 0 best solutions below