[net.micro.pc] Arg

lowey@sask.UUCP (10/31/86)

(*

Hi There;

There has been a lot of discussion on how to get Argv(0) in Turbo Pascal.

  If you have version 3.xx of turbo pascal, you can get all the arguments
greater than 1, but not argument 0 using PARAMSTR.

  Alas, Turbo pascal's PARAMSTR function strips out extra blanks. We cannot
have quoted parameters.  Using modifications of the routines previously
posted will allow us to have quoted parameters in all versions of Turbo.
(eg. search filename 'this text').  Remember that the parameter information
is also used by DOS as the Disk Transfer Area (DTA) so the parameters may
be overwritten.  Save them immediately after starting the program.

In MS-DOS version 3.0 and higher, argv(0) is stored three bytes after the end
of the environment table.

The following program will display all environment strings, then display
argv(0).

The program first finds the address of the environment table.
It then displays all the environment strings found there.
Each string is separated by the next with the null character (ascii 0).
The environment table is finished when two nulls are found beside each other.

After the environment table comes three bytes.  What they are I have NO idea.
Anyone know?  Right now I'm just skipping them.

After these two bytes is the argv(0).  It contains not only the name of the
program, but also the disk and path the file is on.  It is terminated by a
single null character.

This was tested with and works on pc-dos version 3.0, 3.1 and 3.2.  It did
not work with PC-DOS 2.1.  I tested it with turbo pascal versions 2.00B
and 3.01a.


Kevin Lowey
User Support and Training Services
Computing Services
University of Saskatchewan, Canada

bitnet:  LOWEY@SASK
uucp:    alberta!sask!lowey
*)

program demo;

var
  i : integer;
  table_address: integer;
  result : string[255];

begin  { program demo }

  { find start segment of environment, stored in offset $002c of code segment }
  table_address := memW[cseg:$002c];

  i := 0;
  { Repeat for all strings in the environment }
  repeat
    result := '';
    while mem[table_address:i] <> 0 do begin   { build string }
      result := result + chr(mem[table_address:i]);
      i := i + 1;
    end;
    writeln (result); {output environment string }
    i := i + 1;
  until result = '';

  { skip two bytes (one is already skipped)}
  i := i + 2; { why? I don't know!}

  { build argv(0) }
  result := '';
  while mem[table_address:i] <> 0 do begin
    result := result + chr(mem[table_address:i]);
    i := i + 1;
  end;
  writeln ('Argv0 is: ',result);
end.