How to read arguments from target [duplicate] - c++

This question already has answers here:
Reading command line parameters
(4 answers)
Closed 9 years ago.
When you right click on a program in windows(such as starcraft.exe) and look at its properties there is a text field called "target" which contains the full path of the binary. I have seen programs able to parse flags added to the target such as "C:\programfiles\myprogram\myprogram.exe -x 1280 -y 360" and the program would start up in the specified resolution. My question is how to read those arguments, if it is done by argv[] please just inform me of my stupidity.
C++ is the language, VS express 2012 desktop is the environment.

you receive those parameters when calling executable main method int main(int argc, char* argv[]) as argc (count) and argv[] parameters all you have to do is just parse them
here is an example How to parse command line parameters

You'll want to avoid comparisons between char* and string literals without strncmp.
Remember that argc is the the argument count (including the program name).
argv is an array of C strings specifying the arguments (first being the program name as invoked).
In this case, you're usually best off using a library, like getopt. This will make interleaved options, long options, and arguments much more sane to manage (assuming order between options and arguments is largely unimportant).

Related

How much memory is allocated for argv[]? [duplicate]

This question already has answers here:
where command line arguments are stored?
(4 answers)
Closed 6 years ago.
I know that the command line arguments are character arrays and that they are stored on the stack. But, I want to know actual memory allocation for of each argument. e.g. suppose I passed the directory name "/tmp" as a command line argument. This will be stored in argv[1]. But as I tested, it is allowed to change argv[1] to "/tmp/log/" (size increased) in the program. How is this possible ?
To answer your question, the total maximum size available to argument strings and the passed environment can be obtained with:
getconf ARG_MAX
from the command line or the syconf equivalent from C (see http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html for more information).
(On my Linux box, the limit is 2097152).
Your example happens to work because the arguments and the environment are realistically stored contiguously, so appending to a string will overwrite what comes after it (following arguments, or the environment).
And that's why it's a bad idea to try and expand the argv strings like that. If you want to modify them, either edit them or shrink them, but trying to expand them is a call for trouble.
On Linux, parameters are populated by create_elf_tables. For this specific platform at least, you are correct that the values are stored on the stack.
Linux only uses exactly as much memory as is necessary to store arguments and (initial) environment variables on the stack; if you try to use more than what is already there, you're overwriting something else (or crashing).
The standard states that the argv can be modified since it is a special internal.
177 — The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination, so it is allocated only what you need at the assignment or replacement.
Standard text:
http://c0x.coding-guidelines.com/5.1.2.2.1.html

How to create files given relative path [duplicate]

This question already has answers here:
Creating files in C++
(8 answers)
Closed 8 years ago.
I have a path given in command line with a filename in it like:
./something.bin infomration /some/some2/thing.txt
I know I can put the /some/some2/thing.txt into a global variable and use it. Now, I want to:
create a file named thing.txt in the path /some/some2/
For things like this you'll have to use the parameters passed to your main entry point:
int main(int argc, char **argv) {
// some other code
return 0;
}
As you can see, main() will receive two parameters:
argc: The first parameter will include the number of parameters passed through the second parameter.
argv: The second parameter points to pointers that point to strings with the actual parameters.
The usage is quite trivial, but it's important to remember that the very first "argument" is the executable file itself.
In your example, argc would be set to 3 and argv would point to the following strings:
argv[0]: ./something.bin (based on the OS, this could also be an absolute path)
argv[1]: infomration [sic]
argv[2]: /some/some2/thing.txt
As you can see, all you'll have to do is the following:
Verify the number of arguments is correct.
Verify the second argument (infomration) is correct.
Use the third argument to receive the actual file name.
Keep in mind that the user might pass invalid parameters (e.g. invalid characters for a file name) or he might try to trick you into doing something you shouldn't do (like passing reserved names).

What does the dash mean in windows command line syntax?

I know basic c++ coding and command line syntax. Often, the main() function looks like this:
int main(int argc, char *argc[])
When I execute my program via command line, I just do this:
cd c:\path\to\foo 1
Where 1 is the argument. And of course, arc would be equal to '2' and the '1' would be at element '1' of the array argc (as opposed to 0).
I've seen the dash used in a lot of places. Specifically:
gcc -v
And if I type just 'gcc', it says there are no arguments. And if I type 'gcc v' I get "error: no such file or directory". But when I take a look at the minGW bin folder where gcc is, there is no folder 'v'.
This is just a style of setting options in programs. It comes from the getopt library provided in Unix/Linux etc. The -- form that kc7zax mentions in their answer is the long-form of these options (allowing a long identifier instead of a single character).
There's nothing magic about these. They are simply parsed out of the argv array. You can implement similar or identical functionality yourself if you want. But it's a pain. That's why libraries exist.
It is simply a standard for identifying the name of a command line parameter from the parameter value that may exist. e.g. gcc -o myfile.o
The windows world usually sees a - or a / as the marker. I've often encountered a -- in the unix/bsd world as well.

GetCommandLine linux *true* equivalent

Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv
Is it possible to get the raw command line in linux? The file /proc/self/cmdline is destroyd.
./a.out files="file 1","file 2" param="2"
prints
./a.outfiles=file 1,file 2param=2
which is junk
Escaping command line does work for all arguments but the first.
./a.out files=\"fil 1\",\"fil 2\"\ param=\"2\"
prints
./a.outfiles="fil1","fil2" param="2"
You can't do that. The command line arguments are actually passed to the new process as individual strings. See the linux kernel source:
kernel_execve
Note that kernel_execve(...) takes a const char *argv[] - so there is no such thing as a long string commandline in Linux - it's the layer above that needs to split the arguments into separate components.
Edit: actually, the system call is here:
excve system call
But the statement above still applies. The parameter for argv is already split by the time the kernel gets it from the C-library call to exec.
It is the responsibility of the "starter of the program" (typically a shell, but doesn't have to be) to produce the argv[] array. It will do the "globbing" (expansion of wildcard filenames to the actual files that it matches) and stripping of quotations, variable replacement and so on.
I would also point out that although there are several variants of "exec" in the C library, there is only one way into the kernel. All variants end up in the execve system call that I linked to above. The other variants are simply because the caller may not fancy splitting arguments into invdividual elements, so the C library "helps out" by doing that for the programmer. Similarly for passing an environment array to the new program - if the programmer don't need specific environment, he/she can just call the variant that automatically take the parent process env.

int main(int argc, char** argv) [duplicate]

This question already has answers here:
Closed 11 years ago.
Duplicate:
What is the proper declaration of main?
What do we mean by the arguments in this main function? What are they trying to tell us?
int main(int argc, char** argv)
UPDATE: And, is the preceding line of code similar to this int main(int argc, char* argv[])? If so, how can we say that char** argv is similar to char* argv[] as they don't look similar from an array point of view?
How is it compared with int main() which does not have any arguments?
Thanks.
The argc parameter is the number of command line options specified, including the executable name, when the executable was invoked. The individual command line options are found in the argv array, which is NULL terminated (the name and path used to invoke the executable is in argv[0]).
The difference between the two versions is simply if you want to parse command line arguments or not - if you are not interested in them then you can ignore them using the second form.
Wikipedia offers a good explanation. The first parameter gives you the number of command line arguments, and the second gives you the actual arguments.
They represent the command line parameters.
argc is the number of command line parameters, including the name of the executable.
argv is an array of null-terminated strings, where argv[0] is the command line parameter, and argv[i] is the ith parameter after that, argv[argc-1] being the last one and argv[argc] is actually well defined and a NULL pointer.
Thus:
foo bar baz
on the command line will have argc=3, argv[0]="foo" argv[1]="bar" argv[2]="baz" argv[3] = NULL
Note that there is no special attachment placed for "flag" arguments.
grep -i foo bar.cpp bar.h
would have 4 arguments (argc=5 including grep itself), -i being one of them and this would apply even if the next parameter was a "value" attached to the flag.
Note if you did a wildcard
grep -i foo *
in UNIX at least, the * would be expanded before the call into grep and thus each file that matched would be an argument.
Incidentally
char** argv and char* argv[]
do the same thing.
Also while the standard says you must use one of these signatures (you shouldn't even add in any consts) there is no law you have to use those two variable names, but it is so conventional now that they are pretty much universal. (i.e. you could use argCount and argValues if you want).
argc gives you the number of arguments and argv gives you those arguments. The first one is the path to the .exe used to run your program, the following ones are arguments the caller of your .exe provided on the command line like this:
my.exe arg1 arg2
Whereas
int main() {}
just ignores the arguments.
argv is an array holding the commandline parameters passed to the application. argc tells you the number of elements contained in that array.