3

How can I run a C program from command line so that argc will be 0 ?

I know this is possible in C with execve("./a.out", (char**){NULL}, NULL); but I'd like it from command line (bash or zsh).

Is there some built-in feature that can achieve this ?

Bilow
  • 2,194
  • 1
  • 19
  • 34
  • 1
    just typing `./a.out` won't do what you want? – Stefan Hegny Nov 21 '16 at 11:02
  • @Stefan no, by default the shell passes one argument that represents the program name. Examples [here](http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi) and [here](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean) – Bilow Nov 21 '16 at 11:07
  • 3
    You are able to specify the command name using `exec -a NAME program`, where `NAME` can be an empty string but I don't think that it is possible to omit it completely when starting programs with `bash` - unless you write a program for that. Relying on the feature is fragile, as you see. What's the problem with having the command name in the options? – hek2mgl Nov 21 '16 at 11:24
  • Why do people have downvoted me ? – Bilow Nov 21 '16 at 20:41
  • @hek2mgl `exec -a` does not accept empty strings. But it gets closer to what I wanted to do, thank you. The purpose is pedagogical and for tests - I want to show other students they have to check everything, even argc, otherwise their program could not be portable or even crash. – Bilow Nov 21 '16 at 20:54
  • `exec -a '' sleep 100` "works" for me on Linux. – hek2mgl Nov 22 '16 at 06:48
  • @hek2mgl Right, just tested on bash it works, but on zsh I've got non-ascii characters in argv[0] (with `argc == 1`), like 0x9B and 0x9C depending on if I use single quotes or oduble quotes tu represent an empty string. Any idea why ? – Bilow Nov 22 '16 at 10:04
  • Hmmm. I don't know. `ps` displays those chars as `??` – hek2mgl Nov 22 '16 at 15:20
  • 1
    @hek2mgl The non-ascii bytes in argv[0] were a bug of zsh, they fixed it after I filed it. The commit is [here](https://github.com/zsh-users/zsh/commit/c9df6bc8d46415e270273777c80025948762e897). I'm wondering what command you used to obtain `??` ? – Bilow Sep 10 '17 at 19:51
  • Thanks for filing the bug and letting me know! – hek2mgl Sep 11 '17 at 01:15

0 Answers0