/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: s_genv.c,v 1.2 1997/09/25 17:59:41 ae59 Exp $ */
/*
$Log: s_genv.c,v $
 * Revision 1.2  1997/09/25  17:59:41  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:39  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_genv.c                              */
/*                                                              */
/*      Entries         : void s_genv(name,value,exists)        */
/*                        s_trng name;                          */
/*                        s_trng *value;                        */
/*                        a_bool *exists;                       */
/*                                                              */
/*      Arguments       : name = name of environment variable   */
/*                        value = value of environment variable */
/*                        exists = TRUE if variable exists      */
/*                                                              */
/*      Description     : Determine value of environment        */
/*                        variable using call to getenv().      */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : cast to char* in malloc    =b=             */
/*      1991-03-14 : string not displayed if ALLOCATION error   */
/*      1990-12-03 : one more allocated for direct use of '\0'  */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local void s_genv(s_trng name,s_trng *value,a_bool *exists)
#else
local void s_genv(name,value,exists)

s_trng name;
s_trng *value;
a_bool *exists;
#endif
        {
        char *p,ch;
        size_t len;

        E_TPUSH("s_genv")

        if (name.clen==0)
           {
           e_trap(I_O_ERROR,2,E_TMSG,61);
           *exists = FALSE;
           value->clen = 0;
           }
        else
           {
           if (name.suba) s_asgn(&name,name);
           ch = name.ptr[name.clen];
           name.ptr[name.clen] = '\0';
           if ((p = getenv(name.ptr))==NULL)
              {
              *exists = FALSE;
              value->clen = 0;
              }
           else
              {
              *exists = TRUE;
              if ((len = strlen(p))>value->alen)
                 {
                 if (value->fix)
                    len = value->alen;
                 else
                    {
                    if (value->alen>0)
                       {
#ifdef HEAP_CHECK
b_freh((a_char *)&value->ptr,(a_char *)value->ptr,(a_char *)"s_genv");
#endif
                       free(value->ptr);
                       }
                    if ((value->ptr = (char*) malloc(len+1))==NULL)
                       {
                       e_trap(ALLOCATION,2,E_TMSG,54);
                       len = 0;
                       }
                    else
                       {
#ifdef HEAP_CHECK
b_geth((a_char *)&value->ptr,(a_char *)value->ptr,(a_char *)"s_genv");
#endif
                       value->alen = len;
                       }
                    }
                 }
              if (len>0) memcpy(value->ptr,p,len);
              value->clen = len;
              }
           name.ptr[name.clen] = ch;
           }

        if (name.tmp) s_free(&name);

        E_TPOPP("s_genv")
        return;
        }

