Repository: libsdl-org/SDL
File: src/dialog/unix/SDL_portaldialog.c (C)

Code before:

    SDL_Window *window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL);
    SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL);
    int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0);
    bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false);
    const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
    const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
    char *location_name = NULL;
    char *location_folder = NULL;
    struct stat statbuf;
    bool open_folders = false;
    bool save_file_existing = false;
    bool save_file_new_named = false;

    switch (type) {
    case SDL_FILEDIALOG_OPENFILE:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open File");
        break;

    case SDL_FILEDIALOG_SAVEFILE:
        method = "SaveFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Save File");
        if (default_location) {
            if (stat(default_location, &statbuf) == 0) {
                save_file_existing = S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode);
            } else if (errno == ENOENT) {
                char *dirc = SDL_strdup(default_location);
                if (dirc) {
                    location_folder = SDL_strdup(dirname(dirc));
                    SDL_free(dirc);
                    if (location_folder) {
                        save_file_new_named = (stat(location_folder, &statbuf) == 0) && S_ISDIR(statbuf.st_mode);
                    }
                }
            }

            if (save_file_existing || save_file_new_named) {
                char *basec = SDL_strdup(default_location);
                if (basec) {
                    location_name = SDL_strdup(basename(basec));
                    SDL_free(basec);
                }
            }
        }
        break;

    case SDL_FILEDIALOG_OPENFOLDER:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open Folder");
        open_folders = true;

Review comment (suggestion):
Should I be using `SDL_GetPathInfo` here? It's the same call under the hood.
`S_ISLNK` is not valid as `stat` follows symlinks.

Code after:

    SDL_Window *window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL);
    SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL);
    int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0);
    bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false);
    const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
    const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
    char *location_name = NULL;
    char *location_folder = NULL;
    struct stat statbuf;
    bool open_folders = false;
    bool save_file_existing = false;
    bool save_file_new_named = false;

    switch (type) {
    case SDL_FILEDIALOG_OPENFILE:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open File");
        break;

    case SDL_FILEDIALOG_SAVEFILE:
        method = "SaveFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Save File");
        if (default_location) {
            if (stat(default_location, &statbuf) == 0) {
                save_file_existing = S_ISREG(statbuf.st_mode);
            } else if (errno == ENOENT) {
                char *dirc = SDL_strdup(default_location);
                if (dirc) {
                    location_folder = SDL_strdup(dirname(dirc));
                    SDL_free(dirc);
                    if (location_folder) {
                        save_file_new_named = (stat(location_folder, &statbuf) == 0) && S_ISDIR(statbuf.st_mode);
                    }
                }
            }

            if (save_file_existing || save_file_new_named) {
                char *basec = SDL_strdup(default_location);
                if (basec) {
                    location_name = SDL_strdup(basename(basec));
                    SDL_free(basec);
                }
            }
        }
        break;

    case SDL_FILEDIALOG_OPENFOLDER:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open Folder");
        open_folders = true;