0

for example if the user type in the command line

match "hello" test*in

it means find the "the" in all .in type of files with name "test".

bool containMany=false;
int i;
for(i= 0; arguments[i]; i++){

    printf("ARGUMENTS  %s\n",arguments[i]);
    if(strstr(arguments[i],"*")){
        containMany=true;
        break;
    }
}

but it does not work. The arguments are:

ARGUMENT match

ARGUMENT hello

ARGUMENT test0.in

ARGUMENT test1.in

ARGUMENT test2.in

So how I make containMany become true when the user type "*" ?

Gavin Z.
  • 423
  • 2
  • 6
  • 16

2 Answers2

2

That's because your shell automatically makes glob-style substitutions. If you don't want shell to do that enclose the input arguments in single quotes or use backslash.

If your working dir has files A and B then this expression:

test *

would be replaced with

test A B

before test gets executed.

If, on the other hand you run like this:

test '*'

or like this:

test \*

then there will be no shell substitution, and your program will actually see the star.

Michael
  • 5,775
  • 2
  • 34
  • 53
  • what can I do if using '*' or \\* is not allowed? – Gavin Z. Feb 09 '14 at 23:12
  • @GavinZ. Switch to using a libc that *doesn't* do glob expansion (e.g. MSVC, non-mingw gcc?). Then realize it's just a hack, as "proper shells" will do glob expansion *anyway* .. – user2864740 Feb 09 '14 at 23:21
  • @GavinZ., not allowed by what or by whom? It's fairly common to use quotes or backslash; for example, "find -name " is almost always used with quotes or backslash in the . If you run your program from shell, or by using commands such as "system", then glob will happen anyway. If you are executing from another program, they you can use "exec" or "spawn", which won't do substitution, although, as user2864740, that may not be desirable. – Michael Feb 09 '14 at 23:39
  • @Michael - I think he means "what can I do if using '*' or \* doesn't work?". Apparently he is using MinGW on Windows. – Michael Burr Feb 09 '14 at 23:41
  • @Michael: well, i'm sorry to say that it's not allowed by the assignment requirements... – Gavin Z. Feb 09 '14 at 23:49
0

Traditionally the shell performs wildcard expansion on the command line - that's the way it's done on Unix-style platforms. If no expansion is desired, the user must typically quote the argument that contains the wildcard character or escape the wildard character.

On Windows, the command.com/cmd.exe shell do not perform filename expansion. However:

  • the MinGW toochain's runtime does perform expansion of filename wildcards. To disable this behavior, link in CRT_noglob.o or add the zero-initialized global variable int _CRT_glob = 0; to one of your .c files to disable the runtime's glob expansion.
  • MSVC includes an object file, setargv.obj that will cause the runtime to expand the command line filename wildcards, if that's the behavior you want.
Michael Burr
  • 333,147
  • 50
  • 533
  • 760