Referring to the Command-Line Tail

ID: Q60869


The information in this article applies to:


SUMMARY

The following code example demonstrates how an application can explicitly reference the command-line tail. The example accesses the Disk Transfer Area (DTA) to refer to the entire command line with spaces intact and prints the command line as one string.

In this method, the command line is limited to 128 bytes. If this code example is compiled as CMDLINE.EXE and invoked with the following command line


   cmdline *.c  abc  def  lab7.pas 
the program produces the following output:

   tail_length = 24
   cmd_tail = < *.c  abc  def  lab7.pas> 
NOTE: A more portable method to retrieve this information is to use the argv and argc parameters for the main function. The output from this method may be easier to use because setargv() function partially parses the command line.


MORE INFORMATION

Sample Code


/*
 * Compile options needed: none
 */ 

#include <stdio.h>
#include <dos.h>

main()
{
   int tail_length;
   char cmd_tail[128];
   char far *p;             /* far pointer */ 
   int i;

   struct SREGS Seg;
   union REGS Reg;

   Reg.h.ah = 0x2F;         /* DOS call:  Get DTA Address   */ 
   segread(&Seg);
   intdosx(&Reg, &Reg, &Seg);

   FP_SEG(p) = Seg.es;      /* make p point to the DTA */ 
   FP_OFF(p) = Reg.x.bx;

   tail_length = *p;        /* First byte is length of tail string */ 

   printf("tail_length = %d\n", tail_length);

   p++;                     /* Move to first byte */ 

   for(i = 0; i < tail_length; i++)
      cmd_tail[i] = p[i];

   cmd_tail[tail_length] = '\0';  /* Add NULL to make a string */ 
   printf("cmd_tail = <%s>\n", cmd_tail);

   return(0);
} 

Additional query words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 27, 1999