grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / c / shellcommands / Setenv.c
blobcce591315fc64be93a2653d2bc1a3df1f7e3f344
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Setenv CLI command
6 Lang: English
7 */
9 /*****************************************************************************
11 NAME
13 Setenv
15 SYNOPSIS
17 NAME,SAVE/S,STRING/F
19 LOCATION
23 FUNCTION
25 Sets a global variable from the current shell. These variables can
26 be accessed from any program executing at any time.
28 These variables are usually not saved in the ENVARC: directory, hence they
29 can only be used by programs during the current execution of the
30 operating system. When using SAVE argument, the variable is also saved
31 in ENVARC:
33 If no parameters are specified, the current list of global variables
34 are displayed.
36 INPUTS
38 NAME - The name of the global variable to set.
40 SAVE - Save the variable also in ENVARC:
42 STRING - The value of the global variable NAME.
44 RESULT
46 Standard DOS error codes.
48 NOTES
50 EXAMPLE
52 Setenv EDITOR Ed
54 Any program that accesses the variable "EDITOR" will be able to
55 find out the name of the text-editor the user would like to use,
56 by examining the contents of the variable.
58 BUGS
60 SEE ALSO
62 Getenv, Unsetenv
64 INTERNALS
66 HISTORY
68 30-Jul-1997 laguest Initial inclusion into the AROS tree
69 13-Aug-1997 srittau Minor changes
71 ******************************************************************************/
73 #include <proto/dos.h>
74 #include <proto/exec.h>
76 #include <dos/dos.h>
77 #include <dos/exall.h>
78 #include <dos/rdargs.h>
79 #include <dos/var.h>
80 #include <exec/memory.h>
81 #include <exec/types.h>
83 #include <utility/tagitem.h>
84 #include <stdio.h>
86 #include <aros/shcommands.h>
88 AROS_SH3(Setenv, 45.0,
89 AROS_SHA(STRPTR, ,NAME, , NULL),
90 AROS_SHA(IPTR , ,SAVE, /S, NULL),
91 AROS_SHA(STRPTR, ,STRING, /F, NULL))
93 AROS_SHCOMMAND_INIT
95 int Return_Value;
96 BOOL Success;
97 BPTR lock;
98 TEXT progname[108];
101 GetProgramName(progname, sizeof(progname));
102 Return_Value = RETURN_OK;
104 if (SHArg(NAME) != NULL || SHArg(STRING) != NULL)
106 /* Make sure we get to here is either arguments are
107 * provided on the command line.
109 if (SHArg(NAME) != NULL && SHArg(STRING) != NULL)
111 /* Add the new global variable to the list.
113 Success = SetVar(SHArg(NAME),
114 SHArg(STRING),
116 SHArg(SAVE) ? GVF_GLOBAL_ONLY | GVF_SAVE_VAR : GVF_GLOBAL_ONLY);
118 if (Success == FALSE)
120 UBYTE buf[FAULT_MAX+128];
122 Fault(-141, NULL, buf, sizeof(buf));
123 Printf(buf, SHArg(NAME));
124 PrintFault(IoErr(), progname);
126 Return_Value = RETURN_ERROR;
130 else
132 /* Display a list of global variables.
135 lock = Lock("ENV:", ACCESS_READ);
136 if (lock)
138 struct FileInfoBlock *FIB = AllocDosObject(DOS_FIB, NULL);
140 if (FIB)
142 if(Examine(lock, FIB))
144 while(ExNext(lock, FIB))
146 /* don't show dirs */
147 if (FIB->fib_DirEntryType < 0)
149 PutStr(FIB->fib_FileName);
150 PutStr("\n");
154 FreeDosObject(DOS_FIB, FIB);
156 UnLock(lock);
158 else
160 PrintFault(IoErr(), progname);
162 Return_Value = RETURN_FAIL;
166 return Return_Value;
168 AROS_SHCOMMAND_EXIT
169 } /* main */