I recently answered a question about “can FreeDOS use spaces in filenames?” The short answer is yes, if the spaces are between other characters in the filename. I gave an example that you can have a file named MY⎵FILE.TXT with the space in the middle, but not MYFILE⎵.TXT where the space is after the base name in the filename. (In this example, I’ve typed each space as ⎵ so you can see it.) Let’s explore why DOS filenames have this limitation with spaces.
8.3 filenames
DOS saves files using a File Allocation Table where filenames are stored in a particular way: the first two fields are the filename and extension. The filename is always stored as 8 characters, and the extension is always 3 characters.
Each field is padded with spaces. That means if you save a file as FILE.C, that’s 4 characters for the filename part and 1 character for the extension. Behind the scenes, DOS saves this as an “8.3” filename, with extra character padded as spaces: FILE⎵⎵⎵⎵.C⎵⎵.
A test program
Let’s create a short test program that saves a new file using a space at the end of the filename part. In this case, we’ll write a text file called BB⎵.TXT (there’s a space after the BB and before the .TXT) that saves just as single line of text.
#include <stdio.h>
int main()
{
FILE *f;
f = fopen("BB .TMP", "w");
if (f==NULL) {
puts("error, cannot open file");
return 1;
}
fputs("Hello world\n", f);
fclose(f);
puts("Ok");
return 0;
}
Just to explain a few things here: the fopen statement opens a file called BB⎵.TMP for writing. If this file doesn’t exist, the program will create it automatically. Then it saves the text Hello world to the file, and closes it before the program ends.
You can compile this program using any C compiler. On FreeDOS, we include several C compiler in the FreeDOS distribution, including Open Watcom C, IA-16 GCC, and BCC; these are all in the “Development” package group on the BonusCD. Let’s save this test program as bb.c and compile it using Open Watcom C:
D:\SRC>wcl -q bb.c
DOS/4GW Protected Mode Run-time Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
DOS/4GW Protected Mode Run-time Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
The -q command line option makes Open Watcom run in quiet mode, so it doesn’t print any extra text. The other output you see isn’t actually from the C compiler, but from the DOS/4GW program, which is a DOS “extender” that Open Watcom uses. If you don’t want this extra output, set the DOS4G environment variable to quiet and DOS/4GW won’t print anything. Let’s compile with Open Watcom C one more time, with DOS/4GW running in “quiet” mode, but without Open Watcom’s -q command line option, to show the normal compiler output:
D:\SRC>set DOS4G=quiet
D:\SRC>wcl bb.c
Open Watcom C/C++16 Compile and Link Utility Version 1.9
Portions Copyright (c) 1988-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
wcc BB.C
Open Watcom C16 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
BB.C: 18 lines, included 771, 0 warnings, 0 errors
Code size: 59
wlink @__wcl__.lnk
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
loading object files
searching libraries
creating a DOS executable
That’s a lot of extra text. You can see why I usually compile using the -q (quiet) command line option.
Spaces in filenames
After compiling the test program, let’s run it to create a new BB⎵.TMP file:
D:\SRC>bb.exe
Ok
The program prints an Ok message to indicate it was able to save a new file called BB⎵.TMP. But if we look for the new file, we’ll see that it doesn’t have the space after the BB filename part:
D:\SRC>dir /b *.tmp
BB.TMP
This is because the DOS filesystem pads out the 8-character filename with spaces, so BB⎵.TMP actually gets saved as BB⎵⎵⎵⎵⎵⎵.TMP (that’s BB plus 6 spaces, then the TMP extension). In fact, saving a file like BB.TMP would also get stored the same way: BB plus 6 spaces, then the extension.