Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #98959

Re: [command line arguments] more elegant solution ??

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: [command line arguments] more elegant solution ??
Date 2016-12-12 00:55 +0000
Organization A noiseless patient Spider
Message-ID <o2ksfc$fvo$1@dont-email.me> (permalink)
References (1 earlier) <o2k5rm$t8h$1@dont-email.me> <871sxew6n8.fsf@bsb.me.uk> <o2ko86$jgi$1@dont-email.me> <o2kpc1$rbg$1@dont-email.me> <o2kq0g$34c$1@dont-email.me>

Show all headers | View raw


On 12/12/2016 00:13, Lew Pitcher wrote:
> On Sunday December 11 2016 19:02, in comp.lang.c, "Lew Pitcher"
> <lew.pitcher@digitalfreehold.ca> wrote:
>
>> On Sunday December 11 2016 18:43, in comp.lang.c, "BartC" <bc@freeuk.com>
>> wrote:

>>> If the 3rd parameter is *, and there happens to be one file in the
>>> directory with a numeric name, such as 9177, that will then appear as a
>>> valid argument. (You may have noticed a big thread in the python group
>>> about this.)
>>
>> Bart, as has been explained elsewhere, in more detail and in more polite
>> language, your statement above is at best misleading, and at worst untrue.
>>
>> 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 <stdio.h>
>>   #include <stdlib.h>
>>
>>   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
  ...

(How did you fix it so that you don't have to type "./" which drives me 
up the wall?)


> And to illustrate permenant disable of globbing:
>   19:08 $ bash -o noglob
> We start in a -o noglob shell.
>
>   19:08 $ ls | wc -l
>   158
>
> 158 files and directories (now includes the previously compiled Args program,
> but we will replace that with a fresh version)
>
>   19:08 $ cat Args.c
>   #include <stdio.h>
>   #include <stdlib.h>
>
>   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;
>   }
>
> Same bog-simple program to list passed-in arguments
>
>   19:08 $ cc -o Args -mtune=native -Wall -std=c99 -pedantic Args.c
>
> Same successful compile to C99 standards
>
>   19:08 $ Args *
>   argc == 2
>   argv[0] -> "Args"
>   argv[1] -> "*"
>
> Passed in argument *, got argument *
> This time, no need to temporarily disable globbing in the commandline.

>
> QED
>
>> I said "at best, misleading", because you neglect to mention that, in Unix,
>> globbing (expansion of wildcard parameters into filenames) is something done
>> by the shell, not by the C runtime and (usually) not by the C program. It
>> can be enabled or disabled on demand.

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'.

>> Please don't mislead the newbies.

I'm a newbie when it comes to Linux. I don't know how to turn off 
globbing except using 'bash -o noglob' like your example (and don't know 
how to turn it on again except by restarting the terminal), but these 
are the sort of extra instructions you don't want to give with your program.

The instructions are to run it using 'program arg1 arg2', not do X 
first, run it, then do Y. Especially for a very unlikely possibility.

 >> Bart, as has been explained elsewhere, in more detail and in more polite
 >> language, your statement above is at best misleading, and at worst 
untrue.
 >>

Is it?

At the moment, I /can/ actually run a program as:

   ../program test.netlist ???

where 'program' sees argv[1] and argv[2] as "test.netlist 123" when 
there is a single file "123" in the directory. It's not a concern for a 
student exercise, but if those parameters were critical, I would have to 
take the possibility seriously.

Your user may have neglected the need to turn off globbing. And in fact 
it is an unreasonable requirement.

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 07:56 -0800
  Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 16:47 +0000
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:16 -0800
      Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:26 -0800
        Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 19:05 +0000
          Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:15 -0800
            Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:53 -0800
              Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:03 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:26 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:33 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:33 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:35 -0800
                Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 16:06 -0500
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:11 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:20 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:25 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:32 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:44 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:47 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:56 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:04 -0800
                Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:07 +0100
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:15 -0800
                Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:32 +0100
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:42 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:52 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:40 -0800
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:55 -0800
                Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 19:44 -0500
                Re: [command line arguments] more elegant solution ?? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-11 23:20 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:28 -0800
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 16:28 -0800
      Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 18:57 +0000
  Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-11 10:04 -0800
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
  Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 18:29 +0000
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
    Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:30 +0000
      Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 23:43 +0000
        Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:02 -0500
          Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:13 -0500
            Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 00:55 +0000
              Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 01:02 +0000
                Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 19:02 -0800
                Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 11:50 +0000
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 13:46 +0000
                Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 14:43 +0000
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 22:09 +0000
                Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-12 08:58 -0800
              Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 20:06 -0500
              Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 17:37 -0800
              Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 18:59 -0800
        Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 02:47 +0000
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:11 -0800
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:25 -0800
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:27 -0800
    Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 01:01 -0800
      Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 09:42 -0800
        Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-12 13:58 -0500
        Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 14:18 -0800

csiph-web