home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / suplib.lzh / SUPLIB / SRC / ENVIROMENT.C < prev    next >
C/C++ Source or Header  |  1991-08-16  |  1KB  |  72 lines

  1.  
  2.  
  3. /*  ENVIROMENT.C
  4.  *
  5.  *  str = GetDEnv(name)
  6.  *  bool= SetDEnv(name, str)    (0=failure, 1=success)
  7.  *
  8.  *    If the enviroment variable 'name' exists, malloc and return a copy
  9.  *    of it.    The user program must free() it (or allow the standard C
  10.  *    exit routine to free() it).
  11.  */
  12.  
  13. #include <local/typedefs.h>
  14. #ifdef LATTICE
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #else
  18. extern void *malloc();
  19. #endif
  20.  
  21. char *
  22. GetDEnv(name)
  23. char *name;
  24. {
  25.     short nlen = strlen(name) + 5;
  26.     char *ptr = AllocMem(nlen, MEMF_PUBLIC);
  27.     char *res = NULL;
  28.     long fh;
  29.     long len;
  30.  
  31.     if (ptr) {
  32.     strcpy(ptr, "ENV:");
  33.     strcat(ptr, name);
  34.     if (fh = (long)Open(ptr, 1005)) {
  35.         len = (Seek(fh, 0L, 1), Seek(fh, 0L, 0));
  36.         if (len >= 0 && (res = malloc(len+1))) {
  37.         Seek(fh, 0L, -1);
  38.         if (Read(fh, res, len) != len)
  39.             len = 0;
  40.         res[len] = 0;
  41.         }
  42.         Close(fh);
  43.     }
  44.     FreeMem(ptr, nlen);
  45.     }
  46.     return(res);
  47. }
  48.  
  49. int
  50. SetDEnv(name, str)
  51. char *name, *str;
  52. {
  53.     short nlen = strlen(name) + 5;
  54.     short slen = strlen(str);
  55.     int res = 0;
  56.     char *ptr = AllocMem(nlen, MEMF_PUBLIC);
  57.     long fh;
  58.  
  59.     if (ptr) {
  60.     strcpy(ptr, "ENV:");
  61.     strcat(ptr, name);
  62.     if (fh = (long)Open(ptr, 1006)) {
  63.         if (Write(fh, str, slen) == slen)
  64.         res = 1;
  65.         Close(fh);
  66.     }
  67.     FreeMem(ptr, nlen);
  68.     }
  69.     return(res);
  70. }
  71.  
  72.