KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q60869: How to Explicitly Reference Command-Line Tail

Article: Q60869
Product(s): See article
Version(s): 5.x 6.00
Operating System(s): MS-DOS
Keyword(s): ENDUSER | s_quickc s_quickasm | mspl13_c
Last Modified: 19-APR-1990

The following code allows you to explicitly reference the command-line
tail. The entire command line with spaces intact is referenced at the
Disk Transfer Area (DTA) address and printed out as one string.

Sample Code
-----------

#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 tailstring */

   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);
}

Note: The command line is limited to 128 bytes.

   cmdline *.c  abc  def  lab7.pas

Output using the above command line is as follows:

tail_length = 24
cmd_tail = < *.c  abc  def  lab7.pas>

Note: A more portable way of getting this information is to use the
argv mechanism built into C. This may be easier because the command
line would be already partially parsed by the setargv() function.

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.

Copyright Microsoft Corporation 1986-2002.