Add new strings.
[AROS.git] / rom / dos / getvar.c
blobad407db10af48c802054c0204bf2bce69e299dab
1 /*
2 Copyright © 1995-2010, 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 */
9 #include <aros/debug.h>
10 #include <dos/dos.h>
11 #include <proto/exec.h>
12 #include "dos_intern.h"
14 /*****************************************************************************
16 NAME */
17 #include <dos/var.h>
18 #include <proto/dos.h>
20 AROS_LH4(LONG, GetVar,
22 /* SYNOPSIS */
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),
28 /* LOCATION */
29 struct DosLibrary *, DOSBase, 151, Dos)
31 /* FUNCTION
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
36 this is not required.
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
41 buffer.
43 INPUTS
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.
57 RESULT
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.
70 ERROR_BAD_NUMBER
71 if the size of the buffer is 0.
72 the total length of the variable
73 otherwise.
75 NOTES
77 EXAMPLE
79 BUGS
80 LV_VAR is the only type that can be global.
82 SEE ALSO
83 DeleteVar(), FindVar(), SetVar()
85 INTERNALS
86 Redo the RESULT documentation.
88 *****************************************************************************/
90 AROS_LIBFUNC_INIT
92 D(bug("GetVar: name = \"%s\", buffer = $%lx, size = %ld, flags = $%lx\n",
93 name, buffer, size, flags));
95 if (0 == size)
97 D(bug("GetVar: bad size\n"));
99 SetIoErr(ERROR_BAD_NUMBER);
101 return 0;
104 if (name && buffer)
106 /* not global only? */
107 if(0 == (flags & GVF_GLOBAL_ONLY))
109 /* look for a local variable */
110 struct LocalVar *lv;
112 D(bug("GetVar: Local variable\n"));
113 /* look for a variable of the given name */
114 lv = FindVar(name, flags);
116 if (lv)
118 int i;
119 /* which size is shorter: the buffer or the size of
120 the value? */
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))
129 int j = 0;
131 while ((buffer[j] != '\n') && (j < i))
133 j++;
136 if (j == size)
138 j = size - 1;
141 buffer[j]= 0x0; /* mark end of string */
142 size = j;
144 else if (0 == (flags & GVF_DONT_NULL_TERM))
146 if (i == size)
148 i = size - 1;
151 buffer[i] = 0x0; /* mark end of string */
152 size = i;
154 else
156 size = i;
159 SetIoErr(lv->lv_Len);
160 D(bug("GetVar: return %d\n", size));
162 return size;
163 } /* Got lv */
164 } /* !global only */
166 /****** GLOBAL VARIABLE TREATMENT ******/
168 if ((flags & 0xff) == LV_VAR && !(flags & GVF_LOCAL_ONLY))
170 BPTR file;
171 LONG i;
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 */
183 ULONG fSize;
184 struct FileInfoBlock fib;
186 if (ExamineFH(file, &fib))
188 /* fSize now contains the size of variable. */
189 fSize = fib.fib_Size;
191 else
193 D(bug("GetVar: can't find size\n"));
195 return -1;
198 /* We return the number of bytes actually read. */
199 i = Read(file, buffer, size);
200 Close(file);
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))
206 int j = 0;
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))
211 j++;
214 if (j == size)
216 j = size - 1;
219 buffer[j]= '\0'; /* mark end of string */
220 size = j;
222 else if (0 == (flags & GVF_DONT_NULL_TERM))
224 if (i == size)
226 i = size - 1;
229 buffer[i] = 0x0; /* mark end of string */
230 size = i;
232 else
234 size = i;
237 SetIoErr(fSize);
238 D(bug("GetVar: return %d\n", size));
240 return size;
241 } /* open(file) */
242 } /* ! local file only */
243 } /* name and buffer */
245 D(bug("GetVar: not found\n"));
247 SetIoErr(ERROR_OBJECT_NOT_FOUND);
249 return -1;
251 AROS_LIBFUNC_EXIT
252 } /* GetVar */