home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12555.ZIP / IFNOTWIN.C < prev    next >
C/C++ Source or Header  |  1990-03-28  |  3KB  |  118 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                 Copyright (c) Microsoft Corp.  1987, 1990                */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /****************************************************************************/
  7. /*
  8.  * IFWIN - IFNOTWIN
  9.  *
  10.  * Executes a program based on whether it is running in a vio window or not.
  11.  *
  12.  * Two versions of this program can be created based on the #define IFWIN.
  13.  * If this symbol is defined, a program will be created that only runs a
  14.  * specified program if it is running in a vio window.  Without this symbol
  15.  * defined, a program is created that will only run the specified program
  16.  * if it is running in a full screen session.
  17.  *
  18.  * Author: Byron Dazey
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #define INCL_DOSINFOSEG
  25. #define INCL_DOSPROCESS
  26. #include <os2.h>
  27.  
  28. USHORT main(USHORT, NPSZ *);
  29. VOID ShowUsage(VOID);
  30.  
  31. PLINFOSEG plis;
  32. SEL gisSel;
  33. SEL lisSel;
  34. RESULTCODES resc;
  35. CHAR ArgBuf[512];
  36. NPCH pch;
  37. USHORT i;
  38.  
  39. NPSZ pszUsage[] = {
  40.     "",
  41.     "Usage:",
  42. #ifdef IFWIN
  43.     "  IFWIN pgmname [arguments]",
  44.     "",
  45.     "    pgmname   - Program to start if executing in a vio window.",
  46. #else
  47.     "  IFNOTWIN pgmname [arguments]",
  48.     "",
  49.     "    pgmname   - Program to start if NOT executing in a vio window.",
  50. #endif
  51.     "                The .EXE extension must be specified.",
  52.     "    arguments - Optional parameters to pgmname.",
  53.     "",
  54.     "  To execute an internal command such as \"copy\", use the following",
  55.     "  for the arguments: \"cmd.exe /c copy a b\"",
  56.     NULL
  57. };
  58.  
  59. USHORT main(argc, argv)
  60. USHORT argc;
  61. NPSZ *argv;
  62. {
  63.     register rc;
  64.  
  65.     if (argc < 2) {
  66.         ShowUsage();
  67.         return 1;
  68.     }
  69.  
  70.     DosGetInfoSeg(&gisSel, &lisSel);
  71.     plis = MAKEPLINFOSEG(lisSel);
  72.  
  73. #ifdef IFWIN
  74.     if (plis->typeProcess == PT_WINDOWABLEVIO) {
  75. #else
  76.     if (plis->typeProcess == PT_FULLSCREEN) {
  77. #endif
  78.  
  79.         strcpy(ArgBuf, argv[1]);
  80.         pch = ArgBuf + strlen(ArgBuf);
  81.         pch++;
  82.         *pch = 0;
  83.  
  84.         for (i = 2; i < argc; i++) {
  85.             if (i > 2)
  86.                 strcat(pch, " ");
  87.  
  88.             strcat(pch, argv[i]);
  89.         }
  90.  
  91.         *(pch + strlen(pch) + 1) = 0;
  92.  
  93.         rc = DosExecPgm(NULL,   /*fail name buffer                          */
  94.                 0,              /*fail name buffer length                   */
  95.                 EXEC_ASYNC,     /*start it asynchronuously                  */
  96.                 ArgBuf,         /*pass it the arguments                     */
  97.                 NULL,           /*don't change the environment              */
  98.                 &resc,          /*result code buffer                        */
  99.                 argv[1]);       /*pass it the program name to start         */
  100.  
  101.         if (rc) {
  102.             printf("Exec failed (rc = %u).\n\a", rc);
  103.             return 1;
  104.         }
  105.     }
  106.  
  107.     return 0;
  108. }
  109.  
  110. VOID ShowUsage()
  111. {
  112.     NPSZ *p;
  113.  
  114.     p = pszUsage;
  115.     while (*p)
  116.         puts(*p++);
  117. }
  118.