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 static LONG
getvar_from(const char *name
, const char *volume
, STRPTR buffer
, LONG size
, LONG flags
, struct DosLibrary
*DOSBase
);
16 /*****************************************************************************
20 #include <proto/dos.h>
22 AROS_LH4(LONG
, GetVar
,
25 AROS_LHA(CONST_STRPTR
, name
, D1
),
26 AROS_LHA(STRPTR
, buffer
, D2
),
27 AROS_LHA(LONG
, size
, D3
),
28 AROS_LHA(LONG
, flags
, D4
),
31 struct DosLibrary
*, DOSBase
, 151, Dos
)
34 This function will return the value of a local or environmental
35 variable in the supplied buffer.
37 It is advised to only use ASCII characters with a variable, but
40 If GVF_BINARY_VAR is not specified, this function will stop putting
41 characters into the destination buffer when a '\n' is hit, or the
42 end of the buffer is reached. Otherwise it will complete fill the
46 name - the name of the variable you want.
47 buffer - Space to store the returned variable.
48 size - Length of the buffer in bytes.
49 flags - A combination of the type of variable to get (lower
50 8 bits) and flags that control the value of this
51 function. Current flags are:
53 GVF_GLOBAL_ONLY - only tries to get a global variable.
54 GVF_LOCAL_ONLY - only tries to get a local variable.
55 GVF_BINARY_VAR - do not stop at a '\n' character.
56 GVF_DONT_NULL_TERM - no NULL termination. This only
57 applies to GVF_BINARY_VAR.
60 Will return the number of characters put in the buffer, or -1
61 if the variable is not defined. The '\n' character if it exists
62 will not be placed in the buffer.
64 If the value would overflow the user buffer, then the number of
65 characters copied into the buffer will be returned and the buffer
66 truncated.The buffer will be NULL terminated unless
67 GVF_DONT_NULL_TERM is set.
69 IoErr() will contain either:
70 ERROR_OBJECT_NOT_FOUND
71 if the variable is not defined.
73 if the size of the buffer is 0.
74 the total length of the variable
82 LV_VAR is the only type that can be global.
85 DeleteVar(), FindVar(), SetVar()
88 Redo the RESULT documentation.
90 *****************************************************************************/
94 D(bug("GetVar: name = \"%s\", buffer = $%lx, size = %ld, flags = $%lx\n",
95 name
, buffer
, size
, flags
));
99 D(bug("GetVar: bad size\n"));
101 SetIoErr(ERROR_BAD_NUMBER
);
108 /* not global only? */
109 if(0 == (flags
& GVF_GLOBAL_ONLY
))
111 /* look for a local variable */
114 D(bug("GetVar: Local variable\n"));
115 /* look for a variable of the given name */
116 lv
= FindVar(name
, flags
);
121 /* which size is shorter: the buffer or the size of
123 i
= (size
< lv
->lv_Len
) ? size
: lv
->lv_Len
;
124 CopyMem(lv
->lv_Value
, buffer
, i
);
126 /* were we supposed to stop after the first "\n"?
127 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM
129 if (0 == (flags
& GVF_BINARY_VAR
))
133 while ((buffer
[j
] != '\n') && (j
< i
))
143 buffer
[j
]= 0x0; /* mark end of string */
146 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
153 buffer
[i
] = 0x0; /* mark end of string */
161 SetIoErr(lv
->lv_Len
);
162 D(bug("GetVar: return %d\n", size
));
168 /****** GLOBAL VARIABLE TREATMENT ******/
170 if ((flags
& 0xff) == LV_VAR
&& !(flags
& GVF_LOCAL_ONLY
))
174 /* as standard: look for the file in ENV: if no path is
175 * given in the variable
177 ret
= getvar_from(name
, "ENV:", buffer
, size
, flags
, DOSBase
);
182 /* If not found in ENV:, look in ENVARC: */
183 ret
= getvar_from(name
, "ENVARC:", buffer
, size
, flags
, DOSBase
);
188 } /* ! local file only */
189 } /* name and buffer */
191 D(bug("GetVar: not found\n"));
193 SetIoErr(ERROR_OBJECT_NOT_FOUND
);
201 static LONG
getvar_from(const char *name
, const char *volume
, STRPTR buffer
, LONG size
, LONG flags
, struct DosLibrary
*DOSBase
)
208 strncpy(filebuf
, volume
, sizeof(filebuf
));
209 /* Just being paranoid here */
210 filebuf
[sizeof(filebuf
)-1]=0;
212 AddPart(filebuf
, name
, 256);
213 D(bug("GetVar: Global variable: %s\n", filebuf
));
214 file
= Open(filebuf
, MODE_OLDFILE
);
216 if (file
) /* file could be opened */
219 struct FileInfoBlock fib
;
221 if (ExamineFH(file
, &fib
))
223 /* fSize now contains the size of variable. */
224 fSize
= fib
.fib_Size
;
228 D(bug("GetVar: can't find size\n"));
233 /* We return the number of bytes actually read. */
234 i
= Read(file
, buffer
, size
);
237 /* were we supposed to stop after the first "\n"?
238 = No GVF_BINARY_VAR and no GVF_DONT_NULL_TERM */
239 if (0 == (flags
& GVF_BINARY_VAR
))
242 /* lets search for the first '\n' (if any) in the
243 * string and replace it by '\0'. */
244 while ((buffer
[j
] != '\n') && (j
< i
))
254 buffer
[j
]= '\0'; /* mark end of string */
257 else if (0 == (flags
& GVF_DONT_NULL_TERM
))
264 buffer
[i
] = 0x0; /* mark end of string */
273 D(bug("GetVar: return %d\n", size
));