2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
8 #include <proto/exec.h>
13 #include "__stdcio_intbase.h"
16 #include <aros/debug.h>
18 /*****************************************************************************
29 Get an environment variable.
32 name - Name of the environment variable.
35 Pointer to the variable's value, or NULL on failure.
36 When no memory is available errno will be set to ENOMEM.
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.
50 Based on libnix getenv
52 ******************************************************************************/
54 struct StdCIOIntBase
*StdCIOBase
=
55 (struct StdCIOIntBase
*)__aros_getbase_StdCIOBase
;
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 lenght with IoErr()
62 if (!GetVar((char *)name
, &c
, 1, GVF_BINARY_VAR
))
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;
80 StdCIOBase
->varsize
= len
+ 1;
83 /* This should not fail, unless someone stealt 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
);
93 D(bug("[getenv] Variable not found\n"));