lapin@cbnewsc.ATT.COM (David Roch) (11/27/89)
Unfortunately, getenv() does not always work in
Lattice C v5.02,4. Lattice uses the same buffer
for the filename to open (e.g. ENV:some_var) as
they use to store the results. When they copy
the results (contents of file ENV:some_var), they
do not place a NULL terminator at the end of their
string. Lattice is aware of the problem, and is
supposed to be coming up with a fix. In the mean
time, you may wish to use my implementation of
getenv().
------------------cut here--------------------
/* Copyright 1989 by David Roch
* This routine may be freely used in any program
* commercial or private, provided this copyright
* notice remains intact.
*/
#include <stdio.h>
#define BUFSIZE 1024
#define ENV "ENV:"
/*
* char *getenv(char *)
* Given a pointer to a character string representing a
* variable name, return a pointer to a character string
* that contains the value of the variable. If no such
* variable exists, return a NULL pointer.
* The returned pointer is valid until the next call to
* getenv().
*
* This routine is different than the one supplied with
* Lattice C in that multiple line variables are supported.
* If the variable contains more than BUFSIZE characters,
* only the first BUFSIZE characters will be returned.
*/
char *getenv(variable)
char *variable;
{
FILE *fp;
char *ptr;
short i;
static char buf[BUFSIZE + 1];
/* if filename overflos the buffer */
if (strlen(ENV) + strlen(variable) > BUFSIZE) {
ptr = NULL; /* then can't open */
} else {
/* create filename of form "ENV:variable_name" */
strcpy(buf, ENV);
strcat(buf, variable);
/* open the file */
fp = fopen(buf, "r");
if (fp == NULL) {
/* we can't open the file */
ptr = NULL;
} else {
ptr = buf; /* set pointer to start of buffer */
i = 0; /* count */
while ((*ptr = fgetc(fp)) != EOF && i < BUFSIZE) {
i++; /* bump count */
ptr++; /* next location in buffer */
} /* end while */
fclose(fp); /* close file */
*ptr = NULL; /* terminate string */
ptr = buf; /* point to start of buf */
} /* end if (fp == NULL) */
} /* end if strlen's > BUFSIZE */
return (ptr);
} /* end getenv() */