open-watcom / open-watcom-v2

Open Watcom V2.0 - Source code repository, Wiki, Latest Binary build, Archived builds including all installers for download.
Other
981 stars 160 forks source link

DOS stat() pathname separator inconsistency #1345

Open dajhorn opened 2 weeks ago

dajhorn commented 2 weeks ago

The DOS runtime recognizes both kinds of slashes as pathname separators, but handles them differently.

Consider this test program:

// dirstat.c
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <path>\n", argv[0]);
        return EXIT_FAILURE;
    }

    struct stat st;
    if (stat(argv[1], &st) == 0) {
        if (S_ISDIR(st.st_mode)) {
            printf("%s is a directory.\n", argv[1]);
        } else {
            printf("%s is not a directory.\n", argv[1]);
        }
    } else {
        perror("stat");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Compile it with:

wpp386 -bt=dos dirstat.c
wlink system dos32a file dirstat.obj

Observed stat() behavior:

command_000

command_001 command_002 command_003 command_004

command_005

Expected stat() behavior:

jmalak commented 1 week ago

What DOS version you are using?

dajhorn commented 1 week ago

This happens in:

MS-DOS and FreeDOS have different / tail folding than DOSBox-X, so at least some of this stat() behavior is platform dependent.

issue1345.zip is a floppy disk image containing the dirstat.exe test program.

jmalak commented 1 week ago

Thanks for your bug report. I reproduced it. It will be corrected ASAP.

dajhorn commented 1 week ago

BTW, should it matter...

The __WATCOM_LFN__ macro significantly changes stat() behavior:

command_007

jmalak commented 1 week ago

There is issue with trailing slash.

jmalak commented 1 week ago

It looks like it is correct behaviour, because appropriate DOS function to access directory fails due to trailing slash. You need to use path\last or path\last\. form. Windows FindFirstFile has same behaviour too. Your application is responsible to use correct format.

Anyway there was small mistake which caused strange behaviour for forward/backward slash on the end . Now stat always report problem if there is trailing slash (forward or backward).