2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: GetVar - Return the value of a local or global variable.
8 #include <aros/debug.h>
10 #include <proto/exec.h>
11 #include "dos_intern.h"
13 /*****************************************************************************
17 #include <proto/dos.h>
19 AROS_LH4(LONG
, GetVar
,
22 AROS_LHA(CONST_STRPTR
, name
, D1
),
23 AROS_LHA(STRPTR
, buffer
, D2
),
24 AROS_LHA(LONG
, size
, D3
),
25 AROS_LHA(LONG
, flags
, D4
),
28 struct DosLibrary
*, DOSBase
, 151, Dos
)
31 This function will return the value of a local or environmental
32 variable in the supplied buffer.
34 It is advised to only use ASCII characters with a variable, but
37 If GVF_BINARY_VAR is not specified, this function will stop putting
38 characters into the destination buffer when a '\n' is hit, or the
39 end of the buffer is reached. Otherwise it will complete fill the
43 name - the name of the variable you want.
44 buffer - Space to store the returned variable.
45 size - Length of the buffer in bytes.
46 flags - A combination of the type of variable to get (lower
47 8 bits) and flags that control the value of this
48 function. Current flags are:
50 GVF_GLOBAL_ONLY - only tries to get a global variable.
51 GVF_LOCAL_ONLY - only tries to get a local variable.
52 GVF_BINARY_VAR - do not stop at a '\n' character.
53 GVF_DONT_NULL_TERM - no NULL termination. This only
54 applies to GVF_BINARY_VAR.
57 Will return the number of characters put in the buffer, or -1
58 if the variable is not defined. The '\n' character if it exists
59 will not be placed in the buffer.
61 If the value would overflow the user buffer, then the number of
62 characters copied into the buffer will be returned and the buffer
63 truncated.The buffer will be NULL terminated unless
64 GVF_DONT_NULL_TERM is set.
66 IoErr() will contain either:
67 ERROR_OBJECT_NOT_FOUND
68 if the variable is not defined.
70 if the size of the buffer is 0.
71 the total length of the variable
79 LV_VAR is the only type that can be global.
82 DeleteVar(), FindVar(), SetVar()
85 Redo the RESULT documentation.
87 *****************************************************************************/
91 D(bug("GetVar: name = \"%s\", buffer = $%lx, size = %ld, flags = $%lx\n",
92 name
, buffer
, size
, flags
));
96 D(bug("GetVar: bad size\n"));
98 SetIoErr(ERROR_BAD_NUMBER
);
105 /* not global only? */
106 if(0 == (flags
& GVF_GLOBAL_ONLY
))
108 /* look for a local variable */
111 /* look for a variable of the given name */
112 lv
= FindVar(name
, flags
);
117 /* which size is shorter: the buffer or the size of
119 i
= (size
< lv
->lv_Len
) ? size
: lv
->lv_Len
;
120 CopyMem(lv
->lv_Value
, buffer
, i
);
122 /* were we supposed to stop after the first "\n"?
123 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM
125 if (0 == (flags
& GVF_BINARY_VAR
))
129 while ((buffer
[j
] != '\n') && (j
< i
))
139 buffer
[j
]= 0x0; /* mark end of string */
142 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
149 buffer
[i
] = 0x0; /* mark end of string */
157 SetIoErr(lv
->lv_Len
);
158 D(bug("GetVar: return %d\n", size
));
164 /****** GLOBAL VARIABLE TREATMENT ******/
166 if ((flags
& 0xff) == LV_VAR
&& !(flags
& GVF_LOCAL_ONLY
))
171 /* as standard: look for the file in ENV: if no path is
172 given in the variable */
173 UBYTE filebuf
[256] = "ENV:";
175 AddPart(filebuf
, name
, 256);
176 file
= Open(filebuf
, MODE_OLDFILE
);
178 if (file
) /* file could be opened */
181 struct FileInfoBlock fib
;
183 if (ExamineFH(file
, &fib
))
185 /* fSize now contains the size of variable. */
186 fSize
= fib
.fib_Size
;
190 D(bug("GetVar: can't find size\n"));
195 /* We return the number of bytes actually read. */
196 i
= Read(file
, buffer
, size
);
199 /* were we supposed to stop after the first "\n"?
200 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM */
201 if (0 == (flags
& GVF_BINARY_VAR
))
204 /* lets search for the first '\n' (if any) in the
205 * string and replace it by '\0'. */
206 while ((buffer
[j
] != '\n') && (j
< i
))
216 buffer
[j
]= '\0'; /* mark end of string */
219 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
226 buffer
[i
] = 0x0; /* mark end of string */
235 D(bug("GetVar: return %d\n", size
));
239 } /* ! local file only */
240 } /* name and buffer */
242 D(bug("GetVar: not found\n"));
244 SetIoErr(ERROR_OBJECT_NOT_FOUND
);