Directories outside of working directory not recocnized by S_ISDIR

56 views Asked by At

So I'm using stat() to check for directories and it works just fine until I navigate outside of the cwd. Then the directories are listed as files.

Here's a snippet from my function that creates the file list. It's implemented into an SDL GUI. Dirs should be red and all other files blue. But when I go to my Desktop for example all the dirs are blue when they should be red.

Note: fMenu is used as a buffer to store the file list. It is updated when the path changes.

    DIR *dir;
    struct dirent *dd;
    struct stat sd;
    dir = opendir(fMenu.fPath[fMenu.mode]);
    for (int a = 0; a < 1023; a++) // clears file list
    {
        fMenu.fList[fMenu.mode][a].isDir = false;
        memset(fMenu.fList[fMenu.mode][a].fName, 0, strlen(fMenu.fList[fMenu.mode][a].fName));
    }
    for (int a = 0; (dd = readdir(dir)) != NULL && a < 1023; a++) // gets files and puts in file list array
    {
        stat(dd->d_name, &sd);
        if (S_ISDIR(sd.st_mode)) //////// something wrong with this?
        {
            fMenu.fList[fMenu.mode][a].isDir = true;
            strncat(fMenu.fList[fMenu.mode][a].fName, dd->d_name, strlen(dd->d_name));
        } else
        {
            strncat(fMenu.fList[fMenu.mode][a].fName, dd->d_name, strlen(dd->d_name));
        }
    }
closedir(dir);

Here's where it is converted to textures for the GUI.

    for (int a = 0; a < 40; a++)
    {
        if (a == fMenu.pos)
        {
            if (fMenu.fList[fMenu.mode][a + fMenu.offset].isDir)
            {
                tempSurf = TTF_RenderText_Shaded(font, fMenu.fList[fMenu.mode][a + fMenu.offset].fName, Red, white);
            } else
            {
                tempSurf = TTF_RenderText_Shaded(font, fMenu.fList[fMenu.mode][a + fMenu.offset].fName, buttonColor, white);
            }
        } else
            {
                if (fMenu.fList[fMenu.mode][a + fMenu.offset].isDir)
            {
                tempSurf = TTF_RenderText_Solid(font, fMenu.fList[fMenu.mode][a + fMenu.offset].fName, Red);
            } else
            {
                tempSurf = TTF_RenderText_Solid(font, fMenu.fList[fMenu.mode][a + fMenu.offset].fName, buttonColor);
            }
        }
        SDL_DestroyTexture(fileDisplayTex[a]);
        fileDisplayTex[a] = SDL_CreateTextureFromSurface(renderer, tempSurf);
        SDL_FreeSurface(tempSurf);
        fileDisplayRect[a].w = strlen(fMenu.fList[fMenu.mode][a + fMenu.offset].fName) * 12;
    }
1

There are 1 answers

0
Jake A On

Ok thanks to Steve for this one :)

Here's the new code

void getFileList()
{
    DIR *dir;
    struct dirent *dd;
    struct stat sd;
    dir = opendir(fMenu.fPath[fMenu.mode]);
    char fullPath[strlen(fMenu.fPath[fMenu.mode]) * 2];
    for (int a = 0; a < 1023; a++) // clears file list
    {
        fMenu.fList[fMenu.mode][a].isDir = false;
        memset(fMenu.fList[fMenu.mode][a].fName, 0, strlen(fMenu.fList[fMenu.mode][a].fName));
    }
    for (int a = 0; (dd = readdir(dir)) != NULL && a < 1023; a++) // gets files and puts in file list array
    {
        //// uses full path
        memset(fullPath, 0, strlen(fullPath));
        strncat(fullPath, fMenu.fPath[fMenu.mode], strlen(fMenu.fPath[fMenu.mode]));
        strncat(fullPath, "\\", 2);
        strncat(fullPath, dd->d_name, strlen(dd->d_name));
        stat(fullPath, &sd);
        //////////
        if (S_ISDIR(sd.st_mode))
        {
            fMenu.fList[fMenu.mode][a].isDir = true;
            strncat(fMenu.fList[fMenu.mode][a].fName, dd->d_name, strlen(dd->d_name));
        } else
        {
            strncat(fMenu.fList[fMenu.mode][a].fName, dd->d_name, strlen(dd->d_name));
        }
    }
    closedir(dir);
    return;
}