home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / GETENVRN.C < prev    next >
Text File  |  1991-08-16  |  5KB  |  167 lines

  1. /*
  2.  * File......: GETENVRN.C
  3.  * Author....: Rick Whitt
  4.  * CIS ID....: 70672,605
  5.  * Date......: $Date:   15 Aug 1991 23:08:42  $
  6.  * Revision..: $Revision:   1.1  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/getenvrn.c_v  $
  8.  * 
  9.  * This is an original work by Rick Whitt and is placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   E:/nanfor/src/getenvrn.c_v  $
  16.  * 
  17.  *    Rev 1.1   15 Aug 1991 23:08:42   GLENN
  18.  * Forest Belt proofread/edited/cleaned up doc
  19.  * 
  20.  *    Rev 1.0   17 Jul 1991 22:08:12   GLENN
  21.  * Initial revision.
  22.  *
  23.  */
  24.  
  25.  
  26. /*  $DOC$
  27.  *  $FUNCNAME$
  28.  *     FT_GETE()
  29.  *  $CATEGORY$
  30.  *     Environment
  31.  *  $ONELINER$
  32.  *     Return the entire current environment 
  33.  *  $SYNTAX$
  34.  *     FT_GETE( [ @<xReceiveVar> ] ) -> nNumStrings
  35.  *  $ARGUMENTS$
  36.  *     <xReceiveVar> is the variable to receive the environment data.
  37.  *
  38.  *     <xReceiveVar> can be a character type variable, in which case
  39.  *     the function will place all environment strings in the variable
  40.  *     separated by carriage return/line feeds (chr 13 + chr(10)).
  41.  *
  42.  *     <xReceiveVar> can be an array type, in which case the function
  43.  *     will place each string in an array element.  The array MUST be
  44.  *     declared with the proper number of elements prior to passing it
  45.  *     to the function.  This can be done by calling FT_GETE() without
  46.  *     parameters first to get the number of strings in the environment.
  47.  *
  48.  *     Note that the argument MUST be passed by reference. Since arrays
  49.  *     are by nature passed by reference, the "@" symbol is optional when
  50.  *     passing an array.
  51.  *
  52.  *     If no argument is passed, FT_GETE() merely returns the number
  53.  *     of strings in the environment.
  54.  *  $RETURNS$
  55.  *     FT_GETE() returns the total number of strings found in the
  56.  *     current program's environment.
  57.  *  $DESCRIPTION$
  58.  *     This function stores ALL of the current program's environment 
  59.  *     variables in either a block of text lines or in an array.  It is
  60.  *     useful for looking at the entire environment at once, or recording
  61.  *     a snapshot of it to a file for later inspection, such as when a
  62.  *     program error occurs.  If the value of ONE SPECIFIC variable is
  63.  *     desired, use Clipper's built-in GETE() function.
  64.  *
  65.  *     This function uses the undocumented internal variable "_environ",
  66.  *     as well as the functions _strcpy(), _strcat(), and _strlen() from
  67.  *     CLIPPER.LIB
  68.  *  $EXAMPLES$
  69.  *     Get the environment in text form and browse it:
  70.  *
  71.  *        cEnvBlock   := ""
  72.  *        nNumStrings := FT_GETE(@cEnvBlock)
  73.  *        @  0, 0 to MAXROW() - 1, MAXCOL()
  74.  *        @  MAXROW(), 0 say 'Browse strings, press ESC to exit...'
  75.  *        MEMOWRIT(cEnvBlock, 1, 1, MAXROW() - 2,MAXCOL() - 1, .F.)
  76.  *
  77.  *     Get the environment in text form and write it to a file:
  78.  *
  79.  *        cEnvBlock := ""
  80.  *        FT_GETE(@cEnvBlock)
  81.  *        MEMOWRIT("ENVIRON.TXT", cEnvBlock)
  82.  *     
  83.  *     Get the environment in Array form:
  84.  *
  85.  *        aEnvArray := ARRAY(FT_GETE())
  86.  *        FT_GETE(aEnvArray)
  87.  *        ? aEnvArray[1]       // "COMSPEC=C:\COMMAND.COM"
  88.  *        ? aEnvArray[2]       // "PATH=C:\;C:\DOS;C:\UTIL;C:\CLIP50\BIN"
  89.  *          ... etc ...
  90.  *  $END$
  91.  */
  92.  
  93. #include <extend.h>
  94.  
  95. #define NORETURN   0
  96. #define CHARTYPE   1
  97. #define ARRAYTYPE  2
  98. #define CRLF       "\x0D\x0A"
  99.  
  100. CLIPPER FT_GETE(void)
  101. {
  102.     /* INTERNALS WARNING: All references to 'environ', strlen(), ;
  103.        strcpy(), and strcat() are undocumented Clipper 5.0 internals.
  104.     */
  105.     extern char **environ;
  106.     char *buffer;
  107.     int x;
  108.     int buffsize = 0;
  109.     int rettype  = NORETURN;
  110.  
  111.     if (ISCHAR(1))
  112.         rettype = CHARTYPE;
  113.     if (ISARRAY(1))
  114.         rettype = ARRAYTYPE;
  115.  
  116.     if (rettype == CHARTYPE)
  117.         // scan strings first and add up total size
  118.         {
  119.         for (x = 0; ;x++)
  120.             {
  121.             if (! environ[x])
  122.                 // null string, we're done
  123.                 break;
  124.             // add length of this string plus 2 for the crlf
  125.             buffsize += (strlen(environ[x]) + 2);
  126.             }
  127.         // add 1 more byte for final nul character
  128.         buffsize++;
  129.     
  130.         //  now allocate that much memory and make sure 1st byte is a nul
  131.         buffer = _xalloc(buffsize);
  132.         strcpy(buffer,"\0");
  133.         }
  134.  
  135.     for (x = 0; ;x++)
  136.         {
  137.         if (! environ[x])
  138.             // null string, we're done
  139.             break;
  140.  
  141.         if (rettype == CHARTYPE)
  142.             {
  143.             // tack string onto end of buffer
  144.             strcat(buffer,environ[x]);
  145.             // add crlf at end of each string
  146.             strcat(buffer,CRLF);
  147.             }
  148.  
  149.         if (rettype == ARRAYTYPE)
  150.             // store string to next array element
  151.             _storc(environ[x],1,x + 1);
  152.  
  153.         }
  154.  
  155.     if (rettype == CHARTYPE)
  156.         {
  157.         // return buffer to app and free memory
  158.         _storc(buffer,1);
  159.         _xfree(buffer);
  160.         }
  161.  
  162.     // return number of strings found
  163.     _retni(x);
  164. }
  165.  
  166.  
  167.