]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/Include/stdlib.h
2 The header <stdlib.h> declares five types and several functions of general
3 utility, and defines several macros.
5 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <sys/EfiCdefs.h>
20 typedef _EFI_SIZE_T_
size_t;
27 typedef _EFI_WCHAR_T
wchar_t;
33 /// A structure type that is the type of the value returned by the div function.
35 int quot
; /* quotient */
36 int rem
; /* remainder */
39 /// A structure type that is the type of the value returned by the ldiv function.
45 /// A structure type that is the type of the value returned by the lldiv function.
51 /** Expand to integer constant expressions that can be used as the argument to
52 the exit function to return unsuccessful or successful termination status,
53 respectively, to the host environment.
55 #define EXIT_FAILURE 1
56 #define EXIT_SUCCESS 0
58 /** Expands to an integer constant expression that is the maximum value
59 returned by the rand function.
61 The value of the RAND_MAX macro shall be at least 32767.
63 #define RAND_MAX 0x7fffffff
65 /** Expands to a positive integer expression with type size_t that is the
66 maximum number of bytes in a multibyte character for the extended character
67 set specified by the current locale (category LC_CTYPE), which is never
68 greater than MB_LEN_MAX.
72 /** Maximum number of functions that can be registered by atexit.
74 The C standard states that the implementation shall support the
75 registration of at least 32 functions.
81 /* ################ Communication with the environment ################## */
83 /** The abort function causes abnormal program termination to occur, unless
84 the signal SIGABRT is being caught and the signal handler does not return.
86 Open streams with unwritten buffered data are not flushed, open
87 streams are not closed, and temporary files are not removed by abort.
89 Unsuccessful termination is returned to the host environment by means of
90 the function call, raise(SIGABRT).
96 /** The atexit function registers the function pointed to by func, to be
97 called without arguments at normal program termination.
99 The implementation supports the registration of up to 32 functions.
101 @return The atexit function returns zero if the registration succeeds,
104 int atexit(void (*)(void));
106 /** The exit function causes normal program termination to occur. If more than
107 one call to the exit function is executed by a program,
108 the behavior is undefined.
110 First, all functions registered by the atexit function are called, in the
111 reverse order of their registration, except that a function is called
112 after any previously registered functions that had already been called at
113 the time it was registered. If, during the call to any such function, a
114 call to the longjmp function is made that would terminate the call to the
115 registered function, the behavior is undefined.
117 Next, all open streams with unwritten buffered data are flushed, all open
118 streams are closed, and all files created by the tmpfile function
121 Finally, control is returned to the host environment. If the value of
122 status is zero, or EXIT_SUCCESS, status is returned unchanged. If the value
123 of status is EXIT_FAILURE, EAPPLICATION is returned.
124 Otherwise, status is returned unchanged.
126 void exit(int status
) __noreturn
;
128 /** The _Exit function causes normal program termination to occur and control
129 to be returned to the host environment.
131 No functions registered by the atexit function or signal handlers
132 registered by the signal function are called. Open streams with unwritten
133 buffered data are not flushed, open streams are not closed, and temporary
134 files are not removed by abort.
136 The status returned to the host environment is determined in the same way
137 as for the exit function.
139 void _Exit(int status
) __noreturn
;
141 /** The getenv function searches an environment list, provided by the host
142 environment, for a string that matches the string pointed to by name. The
143 set of environment names and the method for altering the environment list
144 are determined by the underlying UEFI Shell implementation.
146 @return The getenv function returns a pointer to a string associated with
147 the matched list member. The string pointed to shall not be
148 modified by the program, but may be overwritten by a subsequent
149 call to the getenv function. If the specified name cannot be
150 found, a null pointer is returned.
152 char *getenv(const char *name
);
154 /** If string is a null pointer, the system function determines whether the
155 host environment has a command processor. If string is not a null pointer,
156 the system function passes the string pointed to by string to that command
157 processor to be executed in a manner which the implementation shall
158 document; this might then cause the program calling system to behave in a
159 non-conforming manner or to terminate.
161 @return If the argument is a null pointer, the system function returns
162 nonzero only if a command processor is available. If the argument
163 is not a null pointer, and the system function does return, it
164 returns an implementation-defined value.
166 int system(const char *string
);
169 /* ################ Integer arithmetic functions ######################## */
171 /** Computes the absolute value of an integer j.
173 @return The absolute value of j.
177 /** Computes the absolute value of an integer j.
179 @return The absolute value of j.
183 /** Computes the absolute value of an integer j.
185 @return The absolute value of j.
190 /** Computes numer / denom and numer % denom in a single operation.
192 @return Returns a structure of type div_t, comprising both the
193 quotient and the remainder.
195 div_t div(int numer
, int denom
);
197 /** Computes numer / denom and numer % denom in a single operation.
199 @return Returns a structure of type ldiv_t, comprising both the
200 quotient and the remainder.
202 ldiv_t ldiv(long numer
, long denom
);
204 /** Computes numer / denom and numer % denom in a single operation.
206 @return Returns a structure of type lldiv_t, comprising both the
207 quotient and the remainder.
209 lldiv_t
lldiv(long long numer
, long long denom
);
211 /* ############ Integer Numeric conversion functions #################### */
213 /** The atoi function converts the initial portion of the string pointed to by
214 nptr to int representation. Except for the behavior on error, it is
216 - atoi: (int)strtol(nptr, (char **)NULL, 10)
218 @return The atoi function returns the converted value.
220 int atoi(const char *nptr
);
222 /** The atol function converts the initial portion of the string pointed to by
223 nptr to long int representation. Except for the behavior on error, it is
225 - atol: strtol(nptr, (char **)NULL, 10)
227 @return The atol function returns the converted value.
229 long atol(const char *nptr
);
231 /** The atoll function converts the initial portion of the string pointed to by
232 nptr to long long int representation. Except for the behavior on error, it
234 - atoll: strtoll(nptr, (char **)NULL, 10)
236 @return The atoll function returns the converted value.
239 atoll(const char *nptr
);
241 /** The strtol, strtoll, strtoul, and strtoull functions convert the initial
242 portion of the string pointed to by nptr to long int, long long int,
243 unsigned long int, and unsigned long long int representation, respectively.
244 First, they decompose the input string into three parts: an initial,
245 possibly empty, sequence of white-space characters (as specified by the
246 isspace function), a subject sequence resembling an integer represented in
247 some radix determined by the value of base, and a final string of one or
248 more unrecognized characters, including the terminating null character of
249 the input string. Then, they attempt to convert the subject sequence to an
250 integer, and return the result.
252 If the value of base is zero, the expected form of the subject sequence is
253 that of an integer constant as described in 6.4.4.1, optionally preceded
254 by a plus or minus sign, but not including an integer suffix. If the value
255 of base is between 2 and 36 (inclusive), the expected form of the subject
256 sequence is a sequence of letters and digits representing an integer with
257 the radix specified by base, optionally preceded by a plus or minus sign,
258 but not including an integer suffix. The letters from a (or A) through z
259 (or Z) are ascribed the values 10 through 35; only letters and digits whose
260 ascribed values are less than that of base are permitted. If the value of
261 base is 16, the characters 0x or 0X may optionally precede the sequence of
262 letters and digits, following the sign if present.
264 The subject sequence is defined as the longest initial subsequence of the
265 input string, starting with the first non-white-space character, that is of
266 the expected form. The subject sequence contains no characters if the input
267 string is empty or consists entirely of white space, or if the first
268 non-white-space character is other than a sign or a permissible letter or digit.
270 If the subject sequence has the expected form and the value of base is
271 zero, the sequence of characters starting with the first digit is
272 interpreted as an integer constant. If the subject sequence has the
273 expected form and the value of base is between 2 and 36, it is used as the
274 base for conversion, ascribing to each letter its value as given above. If
275 the subject sequence begins with a minus sign, the value resulting from the
276 conversion is negated (in the return type). A pointer to the final string
277 is stored in the object pointed to by endptr, provided that endptr is
280 In other than the "C" locale, additional locale-specific subject sequence
281 forms may be accepted.
283 If the subject sequence is empty or does not have the expected form, no
284 conversion is performed; the value of nptr is stored in the object pointed
285 to by endptr, provided that endptr is not a null pointer.
287 @return The strtol, strtoll, strtoul, and strtoull functions return the
288 converted value, if any. If no conversion could be performed, zero
289 is returned. If the correct value is outside the range of
290 representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
291 ULONG_MAX, or ULLONG_MAX is returned (according to the return type
292 and sign of the value, if any), and the value of the macro ERANGE
295 long strtol(const char * __restrict nptr
, char ** __restrict endptr
, int base
);
297 /** The strtoul function converts the initial portion of the string pointed to
298 by nptr to unsigned long int representation.
300 See the description for strtol for more information.
302 @return The strtoul function returns the converted value, if any. If no
303 conversion could be performed, zero is returned. If the correct
304 value is outside the range of representable values, ULONG_MAX is
305 returned and the value of the macro ERANGE is stored in errno.
308 strtoul(const char * __restrict nptr
, char ** __restrict endptr
, int base
);
310 /** The strtoll function converts the initial portion of the string pointed to
311 by nptr to long long int representation.
313 See the description for strtol for more information.
315 @return The strtoll function returns the converted value, if any. If no
316 conversion could be performed, zero is returned. If the correct
317 value is outside the range of representable values, LLONG_MIN or
318 LLONG_MAX is returned (according to the sign of the value, if any),
319 and the value of the macro ERANGE is stored in errno.
322 strtoll(const char * __restrict nptr
, char ** __restrict endptr
, int base
);
324 /** The strtoull function converts the initial portion of the string pointed to
325 by nptr to unsigned long long int representation.
327 See the description for strtol for more information.
329 @return The strtoull function returns the converted value, if any. If no
330 conversion could be performed, zero is returned. If the correct
331 value is outside the range of representable values, ULLONG_MAX is
332 returned and the value of the macro ERANGE is stored in errno.
335 strtoull(const char * __restrict nptr
, char ** __restrict endptr
, int base
);
337 /* ######### Floating-point Numeric conversion functions ################ */
343 double atof(const char *);
349 double strtod(const char * __restrict nptr
, char ** __restrict endptr
);
355 float strtof(const char * __restrict nptr
, char ** __restrict endptr
);
362 strtold(const char * __restrict nptr
, char ** __restrict endptr
);
364 /* ################ Pseudo-random sequence generation functions ######### */
366 /** The rand function computes a sequence of pseudo-random integers in the
369 @return The rand function returns a pseudo-random integer.
373 /** The srand function uses the argument as a seed for a new sequence of
374 pseudo-random numbers to be returned by subsequent calls to rand.
376 If srand is then called with the same seed value, the sequence of
377 pseudo-random numbers shall be repeated. If rand is called before any calls
378 to srand have been made, the same sequence shall be generated as when srand
379 is first called with a seed value of 1.
381 void srand(unsigned seed
);
383 /* ################ Memory management functions ######################### */
385 /** The calloc function allocates space for an array of Num objects, each of
386 whose size is Size. The space is initialized to all bits zero.
388 @return NULL is returned if the space could not be allocated and errno
389 contains the cause. Otherwise, a pointer to an 8-byte aligned
390 region of the requested size is returned.
392 void *calloc(size_t Num
, size_t Size
);
394 /** The free function causes the space pointed to by Ptr to be deallocated,
395 that is, made available for further allocation.
397 If Ptr is a null pointer, no action occurs. Otherwise, if the argument
398 does not match a pointer earlier returned by the calloc, malloc, or realloc
399 function, or if the space has been deallocated by a call to free or
400 realloc, the behavior is undefined.
402 @param Ptr Pointer to a previously allocated region of memory to be freed.
407 /** The malloc function allocates space for an object whose size is specified
408 by size and whose value is indeterminate.
410 This implementation uses the UEFI memory allocation boot services to get a
411 region of memory that is 8-byte aligned and of the specified size. The
412 region is allocated with type EfiLoaderData.
414 @param size Size, in bytes, of the region to allocate.
416 @return NULL is returned if the space could not be allocated and errno
417 contains the cause. Otherwise, a pointer to an 8-byte aligned
418 region of the requested size is returned.<BR>
419 If NULL is returned, errno may contain:
420 - EINVAL: Requested Size is zero.
421 - ENOMEM: Memory could not be allocated.
423 void *malloc(size_t);
425 /** The realloc function changes the size of the object pointed to by Ptr to
426 the size specified by NewSize.
428 The contents of the object are unchanged up to the lesser of the new and
429 old sizes. If the new size is larger, the value of the newly allocated
430 portion of the object is indeterminate.
432 If Ptr is a null pointer, the realloc function behaves like the malloc
433 function for the specified size.
435 If Ptr does not match a pointer earlier returned by the calloc, malloc, or
436 realloc function, or if the space has been deallocated by a call to the free
437 or realloc function, the behavior is undefined.
439 If the space cannot be allocated, the object pointed to by Ptr is unchanged.
441 If NewSize is zero and Ptr is not a null pointer, the object it points to
444 This implementation uses the UEFI memory allocation boot services to get a
445 region of memory that is 8-byte aligned and of the specified size. The
446 region is allocated with type EfiLoaderData.
448 @param Ptr Pointer to a previously allocated region of memory to be resized.
449 @param NewSize Size, in bytes, of the new object to allocate space for.
451 @return NULL is returned if the space could not be allocated and errno
452 contains the cause. Otherwise, a pointer to an 8-byte aligned
453 region of the requested size is returned. If NewSize is zero,
454 NULL is returned and errno will be unchanged.
456 void *realloc(void *Ptr
, size_t NewSize
);
458 /* ################ Searching and Sorting utilities ##################### */
460 /** The bsearch function searches an array of nmemb objects, the initial
461 element of which is pointed to by base, for an element that matches the
462 object pointed to by key. The size of each element of the array is
465 The comparison function pointed to by compar is called with two arguments
466 that point to the key object and to an array element, in that order. The
467 function returns an integer less than, equal to, or greater than zero if
468 the key object is considered, respectively, to be less than, to match, or
469 to be greater than the array element. The array consists of: all the
470 elements that compare less than, all the elements that compare equal to,
471 and all the elements that compare greater than the key object,
474 @return The bsearch function returns a pointer to a matching element of the
475 array, or a null pointer if no match is found. If two elements
476 compare as equal, which element is matched is unspecified.
479 bsearch( const void *key
, const void *base0
,
480 size_t nmemb
, size_t size
,
481 int (*compar
)(const void *, const void *)
484 /** The qsort function sorts an array of nmemb objects, the initial element of
485 which is pointed to by base. The size of each object is specified by size.
487 The contents of the array are sorted into ascending order according to a
488 comparison function pointed to by compar, which is called with two
489 arguments that point to the objects being compared. The function shall
490 return an integer less than, equal to, or greater than zero if the first
491 argument is considered to be respectively less than, equal to, or greater
494 If two elements compare as equal, their order in the resulting sorted array
497 void qsort( void *base
, size_t nmemb
, size_t size
,
498 int (*compar
)(const void *, const void *));
500 /* ################ Multibyte/wide character conversion functions ####### */
506 int mblen(const char *, size_t);
512 int mbtowc(wchar_t * __restrict
, const char * __restrict
, size_t);
518 int wctomb(char *, wchar_t);
520 /* ################ Multibyte/wide string conversion functions ########## */
526 size_t mbstowcs(wchar_t * __restrict
, const char * __restrict
, size_t);
532 size_t wcstombs(char * __restrict
, const wchar_t * __restrict
, size_t);
536 #endif /* _STDLIB_H */