--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation; either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library; if not, write to the Free Software
--	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module hostio

#COMMENT "Host i/o library, V1.00, 10/03/89"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC simplified version 1995"

#INCLUDE "hostio.inc"
#USE "conv"

--{{{  OC.getenv
VAL unix.max.getenvname.size IS (sp.max.getenvname.size/4) + 1:
VAL unix.max.readbuffer.size IS (sp.max.readbuffer.size/4) + 1:

#PRAGMA EXTERNAL "PROC C.getenvval (INT result, VAL [unix.max.getenvname.size]INT envname, INT len, [unix.max.readbuffer.size]INT block, VAL INT bytespace) = 0"

PROC OC.getenv (INT result, VAL []BYTE envname, INT len, []BYTE block)
  [unix.max.getenvname.size]INT unixenvname:
  [unix.max.readbuffer.size]INT unixblock:
  SEQ
    bytes.to.chars (envname, unixenvname)
    C.getenvval (result, unixenvname, len, unixblock, SIZE block)
    IF
      (result = 0) AND (len > 0)
        chars.to.bytes (unixblock, [block FOR len])
      TRUE
        SKIP
:
--}}}  
--{{{  sp.getenv
--* Return the contents of an environment variable from whatever host
-- is being used.
--
-- The largest size the returned contents of an environment variable
-- can be is limited by the functionality of the server, not by this
-- routine.
--
-- @param fs from server
-- @param ts to server
-- @param name a string containing the name of the desired environment
--   variable.  name should not have more than sp.max.getenvname.size
--   characters in it.
-- @param length if result is spr.ok, the number of bytes in value,
--   starting at value[ 0 ]; (SIZE value) if result is
--   spr.buffer.overflow; zero otherwise.
-- @param value the contents of the environment variable, if result is
--   spr.ok; truncated environment variable if result is
--   spr.buffer.overflow; undefined otherwise.
-- @param result spr.ok if the environment string successfully
--   returned; some other value otherwise, indicating the sort of
--   problem: spr.bad.name          the specified name is a null string
--   spr.bad.packet.size   (SIZE name) > sp.max.getenvname.size
--   spr.buffer.overflow   environment string too large for value; it
--   is truncated to fit >=spr.operation.failed  could not read
--   environment string - see hostio.inc or iserver documentation for
--   further details
PROC sp.getenv (CHAN OF SP fs, ts, VAL []BYTE name,
                INT length, []BYTE value, BYTE result)
  IF
    (SIZE name) > sp.max.getenvname.size
      SEQ
        result := spr.bad.packet.size
        length := 0
    (SIZE name) = 0
      SEQ
        result := spr.bad.name
        length := 0
    TRUE
      INT iresult:
      SEQ
        OC.getenv (iresult, name, length, value)
        IF
          iresult = 0
            result := spr.ok
          iresult = (-1)
            result := spr.buffer.overflow
          TRUE
            result := spr.operation.failed
:
--}}}  sp.getenv
--{{{  so.getenv
PROC so.getenv (CHAN OF SP fs, ts, VAL []BYTE name,
                INT length, []BYTE value, BYTE result)

  sp.getenv (fs, ts, name, length, value, result)
:
--}}}  so.getenv
