home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / prargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  3.4 KB  |  105 lines

  1. ;/* prargs.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 prargs.c
  3. Blink FROM LIB:c.o,prargs.o LIB LIB:LC.lib,LIB:Amiga.lib TO prargs DEFINE __main=__tinymain
  4. quit
  5. **
  6. ** The following example will display all WBArgs if started from
  7. ** Workbench, and all Shell arguments if started from the Shell.
  8. **
  9. ** NOTE: main and tinymain are prepended with two underscores.
  10. **
  11. ** PrArgs.c - This program prints all Workbench or Shell (CLI) arguments.
  12. */
  13. #include <exec/types.h>
  14. #include <workbench/startup.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/icon_protos.h>
  17.  
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20.  
  21. #ifdef LATTICE
  22. int CXBRK(void) { return(0); }   /* Disable SAS Lattice CTRL/C handling */
  23. int chkabort(void) { return(0); }/* really */
  24. #endif
  25.  
  26. void main(int argc, char **argv)
  27. {
  28. struct WBStartup *argmsg;
  29. struct WBArg *wb_arg;
  30. LONG ktr;
  31. BPTR olddir;
  32. FILE *outFile;
  33.  
  34. /* argc is zero when run from the Workbench,
  35. **         positive when run from the CLI.
  36. */
  37. if (argc == 0)
  38.     {
  39.     /* AmigaDOS has a special facility that  allows a window  */
  40.     /* with a console and a file handle to be easily created. */
  41.     /* CON: windows allow you to use fprintf() with no hassle */
  42.     if (NULL != (outFile = fopen("CON:0/0/640/200/PrArgs","r+")))
  43.         {
  44.         /* in SAS/Lattice, argv is a pointer to the WBStartup message
  45.         ** when argc is zero.  (run under the Workbench.)
  46.         */
  47.         argmsg = (struct WBStartup *)argv ;
  48.         wb_arg = argmsg->sm_ArgList ;         /* head of the arg list */
  49.  
  50.         fprintf(outFile, "Run from the workbench, %ld args.\n",
  51.                          argmsg->sm_NumArgs);
  52.  
  53.         for (ktr = 0; ktr < argmsg->sm_NumArgs; ktr++, wb_arg++)
  54.             {
  55.             if (NULL != wb_arg->wa_Lock)
  56.                 {
  57.                 /* locks supported, change to the proper directory */
  58.                 olddir = CurrentDir(wb_arg->wa_Lock) ;
  59.  
  60.                 /* process the file.
  61.                 ** If you have done the CurrentDir() above, then you can
  62.                 ** access the file by its name.  Otherwise, you have to
  63.                 ** examine the lock to get a complete path to the file.
  64.                 */
  65.                 fprintf(outFile, "\tArg %2.2ld (w/ lock): '%s'.\n",
  66.                                  ktr, wb_arg->wa_Name);
  67.  
  68.                 /* change back to the original directory when done.
  69.                 ** be sure to change back before you exit.
  70.                 */
  71.                 CurrentDir(olddir) ;
  72.                 }
  73.             else
  74.                 {
  75.                 /* something that does not support locks */
  76.                 fprintf(outFile, "\tArg %2.2ld (no lock): '%s'.\n",
  77.                                  ktr, wb_arg->wa_Name);
  78.                 }
  79.             }
  80.         /* wait before closing down */
  81.         Delay(500L);
  82.         fclose(outFile);
  83.         }
  84.     }
  85. else
  86.     {
  87.     /* using 'tinymain' from lattice c.
  88.     ** define a place to send the output (originating CLI window = "*")
  89.     ** Note - if you open "*" and your program is RUN, the user will not
  90.     ** be able to close the CLI window until you close the "*" file.
  91.     */
  92.     if (NULL != (outFile = fopen("*","r+")))
  93.         {
  94.         fprintf(outFile, "Run from the CLI, %d args.\n", argc);
  95.  
  96.         for ( ktr = 0; ktr < argc; ktr++)
  97.             {
  98.             /* print an arg, and its number */
  99.             fprintf(outFile, "\tArg %2.2ld: '%s'.\n", ktr, argv[ktr]);
  100.             }
  101.         fclose(outFile);
  102.         }
  103.     }
  104. }
  105.