home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / PCENVCHK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-22  |  2.9 KB  |  100 lines

  1. /**
  2. *
  3. * Name        pcenvchk -- Return an environment parameter specification
  4. *
  5. * Synopsis    pparm = pcenvchk(pspec,max_len);
  6. *
  7. *        char *pparm      The returned parameter
  8. *        char *pspec      The environment specification for which
  9. *                  to search
  10. *        int  max_len      The maximum number of characters of the
  11. *                  parameter to return.
  12. *
  13. * Description    This function searches the environment for the parameter
  14. *        associated with the specification pointed to by pspec.
  15. *        If the parameter is found in the environment, space is
  16. *        allocated for its value and a copy is made.  The address
  17. *        of the copy is returned as the value of the function
  18. *        (i.e., pparm).    No more than max_len characters are
  19. *        returned (in addition to the trailing NUL ('\0')).
  20. *
  21. *        The resulting string is placed in space allocated by
  22. *        calloc().  The address of the string is returned as the
  23. *        value of the function.    The space should be freed by the
  24. *        calling function when it is no longer needed.
  25. *
  26. *        The environment specification must be specified in upper
  27. *        case and be terminated by an equals sign ('=') and a NUL
  28. *        ('\0').
  29. *
  30. *        The result is NIL if the parameter is not found in the
  31. *        environment or if there is insufficient memory to create
  32. *        a copy of the found string.
  33. *
  34. * Returns    pparm          Pointer to the parameter string
  35. *
  36. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983, 1984, 1986
  37. *
  38. * Version    3.01 August 20, 1986  PLD
  39. *        Corrected declaration of "environ" for Lattice compiler.
  40. *
  41. **/
  42.  
  43. #include <stdlib.h>              /* Defines environ (for MSC300),*/
  44.                       /* calloc (for LAT300)          */
  45. #include <string.h>
  46.  
  47. #include <bprogctl.h>
  48. #include <bstring.h>
  49. #include <butility.h>
  50.  
  51. #if MSC300
  52. #include <malloc.h>
  53. #endif
  54. #if LAT300
  55. extern char **environ;
  56. #endif
  57.  
  58. char *pcenvchk(pspec,max_len)
  59. char *pspec;
  60. int  max_len;
  61. {
  62.     char **envtbl;              /* Moving pointer into table    */
  63.                       /* of pointers to environment   */
  64.                       /* strings.              */
  65.  
  66.     register char *envstr;          /* Pointer to one environment   */
  67.                       /* entry.               */
  68.     char *result;
  69.     register char *presult;
  70.     register int i;
  71.     int len;
  72.  
  73.     envtbl = environ;
  74.     while (envstr = *envtbl++)
  75.     {
  76.     if (stsindex(pspec,envstr) == 0)    /* Have a match          */
  77.     {
  78.         envstr += (int) strlen(pspec);  /* Step past matching     */
  79.                         /* parameter name.          */
  80.  
  81.         len = min(max_len,(int) strlen(envstr));
  82.  
  83.                       /* Allocate space for result.   */
  84.         if (NIL == (result = calloc((unsigned int) (len + 1),1)))
  85.         return NIL;          /* Failure.              */
  86.  
  87.                       /* Copy found string into       */
  88.                       /* result.              */
  89.         for (i = 0, presult = result; i < len; i++)
  90.         *presult++ = *envstr++;
  91.         *presult = '\0';
  92.  
  93.         return result;
  94.     }
  95.     }
  96.  
  97.     return NIL;               /* Fall through implies not     */
  98.                       /* found.               */
  99. }
  100.