/*************************************************************************** * env.c: Environment variable functions. * * Part of... * * FREE: Display free space on your disk volumes. * Author: Daniel Jay Barrett, barrett@cs.jhu.edu. * * This program is Freely Distributable. Make all the copies you want * and give them away. Use this code in any way you like. ***************************************************************************/ #include "free.h" /* Return the value of ENV: environment variable "variableName", if it * exists. */ char *GetEnv(char *variableName) { char envVar[ENV_NAME_LENGTH]; BPTR fileHandle; sprintf(envVar, "ENV:%s", variableName); if ((fileHandle = Open(envVar, MODE_OLDFILE)) == 0) return(NULL); else return(TheEnvValue(fileHandle)); } /* Given a fileHandle on an environment variable, read the "file" and * find the value of the variable. * As far as I can tell, the value may have some garbage (control) * characters after it. I make sure to put a null ('\0') in place of * the first such control character (if any). * * If the value is too large (>= BUFSIZ-1), then we can't handle it. * Return NULL. * Otherwise, terminate the value with a null and return a pointer to * it. We use a static array for this. */ char *TheEnvValue(BPTR fileHandle) { static char envString[BUFSIZ]; int numChars; numChars = 0; if (Read(fileHandle, envString, (long)sizeof(envString)) > 0) { envString[BUFSIZ-1] = '\0'; /* Terminate for safety. */ while (envString[numChars] >= ' ') numChars++; if (numChars < sizeof(envString)-1) envString[numChars] = '\0'; else numChars = 0; } Close(fileHandle); return(numChars ? envString : NULL); }