#include <exec/types.h>
#include <workbench/startup.h>
#include <dos/dosextens.h>
#include <clib/dos_protos.h>
#include <stdio.h>

void main(int argc, char **argv)
{
  struct WBStartup *wbs;
  struct WBArg *wbarg;
  BPTR fh,oldlock;
  int i;

  if (argc == 0)
  /* programma lanciato da Workbench */
  {
    wbs = (struct WBStartup *)argv;
	wbarg = &(wbs->sm_ArgList[0]);
    printf("Programma %s ",wbarg->wa_Name);
    printf("lanciato da Workbench");
    printf("con %d parametri\n",wbs->sm_NumArgs - 1);
    for(i=1; i<wbs->sm_NumArgs; i++)
    {
      wbarg = &(wbs->sm_ArgList[i]);
      if (wbarg->wa_Name[0] != '\x0')
        /* è un file */
        printf("   file = %s\n",wbarg->wa_Name);
      else
        /* è un disco, una dir o il cestino */
        printf("   (directory)\n");
      /* sposto la dir corrente su quella del file */
      oldlock = CurrentDir(wbarg->wa_Lock);

      /* apro il file */
      fh = Open(wbarg->wa_Name,MODE_OLDFILE);

      /* qui posso leggere i dati del file */

      /* chiudo il file */
      Close(fh);

      /* ripristina la dir precedente */
      CurrentDir(oldlock);
    }
  }
  else
  /* programma lanciato da shell */
  {
    printf("Programma %s ",argv[0]);
    printf("lanciato da shell ");
    printf("con %d parametri\n",argc - 1);
    for(i=1; i<argc; i++)
      printf("   parm = %s\n",argv[i]);
  }
}