BartC said:
I was using Windows, and the problem manifested itself under Windows. I
assumed it was feature of the C runtime, and that it would be the same under
Linux. It turns out it *was* done by the C runtime in this case.
So a program compiled on and for Windows with gcc (MinGW?) implicitly
invokes some C runtime routine that expand wildcards that appear
on the command line?
That's consistent with what I'm seeing. On Windows 7, a C program
compiled with MinGW and invoked with *.c on the command line sees a
list of file names as separate elements of the argv array; a similar
program compiled with MS Visual C++ 2010 sees a single argument,
the string "*.c".
The difference in behavior does seem a little odd. An alternative
would have been for MinGW to fit in with the usual Windows behavior,
where individual programs are responsible for expanding (or not
expanding) wildcards. I think the idea behind MinGW is to provide
a minimal POSIX-like environment under Windows, not to provide a
gcc compiler integrated into the Windows environment. (There are
likely options to make it do the latter; I haven't looked into it.)
But I've also been using the C runtime for years in the background of other
projects, and I'd never seen it behave that way. So it was specific to gcc
which I've switched to recently.
The C runtime library is not part of gcc; gcc is just the compiler
(it can also invoke the linker). Apparently the behavior you're
seeing is the result of the way the MinGW developers have chosen
to integrate the gcc compiler, the Microsoft C runtime library,
and whatever other components they've added.
Since you seem to be happier with the Windows behavior (where
programs are responsible for their own wildcard handling) than
with the POSIX behavior (where wildcards are expanded before main()
starts running), why are you using MinGW in the first place?
If you want to write programs that run on POSIX systems (Linux,
Unix, Cygwin, etc.), I advise you *not* to try to override the way
wildcards are expanded. Even if you managed to do it, the resulting
behavior would be extremely counterintuitive to your users, who are
likely to be as uncomfortable with wildcards *not* being pre-expanded
as you are with them being pre-expanded. If I have a copy of your
program and I need to pass the string "*.c" to it as an argument,
I know exactly how to do it. Think about what behavior your users
are going to expect, not just what you personally prefer.