what does it take to make my program accept command-line arguments with spaces?
Yet-another EDIT: I have just recognized that the program is started from a shell-script that sets up the environment for the execution of the program. As there are some external libraries, LD_LIBRARY_PATH is set to the current working directory:
#!/bin/sh
ARCH=`uname -m`
export LD_LIBRARY_PATH=".:lib/magic/linux-${ARCH}"
./data_sniffer.bin $*
exit $?
The issue is definitely related to $*. Is there any solution to correctly forward the command-line parameters?
Here is a code-snippet from main():
if (stat(argv[1], &lStat) != 0)
{
fprintf(stderr, "Cannot stat(2) given file: %s. \n", argv[1]);
return EXIT_FAILURE;
}
I am starting my program with the following parameters:
./data_sniffer /mnt/pod/movies/some\ test\ movie\ file.avi
The resulting error message looks like this:
Cannot stat(2) given file: /mnt/pod/movies/some.
Anyone an idea what’s wrong here? I think that I am not the first one with this problem (though, I could not find a related question here).
Replace the use of
./data_sniffer.bin $*with./data_sniffer.bin "$@"in your wrapper script and the arguments should be forwarded in a correct manner.More regarding the difference between
$*,$@and"$@"can be found here.Regarding
argv..It doesn’t require much from you as a developer, I’m tempted (and it’s much more truthful) to say that it requires nothing of you.
Arguments passed to the application is handled by the shell executing the binary, doing this below should definitely work the way you want it to (even though I find it odd that you are claiming that the current shell doesn’t handle
'\ 'correctely):My recommendation(s)
Have you tried doing
printf ("argv[1]: %s\n", argv[1]);in the beginning of main to validate the contents of it?Are you sure that you are invoking the correct binary with the correct command-line arguments?
Sadly the only reasonable thing to write is that you are doing something wrong. We are however unable to answer what without further information regarding the issue.
I find it very hard to believe that there is a bug in your shell, even though that is of course possible – I doubt it.
Parsing the command-line into
argvisn’t something that you as a developer should worry about, there are no enforced functionality that you have to implement for the binary itself to handle spaces in it’s argument(s).