home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 171.lha / SupLib / envirome.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  1KB  |  66 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. extern void *AllocMem();
  14. extern void *malloc();
  15.  
  16. char *
  17. GetDEnv(name)
  18. char *name;
  19. {
  20.     short nlen = strlen(name) + 5;
  21.     char *ptr = AllocMem(nlen, MEMF_PUBLIC);
  22.     char *res = NULL;
  23.     long fh;
  24.     long len;
  25.  
  26.     if (ptr) {
  27.     strcpy(ptr, "ENV:");
  28.     strcat(ptr, name);
  29.     if (fh = Open(ptr, 1005)) {
  30.         len = (Seek(fh, 0L, 1), Seek(fh, 0L, 0));
  31.         if (len >= 0 && (res = malloc(len+1))) {
  32.         Seek(fh, 0L, -1);
  33.         if (Read(fh, res, len) != len)
  34.             len = 0;
  35.         res[len] = 0;
  36.         }
  37.         Close(fh);
  38.     }
  39.     FreeMem(ptr, nlen);
  40.     }
  41.     return(res);
  42. }
  43.  
  44. SetDEnv(name, str)
  45. char *name, *str;
  46. {
  47.     short nlen = strlen(name) + 5;
  48.     short slen = strlen(str);
  49.     short res = 0;
  50.     char *ptr = AllocMem(nlen, MEMF_PUBLIC);
  51.     long fh;
  52.  
  53.     if (ptr) {
  54.     strcpy(ptr, "ENV:");
  55.     strcat(ptr, name);
  56.     if (fh = Open(ptr, 1006)) {
  57.         if (Write(fh, str, slen) == slen)
  58.         res = 1;
  59.         Close(fh);
  60.     }
  61.     FreeMem(ptr, nlen);
  62.     }
  63.     return(res);
  64. }
  65.  
  66.