Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: [command line arguments] more elegant solution ?? Date: Sun, 11 Dec 2016 18:59:32 -0800 Organization: None to speak of Lines: 100 Message-ID: References: <871sxew6n8.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="5ae493c5c0eddd0692ad00c7942bf15f"; logging-data="27278"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qAzHB2u0yLB621xFKIbwn" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:yBM/ghjrQE86JOIXtTmp3KyTM5k= sha1:w86GZqcVs9BFY+dHa89evyrWFo8= Xref: csiph.com comp.lang.c:98972 BartC writes: > On 12/12/2016 00:13, Lew Pitcher wrote: >> On Sunday December 11 2016 19:02, in comp.lang.c, "Lew Pitcher" >> wrote: [...] >>> Witness: > 18:55 $ ls | wc -l >>> 157 >>> >>> That's 157 files and directories in the current directory, including the >>> source code Args.c >>> >>> 18:55 $ cat Args.c >>> #include >>> #include >>> >>> int main(int argc, char *argv[]) >>> { >>> int argnum; >>> >>> printf("argc == %d\n",argc); >>> for (argnum = 0; argnum < argc; ++argnum) >>> printf("argv[%d] -> \"%s\"\n",argnum,argv[argnum]); >>> >>> return EXIT_SUCCESS; >>> } >>> >>> >>> Simple program, wot? Just prints argc, and each given argument >>> >>> 18:55 $ cc -o Args -mtune=native -Wall -std=c99 -pedantic Args.c >>> >>> Compiles clean to C99 standards >>> >>> 18:55 $ Args '*' >>> argc == 2 >>> argv[0] -> "Args" >>> argv[1] -> "*" >>> >>> Oops.... Passed * as argument, got * as argument. >> > I tried your program, and I got 125 arguments: > > ...$ gcc c.c -oc > ...$ ./c > argc == 125 > ... If you had run the program exactly as you show it, you would have gotten no arguments (argc==1). I'll bet you actually ran ./c * The * is expanded by the shell to a list of file names; there's nothing the program can do about it. That's just something you have to be aware of. (I won't argue about whether the behavior is a good idea.) > (How did you fix it so that you don't have to type "./" which drives me > up the wall?) You can do that by adding the current directory, ".", to $PATH -- but I strongly suggest *not* doing that. If you must, be sure to add it to the end of $PATH, not the beginning. Otherwise you run the risk of running arbitrary commands that happen to be in the current directory. (Imagine you're in a directory containing a script called "ls" that does something nasty.) I'm sorry that typing "./" drives you up the wall; my honest advice is to get used to it. [...] > I said it was a Linux quirk. And while it may be possible to disable it > (you said you did it temporarily, but didn't see how, unless you mean > what you called the 'permanent' disablement), I don't see that as an > answer. If you disable it, then a whole load of other things that depend > on it will stop working, such as 'ls'. I hadn't even realized you could disable globbing; I probably knew about it at some po, but I've never wanted to use it. My own approach, and the one I recommend, is simply to be aware that * expands to a list of file names, and to quote it if I want a literal * character. And of course to be aware of the other expansion rules. If you insist on disabling and reenabling globbing, if you're using bash you can do it with these commands: set -o noglob # disable globbing set +o noglob # re-enable globbing Again, I don't recommmend this (and I won't argue about the ugly syntax). [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"