midrange.com code scratchpad
Name:
Pgm to test whether it's running in QSHELL by examining the stack with the QWVRCSTK API.
Scriptlanguage:
C
Tabwidth:
4
Date:
09/30/2009 06:05:20 am
IP:
Logged
Description:
Function ckQSHELL() looks through the stack for a program in the QSHELL library, and returns 1 if found -- 0 otherwise. Tested on V5R3M0.

The main() function calls ckQSHELL and prints 'YES' if running in QSHELL. If the pgm is MYLIB/MYPGM, it can be run in QSHELL by:
QSH CMD('/qsys.lib/mylib.lib/mypgm.pgm')

Look in your outq for the spoolfile.
Code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <recio.h>
  5. #include <qwvrcstk.h>
  6.  
  7. typedef _Packed struct API_Error {
  8.   int       bytesProvided;
  9.   int       bytesAvail;
  10.   char      exceptionID[7];
  11.   char      reserved[1];
  12.   char      exception[128];
  13. } API_Error_t;
  14.  
  15.  _RFILE            *qsysprtFp;
  16.  
  17. static int  ckQSHELL( void );
  18. /*---------------------------------------------------------------------
  19.  */
  20. int main (int argc, char *argv[])
  21. {
  22.   qsysprtFp = _Ropen("QSYSPRT", "wr");
  23.  
  24.   if (ckQSHELL()) {
  25.     _Rwrite(qsysprtFp, "YES", 3);
  26.   } else {
  27.     _Rwrite(qsysprtFp, "NO", 2);
  28.   }
  29.  
  30.   if (qsysprtFp != NULL) {
  31.     _Rclose(qsysprtFp);
  32.   }
  33.   return;
  34. }
  35. /*---------------------------------------------------------------------
  36.  *  Return 1 if we're running in QSHELL; 0 otherwise.
  37.  */
  38. int  ckQSHELL( void )
  39. {
  40.   int                 retc = 0;
  41.   int                 alcSize;
  42.   int                 eX;
  43.   char               *cRCSTKp;
  44.   Qwv_RCSTK_Entry_t  *RCSTKp;
  45.   char               *cCSTK0100p;
  46.   Qwv_CSTK0100_t     *CSTK0100p;
  47.   Qwv_CSTK0100_t      CSTK0100;
  48.   Qwc_JIDF0100_t      JIDF0100;
  49.   API_Error_t         locAPI_Err;
  50.  
  51.   memset(&locAPI_Err, 0, sizeof(locAPI_Err));
  52.   locAPI_Err.bytesProvided = sizeof(locAPI_Err);
  53.   memset(&JIDF0100, ' 'sizeof(JIDF0100));
  54.   memset(&JIDF0100.Reserved, 0, sizeof(JIDF0100.Reserved));
  55.   memset(&JIDF0100.Thread_Id, 0, sizeof(JIDF0100.Thread_Id));
  56.   JIDF0100.Job_Name[0]  = '*';                   /* cur job    */
  57.   JIDF0100.Thread_Indicator = 1;                 /* cur thread */
  58.  
  59.   /*  Call API once to get available size  */
  60.  
  61.   QWVRCSTK(&CSTK0100,
  62.            sizeof(CSTK0100),
  63.            "CSTK0100",
  64.            &JIDF0100,
  65.            "JIDF0100",
  66.            &locAPI_Err);
  67.  
  68.   alcSize = CSTK0100.Bytes_Available;
  69.   cCSTK0100p = malloc(alcSize);
  70.   CSTK0100p = (Qwv_CSTK0100_t *) cCSTK0100p;
  71.  
  72.   /*  Call API to get whole stack  */
  73.  
  74.   QWVRCSTK(cCSTK0100p,
  75.            alcSize,
  76.            "CSTK0100",
  77.            &JIDF0100,
  78.            "JIDF0100",
  79.            &locAPI_Err);
  80.  
  81.   cRCSTKp = cCSTK0100p + CSTK0100p->Entry_Offset;
  82.   RCSTKp = (Qwv_RCSTK_Entry_t *) cRCSTKp;
  83.  
  84.   /*  Loop thru stack entries  */
  85.  
  86.   for (eX = 0; eX < CSTK0100p->Entry_Returned; eX++) {
  87.  
  88.     /*  Answer yes if we see QSHELL library  */
  89.  
  90.     if (0 == memcmp(RCSTKp->Program_Library, "QSHELL ", 7)) {
  91.       retc = 1;
  92.       break;
  93.     }
  94.     cRCSTKp = cRCSTKp + RCSTKp->Entry_Length;
  95.     RCSTKp = (Qwv_RCSTK_Entry_t *) cRCSTKp;
  96.   }
  97.   if (cCSTK0100p != NULL) {
  98.     free(cCSTK0100p);
  99.     cCSTK0100p = NULL;
  100.   }
  101.   return retc;
  102. }
  103.  
© 2004-2019 by midrange.com generated in 0.012s valid xhtml & css