Sunday 20 November 2011

Should programs always assume that command-line parameters can be used? in C programming

Should programs always assume that command-line parameters can be used?

These days, you can usually assume that the command-line parameters can be used by your program. Before DOS 2.0, the command-line information stored in the PSP was slightly different (it didn’t strip input and output redirection data from the command line). In addition, the data pointed to by argv[0] did not reliably contain the executable’s pathname until DOS 2.0. The DOS interrupt 62 that retrieves the PSP segment wasn’t available (or at least documented) until DOS 3.0. You therefore can at least assume that you can get consistent command-line information on PCs running DOS 3.0 or newer.

After you have determined that you are running DOS 3.0 or greater, you can basically do whatever you want
to the command-line data, because the information is placed on the stack for you to play with (via argv). Of course, normal data manipulation rules apply to command-line data as they do to all data arriving on the stack. The real problems arise when you don’t have argv provided by your compiler. For example, you could write your program in assembly language, or in some archaic compiler that does not provide argc and argv. In these cases, you will have to find some method of retrieving the command-line information yourself. That is where the DOS interrupt 62 comes in handy. If you use DOS interrupt 62 to retrieve a pointer to the command line, you must be aware that you are pointing to data that is used by DOS and for DOS. Although the data is there for you to see, you should not assume that the data is there for you to alter. If you need to use the command-line information to make decisions at various times throughout your program, you should copy the data into a local buffer before actually using the data. This technique enables you to have complete control of the data without worrying about stepping on DOS’s toes. In fact, this applies also to C programs that supply argv. It is not uncommon

for a function outside main() to need access to the command-line data. For it to get access to the data, your main() must save it globally, or pass it (once again) on the stack to the function that needs it. It is therefore good programming practice to save the command-line information into a local buffer if you intend to use it.

Cross Reference:

XX.1: How are command-line parameters obtained?


No comments:

Post a Comment