Question

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.

Was it helpful?

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").

OTHER TIPS

This is left over from times gone by. OpenVMS is an operating system which roughly competed with Unix in the past. There is still a fair amount of OpenVMS running in the world, but HP have dropped support for it and it will be going away in the next 10-15 years.

Anyway, this function is used on OpenVMS to allow stdout and stderr redirection on VMS.

Since cat foo.txt > stuff.txt on Unix, the cat command only sees one argument foo.txt, but on VMS, which knows nothing of the > symbol, the cat command sees 3 arguments.

The code inside initialize_main on VMS, allows the basic unix style commands to support output redirection, such as ls and

OpenVMS later added a command called pipe which allows redirection to work via any command.

You can view the source code for initialize_main on VMS here: Link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top