some fixes to accented characters
[tangerine.git] / rom / dos / getvar.c
bloba54f20747b61b60a5c5ccabd999f4fc0a648018a
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: GetVar - Return the value of a local or global variable.
6 Lang: English
7 */
8 #include <aros/debug.h>
9 #include <dos/dos.h>
10 #include <proto/exec.h>
11 #include "dos_intern.h"
13 /*****************************************************************************
15 NAME */
16 #include <dos/var.h>
17 #include <proto/dos.h>
19 AROS_LH4(LONG, GetVar,
21 /* SYNOPSIS */
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),
27 /* LOCATION */
28 struct DosLibrary *, DOSBase, 151, Dos)
30 /* FUNCTION
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
35 this is not required.
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
40 buffer.
42 INPUTS
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.
56 RESULT
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.
69 ERROR_BAD_NUMBER
70 if the size of the buffer is 0.
71 the total length of the variable
72 otherwise.
74 NOTES
76 EXAMPLE
78 BUGS
79 LV_VAR is the only type that can be global.
81 SEE ALSO
82 DeleteVar(), FindVar(), SetVar()
84 INTERNALS
85 Redo the RESULT documentation.
87 *****************************************************************************/
89 AROS_LIBFUNC_INIT
91 D(bug("GetVar: name = \"%s\", buffer = $%lx, size = %ld, flags = $%lx\n",
92 name, buffer, size, flags));
94 if (0 == size)
96 D(bug("GetVar: bad size\n"));
98 SetIoErr(ERROR_BAD_NUMBER);
100 return 0;
103 if (name && buffer)
105 /* not global only? */
106 if(0 == (flags & GVF_GLOBAL_ONLY))
108 /* look for a local variable */
109 struct LocalVar *lv;
111 /* look for a variable of the given name */
112 lv = FindVar(name, flags);
114 if (lv)
116 int i;
117 /* which size is shorter: the buffer or the size of
118 the value? */
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))
127 int j = 0;
129 while ((buffer[j] != '\n') && (j < i))
131 j++;
134 if (j == size)
136 j = size - 1;
139 buffer[j]= 0x0; /* mark end of string */
140 size = j;
142 else if (0 == (flags & GVF_DONT_NULL_TERM))
144 if (i == size)
146 i = size - 1;
149 buffer[i] = 0x0; /* mark end of string */
150 size = i;
152 else
154 size = i;
157 SetIoErr(lv->lv_Len);
158 D(bug("GetVar: return %d\n", size));
160 return size;
161 } /* Got lv */
162 } /* !global only */
164 /****** GLOBAL VARIABLE TREATMENT ******/
166 if ((flags & 0xff) == LV_VAR && !(flags & GVF_LOCAL_ONLY))
168 BPTR file;
169 LONG i;
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 */
180 ULONG fSize;
181 struct FileInfoBlock fib;
183 if (ExamineFH(file, &fib))
185 /* fSize now contains the size of variable. */
186 fSize = fib.fib_Size;
188 else
190 D(bug("GetVar: can't find size\n"));
192 return -1;
195 /* We return the number of bytes actually read. */
196 i = Read(file, buffer, size);
197 Close(file);
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))
203 int j = 0;
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))
208 j++;
211 if (j == size)
213 j = size - 1;
216 buffer[j]= '\0'; /* mark end of string */
217 size = j;
219 else if (0 == (flags & GVF_DONT_NULL_TERM))
221 if (i == size)
223 i = size - 1;
226 buffer[i] = 0x0; /* mark end of string */
227 size = i;
229 else
231 size = i;
234 SetIoErr(fSize);
235 D(bug("GetVar: return %d\n", size));
237 return size;
238 } /* open(file) */
239 } /* ! local file only */
240 } /* name and buffer */
242 D(bug("GetVar: not found\n"));
244 SetIoErr(ERROR_OBJECT_NOT_FOUND);
246 return -1;
248 AROS_LIBFUNC_EXIT
249 } /* GetVar */