stdlib.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. stdlib.h
  9. Abstract:
  10. This header contains standard C Library definitions.
  11. Author:
  12. Evan Green 6-Mar-2013
  13. --*/
  14. #ifndef _STDLIB_H
  15. #define _STDLIB_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <libcbase.h>
  20. #include <stddef.h>
  21. #include <wchar.h>
  22. #include <sys/select.h>
  23. #include <sys/types.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. //
  31. // Define values to give to exit on failure and success.
  32. //
  33. #define EXIT_FAILURE 1
  34. #define EXIT_SUCCESS 0
  35. //
  36. // Define the maximum value that the random functions will return.
  37. //
  38. #define RAND_MAX 0x7FFFFFFF
  39. //
  40. // ------------------------------------------------------ Data Type Definitions
  41. //
  42. //
  43. // Define the type returned by the div function.
  44. //
  45. typedef struct {
  46. int quot;
  47. int rem;
  48. } div_t;
  49. //
  50. // Define the type returned by the ldiv function.
  51. //
  52. typedef struct {
  53. long quot;
  54. long rem;
  55. } ldiv_t;
  56. //
  57. // Define the type returned by the lldiv function.
  58. //
  59. typedef struct {
  60. long long quot;
  61. long long rem;
  62. } lldiv_t;
  63. /*++
  64. Structure Description:
  65. This structure stores random state information.
  66. Members:
  67. fptr - Stores the front pointer.
  68. rptr - Stores the rear pointer.
  69. state - Stores the array of state values.
  70. rand_type - Stores the type of random number generator.
  71. rand_deg - Stores the degree of the generator.
  72. rand_sep - Stores the distance between the front and the rear.
  73. end_ptr - Stores a pointer to the end of the state table.
  74. --*/
  75. struct random_data {
  76. int32_t *fptr;
  77. int32_t *rptr;
  78. int32_t *state;
  79. int rand_type;
  80. int rand_deg;
  81. int rand_sep;
  82. int32_t *end_ptr;
  83. };
  84. //
  85. // -------------------------------------------------------------------- Globals
  86. //
  87. //
  88. // Define the maximum number of bytes in a multibyte character for the current
  89. // locale.
  90. //
  91. LIBC_API extern int MB_CUR_MAX;
  92. //
  93. // -------------------------------------------------------- Function Prototypes
  94. //
  95. LIBC_API
  96. int
  97. abs (
  98. int Value
  99. );
  100. /*++
  101. Routine Description:
  102. This routine returns the absolute value of the given value.
  103. Arguments:
  104. Value - Supplies the value to get the absolute value of.
  105. Return Value:
  106. Returns the absolute value.
  107. --*/
  108. LIBC_API
  109. long
  110. labs (
  111. long Value
  112. );
  113. /*++
  114. Routine Description:
  115. This routine returns the absolute value of the given long value.
  116. Arguments:
  117. Value - Supplies the value to get the absolute value of.
  118. Return Value:
  119. Returns the absolute value.
  120. --*/
  121. LIBC_API
  122. long long
  123. llabs (
  124. long long Value
  125. );
  126. /*++
  127. Routine Description:
  128. This routine returns the absolute value of the given long long value.
  129. Arguments:
  130. Value - Supplies the value to get the absolute value of.
  131. Return Value:
  132. Returns the absolute value.
  133. --*/
  134. LIBC_API
  135. div_t
  136. div (
  137. int Numerator,
  138. int Denominator
  139. );
  140. /*++
  141. Routine Description:
  142. This routine divides two integers. If the division is inexact, the
  143. resulting quotient is the integer of lesser magnitude that is nearest to
  144. the algebraic quotient. If the result cannot be represented, the behavior
  145. is undefined.
  146. Arguments:
  147. Numerator - Supplies the dividend.
  148. Denominator - Supplies the divisor.
  149. Return Value:
  150. Returns the quotient of the division.
  151. --*/
  152. LIBC_API
  153. ldiv_t
  154. ldiv (
  155. long Numerator,
  156. long Denominator
  157. );
  158. /*++
  159. Routine Description:
  160. This routine divides two long integers. If the division is inexact, the
  161. resulting quotient is the integer of lesser magnitude that is nearest to
  162. the algebraic quotient. If the result cannot be represented, the behavior
  163. is undefined.
  164. Arguments:
  165. Numerator - Supplies the dividend.
  166. Denominator - Supplies the divisor.
  167. Return Value:
  168. Returns the quotient of the division.
  169. --*/
  170. LIBC_API
  171. lldiv_t
  172. lldiv (
  173. long long Numerator,
  174. long long Denominator
  175. );
  176. /*++
  177. Routine Description:
  178. This routine divides two long long integers. If the division is inexact,
  179. the resulting quotient is the integer of lesser magnitude that is nearest
  180. to the algebraic quotient. If the result cannot be represented, the
  181. behavior is undefined.
  182. Arguments:
  183. Numerator - Supplies the dividend.
  184. Denominator - Supplies the divisor.
  185. Return Value:
  186. Returns the quotient of the division.
  187. --*/
  188. LIBC_API
  189. __NO_RETURN
  190. void
  191. abort (
  192. void
  193. );
  194. /*++
  195. Routine Description:
  196. This routine causes abnormal process termination to occur, unless the
  197. signal SIGABRT is being caught and the signal handler does not return. The
  198. abort function shall override ignoring or blocking of the SIGABRT signal.
  199. Arguments:
  200. None.
  201. Return Value:
  202. This routine does not return.
  203. --*/
  204. __HIDDEN
  205. int
  206. atexit (
  207. void (*ExitFunction)(void)
  208. );
  209. /*++
  210. Routine Description:
  211. This routine registers a function to be called when the process exits
  212. normally via a call to exit or a return from main. Calls to exec clear
  213. the list of registered exit functions. This routine may allocate memory.
  214. Functions are called in the reverse order in which they were registered.
  215. If this function is called from within a shared library, then the given
  216. function will be called when the library is unloaded.
  217. Arguments:
  218. ExitFunction - Supplies a pointer to the function to call when the
  219. process exits normally or the shared object is unloaded.
  220. Return Value:
  221. 0 on success.
  222. Non-zero on failure.
  223. --*/
  224. LIBC_API
  225. int
  226. __cxa_atexit (
  227. void (*DestructorFunction)(void *),
  228. void *Argument,
  229. void *SharedObject
  230. );
  231. /*++
  232. Routine Description:
  233. This routine is called to register a global static destructor function.
  234. Arguments:
  235. DestructorFunction - Supplies a pointer to the function to call.
  236. Argument - Supplies an argument to pass the function when it is called.
  237. SharedObject - Supplies a pointer to the shared object this destructor is
  238. associated with.
  239. Return Value:
  240. 0 on success.
  241. Non-zero on failure.
  242. --*/
  243. LIBC_API
  244. __NO_RETURN
  245. void
  246. exit (
  247. int Status
  248. );
  249. /*++
  250. Routine Description:
  251. This routine terminates the current process, calling any routines registered
  252. to run upon exiting.
  253. Arguments:
  254. Status - Supplies a status code to return to the parent program.
  255. Return Value:
  256. None. This routine does not return.
  257. --*/
  258. LIBC_API
  259. __NO_RETURN
  260. void
  261. _Exit (
  262. int Status
  263. );
  264. /*++
  265. Routine Description:
  266. This routine terminates the current process. It does not call any routines
  267. registered to run upon exit.
  268. Arguments:
  269. Status - Supplies a status code to return to the parent program.
  270. Return Value:
  271. None. This routine does not return.
  272. --*/
  273. LIBC_API
  274. void
  275. free (
  276. void *Memory
  277. );
  278. /*++
  279. Routine Description:
  280. This routine frees previously allocated memory.
  281. Arguments:
  282. Memory - Supplies a pointer to memory returned by the allocation function.
  283. Return Value:
  284. None.
  285. --*/
  286. LIBC_API
  287. void *
  288. malloc (
  289. size_t AllocationSize
  290. );
  291. /*++
  292. Routine Description:
  293. This routine allocates memory from the heap.
  294. Arguments:
  295. AllocationSize - Supplies the required allocation size in bytes.
  296. Return Value:
  297. Returns a pointer to the allocated memory on success.
  298. NULL on failure.
  299. --*/
  300. LIBC_API
  301. void *
  302. realloc (
  303. void *Allocation,
  304. size_t AllocationSize
  305. );
  306. /*++
  307. Routine Description:
  308. This routine resizes the given buffer. If the new allocation size is
  309. greater than the original allocation, the contents of the new bytes are
  310. unspecified (just like the contents of a malloced buffer).
  311. Arguments:
  312. Allocation - Supplies the optional original allocation. If this is not
  313. supplied, this routine is equivalent to malloc.
  314. AllocationSize - Supplies the new required allocation size in bytes.
  315. Return Value:
  316. Returns a pointer to the new buffer on success.
  317. NULL on failure or if the supplied size was zero. If the new buffer could
  318. not be allocated, errno will be set to ENOMEM.
  319. --*/
  320. LIBC_API
  321. void *
  322. calloc (
  323. size_t ElementCount,
  324. size_t ElementSize
  325. );
  326. /*++
  327. Routine Description:
  328. This routine allocates memory from the heap large enough to store the
  329. given number of elements of the given size (the product of the two
  330. parameters). The buffer returned will have all zeros written to it.
  331. Arguments:
  332. ElementCount - Supplies the number of elements to allocate.
  333. ElementSize - Supplies the size of each element in bytes.
  334. Return Value:
  335. Returns a pointer to the new zeroed buffer on success.
  336. NULL on failure or if the either the element count of the element size was
  337. zero. If the new buffer could not be allocated, errno will be set to ENOMEM.
  338. --*/
  339. LIBC_API
  340. int
  341. posix_memalign (
  342. void **AllocationPointer,
  343. size_t AllocationAlignment,
  344. size_t AllocationSize
  345. );
  346. /*++
  347. Routine Description:
  348. This routine allocates aligned memory from the heap. The given alignment
  349. must be a power of 2 and a multiple of the size of a pointer.
  350. Arguments:
  351. AllocationPointer - Supplies a pointer that receives a pointer to the
  352. allocated memory on success.
  353. AllocationAlignment - Supplies the required allocation alignment in bytes.
  354. AllocationSize - Supplies the required allocation size in bytes.
  355. Return Value:
  356. 0 on success.
  357. Returns an error number on failure.
  358. --*/
  359. LIBC_API
  360. char *
  361. getenv (
  362. const char *Name
  363. );
  364. /*++
  365. Routine Description:
  366. This routine returns the value for the environment variable with the
  367. given name. This function is neither reentrant nor thread safe.
  368. Arguments:
  369. Name - Supplies a pointer to the null terminated string containing the name
  370. of the environment variable to get.
  371. Return Value:
  372. Returns a pointer to the value of the environment variable on success. This
  373. memory may be destroyed by subsequent calls to getenv, setenv, or
  374. unsetenv. The caller does not own it and must not modify or free this
  375. memory.
  376. --*/
  377. LIBC_API
  378. int
  379. setenv (
  380. const char *Name,
  381. const char *Value,
  382. int Overwrite
  383. );
  384. /*++
  385. Routine Description:
  386. This routine sets the value for the given environment variable. This
  387. function is neither reentrant nor thread safe.
  388. Arguments:
  389. Name - Supplies a pointer to the null terminated string containing the name
  390. of the environment variable to set. The routine will fail if this
  391. string has an equal in it.
  392. Value - Supplies a pointer to the null terminated string containing the
  393. value to set for this variable.
  394. Overwrite - Supplies an integer that if non-zero will cause an existing
  395. environment variable with the same name to be overwritten. If this is
  396. zero and the given name exists, the function will return successfully
  397. but the value will not be changed.
  398. Return Value:
  399. 0 on success.
  400. -1 on failure, an errno will be set to contain the error code.
  401. --*/
  402. LIBC_API
  403. int
  404. putenv (
  405. char *String
  406. );
  407. /*++
  408. Routine Description:
  409. This routine adds the given string to the environment list.
  410. Arguments:
  411. String - Supplies a pointer to the null terminated string in the form
  412. "name=value". This string will become part of the environment, if it
  413. is modified then that modification will be reflected in the
  414. environment. The memory supplied in this argument is used directly, so
  415. the argument must not be automatically allocated. If the given string
  416. contains no equal sign, then the functionality is equivalent to
  417. unsetenv with the given string.
  418. Return Value:
  419. 0 on success.
  420. -1 on failure, and errno will be set to contain more information.
  421. --*/
  422. LIBC_API
  423. int
  424. unsetenv (
  425. const char *Name
  426. );
  427. /*++
  428. Routine Description:
  429. This routine removes the environment variable with the given name from
  430. the current environment. This routine is neither re-entrant nor thread safe.
  431. Arguments:
  432. Name - Supplies a pointer to the name of the variable to unset. This
  433. string must not have an equals '=' in it.
  434. Return Value:
  435. 0 on success (whether or not the environment variable previously existed).
  436. -1 on failure, and errno will be set to contain more information. Errno is
  437. commonly set to EINVAL if the argument is a null pointer, an empty string,
  438. or contains an equals.
  439. --*/
  440. LIBC_API
  441. const char *
  442. getexecname (
  443. void
  444. );
  445. /*++
  446. Routine Description:
  447. This routine returns the path name of the executable.
  448. Arguments:
  449. None.
  450. Return Value:
  451. Returns a pointer to the pathname of the executable on success. The caller
  452. must not alter this memory.
  453. --*/
  454. LIBC_API
  455. void *
  456. bsearch (
  457. const void *Key,
  458. const void *Base,
  459. size_t ElementCount,
  460. size_t ElementSize,
  461. int (*CompareFunction)(const void *, const void *)
  462. );
  463. /*++
  464. Routine Description:
  465. This routine searches an array of sorted objects for one matching the given
  466. key.
  467. Arguments:
  468. Key - Supplies a pointer to the element to match against in the given
  469. array.
  470. Base - Supplies a pointer to the base of the array to search.
  471. ElementCount - Supplies the number of elements in the array. Searching an
  472. element with a count of zero shall return NULL.
  473. ElementSize - Supplies the size of each element in the array.
  474. CompareFunction - Supplies a pointer to a function that will be called to
  475. compare elements. The function takes in two pointers that will point
  476. to elements within the array. It shall return less than zero if the
  477. left element is considered less than the right object, zero if the left
  478. object is considered equal to the right object, and greater than zero
  479. if the left object is considered greater than the right object.
  480. Return Value:
  481. Returns a pointer to the element within the array matching the given key.
  482. NULL if no such element exists or the element count was zero.
  483. --*/
  484. LIBC_API
  485. void
  486. qsort (
  487. void *ArrayBase,
  488. size_t ElementCount,
  489. size_t ElementSize,
  490. int (*CompareFunction)(const void *, const void *)
  491. );
  492. /*++
  493. Routine Description:
  494. This routine sorts an array of items in place using the QuickSort algorithm.
  495. Arguments:
  496. ArrayBase - Supplies a pointer to the array of items that will get pushed
  497. around.
  498. ElementCount - Supplies the number of elements in the array.
  499. ElementSize - Supplies the size of one of the elements.
  500. CompareFunction - Supplies a pointer to a function that will be used to
  501. compare elements. The function takes in two pointers that will point
  502. within the array. It returns less than zero if the first element is
  503. less than the second, zero if the first element is equal to the second,
  504. and greater than zero if the first element is greater than the second.
  505. The routine must not modify the array itself or inconsistently
  506. report comparisons, otherwise the sorting will not come out correctly.
  507. Return Value:
  508. None.
  509. --*/
  510. LIBC_API
  511. int
  512. atoi (
  513. const char *String
  514. );
  515. /*++
  516. Routine Description:
  517. This routine converts a string to an integer. This routine is provided for
  518. compatibility with existing applications. New applications should use
  519. strtol instead.
  520. Arguments:
  521. String - Supplies a pointer to the null terminated string to convert to an
  522. integer.
  523. Return Value:
  524. Returns the integer representation of the string. If the value could not be
  525. converted, 0 is returned.
  526. --*/
  527. LIBC_API
  528. double
  529. atof (
  530. const char *String
  531. );
  532. /*++
  533. Routine Description:
  534. This routine converts a string to a double floating point value. This
  535. routine is provided for compatibility with existing applications. New
  536. applications should use strtod instead.
  537. Arguments:
  538. String - Supplies a pointer to the null terminated string to convert to a
  539. double.
  540. Return Value:
  541. Returns the floating point representation of the string. If the value could
  542. not be converted, 0 is returned.
  543. --*/
  544. LIBC_API
  545. long
  546. atol (
  547. const char *String
  548. );
  549. /*++
  550. Routine Description:
  551. This routine converts a string to an integer. This routine is provided for
  552. compatibility with existing applications. New applications should use
  553. strtol instead.
  554. Arguments:
  555. String - Supplies a pointer to the null terminated string to convert to an
  556. integer.
  557. Return Value:
  558. Returns the integer representation of the string. If the value could not be
  559. converted, 0 is returned.
  560. --*/
  561. LIBC_API
  562. long long
  563. atoll (
  564. const char *String
  565. );
  566. /*++
  567. Routine Description:
  568. This routine converts a string to an integer. This routine is provided for
  569. compatibility with existing applications. New applications should use
  570. strtoll instead.
  571. Arguments:
  572. String - Supplies a pointer to the null terminated string to convert to an
  573. integer.
  574. Return Value:
  575. Returns the integer representation of the string. If the value could not be
  576. converted, 0 is returned.
  577. --*/
  578. LIBC_API
  579. float
  580. strtof (
  581. const char *String,
  582. char **StringAfterScan
  583. );
  584. /*++
  585. Routine Description:
  586. This routine converts the initial portion of the given string into a
  587. float. This routine will scan past any whitespace at the beginning of
  588. the string.
  589. Arguments:
  590. String - Supplies a pointer to the null terminated string to convert to a
  591. float.
  592. StringAfterScan - Supplies a pointer where a pointer will be returned
  593. representing the remaining portion of the string after the float was
  594. scanned. If the entire string is made up of whitespace or invalid
  595. characters, then this will point to the beginning of the given string
  596. (the scanner will not be advanced).
  597. Return Value:
  598. Returns the float representation of the string. If the value could not be
  599. converted, 0 is returned, and errno will be set to either EINVAL if the
  600. number could not be converted or ERANGE if the number is outside of the
  601. return type's expressible range.
  602. --*/
  603. LIBC_API
  604. double
  605. strtod (
  606. const char *String,
  607. char **StringAfterScan
  608. );
  609. /*++
  610. Routine Description:
  611. This routine converts the initial portion of the given string into a
  612. double. This routine will scan past any whitespace at the beginning of
  613. the string.
  614. Arguments:
  615. String - Supplies a pointer to the null terminated string to convert to a
  616. double.
  617. StringAfterScan - Supplies a pointer where a pointer will be returned
  618. representing the remaining portion of the string after the double was
  619. scanned. If the entire string is made up of whitespace or invalid
  620. characters, then this will point to the beginning of the given string
  621. (the scanner will not be advanced).
  622. Return Value:
  623. Returns the integer representation of the string. If the value could not be
  624. converted, 0 is returned, and errno will be set to either EINVAL if the
  625. number could not be converted or ERANGE if the number is outside of the
  626. return type's expressible range.
  627. --*/
  628. LIBC_API
  629. long double
  630. strtold (
  631. const char *String,
  632. char **StringAfterScan
  633. );
  634. /*++
  635. Routine Description:
  636. This routine converts the initial portion of the given string into a
  637. long double. This routine will scan past any whitespace at the beginning of
  638. the string.
  639. Arguments:
  640. String - Supplies a pointer to the null terminated string to convert to a
  641. long double.
  642. StringAfterScan - Supplies a pointer where a pointer will be returned
  643. representing the remaining portion of the string after the long double
  644. was scanned. If the entire string is made up of whitespace or invalid
  645. characters, then this will point to the beginning of the given string
  646. (the scanner will not be advanced).
  647. Return Value:
  648. Returns the long double representation of the string. If the value could not
  649. be converted, 0 is returned, and errno will be set to either EINVAL if the
  650. number could not be converted or ERANGE if the number is outside of the
  651. return type's expressible range.
  652. --*/
  653. LIBC_API
  654. long
  655. strtol (
  656. const char *String,
  657. char **StringAfterScan,
  658. int Base
  659. );
  660. /*++
  661. Routine Description:
  662. This routine converts the initial portion of the given string into an
  663. integer. This routine will scan past any whitespace at the beginning of
  664. the string. The string may have an optional plus or minus in front of the
  665. number to indicate sign.
  666. Arguments:
  667. String - Supplies a pointer to the null terminated string to convert to an
  668. integer.
  669. StringAfterScan - Supplies a pointer where a pointer will be returned
  670. representing the remaining portion of the string after the integer was
  671. scanned. If the entire string is made up of whitespace or invalid
  672. characters, then this will point to the beginning of the given string
  673. (the scanner will not be advanced).
  674. Base - Supplies the base system to interpret the number as. If zero is
  675. supplied, the base will be figured out based on the contents of the
  676. string. If the string begins with 0, it's treated as an octal (base 8)
  677. number. If the string begins with 1-9, it's treated as a decimal
  678. (base 10) number. And if the string begins with 0x or 0X, it's treated
  679. as a hexadecimal (base 16) number. Other base values must be specified
  680. explicitly here.
  681. Return Value:
  682. Returns the integer representation of the string. If the value could not be
  683. converted, 0 is returned, and errno will be set to either EINVAL if the
  684. number could not be converted or ERANGE if the number is outside of the
  685. return type's expressible range.
  686. --*/
  687. LIBC_API
  688. long long
  689. strtoll (
  690. const char *String,
  691. char **StringAfterScan,
  692. int Base
  693. );
  694. /*++
  695. Routine Description:
  696. This routine converts the initial portion of the given string into an
  697. integer. This routine will scan past any whitespace at the beginning of
  698. the string. The string may have an optional plus or minus in front of the
  699. number to indicate sign.
  700. Arguments:
  701. String - Supplies a pointer to the null terminated string to convert to an
  702. integer.
  703. StringAfterScan - Supplies a pointer where a pointer will be returned
  704. representing the remaining portion of the string after the integer was
  705. scanned. If the entire string is made up of whitespace or invalid
  706. characters, then this will point to the beginning of the given string
  707. (the scanner will not be advanced).
  708. Base - Supplies the base system to interpret the number as. If zero is
  709. supplied, the base will be figured out based on the contents of the
  710. string. If the string begins with 0, it's treated as an octal (base 8)
  711. number. If the string begins with 1-9, it's treated as a decimal
  712. (base 10) number. And if the string begins with 0x or 0X, it's treated
  713. as a hexadecimal (base 16) number. Other base values must be specified
  714. explicitly here.
  715. Return Value:
  716. Returns the integer representation of the string. If the value could not be
  717. converted, 0 is returned, and errno will be set to EINVAL to indicate the
  718. number could not be converted.
  719. --*/
  720. LIBC_API
  721. unsigned long
  722. strtoul (
  723. const char *String,
  724. char **StringAfterScan,
  725. int Base
  726. );
  727. /*++
  728. Routine Description:
  729. This routine converts the initial portion of the given string into an
  730. integer. This routine will scan past any whitespace at the beginning of
  731. the string. The string may have an optional plus or minus in front of the
  732. number to indicate sign.
  733. Arguments:
  734. String - Supplies a pointer to the null terminated string to convert to an
  735. integer.
  736. StringAfterScan - Supplies a pointer where a pointer will be returned
  737. representing the remaining portion of the string after the integer was
  738. scanned. If the entire string is made up of whitespace or invalid
  739. characters, then this will point to the beginning of the given string
  740. (the scanner will not be advanced).
  741. Base - Supplies the base system to interpret the number as. If zero is
  742. supplied, the base will be figured out based on the contents of the
  743. string. If the string begins with 0, it's treated as an octal (base 8)
  744. number. If the string begins with 1-9, it's treated as a decimal
  745. (base 10) number. And if the string begins with 0x or 0X, it's treated
  746. as a hexadecimal (base 16) number. Other base values must be specified
  747. explicitly here.
  748. Return Value:
  749. Returns the integer representation of the string. If the value could not be
  750. converted, 0 is returned, and errno will be set to either EINVAL if the
  751. number could not be converted or ERANGE if the number is outside of the
  752. return type's expressible range.
  753. --*/
  754. LIBC_API
  755. unsigned long long
  756. strtoull (
  757. const char *String,
  758. char **StringAfterScan,
  759. int Base
  760. );
  761. /*++
  762. Routine Description:
  763. This routine converts the initial portion of the given string into an
  764. integer. This routine will scan past any whitespace at the beginning of
  765. the string. The string may have an optional plus or minus in front of the
  766. number to indicate sign.
  767. Arguments:
  768. String - Supplies a pointer to the null terminated string to convert to an
  769. integer.
  770. StringAfterScan - Supplies a pointer where a pointer will be returned
  771. representing the remaining portion of the string after the integer was
  772. scanned. If the entire string is made up of whitespace or invalid
  773. characters, then this will point to the beginning of the given string
  774. (the scanner will not be advanced).
  775. Base - Supplies the base system to interpret the number as. If zero is
  776. supplied, the base will be figured out based on the contents of the
  777. string. If the string begins with 0, it's treated as an octal (base 8)
  778. number. If the string begins with 1-9, it's treated as a decimal
  779. (base 10) number. And if the string begins with 0x or 0X, it's treated
  780. as a hexadecimal (base 16) number. Other base values must be specified
  781. explicitly here.
  782. Return Value:
  783. Returns the integer representation of the string. If the value could not be
  784. converted, 0 is returned, and errno will be set to EINVAL to indicate the
  785. number could not be converted.
  786. --*/
  787. //
  788. // Random number functions.
  789. //
  790. LIBC_API
  791. int
  792. rand (
  793. void
  794. );
  795. /*++
  796. Routine Description:
  797. This routine returns a pseudo-random number.
  798. Arguments:
  799. None.
  800. Return Value:
  801. Returns a pseudo-random integer between 0 and RAND_MAX, inclusive.
  802. --*/
  803. LIBC_API
  804. int
  805. rand_r (
  806. unsigned *Seed
  807. );
  808. /*++
  809. Routine Description:
  810. This routine implements the re-entrant and thread-safe version of the
  811. pseudo-random number generator.
  812. Arguments:
  813. Seed - Supplies a pointer to the seed to use. This seed will be updated
  814. to contain the next seed.
  815. Return Value:
  816. Returns a pseudo-random integer between 0 and RAND_MAX, inclusive.
  817. --*/
  818. LIBC_API
  819. void
  820. srand (
  821. unsigned Seed
  822. );
  823. /*++
  824. Routine Description:
  825. This routine sets the seed for the rand function.
  826. Arguments:
  827. Seed - Supplies the seed to use.
  828. Return Value:
  829. None.
  830. --*/
  831. LIBC_API
  832. char *
  833. initstate (
  834. unsigned int Seed,
  835. char *State,
  836. size_t Size
  837. );
  838. /*++
  839. Routine Description:
  840. This routine initializes the state of the random number generator using
  841. the given state data. This routine is neither thread-safe nor reentrant.
  842. Arguments:
  843. Seed - Supplies the seed value to use.
  844. State - Supplies a pointer the random state data to use.
  845. Size - Supplies the size of the random state data. Valid values are 8, 32,
  846. 64, 128, and 256. If the value is not one of these values, it will be
  847. truncated down to one of these values. For data sizes less than 32, a
  848. simple linear congruential random number generator is used. The minimum
  849. valid size is 8.
  850. Return Value:
  851. Returns a pointer to the previous state.
  852. --*/
  853. LIBC_API
  854. char *
  855. setstate (
  856. const char *State
  857. );
  858. /*++
  859. Routine Description:
  860. This routine resets the state of the random number generator to the given
  861. state, previously acquired from initstate. This routine is neither
  862. thread-safe nor reentrant.
  863. Arguments:
  864. State - Supplies a pointer to the state to set.
  865. Return Value:
  866. Returns a pointer to the previous state.
  867. --*/
  868. LIBC_API
  869. void
  870. srandom (
  871. unsigned int Seed
  872. );
  873. /*++
  874. Routine Description:
  875. This routine seeds the non-linear additive feedback random number
  876. generator. This routine is neither thread-safe nor reentrant.
  877. Arguments:
  878. Seed - Supplies the seed value to use.
  879. Return Value:
  880. None.
  881. --*/
  882. LIBC_API
  883. long
  884. random (
  885. void
  886. );
  887. /*++
  888. Routine Description:
  889. This routine returns a random number in the range of 0 to 0x7FFFFFFF,
  890. inclusive. This routine is neither thread-safe nor reentrant.
  891. Arguments:
  892. None.
  893. Return Value:
  894. Returns a pseudo-random number in the range 0 to 2^32 - 1, inclusive.
  895. --*/
  896. LIBC_API
  897. int
  898. initstate_r (
  899. unsigned int Seed,
  900. char *State,
  901. size_t Size,
  902. struct random_data *RandomData
  903. );
  904. /*++
  905. Routine Description:
  906. This routine initializes the state of the random number generator using
  907. the given state data.
  908. Arguments:
  909. Seed - Supplies the seed value to use.
  910. State - Supplies a pointer the random state data to use.
  911. Size - Supplies the size of the random state data. Valid values are 8, 32,
  912. 64, 128, and 256. If the value is not one of these values, it will be
  913. truncated down to one of these values. For data sizes less than 32, a
  914. simple linear congruential random number generator is used. The minimum
  915. valid size is 8.
  916. RandomData - Supplies a pointer to the random state context.
  917. Return Value:
  918. 0 on success.
  919. -1 on failure.
  920. --*/
  921. LIBC_API
  922. int
  923. setstate_r (
  924. const char *State,
  925. struct random_data *RandomData
  926. );
  927. /*++
  928. Routine Description:
  929. This routine resets the state of the random number generator to the given
  930. state.
  931. Arguments:
  932. State - Supplies a pointer to the state to set.
  933. RandomData - Supplies a pointer to the random state to use.
  934. Return Value:
  935. 0 on success.
  936. -1 on failure.
  937. --*/
  938. LIBC_API
  939. int
  940. srandom_r (
  941. unsigned int Seed,
  942. struct random_data *RandomData
  943. );
  944. /*++
  945. Routine Description:
  946. This routine seeds the non-linear additive feedback random number generator.
  947. Arguments:
  948. Seed - Supplies the seed value to use.
  949. RandomData - Supplies a pointer to the random state to use.
  950. Return Value:
  951. 0 on success.
  952. -1 on failure.
  953. --*/
  954. LIBC_API
  955. int
  956. random_r (
  957. struct random_data *RandomData,
  958. int32_t *Result
  959. );
  960. /*++
  961. Routine Description:
  962. This routine returns a random number in the range of 0 to 0x7FFFFFFF,
  963. inclusive.
  964. Arguments:
  965. RandomData - Supplies a pointer to the random state to use.
  966. Result - Supplies a pointer where the result will be returned on success.
  967. Return Value:
  968. 0 on success.
  969. -1 on failure.
  970. --*/
  971. LIBC_API
  972. int
  973. system (
  974. const char *Command
  975. );
  976. /*++
  977. Routine Description:
  978. This routine passes the given command to the command line interpreter. If
  979. the command is null, this routine determines if the host environment has
  980. a command processor. The environment of the executed command shall be as if
  981. the child process were created using the fork function, and then the
  982. child process invoked execl(<shell path>, "sh", "-c", <command>, NULL).
  983. This routine will ignore the SIGINT and SIGQUIT signals, and shall block
  984. the SIGCHLD signal while waiting for the command to terminate.
  985. Arguments:
  986. Command - Supplies an optional pointer to the command to execute.
  987. Return Value:
  988. Returns a non-zero value if the command is NULL and a command processor is
  989. available.
  990. 0 if no command processor is available.
  991. 127 if the command processor could not be executed.
  992. Otherwise, returns the termination status of the command language
  993. interpreter.
  994. --*/
  995. LIBC_API
  996. char *
  997. mktemp (
  998. char *Template
  999. );
  1000. /*++
  1001. Routine Description:
  1002. This routine creates replaces the contents of the given string by a unique
  1003. filename.
  1004. Arguments:
  1005. Template - Supplies a pointer to a template string that will be modified
  1006. in place. The string must end in six X characters. Each X character
  1007. will be replaced by a random valid filename character.
  1008. Return Value:
  1009. Returns a pointer to the template string.
  1010. --*/
  1011. LIBC_API
  1012. char *
  1013. mkdtemp (
  1014. char *Template
  1015. );
  1016. /*++
  1017. Routine Description:
  1018. This routine creates replaces the contents of the given string by a unique
  1019. directory name, and attempts to create that directory.
  1020. Arguments:
  1021. Template - Supplies a pointer to a template string that will be modified
  1022. in place. The string must end in six X characters. Each X character
  1023. will be replaced by a random valid filename character.
  1024. Return Value:
  1025. Returns a pointer to the template string.
  1026. --*/
  1027. LIBC_API
  1028. int
  1029. mkstemp (
  1030. char *Template
  1031. );
  1032. /*++
  1033. Routine Description:
  1034. This routine creates replaces the contents of the given string by a unique
  1035. filename, and returns an open file descriptor to that file.
  1036. Arguments:
  1037. Template - Supplies a pointer to a template string that will be modified
  1038. in place. The string must end in six X characters. Each X character
  1039. will be replaced by a random valid filename character.
  1040. Return Value:
  1041. Returns the open file descriptor to the newly created file on success.
  1042. -1 on failure, and errno will be set to contain more information.
  1043. --*/
  1044. LIBC_API
  1045. int
  1046. getpt (
  1047. void
  1048. );
  1049. /*++
  1050. Routine Description:
  1051. This routine creates and opens a new pseudo-terminal master.
  1052. Arguments:
  1053. None.
  1054. Return Value:
  1055. Returns a file descriptor to the new terminal master device on success.
  1056. -1 on failure, and errno will be set to contain more information.
  1057. --*/
  1058. LIBC_API
  1059. int
  1060. posix_openpt (
  1061. int Flags
  1062. );
  1063. /*++
  1064. Routine Description:
  1065. This routine creates and opens a new pseudo-terminal master.
  1066. Arguments:
  1067. Flags - Supplies a bitfield of open flags governing the open. Only O_RDWR
  1068. and O_NOCTTY are observed.
  1069. Return Value:
  1070. Returns a file descriptor to the new terminal master device on success.
  1071. -1 on failure, and errno will be set to contain more information.
  1072. --*/
  1073. LIBC_API
  1074. int
  1075. grantpt (
  1076. int Descriptor
  1077. );
  1078. /*++
  1079. Routine Description:
  1080. This routine changes the ownership and access permission of the slave
  1081. pseudo-terminal associated with the given master pseudo-terminal file
  1082. descriptor so that folks can open it.
  1083. Arguments:
  1084. Descriptor - Supplies the file descriptor of the master pseudo-terminal.
  1085. Return Value:
  1086. 0 on success.
  1087. -1 on failure, and errno will be set to contain more information.
  1088. --*/
  1089. LIBC_API
  1090. int
  1091. unlockpt (
  1092. int Descriptor
  1093. );
  1094. /*++
  1095. Routine Description:
  1096. This routine unlocks the slave side of the pseudo-terminal associated with
  1097. the given master side file descriptor.
  1098. Arguments:
  1099. Descriptor - Supplies the open file descriptor to the master side of the
  1100. terminal.
  1101. Return Value:
  1102. 0 on success.
  1103. -1 on failure, and errno will be set to contain more information.
  1104. --*/
  1105. LIBC_API
  1106. char *
  1107. ptsname (
  1108. int Descriptor
  1109. );
  1110. /*++
  1111. Routine Description:
  1112. This routine returns the name of the slave pseudoterminal associated
  1113. with the given master file descriptor. This function is neither thread-safe
  1114. nor reentrant.
  1115. Arguments:
  1116. Descriptor - Supplies the open file descriptor to the master side of the
  1117. terminal.
  1118. Return Value:
  1119. Returns a pointer to a static area containing the name of the terminal on
  1120. success. The caller must not modify or free this buffer, and it may be
  1121. overwritten by subsequent calls to ptsname.
  1122. NULL on failure, and errno will be set to contain more information.
  1123. --*/
  1124. LIBC_API
  1125. int
  1126. ptsname_r (
  1127. int Descriptor,
  1128. char *Buffer,
  1129. size_t BufferSize
  1130. );
  1131. /*++
  1132. Routine Description:
  1133. This routine returns the name of the slave pseudoterminal associated
  1134. with the given master file descriptor. This is the reentrant version of the
  1135. ptsname function.
  1136. Arguments:
  1137. Descriptor - Supplies the open file descriptor to the master side of the
  1138. terminal.
  1139. Buffer - Supplies a pointer where the name will be returned on success.
  1140. BufferSize - Supplies the size of the given buffer in bytes.
  1141. Return Value:
  1142. 0 on success.
  1143. -1 on failure, and errno will be set to contain more information.
  1144. --*/
  1145. LIBC_API
  1146. char *
  1147. realpath (
  1148. const char *Path,
  1149. char *ResolvedPath
  1150. );
  1151. /*++
  1152. Routine Description:
  1153. This routine returns the canonical path for the given file path. This
  1154. canonical path will include no '.' or '..' components, and will not
  1155. contain symbolic links in any components of the path. All path components
  1156. must exist.
  1157. Arguments:
  1158. Path - Supplies a pointer to the path to canonicalize.
  1159. ResolvedPath - Supplies an optional pointer to the buffer to place the
  1160. resolved path in. This must be at least PATH_MAX bytes.
  1161. Return Value:
  1162. Returns a pointer to the resolved path on success.
  1163. NULL on failure.
  1164. --*/
  1165. LIBC_API
  1166. int
  1167. getloadavg (
  1168. double LoadAverage[],
  1169. int ElementCount
  1170. );
  1171. /*++
  1172. Routine Description:
  1173. This routine returns the number of processes in the system run queue
  1174. averaged over various periods of time. The elements returned (up to three)
  1175. return the number of processes over the past one, five, and fifteen minutes.
  1176. Arguments:
  1177. LoadAverage - Supplies a pointer where up to three load average values
  1178. will be returned on success.
  1179. ElementCount - Supplies the number of elements in the supplied array.
  1180. Return Value:
  1181. Returns the number of elements returned on success.
  1182. -1 on failure, and errno will be set to contain more information.
  1183. --*/
  1184. #ifdef __cplusplus
  1185. }
  1186. #endif
  1187. #endif