]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/Environs.c
2 Implementation of the <stdlib.h> functions responsible for communication with
5 - atexit(void(*handler)(void))
9 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials are licensed and made available under
11 the terms and conditions of the BSD License that accompanies this distribution.
12 The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/ShellLib.h>
25 #include <LibConfig.h>
32 /** Internal worker function used by exit().
34 void exitCleanup(INTN ExitVal
);
36 /* ################# Public Functions ################################### */
38 /** The abort function causes abnormal program termination to occur, unless
39 the signal SIGABRT is being caught and the signal handler does not return.
41 Open streams with unwritten buffered data are not flushed, open
42 streams are not closed, and temporary files are not removed by abort.
51 if (gMD
->cleanup
!= NULL
) {
56 _Exit(EXIT_FAILURE
); // In case raise returns.
59 /** The atexit function registers the function pointed to by func, to be
60 called without arguments at normal program termination.
62 The implementation shall support the registration of
63 at least 32 functions.
65 @return The atexit function returns zero if the registration succeeds,
69 atexit(void (*handler
)(void))
73 if((handler
!= NULL
) && (gMD
->num_atexit
< ATEXIT_MAX
)) {
74 gMD
->atexit_handler
[gMD
->num_atexit
++] = handler
;
80 /** The exit function causes normal program termination to occur. If more than
81 one call to the exit function is executed by a program,
82 the behavior is undefined.
84 First, all functions registered by the atexit function are called, in the
85 reverse order of their registration. If, during the call to any such function, a
86 call to the longjmp function is made that would terminate the call to the
87 registered function, the behavior is undefined.
89 Next, all open streams with unwritten buffered data are flushed, all open
90 streams are closed, and all files created by the tmpfile function
93 The status returned to the host environment is determined in the same way
94 as for the _Exit function.
99 exitCleanup((INTN
) status
);
103 /** The _Exit function causes normal program termination to occur and control
104 to be returned to the host environment.
106 No functions registered by the atexit function or signal handlers
107 registered by the signal function are called. Open streams with unwritten
108 buffered data are not flushed, open streams are not closed, and temporary
109 files are not removed by abort.
111 Finally, control is returned to the host environment. If the value of
112 status is zero, or EXIT_SUCCESS, status is returned unchanged. If the value
113 of status is EXIT_FAILURE, RETURN_ABORTED is returned.
114 Otherwise, status is returned unchanged.
119 gMD
->ExitValue
= status
; // Save our exit status. Allows a status of 0.
120 longjmp(gMD
->MainExit
, 0x55); // Get out of here. longjmp can't return 0. Use 0x55 for a non-zero value.
123 _Exit(status
); /* Keep GCC happy - never reached */
127 /** If string is a null pointer, the system function determines whether the
128 host environment has a command processor. If string is not a null pointer,
129 the system function passes the string pointed to by string to that command
130 processor to be executed in a manner which the implementation shall
131 document; this might then cause the program calling system to behave in a
132 non-conforming manner or to terminate.
134 @retval EXIT_FAILURE EFIerrno will contain the EFI status code
135 indicating the cause of failure.
137 @retval EXIT_SUCCESS EFIerrno will contain the EFI status returned
138 by the executed command string.
139 @retval 0 If string is NULL, 0 means a command processor
141 @retval 1 If string is NULL, 1 means a command processor
145 system(const char *string
)
149 EFI_HANDLE MyHandle
= gImageHandle
;
151 if( string
== NULL
) {
154 (void)AsciiStrToUnicodeStr( string
, gMD
->UString
);
155 OpStat
= ShellExecute( &MyHandle
, gMD
->UString
, FALSE
, NULL
, &CmdStat
);
156 if(OpStat
== RETURN_SUCCESS
) {
164 /** The getenv function searches an environment list, provided by the host
165 environment, for a string that matches the string pointed to by name. The
166 set of environment names and the method for altering the environment list
167 are determined by the underlying UEFI Shell implementation.
169 @return The getenv function returns a pointer to a string associated with
170 the matched list member. The string pointed to shall not be
171 modified by the program, but may be overwritten by a subsequent
172 call to the getenv function. If the specified name cannot be
173 found, a null pointer is returned.
175 char *getenv(const char *name
)
177 const CHAR16
*EfiEnv
;
180 (void)AsciiStrToUnicodeStr( name
, gMD
->UString
);
181 EfiEnv
= ShellGetEnvironmentVariable(gMD
->UString
);
183 retval
= UnicodeStrToAsciiStr( EfiEnv
, gMD
->ASgetenv
);
191 Add or update a variable in the environment list
193 @param name Address of a zero terminated name string
194 @param value Address of a zero terminated value string
195 @param rewrite TRUE allows overwriting existing values
197 @retval Returns 0 upon success
198 @retval Returns -1 upon failure, sets errno with more information
202 EINVAL - name is NULL or points to a zero length string
203 EALREADY - name already set and rewrite set to FALSE
204 ENODEV - Unable to set non-volatile version of environment variable
205 ENOMEM - Unable to set volatile version of environment variable
206 ENOTSUP - Variable storage not supported
211 register const char * name
,
212 register const char * value
,
216 CONST CHAR16
* HostName
;
228 // Validate the inputs
231 if (( NULL
!= name
) && ( 0 != *name
)) {
233 // Get the storage locations for the unicode strings
235 UName
= &gMD
->UString
[0];
236 UValue
= &gMD
->UString2
[0];
239 // Convert the strings
241 AsciiStrToUnicodeStr ( name
, UName
);
242 AsciiStrToUnicodeStr ( value
, UValue
);
245 // Determine if the string is already present
248 HostName
= ShellGetEnvironmentVariable ( UName
);
249 if ( rewrite
|| ( NULL
== HostName
)) {
251 // Support systems that don't have non-volatile memory
254 Status
= ShellSetEnvironmentVariable ( UName
, UValue
, TRUE
);
255 if ( EFI_ERROR ( Status
)) {
256 if ( EFI_UNSUPPORTED
== Status
) {
262 // Permanently set the environment variable
265 Status
= ShellSetEnvironmentVariable ( UName
, UValue
, FALSE
);
266 if ( !EFI_ERROR ( Status
)) {
278 // Return the operation status