Referring to the Command-Line Tail

Last reviewed: July 17, 1997
Article ID: Q60869
5.00 5.10 6.00 6.00 6.00ax 7.00 | 1.00 1.50
MS-DOS                          | WINDOWS
kbprg

The information in this article applies to:

  • Microsoft C for MS-DOS, versions 5.0, 5.1, 6.0, 6.0a, and 6.0ax
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

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 reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00
KBCategory: kbprg
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.