home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / getenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  1.1 KB  |  71 lines

  1. /*
  2.  *  GETENV.C
  3.  *
  4.  *  Lattice's screws up.  Only part of lib when not compiled under DICE
  5.  *  which implements getenv() properly (even better) then this.
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "config.h"
  14.  
  15. Prototype char *gettmpenv (const char *);
  16. Prototype char *getenv (const char *);
  17.  
  18. #ifndef _DCC
  19.  
  20. char *
  21. gettmpenv (const char *id)
  22. {
  23.     static char
  24.         *buf,
  25.         *res = NULL;
  26.     BPTR
  27.         fh;
  28.     int
  29.         len;
  30.  
  31.     buf = malloc (strlen (id) + 8);
  32.     sprintf (buf, "ENV:%s", id);
  33.     fh = Open ((UBYTE *) buf, MODE_OLDFILE);
  34.     free (buf);
  35.     if (fh) {
  36.         Seek (fh, 0L, OFFSET_END);
  37.         len = Seek (fh, 0L, OFFSET_BEGINNING);
  38.         if (len < 0) {
  39.             Close (fh);
  40.             return NULL;
  41.         }
  42.  
  43.         if (res)
  44.             free (res);
  45.         res = malloc (len + 1);
  46.         Read (fh, res, len);
  47.         Close (fh);
  48.         if (len > 0 && res [len - 1] == '\n')
  49.             --len;
  50.         res [len] = 0;
  51.         return res;
  52.     }
  53.  
  54.     return NULL;
  55. }
  56.  
  57. char *
  58. getenv (const char *id)
  59. {
  60.     char
  61.         *res = gettmpenv (id),
  62.         *perm = NULL;
  63.  
  64.     if (res)
  65.         perm = strdup (res);
  66.  
  67.     return perm;
  68. }
  69.  
  70. #endif
  71.