10

I have an application built with the MinGW C++ compiler that works something like grep - acommand looks something like this:

myapp -e '.*' *.txt

where the thing that comes after the -e switch is a regex, and the thing after that is file name pattern. It seems that MinGW automatically expands (globs in UNIX terms) the command line so my regex gets mangled. I can turn this behaviour off, I discovered, by setting the global variable _CRT_glob to zero. This will be fine for bash and other sensible shell users, as the shell will expand the file pattern. For MS cmd.exe users however, it looks like I will have to expand the file pattern myself.

So my question - does anyone know of a globbing library (or facility in MinGW) to do partial command line expansion? I'm aware of the _setargv feature of the Windows CRT, but that expands the full command line. Please note I've seen this question, but it really does not address partial expansion.

I've ended up using conditional compilation to write my own globbing code for the Windows version of my app. This was pretty easy as I have my own CommandLine class which encapsulates argc and argv from main(). Still, I'd be interested to hear of other solutions.

Community
  • 1
  • 1
  • 1
    Strange, putting quotes around your regex should prevent the globbing behavior. – Brian Neal Jun 01 '10 at 15:22
  • 1
    @Brian It prevents globbing by the shell (say bash), but bash removes the quotes and passes it to my app which then does another round of globbing. –  Jun 01 '10 at 15:24
  • 1
    I guess I don't follow then. If your app gets `.*` in argc/argv then what is the problem? – Brian Neal Jun 01 '10 at 17:54
  • @Brian the app gets .* which it automatically expands as a filespec, before main() is entered, so my own code doesn't see .* - it sees a list of filenames. –  Jun 01 '10 at 18:10

2 Answers2

2

<glob.h> has glob and globfree and lots of flags for glob.

nategoose
  • 12,054
  • 27
  • 42
0

I'm not sure if I fully understand your problem here, but in Windows you should be able to glob using FindFirstFile / FindNextFile functions from the WIN32 API. Honestly I don't know if their globing capability is comparable to glob() but you could give them a try

asr
  • 220
  • 2
  • 9