Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOS stat() pathname separator inconsistency #1345

Open
dajhorn opened this issue Oct 29, 2024 · 6 comments
Open

DOS stat() pathname separator inconsistency #1345

dajhorn opened this issue Oct 29, 2024 · 6 comments

Comments

@dajhorn
Copy link

dajhorn commented Oct 29, 2024

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:

  • Trailing forward-slash pathname separators are folded and stat() behaves like posix.
  • Trailing backward-slash pathname separators return errno=1 (ENOENT: No such file or directory).

command_000

  • Except when the pathname resolves to a drive root. (X:\ is recognized as a corner case.)

command_001
command_002
command_003
command_004

  • If the stat() parameter is a file, then both slashes are folded similarly.

command_005

Expected stat() behavior:

  • The DOS runtime handles both pathname separator characters similarly.
  • Trailing backslashes are folded, especially in pathnames that are directories.
@jmalak
Copy link
Member

jmalak commented Oct 30, 2024

What DOS version you are using?

@dajhorn
Copy link
Author

dajhorn commented Oct 30, 2024

This happens in:

  • MS-DOS 6.22
  • FreeDOS T2410
  • DOSBox-X 2024.10.01

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 jmalak added bug CRTL C run-time library labels Oct 30, 2024
@jmalak
Copy link
Member

jmalak commented Oct 30, 2024

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

@dajhorn
Copy link
Author

dajhorn commented Oct 30, 2024

BTW, should it matter...

The __WATCOM_LFN__ macro significantly changes stat() behavior:

command_007

@jmalak
Copy link
Member

jmalak commented Oct 30, 2024

There is issue with trailing slash.

@jmalak
Copy link
Member

jmalak commented Oct 30, 2024

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).

@jmalak jmalak removed bug CRTL C run-time library labels Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants