Search code examples
clinuxgnu-coreutils

What does initialize_main (&argc, &argv) do?


I 'm reading coreutils source code to learn programming under linux.

I find that in most of the programs like ls.c, cat.c, they invoke the macro function initialize_main() at the first few lines. So I looked into system.h to find the implementation:

/* Redirection and wildcarding when done by the utility itself.
Generally a noop, but used in particular for native VMS. */
#ifndef initialize_main
# define initialize_main(ac, av)
#endif

I don't understand why define such a macro and I don't understand the comment.


Solution

  • The first step in understanding the comment is to know what VMS is. So here's a link for that: http://en.wikipedia.org/wiki/OpenVMS

    The next step is to understand redirection and wildcarding. In Linux and other members of the unix family, a command like

    cat foo* > /tmp/foolist
    

    will call the main function of cat with argv containing the matches for foo*. The output file /tmp/foolist will already be open as stdout before main is entered.

    VMS doesn't do that. cat will find the unexpanded string "foo*" and the redirection operator > in its argv. So the utility itself (cat) must do the redirection (opening the output file) and wildcarding (replacing "foo*" with "foo1", "foo2", "foo3"). That's what initialize_main will do on VMS. On unix, it'll do nothing ("Generally a noop").