midrange.com code scratchpad
Name:
Pointer in ILE CL for getenv()
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
02/14/2008 09:55:32 pm
IP:
Logged
Description:
A simple example of pointer usage in V5R4 ILE CL -- the example retrieves and extracts the environment variable 'MyEnvVar'. The value returned from getenv() is a pointer. The pointer becomes the basing address for &EnvVarVal. Once the pointer is returned, the storage pointed to is scanned with QCLSCAN for the null-terminator. The substring up to (and including) the null-terminator is copied into &MyEnvVar. Execute ADDENVVAR ENVVAR('MyEnvVar') VALUE('Some useless value to return...') before calling program to see the 'useless' value that's returned. Or run the program without executing ADDENVVAR to see the result of the null pointer from the API.

Note that (1) V5R4 is required and (2) PTFs must be recent enough to get decent *PTR handling (there were various bugs early on).
Code:
  1. pgm
  2.  
  3. /* General working variables:                            */
  4.  
  5.    /* The name of the ENVVAR to retrieve...      */
  6.    dcl   &EnvVar      *char   32
  7.  
  8.    /* The extracted ENVVAR value...              */
  9.    dcl   &MyEnvVar    *char  256     value( '*****' )
  10.  
  11.    /* Used as a null-terminator constant...      */
  12.    dcl   &x00         *char    1     value( x'00' )  /* Null-terminator */
  13.  
  14. /* A couple pointers:                                    */
  15.  
  16.    /* Used whenever a comparison to a *NULL pointer is needed... */
  17.    dcl   &pNull       *ptr   /* Auto-initialized to *null */
  18.  
  19.    /* Pointer to be returned from getenv()...    */
  20.    dcl   &pEnvVar     *ptr
  21.  
  22. /* The "based" variable:                                 */
  23.  
  24.    dcl   &EnvVarVal   *char  256     stg( *BASED ) +
  25.                                      basptr( &pEnvVar )
  26.  
  27. /* Variables for QCLSCAN API                             */
  28.  
  29.    dcl   &STRLEN      *dec  (  3 0 ) value( 256 )
  30.    dcl   &STRPOS      *dec  (  3 0 ) value( 1 )
  31.    dcl   &PATLEN      *dec  (  3 0 ) value( 1 )
  32.    dcl   &TRANSLATE   *char    1     value( '0' )
  33.    dcl   &TRIM        *char    1     value( '0' )
  34.    dcl   &WILD        *char    1     value( ' ' )
  35.    dcl   &RESULTS     *dec  (  3 0 ) value( 0 )
  36.  
  37.  
  38. /* Set the ENVVAR name to retrieve -- null-terminated... */
  39.  
  40.    chgvar      &EnvVar    ( 'MyEnvVar' *cat &x00 )
  41.  
  42.  
  43. /* Retrieve the chosen ENVVAR...                         */
  44.  
  45.    callprc     'getenv'   ( +
  46.                             &EnvVar      +
  47.                           ) +
  48.                     rtnval( &pEnvVar )  /* Pointer is returned */
  49.  
  50.  
  51. /* Test for *null pointer returned -- "not found"...       */
  52.  
  53.    if ( &pEnvVar *eq &pNull )  do
  54.       sndpgmmsg   msgid( CPF9898 ) msgf( QCPFMSG ) +
  55.                     msgdta( 'Not found' ) msgtype( *ESCAPE )
  56.       return
  57.    enddo
  58.  
  59.  
  60. /* Scan for null-terminator -- end of string...          */
  61.  
  62.    call        QCLSCAN    ( +
  63.                             &EnvVarVal   +
  64.                             &STRLEN      +
  65.                             &STRPOS      +
  66.                             &x00         +
  67.                             &PATLEN      +
  68.                             &TRANSLATE   +
  69.                             &TRIM        +
  70.                             &WILD        +
  71.                             &RESULTS     +
  72.                           )
  73.  
  74.  
  75. /* If we get a positive &RESULTS:                        */
  76.  
  77. /* Grab substring up to/including null-terminator...     */
  78. /* Use &RESULTS-1 if we don't want the null...           */
  79.  
  80.    if  ( &RESULTS *gt 0 )  +
  81.       chgvar   &MyEnvVar    %sst( &EnvVarVal 1 &RESULTS )
  82.  
  83. /* ...otherwise note that we found no null...            */
  84.  
  85.    else  +
  86.       chgvar   &MyEnvVar    'No null-terminator found'
  87.  
  88. /* If no null-terminator was found, we could have done   */
  89. /* any number of things including just grabbing the      */
  90. /* entire 256 bytes...                                   */
  91.  
  92.  
  93. /* Dump the results to verify...                         */
  94.  
  95. dmpclpgm
  96.  
  97.    return
  98.  
  99. endpgm
  100.  
© 2004-2019 by midrange.com generated in 0.005s valid xhtml & css