Use more appropriate types for pt_regs struct, e.g. 16-bit types for 16-bit
[cake.git] / rom / dos / setvar.c
blob56c43b64a780644b7d107fa48044f5859465c638
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 #include "dos_intern.h"
10 #include <exec/memory.h>
11 #include <dos/dos.h>
12 #include <dos/dosextens.h>
13 #include <proto/exec.h>
14 #include <proto/utility.h>
15 #include <exec/lists.h>
16 #include <string.h>
18 /*****************************************************************************
20 NAME */
21 #include <dos/var.h>
22 #include <proto/dos.h>
24 AROS_LH4(BOOL, SetVar,
26 /* SYNOPSIS */
27 AROS_LHA(CONST_STRPTR, name, D1),
28 AROS_LHA(CONST_STRPTR, buffer, D2),
29 AROS_LHA(LONG , size, D3),
30 AROS_LHA(LONG , flags, D4),
32 /* LOCATION */
33 struct DosLibrary *, DOSBase, 150, Dos)
35 /* FUNCTION
36 This function will set a local or environmental variable. Although
37 it is recommended that you only use ASCII strings within variables,
38 this is not actually required.
40 Variable names are not case sensitive.
42 SetVar() for an already existing variable changes the variable's
43 value to "buffer".
45 INPUTS
46 name - The name of the variable to set.
47 buffer - The actual data of the variable.
48 size - The size of the data in the buffer.
49 flags - Combination of the type of variable to set (lower
50 8 bits of the value), and various flags which control
51 this function. Flags defined are:
53 GVF_LOCAL_ONLY - set a local variable only,
54 GVF_GLOBAL_ONLY - set a global environmental
55 variable only.
56 GVF_SAVE_VAR - If GVF_GLOBAL_ONLY is set, then
57 this flag will cause SetVar() to
58 save the variable to ENVARC: as well
59 as to ENV:.
60 GVF_BINARY_VAR and GVF_DONT_NULL_TERM are stored in
61 the lv_Flags field for local variables, but not
62 used otherwise by SetVar().
64 Note the default is to set a local environmental
65 variable.
67 The following variable types are defined:
68 LV_VAR - local environment variable
69 LV_ALIAS - shell alias
70 LVF_IGNORE - internal shell use
72 LV_VAR and LV_ALIAS should be treated as
73 "exclusive or".
76 RESULT
77 Zero if this function failed, non-zero otherwise.
79 NOTES
80 It is possible to have two variables with the same name as
81 long as they have different types.
83 EXAMPLE
85 BUGS
86 Only type LV_VAR can be made global.
88 If you set GVF_SAVE_VAR, and this function returns failure, the
89 variable may have still been set in ENV:.
91 SEE ALSO
92 DeleteVar(), FindVar(), GetVar(),
94 INTERNALS
95 See FindVar() for a description of the inner workings of local
96 variables.
98 *****************************************************************************/
100 AROS_LIBFUNC_INIT
102 /* valid input? */
103 if(name && buffer)
105 /* Local variable is default. */
106 if((flags & GVF_GLOBAL_ONLY) == 0)
108 ULONG nameLen = strlen(name);
109 struct LocalVar *lv;
111 /* does a Variable with that name already exist? */
112 if (NULL != (lv = FindVar(name, flags)))
114 /* delete old value of that existing variable */
115 FreeMem(lv->lv_Value,lv->lv_Len);
117 else
120 ** create a LocalVar-structure and insert it into the list
122 if (NULL != (lv = AllocVec(sizeof(struct LocalVar) + nameLen + 1,
123 MEMF_CLEAR|MEMF_PUBLIC) ) )
125 struct Process *pr = (struct Process *)FindTask(NULL);
126 struct LocalVar *n = (struct LocalVar *)pr->pr_LocalVars.mlh_Head;
128 /* init the newly created structure */
130 lv->lv_Node.ln_Type = flags; /* ln_Type is UBYTE! */
131 lv->lv_Node.ln_Name = (UBYTE *)lv + sizeof(struct LocalVar);
132 CopyMem(name, lv->lv_Node.ln_Name, nameLen);
133 lv->lv_Flags = flags & (GVF_BINARY_VAR|GVF_DONT_NULL_TERM);
136 ** First let's see whether we have to add it at the head
137 ** of the list as the list is still empty OR
138 ** the very first element is already greater than the one
139 ** we want to insert
142 if (n == (struct LocalVar *)&(pr->pr_LocalVars.mlh_Tail) ||
143 Stricmp(name, n->lv_Node.ln_Name) < 0)
145 AddHead((struct List *)&pr->pr_LocalVars,
146 (struct Node *)lv);
148 else
151 ** Now we can be sure that we will have to insert
152 ** somewhere behind the first element in the list
154 ForeachNode(&pr->pr_LocalVars, n)
156 if (Stricmp(name, n->lv_Node.ln_Name) < 0)
158 break;
162 if (NULL != n->lv_Node.ln_Succ)
164 Insert((struct List *)&pr->pr_LocalVars ,
165 (struct Node *) lv ,
166 (struct Node *) n->lv_Node.ln_Pred);
168 else
170 AddTail((struct List *)&pr->pr_LocalVars,
171 (struct Node *) lv);
177 /* -1 as size means: buffer contains a null-terminated string */
178 if (-1 == size)
180 /* Do NOT add 1 byte to account for the NUL char, AmigaOS(R) doesn't
181 do it that way. */
182 lv->lv_Len = strlen(buffer);
184 else
186 lv->lv_Len = size;
189 /* now get some memory for the value */
190 lv->lv_Value = AllocMem(lv->lv_Len, MEMF_PUBLIC);
192 CopyMem(buffer, lv->lv_Value, lv->lv_Len);
194 if (flags & GVF_LOCAL_ONLY)
195 return DOSTRUE;
196 } /* set a local variable */
198 /* Ok, try and set a global variable. */
199 if ((flags & GVF_LOCAL_ONLY) == 0)
201 BPTR file;
202 /* as a standard: look for the file in ENV: if no path is
203 given in the variable */
204 UBYTE nameBuffer[384]= "ENV:";
206 AddPart(nameBuffer, name, 384);
208 /* Just try and open the file */
209 file = Open(nameBuffer, MODE_NEWFILE);
211 if (file != NULL)
213 /* Write the data to the file */
214 /* size = -1 means that the value is a null-terminated
215 string */
216 if (-1 == size)
218 Write(file, buffer, strlen(buffer));
220 else
222 Write(file, buffer, size);
225 Close(file);
227 else
229 return DOSFALSE;
232 /* Let's see whether we're supposed to make a copy of this to
233 * envarc also...
235 if (0 != (flags & GVF_SAVE_VAR))
237 CopyMem("ENVARC:", nameBuffer, 8);
238 AddPart(nameBuffer, name, 384);
240 file = Open(nameBuffer, MODE_NEWFILE);
242 if (file != NULL)
244 /* Write the data to the file */
245 /* size = -1 means that the value is a null-terminated
246 string */
247 if (-1 == size)
249 Write(file, buffer, strlen(buffer));
251 else
253 Write(file, buffer, size);
256 Close(file);
260 /* We created both, bye bye */
261 return DOSTRUE;
262 } /* try a global variable */
263 } /* input was valid */
265 return DOSFALSE;
267 AROS_LIBFUNC_EXIT
268 } /* SetVar */