home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name pcenvchk -- Return an environment parameter specification
- *
- * Synopsis pparm = pcenvchk(pspec,max_len);
- *
- * char *pparm The returned parameter
- * char *pspec The environment specification for which
- * to search
- * int max_len The maximum number of characters of the
- * parameter to return.
- *
- * Description This function searches the environment for the parameter
- * associated with the specification pointed to by pspec.
- * If the parameter is found in the environment, space is
- * allocated for its value and a copy is made. The address
- * of the copy is returned as the value of the function
- * (i.e., pparm). No more than max_len characters are
- * returned (in addition to the trailing NUL ('\0')).
- *
- * The resulting string is placed in space allocated by
- * calloc(). The address of the string is returned as the
- * value of the function. The space should be freed by the
- * calling function when it is no longer needed.
- *
- * The environment specification must be specified in upper
- * case and be terminated by an equals sign ('=') and a NUL
- * ('\0').
- *
- * The result is NIL if the parameter is not found in the
- * environment or if there is insufficient memory to create
- * a copy of the found string.
- *
- * Returns pparm Pointer to the parameter string
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1983, 1984, 1986
- *
- * Version 3.01 August 20, 1986 PLD
- * Corrected declaration of "environ" for Lattice compiler.
- *
- **/
-
- #include <stdlib.h> /* Defines environ (for MSC300),*/
- /* calloc (for LAT300) */
- #include <string.h>
-
- #include <bprogctl.h>
- #include <bstring.h>
- #include <butility.h>
-
- #if MSC300
- #include <malloc.h>
- #endif
- #if LAT300
- extern char **environ;
- #endif
-
- char *pcenvchk(pspec,max_len)
- char *pspec;
- int max_len;
- {
- char **envtbl; /* Moving pointer into table */
- /* of pointers to environment */
- /* strings. */
-
- register char *envstr; /* Pointer to one environment */
- /* entry. */
- char *result;
- register char *presult;
- register int i;
- int len;
-
- envtbl = environ;
- while (envstr = *envtbl++)
- {
- if (stsindex(pspec,envstr) == 0) /* Have a match */
- {
- envstr += (int) strlen(pspec); /* Step past matching */
- /* parameter name. */
-
- len = min(max_len,(int) strlen(envstr));
-
- /* Allocate space for result. */
- if (NIL == (result = calloc((unsigned int) (len + 1),1)))
- return NIL; /* Failure. */
-
- /* Copy found string into */
- /* result. */
- for (i = 0, presult = result; i < len; i++)
- *presult++ = *envstr++;
- *presult = '\0';
-
- return result;
- }
- }
-
- return NIL; /* Fall through implies not */
- /* found. */
- }