Indentation fix, cleanup.
[AROS.git] / compiler / stdc / getenv.c
blobaec21af0331733fcfc404f305592292850f47aad
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function getenv().
6 */
8 #include <proto/exec.h>
9 #include <proto/dos.h>
11 #include <errno.h>
13 #include "__stdcio_intbase.h"
15 #define DEBUG 0
16 #include <aros/debug.h>
18 /*****************************************************************************
20 NAME */
21 #include <stdlib.h>
23 char *getenv (
25 /* SYNOPSIS */
26 const char *name)
28 /* FUNCTION
29 Get an environment variable.
31 INPUTS
32 name - Name of the environment variable.
34 RESULT
35 Pointer to the variable's value, or NULL on failure.
36 When no memory is available errno will be set to ENOMEM.
38 NOTES
39 The returned contents of the environment variable is cached per
40 StdCIOBase. So the returned value is valid and does not change
41 until a next call to getenv on the same StdCIOBase.
43 EXAMPLE
45 BUGS
47 SEE ALSO
49 INTERNALS
50 Based on libnix getenv
52 ******************************************************************************/
54 struct StdCIOIntBase *StdCIOBase =
55 (struct StdCIOIntBase *)__aros_getbase_StdCIOBase;
56 char c;
59 This will always return 0 if the var exists and EOF if it doesn't,
60 then we'll be able to retrieve the var length with IoErr()
62 if (!GetVar((char *)name, &c, 1, GVF_BINARY_VAR))
64 LONG len = IoErr();
66 D(bug("[getenv] Variable found of size %d\n", size));
68 if (len + 1 > StdCIOBase->varsize)
70 if (StdCIOBase->envvar)
71 FreeMem(StdCIOBase->envvar, StdCIOBase->varsize);
73 StdCIOBase->envvar = AllocMem(len + 1, MEMF_ANY);
74 if (StdCIOBase->envvar == NULL)
76 StdCIOBase->varsize = 0;
77 errno = ENOMEM;
78 return NULL;
80 StdCIOBase->varsize = len + 1;
83 /* This should not fail, unless someone stole our variable */
84 /* FIXME: maybe this function should be atomic */
85 GetVar((char *)name, StdCIOBase->envvar, StdCIOBase->varsize, GVF_BINARY_VAR);
87 D(bug("[getenv] Got value \"%s\"\n", StdCIOBase->envvar));
89 return (StdCIOBase->envvar);
91 else
93 D(bug("[getenv] Variable not found\n"));
95 return NULL;
97 } /* getenv */