/*
** This program demonstrates how an OS/2 .exe file
** can determine the path from which it was loaded.
**
** This eliminates dependance on DPATH and should
** speed access to program dependant files.
**
** It is designed for execution as a 32 Bit OS/2 application.
**
** Joe Nord 29-Jan-92
*/
#include <stdlib.h>
#include <stdio.h>

#define INCL_DOSPROCESS
#include <os2.h>

int main (int argc, char *argv[])
{
  PSZ   pszEnv;
  ULONG ulRC;

  PTIB ptib;
  PPIB ppib;

  ulRC = DosGetInfoBlocks (&ptib, &ppib);
  if (ulRC)
    {
    printf ("Error: DosGetInfoBlocks call failed, RC = %li.\n", ulRC);
    return (1);
    }

  pszEnv = ppib->pib_pchenv;

  /*
  ** Seach environment for first occurance of
  ** two successive zero bytes.  This signals
  ** the end of the environment structure.
  */
  while (*pszEnv != '\0')
  {
    while (*(++pszEnv) != '\0')  /* Find a single zero byte */
      ; /* Null statement */
    pszEnv++;                    /* Point to first char past first zero byte */
  } /* falls out of loop on two successive zero bytes */

  /*
  ** The name of the executing program is stored (with full path)
  ** as an ASCII Z string immediately beyond the environment.
  */
  pszEnv++;
  printf ("%s\n", pszEnv);  /* Executable name */

  return (0);
}
