getopt does not order arguments in Windows - c

It seems that getopt behaves differently in Windows than in Linux. Windows requires a strict ordering of parameters while in Linux I can put the arguments in any order. Consider a program test which uses getopt compiled for Linux (gcc) and Windows (MinGW) and take this command line for example:
test file1.bin file2.bin -o output.txt
Executing this command in Linux would correctly parse -o output.txt as an option(+parameter) regardless of where I put -o output.txt. It could be between file1.bin and file2.bin as well and getopt parsing would still work correctly since it sorts the arguments by putting the optional ones infront of the mandatories.
Executing this command in Windows however results in incorrect parsing, giving me a wrong index in optind variable. It seems that the exact same code when compiled in Windows does not do the sorting part for me. Why is this, can we workaround this?

Transferring my comments to an answer as requested.
GNU getopt() permutes the arguments (so options can occur after non-option arguments) by default. Standard POSIX getopt() does not allow that. You can make GNU getopt() conform to POSIX by exporting the environment variable POSIXLY_CORRECT=1, or by starting the options argument with a + symbol.
Check the manual (or source) for MinGW getopt(), or the Microsoft implementation of it. Given what you see, it probably doesn't do the permutation. However, the linked source code does support permutation — you'll need to investigate what goes on there.
If you decide to use GNU getopt(), you'll need to get a copy of the GNU getopt() source code (maybe from GitHub getopt.c) and include it in your build process. If you use that, you'll also need ansidecl.h and getopt.h at minimum and you will need to tweak the configuration appropriately.
Or you'll have to decide that the GNU extension of permuting options isn't portable and therefore shouldn't be used at all.

Related

Is there a solid way in C to parse optional command line arguments where optional flags are words rather than characters? [duplicate]

This question already has answers here:
Parsing command-line arguments in C
(14 answers)
Closed 1 year ago.
I need to parse command options as given below in C:
./myBinary --option1 name --option2 age --option3 address
Getopt only support -l , -a kind of flags. Any suggestions?
The only solid way is parsing those arguments without depending on external libraries. There is no widely accepted standard for handling command line arguments.
The best you have is the standardised call to main providing access to command line arguments:
int main(int argc, char *argv[]) {
}
There are major differences between operating systems, but it's worse than that.
Windows tends to use /option or -option, but many tools borrow from other platforms.
Linux tends to go the GNU way, offering both --option and -o via getopt_long.
BSD and Unix tend to use only the short -o, but some applications may be ported from Linux.
Many macOS tools use -option but borrows from BSD and sometimes Linux.
So only GNU provides a "standard" library for parsing such options. Consider it a non-portable extension.
Long story short: you need to provide more details about your target platform(s) to get a better answer, but don't expect some perfect solution.

Examining a C source code easily

In Java, just by clicking the classes in Eclipse, I can go to that specific class being referenced. In C, how can I do that? As far as I can remember I wasn't able to do that in Eclipse. I had hard times manually finding where externs are declared/defined etc.
use
ctags
This is command line tool. You need to first create tags for your whole source code and then you can jump to defination of any function or variable.
In eclipce IDE
You can go its defination by using F3 and come back using Alt + <-
If you do not want to use F3 then press Ctrl and move curcer to that place and click on that...you will go to its defination.
How to go to a certain place in the source code is IDE-specific and not related to the programming language, so your question doesn't make any sense.
Many C compilers support Eclipse.
In Eclipse, you do what you are asking for by placing the cursor on the item you are interested in, then press F3.
In addition of the other replies (notably mentionning ctags), and if using a recent GCC compiler (i.e. gcc, g++, preferably version 4.8 or 4.9, etc....) you could use the MELT plugin and DSL.
MELT enables you to work on the internal representations (such as Gimple) of the GCC compiler.
In particular, MELT has powerful pattern matching facilities, and a command line interface to find particular Gimple patterns. So for example you could, with a few command line arguments to gcc, find all the calls to malloc with a constant argument bigger than 30 bytes. This requires working on the compiler internal representations (e.g. because of sizeof operators) and is not possible in a purely textual tool.
For finding occurrences of identifiers, you could use grep or also the ack perl tool.
The tools mentioned here (ctags, grep, MELT, ack) are command line tools. It is up to you to configure or adapt your IDE (or editor like emacs) and/or your builder (like make) to invoke them.
Remember that compilers are command-line tools, at least on Linux
PS. I am the main author of MELT.

Check GCC version in runtime

I need to find out the available (installed in the system) GCC version (Major and minor) inside the execution of a c program (in runtime). Meaning, programatically extract the version of the available gcc (same as if I was in a shell and typed gcc --version, but in a c program).
The __GNUC__ and __GNUC_MINOR__ are only useful in compile time and I've found the gnu_get_libc_version() function from gnu/libc_version.h, but it only gets me the libc version and I need the GCC version. If there is something similar for GCC it would be great...
I would really like to avoid calling a shell command to do this.
There is a simple way:
$ gcc -dumpversion
4.6
Invoke the gcc shell command with the parameter --version; it's the correct way to do this. See popen() to do that.
Or you can invoke GCC with to compile a program which prints the values for __GNUC__ and __GNUC_MINOR__. But that will not work if the GCC in question is configured for cross compilaton.
Alternatives would be to search the binary for version strings and hoping that you get the right one, that the format doesn't change and that the version string is distinct enough for you to recognize it with enough confidence.
In 1.5 words: Don't.
I need to find out the available (installed in the system) GCC version (Major and minor)
What are you going to do with the information?
You can't get a meaningful answer to your question, because
The user may not have any GCC installed in /usr/bin
May have 5 different versions installed elsewhere on the system
May have a version in /usr/bin which pretentds to be gcc-X.Y, but is actually gcc-Z.W, or Clang, or icc, etc.
But if you insist on getting a meaningless answer, popen("gcc --version") and parse the resulting output.

A Windows C compiler that doesn't split arguments in its runtime libraries?

I have heard that in Windows, parameters are passed a single parameter, and then the program splits it into arguments, either in its runtime libraries, or sometimes, in the actual code.
I've heard that most C/C++ compilers do it in runtime libararies (for example, TCC - Tiny C Compiler, which I downloaded)
Are there any C compilers I can download, that don't? Any links to them?
And in such a compiler, would argsv[0] have the whole string?
Added
It's based on what this person (jdedb) said in Super User question Can't pipe or redirect Cygwin grep output, after seeming to suggest that I ask on Stack Overflow.
"It's up to the called program to split the command tail into words, if it wants to operate in Unix (and C language) fashion. (The runtime support libraries of most C and C++ language implementations for Win32 do this splitting behind the scenes."
He said it's the compilers.. But according to Necrolis, it's not the compiler.
(added- Necrolis commented correcting my misreading, compiler!=runtime library)
If you are on Windows, just use GetCommandLine. This is how most CRT wrappers get the command line to split to start with.
As for your actual question, it's not the compiler, but the CRT startup wrapper that they use. If you implement mainCRTstartup, and override the entrypoint with it, you can do whatever you want. A good example of how it works can be seen here.
That "parameter splitting" is the way mandated by the C99 Standard (PDF file) in 5.1.2.2.1.
If an implementation (compiler + library + options) recognizes but does not separate the program name from the other parameters (and parameters from each other) it is not conforming.
Of course, if you use a free-standing implementation none of this applies.

Mandatory options with getopt_long() in C

With C/ C++, getopt_long() can be used to parse command line arguments. Is it possible to tell the function that some of the options are mandatory? For example, how can I tell getopt_long that the parsing should give error if startServer is called without mentioning the port below?
./startServer -port 80
Note: I am not talking of option arguments as mandatory but rather the options themselves.
getopt_long() is not part of the C language. It is a GNU invention which is available in some C implementations, but far from all.
The version of getopt_long() in glibc (used in most Linux distributions) does not allow you to specify that an option is mandatory. You'll have to check them explicitly after you're done parsing the command line.

Resources