]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/Include/stdlib.h
Clean up DEC files:
[mirror_edk2.git] / StdLib / Include / stdlib.h
CommitLineData
2aa62f2b 1/** @file\r
2 The header <stdlib.h> declares five types and several functions of general\r
3 utility, and defines several macros.\r
4\r
5 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials are licensed and made available under\r
7 the terms and conditions of the BSD License that accompanies this distribution.\r
8 The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15#ifndef _STDLIB_H\r
16#define _STDLIB_H\r
17#include <sys/EfiCdefs.h>\r
18\r
19#ifdef _EFI_SIZE_T_\r
20 typedef _EFI_SIZE_T_ size_t;\r
21 #undef _EFI_SIZE_T_\r
22 #undef _BSD_SIZE_T_\r
23#endif\r
24\r
25#ifndef __cplusplus\r
26 #ifdef _EFI_WCHAR_T\r
27 typedef _EFI_WCHAR_T wchar_t;\r
28 #undef _EFI_WCHAR_T\r
29 #undef _BSD_WCHAR_T_\r
30 #endif\r
31#endif\r
32\r
33/// A structure type that is the type of the value returned by the div function.\r
34typedef struct {\r
35 int quot; /* quotient */\r
36 int rem; /* remainder */\r
37} div_t;\r
38\r
39/// A structure type that is the type of the value returned by the ldiv function.\r
40typedef struct {\r
41 long quot;\r
42 long rem;\r
43} ldiv_t;\r
44\r
45/// A structure type that is the type of the value returned by the lldiv function.\r
46typedef struct {\r
47 long long quot;\r
48 long long rem;\r
49} lldiv_t;\r
50\r
51/** Expand to integer constant expressions that can be used as the argument to\r
52 the exit function to return unsuccessful or successful termination status,\r
53 respectively, to the host environment.\r
54**/\r
55#define EXIT_FAILURE 1\r
56#define EXIT_SUCCESS 0\r
57\r
58/** Expands to an integer constant expression that is the maximum value\r
59 returned by the rand function.\r
60\r
61 The value of the RAND_MAX macro shall be at least 32767.\r
62**/\r
63#define RAND_MAX 0x7fffffff\r
64\r
65/** Expands to a positive integer expression with type size_t that is the\r
66 maximum number of bytes in a multibyte character for the extended character\r
67 set specified by the current locale (category LC_CTYPE), which is never\r
68 greater than MB_LEN_MAX.\r
69**/\r
70#define MB_CUR_MAX 2\r
71\r
72/** Maximum number of functions that can be registered by atexit.\r
73\r
74 The C standard states that the implementation shall support the\r
75 registration of at least 32 functions.\r
76**/\r
77#define ATEXIT_MAX 32\r
78\r
79__BEGIN_DECLS\r
80\r
81/* ################ Communication with the environment ################## */\r
82\r
83/** The abort function causes abnormal program termination to occur, unless\r
84 the signal SIGABRT is being caught and the signal handler does not return.\r
85\r
86 Open streams with unwritten buffered data are not flushed, open\r
87 streams are not closed, and temporary files are not removed by abort.\r
88\r
89 Unsuccessful termination is returned to the host environment by means of\r
90 the function call, raise(SIGABRT).\r
91\r
92 @sa signal.h\r
93**/\r
94void abort(void);\r
95\r
96/** The atexit function registers the function pointed to by func, to be\r
97 called without arguments at normal program termination.\r
98\r
99 The implementation supports the registration of up to 32 functions.\r
100\r
101 @return The atexit function returns zero if the registration succeeds,\r
102 nonzero if it fails.\r
103**/\r
104int atexit(void (*)(void));\r
105\r
106/** The exit function causes normal program termination to occur. If more than\r
107 one call to the exit function is executed by a program,\r
108 the behavior is undefined.\r
109\r
110 First, all functions registered by the atexit function are called, in the\r
111 reverse order of their registration, except that a function is called\r
112 after any previously registered functions that had already been called at\r
113 the time it was registered. If, during the call to any such function, a\r
114 call to the longjmp function is made that would terminate the call to the\r
115 registered function, the behavior is undefined.\r
116\r
117 Next, all open streams with unwritten buffered data are flushed, all open\r
118 streams are closed, and all files created by the tmpfile function\r
119 are removed.\r
120\r
121 Finally, control is returned to the host environment. If the value of\r
122 status is zero, or EXIT_SUCCESS, status is returned unchanged. If the value\r
123 of status is EXIT_FAILURE, EAPPLICATION is returned.\r
124 Otherwise, status is returned unchanged.\r
125**/\r
126void exit(int status) __noreturn;\r
127\r
128/** The _Exit function causes normal program termination to occur and control\r
129 to be returned to the host environment.\r
130\r
131 No functions registered by the atexit function or signal handlers\r
132 registered by the signal function are called. Open streams with unwritten\r
133 buffered data are not flushed, open streams are not closed, and temporary\r
134 files are not removed by abort.\r
135\r
136 The status returned to the host environment is determined in the same way\r
137 as for the exit function.\r
138**/\r
139void _Exit(int status) __noreturn;\r
140\r
141/** The getenv function searches an environment list, provided by the host\r
142 environment, for a string that matches the string pointed to by name. The\r
143 set of environment names and the method for altering the environment list\r
144 are determined by the underlying UEFI Shell implementation.\r
145\r
146 @return The getenv function returns a pointer to a string associated with\r
147 the matched list member. The string pointed to shall not be\r
148 modified by the program, but may be overwritten by a subsequent\r
149 call to the getenv function. If the specified name cannot be\r
150 found, a null pointer is returned.\r
151**/\r
152char *getenv(const char *name);\r
153\r
154/** If string is a null pointer, the system function determines whether the\r
155 host environment has a command processor. If string is not a null pointer,\r
156 the system function passes the string pointed to by string to that command\r
157 processor to be executed in a manner which the implementation shall\r
158 document; this might then cause the program calling system to behave in a\r
159 non-conforming manner or to terminate.\r
160\r
161 @return If the argument is a null pointer, the system function returns\r
162 nonzero only if a command processor is available. If the argument\r
163 is not a null pointer, and the system function does return, it\r
164 returns an implementation-defined value.\r
165**/\r
166int system(const char *string);\r
167\r
168\r
169/* ################ Integer arithmetic functions ######################## */\r
170\r
171/** Computes the absolute value of an integer j.\r
172\r
173 @return The absolute value of j.\r
174**/\r
175int abs(int j);\r
176\r
177/** Computes the absolute value of an integer j.\r
178\r
179 @return The absolute value of j.\r
180**/\r
181long labs(long j);\r
182\r
183/** Computes the absolute value of an integer j.\r
184\r
185 @return The absolute value of j.\r
186**/\r
187long long\r
188 llabs(long long j);\r
189\r
190/** Computes numer / denom and numer % denom in a single operation.\r
191\r
192 @return Returns a structure of type div_t, comprising both the\r
193 quotient and the remainder.\r
194**/\r
195div_t div(int numer, int denom);\r
196\r
197/** Computes numer / denom and numer % denom in a single operation.\r
198\r
199 @return Returns a structure of type ldiv_t, comprising both the\r
200 quotient and the remainder.\r
201**/\r
202ldiv_t ldiv(long numer, long denom);\r
203\r
204/** Computes numer / denom and numer % denom in a single operation.\r
205\r
206 @return Returns a structure of type lldiv_t, comprising both the\r
207 quotient and the remainder.\r
208**/\r
209lldiv_t lldiv(long long numer, long long denom);\r
210\r
211/* ############ Integer Numeric conversion functions #################### */\r
212\r
213/** The atoi function converts the initial portion of the string pointed to by\r
214 nptr to int representation. Except for the behavior on error, it is\r
215 equivalent to:\r
216 - atoi: (int)strtol(nptr, (char **)NULL, 10)\r
217\r
218 @return The atoi function returns the converted value.\r
219**/\r
220int atoi(const char *nptr);\r
221\r
222/** The atol function converts the initial portion of the string pointed to by\r
223 nptr to long int representation. Except for the behavior on error, it is\r
224 equivalent to:\r
225 - atol: strtol(nptr, (char **)NULL, 10)\r
226\r
227 @return The atol function returns the converted value.\r
228**/\r
229long atol(const char *nptr);\r
230\r
231/** The atoll function converts the initial portion of the string pointed to by\r
232 nptr to long long int representation. Except for the behavior on error, it\r
233 is equivalent to:\r
234 - atoll: strtoll(nptr, (char **)NULL, 10)\r
235\r
236 @return The atoll function returns the converted value.\r
237**/\r
238long long\r
239 atoll(const char *nptr);\r
240\r
241/** The strtol, strtoll, strtoul, and strtoull functions convert the initial\r
242 portion of the string pointed to by nptr to long int, long long int,\r
243 unsigned long int, and unsigned long long int representation, respectively.\r
244 First, they decompose the input string into three parts: an initial,\r
245 possibly empty, sequence of white-space characters (as specified by the\r
246 isspace function), a subject sequence resembling an integer represented in\r
247 some radix determined by the value of base, and a final string of one or\r
248 more unrecognized characters, including the terminating null character of\r
249 the input string. Then, they attempt to convert the subject sequence to an\r
250 integer, and return the result.\r
251\r
252 If the value of base is zero, the expected form of the subject sequence is\r
253 that of an integer constant as described in 6.4.4.1, optionally preceded\r
254 by a plus or minus sign, but not including an integer suffix. If the value\r
255 of base is between 2 and 36 (inclusive), the expected form of the subject\r
256 sequence is a sequence of letters and digits representing an integer with\r
257 the radix specified by base, optionally preceded by a plus or minus sign,\r
258 but not including an integer suffix. The letters from a (or A) through z\r
259 (or Z) are ascribed the values 10 through 35; only letters and digits whose\r
260 ascribed values are less than that of base are permitted. If the value of\r
261 base is 16, the characters 0x or 0X may optionally precede the sequence of\r
262 letters and digits, following the sign if present.\r
263\r
264 The subject sequence is defined as the longest initial subsequence of the\r
265 input string, starting with the first non-white-space character, that is of\r
266 the expected form. The subject sequence contains no characters if the input\r
267 string is empty or consists entirely of white space, or if the first\r
268 non-white-space character is other than a sign or a permissible letter or digit.\r
269\r
270 If the subject sequence has the expected form and the value of base is\r
271 zero, the sequence of characters starting with the first digit is\r
272 interpreted as an integer constant. If the subject sequence has the\r
273 expected form and the value of base is between 2 and 36, it is used as the\r
274 base for conversion, ascribing to each letter its value as given above. If\r
275 the subject sequence begins with a minus sign, the value resulting from the\r
276 conversion is negated (in the return type). A pointer to the final string\r
277 is stored in the object pointed to by endptr, provided that endptr is\r
278 not a null pointer.\r
279\r
280 In other than the "C" locale, additional locale-specific subject sequence\r
281 forms may be accepted.\r
282\r
283 If the subject sequence is empty or does not have the expected form, no\r
284 conversion is performed; the value of nptr is stored in the object pointed\r
285 to by endptr, provided that endptr is not a null pointer.\r
286\r
287 @return The strtol, strtoll, strtoul, and strtoull functions return the\r
288 converted value, if any. If no conversion could be performed, zero\r
289 is returned. If the correct value is outside the range of\r
290 representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,\r
291 ULONG_MAX, or ULLONG_MAX is returned (according to the return type\r
292 and sign of the value, if any), and the value of the macro ERANGE\r
293 is stored in errno.\r
294**/\r
295long strtol(const char * __restrict nptr, char ** __restrict endptr, int base);\r
296\r
297/** The strtoul function converts the initial portion of the string pointed to\r
298 by nptr to unsigned long int representation.\r
299\r
300 See the description for strtol for more information.\r
301\r
302 @return The strtoul function returns the converted value, if any. If no\r
303 conversion could be performed, zero is returned. If the correct\r
304 value is outside the range of representable values, ULONG_MAX is\r
305 returned and the value of the macro ERANGE is stored in errno.\r
306**/\r
307unsigned long\r
308 strtoul(const char * __restrict nptr, char ** __restrict endptr, int base);\r
309\r
310/** The strtoll function converts the initial portion of the string pointed to\r
311 by nptr to long long int representation.\r
312\r
313 See the description for strtol for more information.\r
314\r
315 @return The strtoll function returns the converted value, if any. If no\r
316 conversion could be performed, zero is returned. If the correct\r
317 value is outside the range of representable values, LLONG_MIN or\r
318 LLONG_MAX is returned (according to the sign of the value, if any),\r
319 and the value of the macro ERANGE is stored in errno.\r
320**/\r
321long long\r
322 strtoll(const char * __restrict nptr, char ** __restrict endptr, int base);\r
323\r
324/** The strtoull function converts the initial portion of the string pointed to\r
325 by nptr to unsigned long long int representation.\r
326\r
327 See the description for strtol for more information.\r
328\r
329 @return The strtoull function returns the converted value, if any. If no\r
330 conversion could be performed, zero is returned. If the correct\r
331 value is outside the range of representable values, ULLONG_MAX is\r
332 returned and the value of the macro ERANGE is stored in errno.\r
333**/\r
334unsigned long long\r
335 strtoull(const char * __restrict nptr, char ** __restrict endptr, int base);\r
336\r
337/* ######### Floating-point Numeric conversion functions ################ */\r
338\r
339/**\r
340\r
341 @return\r
342**/\r
343double atof(const char *);\r
344\r
345/**\r
346\r
347 @return\r
348**/\r
349double strtod(const char * __restrict nptr, char ** __restrict endptr);\r
350\r
351/**\r
352\r
353 @return\r
354**/\r
355float strtof(const char * __restrict nptr, char ** __restrict endptr);\r
356\r
357/**\r
358\r
359 @return\r
360**/\r
361long double\r
362 strtold(const char * __restrict nptr, char ** __restrict endptr);\r
363\r
364/* ################ Pseudo-random sequence generation functions ######### */\r
365\r
366/** The rand function computes a sequence of pseudo-random integers in the\r
367 range 0 to RAND_MAX.\r
368\r
369 @return The rand function returns a pseudo-random integer.\r
370**/\r
371int rand(void);\r
372\r
373/** The srand function uses the argument as a seed for a new sequence of\r
374 pseudo-random numbers to be returned by subsequent calls to rand.\r
375\r
376 If srand is then called with the same seed value, the sequence of\r
377 pseudo-random numbers shall be repeated. If rand is called before any calls\r
378 to srand have been made, the same sequence shall be generated as when srand\r
379 is first called with a seed value of 1.\r
380**/\r
381void srand(unsigned seed);\r
382\r
383/* ################ Memory management functions ######################### */\r
384\r
385/** The calloc function allocates space for an array of Num objects, each of\r
386 whose size is Size. The space is initialized to all bits zero.\r
387\r
388 @return NULL is returned if the space could not be allocated and errno\r
389 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
390 region of the requested size is returned.\r
391**/\r
392void *calloc(size_t Num, size_t Size);\r
393\r
394/** The free function causes the space pointed to by Ptr to be deallocated,\r
395 that is, made available for further allocation.\r
396\r
397 If Ptr is a null pointer, no action occurs. Otherwise, if the argument\r
398 does not match a pointer earlier returned by the calloc, malloc, or realloc\r
399 function, or if the space has been deallocated by a call to free or\r
400 realloc, the behavior is undefined.\r
401\r
402 @param Ptr Pointer to a previously allocated region of memory to be freed.\r
403\r
404**/\r
405void free(void *);\r
406\r
407/** The malloc function allocates space for an object whose size is specified\r
408 by size and whose value is indeterminate.\r
409\r
410 This implementation uses the UEFI memory allocation boot services to get a\r
411 region of memory that is 8-byte aligned and of the specified size. The\r
412 region is allocated with type EfiLoaderData.\r
413\r
414 @param size Size, in bytes, of the region to allocate.\r
415\r
416 @return NULL is returned if the space could not be allocated and errno\r
417 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
418 region of the requested size is returned.<BR>\r
419 If NULL is returned, errno may contain:\r
420 - EINVAL: Requested Size is zero.\r
421 - ENOMEM: Memory could not be allocated.\r
422**/\r
423void *malloc(size_t);\r
424\r
425/** The realloc function changes the size of the object pointed to by Ptr to\r
426 the size specified by NewSize.\r
427\r
428 The contents of the object are unchanged up to the lesser of the new and\r
429 old sizes. If the new size is larger, the value of the newly allocated\r
430 portion of the object is indeterminate.\r
431\r
432 If Ptr is a null pointer, the realloc function behaves like the malloc\r
433 function for the specified size.\r
434\r
435 If Ptr does not match a pointer earlier returned by the calloc, malloc, or\r
436 realloc function, or if the space has been deallocated by a call to the free\r
437 or realloc function, the behavior is undefined.\r
438\r
439 If the space cannot be allocated, the object pointed to by Ptr is unchanged.\r
440\r
441 If NewSize is zero and Ptr is not a null pointer, the object it points to\r
442 is freed.\r
443\r
444 This implementation uses the UEFI memory allocation boot services to get a\r
445 region of memory that is 8-byte aligned and of the specified size. The\r
446 region is allocated with type EfiLoaderData.\r
447\r
448 @param Ptr Pointer to a previously allocated region of memory to be resized.\r
449 @param NewSize Size, in bytes, of the new object to allocate space for.\r
450\r
451 @return NULL is returned if the space could not be allocated and errno\r
452 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
453 region of the requested size is returned. If NewSize is zero,\r
454 NULL is returned and errno will be unchanged.\r
455**/\r
456void *realloc(void *Ptr, size_t NewSize);\r
457\r
458/* ################ Searching and Sorting utilities ##################### */\r
459\r
460/** The bsearch function searches an array of nmemb objects, the initial\r
461 element of which is pointed to by base, for an element that matches the\r
462 object pointed to by key. The size of each element of the array is\r
463 specified by size.\r
464\r
465 The comparison function pointed to by compar is called with two arguments\r
466 that point to the key object and to an array element, in that order. The\r
467 function returns an integer less than, equal to, or greater than zero if\r
468 the key object is considered, respectively, to be less than, to match, or\r
469 to be greater than the array element. The array consists of: all the\r
470 elements that compare less than, all the elements that compare equal to,\r
471 and all the elements that compare greater than the key object,\r
472 in that order.\r
473\r
474 @return The bsearch function returns a pointer to a matching element of the\r
475 array, or a null pointer if no match is found. If two elements\r
476 compare as equal, which element is matched is unspecified.\r
477**/\r
478void *\r
479bsearch( const void *key, const void *base0,\r
480 size_t nmemb, size_t size,\r
481 int (*compar)(const void *, const void *)\r
482);\r
483\r
484/** The qsort function sorts an array of nmemb objects, the initial element of\r
485 which is pointed to by base. The size of each object is specified by size.\r
486\r
487 The contents of the array are sorted into ascending order according to a\r
488 comparison function pointed to by compar, which is called with two\r
489 arguments that point to the objects being compared. The function shall\r
490 return an integer less than, equal to, or greater than zero if the first\r
491 argument is considered to be respectively less than, equal to, or greater\r
492 than the second.\r
493\r
494 If two elements compare as equal, their order in the resulting sorted array\r
495 is unspecified.\r
496**/\r
497void qsort( void *base, size_t nmemb, size_t size,\r
498 int (*compar)(const void *, const void *));\r
499\r
500/* ################ Multibyte/wide character conversion functions ####### */\r
501\r
502/**\r
503\r
504 @return\r
505**/\r
506int mblen(const char *, size_t);\r
507\r
508/**\r
509\r
510 @return\r
511**/\r
512int mbtowc(wchar_t * __restrict, const char * __restrict, size_t);\r
513\r
514/**\r
515\r
516 @return\r
517**/\r
518int wctomb(char *, wchar_t);\r
519\r
520/* ################ Multibyte/wide string conversion functions ########## */\r
521\r
522/**\r
523\r
524 @return\r
525**/\r
526size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);\r
527\r
528/**\r
529\r
530 @return\r
531**/\r
532size_t wcstombs(char * __restrict, const wchar_t * __restrict, size_t);\r
533\r
534__END_DECLS\r
535\r
536#endif /* _STDLIB_H */\r