2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
5 Desc: GetVar - Return the value of a local or global variable.
9 #include <aros/debug.h>
11 #include <proto/exec.h>
12 #include "dos_intern.h"
14 /*****************************************************************************
18 #include <proto/dos.h>
20 AROS_LH4(LONG
, GetVar
,
23 AROS_LHA(CONST_STRPTR
, name
, D1
),
24 AROS_LHA(STRPTR
, buffer
, D2
),
25 AROS_LHA(LONG
, size
, D3
),
26 AROS_LHA(LONG
, flags
, D4
),
29 struct DosLibrary
*, DOSBase
, 151, Dos
)
32 This function will return the value of a local or environmental
33 variable in the supplied buffer.
35 It is advised to only use ASCII characters with a variable, but
38 If GVF_BINARY_VAR is not specified, this function will stop putting
39 characters into the destination buffer when a '\n' is hit, or the
40 end of the buffer is reached. Otherwise it will complete fill the
44 name - the name of the variable you want.
45 buffer - Space to store the returned variable.
46 size - Length of the buffer in bytes.
47 flags - A combination of the type of variable to get (lower
48 8 bits) and flags that control the value of this
49 function. Current flags are:
51 GVF_GLOBAL_ONLY - only tries to get a global variable.
52 GVF_LOCAL_ONLY - only tries to get a local variable.
53 GVF_BINARY_VAR - do not stop at a '\n' character.
54 GVF_DONT_NULL_TERM - no NULL termination. This only
55 applies to GVF_BINARY_VAR.
58 Will return the number of characters put in the buffer, or -1
59 if the variable is not defined. The '\n' character if it exists
60 will not be placed in the buffer.
62 If the value would overflow the user buffer, then the number of
63 characters copied into the buffer will be returned and the buffer
64 truncated.The buffer will be NULL terminated unless
65 GVF_DONT_NULL_TERM is set.
67 IoErr() will contain either:
68 ERROR_OBJECT_NOT_FOUND
69 if the variable is not defined.
71 if the size of the buffer is 0.
72 the total length of the variable
80 LV_VAR is the only type that can be global.
83 DeleteVar(), FindVar(), SetVar()
86 Redo the RESULT documentation.
88 *****************************************************************************/
92 D(bug("GetVar: name = \"%s\", buffer = $%lx, size = %ld, flags = $%lx\n",
93 name
, buffer
, size
, flags
));
97 D(bug("GetVar: bad size\n"));
99 SetIoErr(ERROR_BAD_NUMBER
);
106 /* not global only? */
107 if(0 == (flags
& GVF_GLOBAL_ONLY
))
109 /* look for a local variable */
112 D(bug("GetVar: Local variable\n"));
113 /* look for a variable of the given name */
114 lv
= FindVar(name
, flags
);
119 /* which size is shorter: the buffer or the size of
121 i
= (size
< lv
->lv_Len
) ? size
: lv
->lv_Len
;
122 CopyMem(lv
->lv_Value
, buffer
, i
);
124 /* were we supposed to stop after the first "\n"?
125 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM
127 if (0 == (flags
& GVF_BINARY_VAR
))
131 while ((buffer
[j
] != '\n') && (j
< i
))
141 buffer
[j
]= 0x0; /* mark end of string */
144 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
151 buffer
[i
] = 0x0; /* mark end of string */
159 SetIoErr(lv
->lv_Len
);
160 D(bug("GetVar: return %d\n", size
));
166 /****** GLOBAL VARIABLE TREATMENT ******/
168 if ((flags
& 0xff) == LV_VAR
&& !(flags
& GVF_LOCAL_ONLY
))
173 /* as standard: look for the file in ENV: if no path is
174 given in the variable */
175 UBYTE filebuf
[256] = "ENV:";
177 AddPart(filebuf
, name
, 256);
178 D(bug("GetVar: Global variable: %s\n", filebuf
));
179 file
= Open(filebuf
, MODE_OLDFILE
);
181 if (file
) /* file could be opened */
184 struct FileInfoBlock fib
;
186 if (ExamineFH(file
, &fib
))
188 /* fSize now contains the size of variable. */
189 fSize
= fib
.fib_Size
;
193 D(bug("GetVar: can't find size\n"));
198 /* We return the number of bytes actually read. */
199 i
= Read(file
, buffer
, size
);
202 /* were we supposed to stop after the first "\n"?
203 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM */
204 if (0 == (flags
& GVF_BINARY_VAR
))
207 /* lets search for the first '\n' (if any) in the
208 * string and replace it by '\0'. */
209 while ((buffer
[j
] != '\n') && (j
< i
))
219 buffer
[j
]= '\0'; /* mark end of string */
222 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
229 buffer
[i
] = 0x0; /* mark end of string */
238 D(bug("GetVar: return %d\n", size
));
242 } /* ! local file only */
243 } /* name and buffer */
245 D(bug("GetVar: not found\n"));
247 SetIoErr(ERROR_OBJECT_NOT_FOUND
);