Update contrib.
1 /** @file ../include/stdlib.h
7 @return The abort function
10 The abort function causes abnormal program termination to occur, unless the
11 signal SIGABRT is being caught and the signal handler does not return.
13 Any open streams are flushed and closed.
24 if( (stream = fopen( "notexist.file", "r" )) == NULL )
26 perror( "Error occurred while opening file" );
38 Error occurred while opening file: No such file or directory
44 The abort function is thread-safe. It is unknown whether it is async-cancel-safe.
48 The signal related functionalities are not applicable to the Symbian OS implementation
49 as there is no support for signals from Symbian OS.
60 @return The abs function
64 The abs function computes the absolute value of an integer j. The absolute value is always postive, it has size but not direction.
76 long lx = -41567L, ly;
77 long long llx = -5343546758, lly;
80 printf( "The absolute value of %d is %d
84 printf( "The absolute value of %ld is %ld
88 printf( "The absolute value of %lld is %lld
99 The absolute value of -4 is 4
100 The absolute value of -41567 is 41567
101 The absolute value of -5343546758 is 5343546758
113 @externallyDefinedApi
116 /** @fn atexit(void(*)(void) func)
118 @return The atexit function returns the value 0 if successful; otherwise it returns
119 the value -1 and sets the global variable errno to indicate the error.
122 registers the given function to be called at program exit, whether via exit or via return from the program's main. Functions so registered are called in reverse order;
123 no arguments are passed.
125 These functions must not call exit . If it is necessary to terminate the process while in such a
126 function, the _exit function should be used. Alternatively,
127 the function may cause abnormal process termination, for example by calling abort
129 At least 32 functions can always be registered and more are allowed as long
130 as sufficient memory can be allocated.
132 In Symbian app with an entry point E32Main, the registered functions will not be called unless exit() is specified explicitly.
135 atexit() is not supported on emulator.
142 void fun1( void ), fun2( void ), fun3( void ), fun4( void );
150 printf( "Before exiting...." );
190 void fun1(void),fun2(void),fun3(void);
197 printf( "Before exiting....\n" );
205 printf( " fun1\n " );
211 printf( " fun2\n " );
217 printf( " fun3\n " );
222 GLDEF_C TInt E32Main()
225 CTrapCleanup* cleanup = CTrapCleanup::New();
231 TRAPD(err,ret = MainL());
255 @externallyDefinedApi
258 /** @fn atof(const char *nptr)
260 @return The atof function
262 the converted value if the value can be represented.
264 The atof function converts the initial portion of the string pointed to by nptr to double
268 It is equivalent to: strtod(nptr, (char **)NULL);
271 character is defined in the program's locale (category LC_NUMERIC).
283 double d = atof(" 86778E-2");
285 printf( "result of atof: %f
293 result of atof: 867.78
297 Implementation notes:
299 The atof function is not thread-safe and also not async-cancel-safe. The atof function has been deprecated. Use strtod instead.
309 @externallyDefinedApi
312 /** @fn atoi(const char *nptr)
314 @return The atoi function
316 the converted value if the value can be represented.
318 The atoi function converts the initial portion of the string pointed to by nptr to int
323 It is equivalent to: (int)strtol(nptr, (char **)NULL, 10);
335 char *str = " -56957jdhfjk";
337 printf( "result of atoi: %d
345 result of atoi: -56957
349 Implementation notes:
351 The atoi function is not thread-safe and also not async-cancel safe. The atoi function has been deprecated. Use strtol instead.
361 @externallyDefinedApi
364 /** @fn atol(const char *nptr)
369 This description also covers the following functions -
372 @return The atol and atoll functions
374 the converted value if the value can be represented.
376 The atol function converts the initial portion of the string pointed to by nptr to long
384 strtol(nptr, (char **)NULL, 10);
386 The atoll function converts the initial portion of the string pointed to by nptr to long long
387 integer representation. It is equivalent to:
389 strtoll(nptr, (char **)NULL, 10);
400 long l = atol( "-000002344" );
402 printf( "result of atol: %ld
410 result of atol: -2344
420 long long l = atoll("454756356bs");
422 printf( "result of atoll: %ld
431 result of atoll: 454756356
442 @externallyDefinedApi
445 /** @fn bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *))
451 @return The bsearch function returns a pointer to a matching member of the array,
452 or a null pointer if no match is found. If two or more matching members are found
453 the result is unspecified.
455 The bsearch function searches an array of nmemb objects, the initial member of which is
456 pointed to by base, for a member that matches the object pointed to by key. The size of each member of the array is specified by size.
458 The contents of the array should be in ascending sorted order according to
459 the comparison function referenced by compar. The compar routine is expected to have two arguments which point to the key object and to an array member, in that order. It return an integer
460 less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be
461 greater than the array member.
468 int search_function(const void* a,const void* b)
470 return(*(int *)a - *(int *)b);
475 int arr[] = {3,5,7,8,10};
481 ele = ( int* ) bsearch(&key;, (void *)arr, i, sizeof(arr[0]), search_function);
485 element found: %d", *ele);
503 @externallyDefinedApi
506 /** @fn calloc(size_t number, size_t size)
510 Refer to malloc() for the documentation
517 @externallyDefinedApi
520 /** @fn div(int num, int denom)
523 @return The div function
524 returns a structure of type div_t that contains two int
525 members named quot (quotient)
529 computes the value num/denom and returns the quotient and remainder in a structure named div_t that contains two int
530 members named quot and rem.
544 div_t x = div(numer, denom);
551 exp_numer = (denom * x.quot) + x.rem;
553 if( exp_numer == (numer) )
554 printf("Result is same as the expected output");
556 printf("Unexpected output");
568 Result is same as the expected output
576 @externallyDefinedApi
579 /** @fn exit(int status)
584 This description also covers the following functions -
587 @return The exit and _Exit functions
590 The exit and _Exit functions terminate a process.
592 Before termination, exit performs the following functions in the order listed:
596 1. Call the functions registered with the atexit function, in the reverse order of their registration.
597 2. Flush all open output streams.
598 3. Close all open streams.
599 4. Unlink all files created with the tmpfile function.
602 The _Exit function terminates without calling the functions registered with the atexit function, and may or may not perform the other actions listed.
603 Both functions make the low-order eight bits of the status argument available to a parent process which has called a wait function.
605 The C Standard (-isoC-99) defines the values 0, EXIT_SUCCESS, and EXIT_FAILURE as possible values of status.
606 Cooperating processes may use other values.
608 Note that exit does nothing to prevent bottomless recursion should a function registered using atexit itself call exit.
609 Such functions must call _Exit instead (although this has other effects as well which may not be desired).
613 /* Detailed description : Sample usage of exit system call. */
618 exit(0) ; //Here 0 is exit status of the process
629 @externallyDefinedApi
632 /** @fn free(void *p)
635 Refer to malloc() for the documentation
642 @externallyDefinedApi
645 /** @fn getenv(const char *name)
650 This description also covers the following functions -
651 setenv() putenv() unsetenv()
653 @return The getenv function returns the value of the environment variable as a NULL -terminated string. If the variable name is not in the current environment, NULL is returned. The setenv and putenv functions return the value 0 if successful; otherwise they
654 return the value -1 and set the global variable errno is set to indicate the error. The unsetenv function never returns.
656 These functions set, unset and fetch environment variables from the
657 host environment list.
658 For compatibility with differing environment conventions,
659 the given arguments name and value may be appended and prepended,
661 with an equal sign "="
663 The getenv function obtains the current value of the environment variable name.
665 The setenv function inserts or resets the environment variable name in the current environment list.
666 If the variable name does not exist in the list,
667 it is inserted with the given value. If the variable does exist, the argument overwrite is tested; if overwrite is
669 variable is not reset, otherwise it is reset
672 The putenv function takes an argument of the form "name=value" and is
673 equivalent to: setenv(name, value, 1);
675 The unsetenv function
676 deletes all instances of the variable name pointed to by name from the list.
680 #include <stdlib.h> //getenv
681 #include <stdio.h> //printf
685 int iret = putenv("PATH=c:\sys\bin;");
688 printf( "putenv() failed!!
691 /* fetch new value. */
692 char *psc = getenv("PATH");
695 printf( "new PATH variable is: %s
705 new PATH variable is: c:\sys\bin;
709 #include <stdlib.h> //setenv, getenv
710 #include <stdio.h> //printf
716 /* Unset the value of the HOME environment variable
717 * to make sure that it is non-existing.
721 /* Set the value of HOME environment with no overwrite option */
722 int iret = setenv("HOME", "/home", 0);
725 printf( "Setenv failed!!" );
728 var = getenv( "HOME" );
731 printf( "new HOME variable is: %s
734 /* Set the value of HOME environment with the overwrite option */
735 iret = setenv("HOME", "/check", 1);
738 var = getenv( "HOME" );
741 printf( "new HOME variable is: %s
751 new HOME variable is: \home
752 new HOME variable is: \check
758 @externallyDefinedApi
763 @return The labs() function shall return the absolute value of the long integer operand.
766 returns the absolute value of the long integer j.
778 long lx = -41567L, ly;
779 long long llx = -5343546758, lly;
782 printf( "The abs value of %d is %d
786 printf( "The abs value of %ld is %ld
790 printf( "The abs value of %lld is %lld
800 The absolute value of -4 is 4
801 The absolute value of -41567 is 41567
802 The absolute value of -5343546758 is 5343546758
812 @externallyDefinedApi
815 /** @fn ldiv(long num, long denom)
818 @return The ldiv function
819 returns a structure of type ldiv_t that contains two long
820 members named quot (quotient)
824 computes the value num / denom and returns the quotient and remainder in a structure named ldiv_t
825 that contains two long
826 members named quot and rem.
839 ldiv_t x = ldiv(numer, denom);
843 Remainder = %ld", x.quot, x.rem);
845 long exp_numer = (denom * x.quot) + x.rem;
847 if( exp_numer == (numer) )
849 Result is same as the expected output");
864 Result is same as the expected output
872 @externallyDefinedApi
875 /** @fn malloc(size_t nbytes)
880 This description also covers the following functions -
881 calloc() realloc() reallocf() free()
883 @return The malloc and calloc functions return a pointer to the allocated memory if successful; otherwise
884 a NULL pointer is returned and errno is set to ENOMEM. The realloc and reallocf functions return a pointer, possibly identical to ptr, to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM if the error was the result of an allocation failure. The realloc function always leaves the original buffer intact when an
885 error occurs, whereas reallocf deallocates it in this case. The free function returns no value.
887 The malloc function allocates nbytes of memory. The allocated space is suitably aligned (after possible
888 pointer coercion) for storage of any type of object. If the space is at least pagesize bytes in length (see getpagesize ) the returned memory will be
889 page boundary aligned as well. If malloc fails a NULL pointer is returned.
891 Note that malloc does NOT normally initialize the returned memory to zero bytes.
893 The calloc function allocates space for number of objects, each nbytes in length. The result is identical to calling malloc with an argument of "number * nbytes", with the exception that the allocated memory is explicitly
894 initialized to zero bytes.
896 The realloc function changes the size of the previously allocated memory
897 referenced by ptr to nbytes. The contents of the memory are unchanged up to the lesser
898 of the new and old sizes. If the new size is larger, the value of the newly
899 allocated portion of the memory is undefined. If the requested memory cannot
900 be allocated, NULL is returned and the memory referenced by ptr is valid and unchanged. If memory can be allocated, the memory referenced
901 by ptr is freed and a pointer to the newly allocated memory is returned.
902 Note that realloc and reallocf may move the memory allocation resulting in a different return
903 value from ptr. If ptr is NULL, the realloc function behaves identically to malloc for the specified nbytes.
905 The reallocf function is identical to the realloc function, except that it
906 will free the passed pointer when the requested memory cannot be allocated.
907 This is a specific API designed to ease the problems with traditional coding styles
908 for realloc causing memory leaks in libraries.
910 The free function causes the allocated memory referenced by ptr to be made available for future allocations.
911 If ptr is NULL, no action occurs.
915 #include <stdlib.h> //malloc
916 #include <stdio.h> //printf
920 /* allocate memory */
921 char *pc = (char *)malloc( sizeof(char) * 3);
924 printf( "memory insufficient
928 printf( "memory allocated
931 printf( "memory freed
947 #include <stdio.h> //printf
948 #include <stdlib.h> //calloc
952 int *pint = (int *)calloc(2, sizeof (int) * 2);
954 printf( "allocated 2 blocks of memory, each of size -
955 twice the size of an integer
958 printf( "can't allocate memory
969 allocated 2 blocks of memory, each of size - 2 integers
973 #include <stdio.h> //printf
974 #include <stdlib.h> //realloc
978 int *pint = (int *)calloc(2, sizeof (int) * 2);
981 printf("calloc failed to allocate memory
987 printf("calloc allocated memory: 128 bytes
991 pint = (int *)realloc(pint, (sizeof (int) * 6) );
994 printf("realloc failed to allocate memory
1000 printf("realloc allocated memory: 192 bytes
1012 calloc allocated memory: 128 bytes
1013 realloc allocated memory: 192 bytes
1022 @externallyDefinedApi
1025 /** @fn mblen(const char *s, size_t n)
1028 @return If s is a null pointer, mblen returns non-zero 0 value depending upon the current locale (see
1029 below). If s is not a null pointer, mblen returns : 0 (if mbchar points to the null byte), the number of bytes that constitute the character (if the next n or fewer bytes form a valid character), or -1 (if they do not form a valid character) and may set errno to indicate the error. In no case is the value returned greater than n or the value of the {MB_CUR_MAX} macro.
1031 The mblen function computes the length in bytes
1032 of a multibyte character s according to the current conversion state.
1033 Up to n bytes are examined.
1035 A call with a null s pointer returns nonzero if the current locale requires shift states,
1037 if shift states are required, the shift state is reset to the initial state.
1039 The behavior of the mblen is affected by LC_CTYPE category of the current locale.
1045 /* Illustrates how to use mblen API */
1046 int example_mblen(wchar_t wc)
1050 /* determine the number bytes in the multibyte char */
1051 len = mblen(wc, MB_CUR_MAX);
1054 wprintf(L"mblen returned error!!
1057 /* return the no of bytes */
1065 The current implementation of mblen is not affected by the LC_CTYPE category of the current locale.
1066 It works only for UTF8 character set.
1073 @externallyDefinedApi
1076 /** @fn mbstowcs(wchar_t * pwcs, const char * s, size_t n)
1080 @return The mbstowcs function returns the number of wide characters converted,
1081 not counting any terminating null wide character, or -1
1082 if an invalid multibyte character was encountered.
1084 The mbstowcs function converts a multibyte character string mbstring beginning in the initial conversion state
1085 into a wide character string wcstring. No more than n wide characters are stored.
1086 A terminating null wide character is appended if there is room.
1088 The behavior of the mbstowcs is affected by LC_CTYPE category of the current locale.
1097 /* Illustrates how to use mbstowcs API */
1098 size_t example_mbstowcs(wchar_t *wc, char *s, size_t n)
1101 /* converting multibyte string to a wide-char string */
1102 len = mbstowcs(wc, s, n);
1103 /* checking for error */
1106 wprintf(L"mbstowcs returned error!!
1109 /* returning no of bytes consumed */
1117 The current implementation of mbstowcs is not affected by the LC_CTYPE category of the current locale.
1118 It works only for UTF8 character set.
1125 @externallyDefinedApi
1128 /** @fn mbtowc(wchar_t * pwc, const char * s, size_t n)
1132 @return If mbchar is NULL, the mbtowc function returns nonzero if shift states are supported,
1133 zero otherwise. Otherwise, if mbchar is not a null pointer, mbtowc either returns 0 if mbchar represents the null wide character, or returns
1134 the number of bytes processed in mbchar, or returns -1 if no multibyte character
1135 could be recognized or converted.
1136 In this case, mbtowc internal conversion state is undefined.
1138 The mbtowc function converts a multibyte character mbchar into a wide character according to the current conversion state,
1139 and stores the result in the object pointed to by wcharp. Up to n bytes are examined.
1141 A call with a null mbchar pointer returns nonzero if the current encoding requires shift
1142 states, zero otherwise. If shift states are required the shift state is reset
1143 to the initial state.
1145 The behavior of the mbtowc is affected by LC_CTYPE category of the current locale.
1153 /* Illustrates how to use mbtowc API */
1154 int example_mbtowc(wchar_t *wc, char *s)
1157 /* converting multibyte sequence to a wide-character */
1158 len = mbtowc(wc, s, MB_CUR_MAX);
1159 /* checking for error */
1162 wprintf(L"mbtowc returned error!!
1165 /* returning no of bytes consumed */
1173 The current implementation of mbtowc is not affected by the LC_CTYPE category of the current locale.
1174 It works only for UTF8 character set.
1184 @externallyDefinedApi
1187 /** @fn qsort(void *, size_t, size_t, int(*)(const void *, const void *))
1189 @return The qsort function
1192 The qsort function is a modified partition-exchange sort, or quicksort.
1194 The qsort function sorts an array of nmemb objects, the initial member of which is pointed to by base. The size of each object is specified by size.
1196 The contents of the array base are sorted in ascending order according to
1197 a comparison function pointed to by compar, which requires two arguments pointing to the objects being
1200 The comparison function must return an integer less than, equal to, or
1201 greater than zero if the first argument is considered to be respectively
1202 less than, equal to, or greater than the second.
1204 The algorithm implemented by qsort is not stable, that is, if two members compare as equal, their order in
1205 the sorted array is undefined.
1207 The qsort function is an implementation of C.A.R.
1210 a variant of partition-exchange sorting; in particular, see D.E. Knuth Ns's Algorithm Q . Quicksort takes O N lg N average time.
1214 This implementation uses median selection to avoid its O N**2 worst-case behavior.
1221 int sort_function( const void *a, const void *b);
1226 int list[5] = {4,2,3,5,1};
1227 qsort((void *)list, 5, sizeof(list[0]), sort_function);
1228 for (x = 0; x < 5; x++)
1234 int sort_function( const void *a, const void *b)
1263 @externallyDefinedApi
1269 Refer to srand() for the documentation
1275 @externallyDefinedApi
1278 /** @fn realloc(void *p, size_t nbytes)
1282 Refer to malloc() for the documentation
1289 @externallyDefinedApi
1292 /** @fn srand(unsigned seed)
1297 This description also covers the following functions -
1300 @return The rand function
1301 never returns return the next pseudo-random number in the sequence. The srand function never returns.
1303 The rand function computes a sequence of pseudo-random integers in the range
1304 of 0 to RAND_MAX (as defined by the header file \#include \<stdlib.h\> ).
1306 The srand function sets its argument seed as the seed for a new sequence of
1307 pseudo-random numbers to be returned by rand. These sequences are repeatable by calling srand with the same seed value.
1309 If no seed value is provided, the functions are automatically
1310 seeded with a value of 1.
1326 randArray[i]=rand();
1366 int seedVal = 45454652;
1370 printf("Random Value returned is %d"), randVal);
1377 Random Value returned is 1599641479
1383 @externallyDefinedApi
1386 /** @fn strtod(const char * s, char ** tail)
1392 This description also covers the following functions -
1395 @return The strtod , strtof ,
1396 and strtold functions return the converted value, if any. If endptr is not NULL ,
1397 a pointer to the character after the last character used
1398 in the conversion is stored in the location referenced by endptr . If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr . If the correct value would cause overflow, plus or minus HUGE_VAL , HUGE_VALF , or HUGE_VALL is returned (according to the sign and type of the return
1399 value), and ERANGE is stored in errno . If the correct value would cause underflow a value, of the appropriate
1400 type, with magnitude is no greater than the smallest normalized positive number
1401 (KMinTReal in case of Symbian OS) is returned and ERANGE is stored in errno .
1403 These conversion functions convert the initial portion of the string pointed to by nptr to double , float , and long double representation, respectively.
1405 The expected form of the string is an optional plus ("+") or minus sign ("-") followed by either:
1409 * a decimal significand consisting of a sequence of decimal digits optionally containing a decimal-point character, or
1410 * a hexadecimal significand consisting of a "0X" or "0x" followed by a sequence of hexadecimal digits optionally containing a decimal-point character.
1413 In both cases, the significand may be optionally followed by an exponent. An exponent consists of an "E" or "e" (for decimal constants) or a "P" or
1414 "p" (for hexadecimal constants), followed by an optional plus or minus sign, followed by a sequence of decimal digits.
1415 For decimal constants, the exponent indicates the power of 10 by which the significand should be scaled. For hexadecimal constants, the scaling is instead done by powers of 2.
1417 Alternatively, if the portion of the string following the optional plus or minus sign begins with "INFINITY" or "NAN", ignoring case, it is interpreted as an infinity or a quiet NaN, respectively.
1419 In any of the above cases, leading white-space characters in the string (as defined by the isspace function) are skipped. The decimal point character is defined in the program's locale (category LC_NUMERIC).
1431 d = strtod("0x00e123bhduitri", &endpt;);
1433 printf("{Expected: 922171.0} %f
1435 printf("{Expected: \"hduitri\"} %s
1444 {Expected: 922171.0} 922171.0
1445 {Expected: "hduitri"} hduitri
1451 All these functions don't support the long double length modifiers.
1462 @externallyDefinedApi
1465 /** @fn strtof(const char * s, char ** tail)
1469 Refer to strtod() for the documentation
1479 @externallyDefinedApi
1482 /** @fn strtol(const char * nptr, char ** endptr, int base)
1489 This description also covers the following functions -
1490 strtoll() strtoimax() strtoq()
1492 @return The strtol , strtoll , strtoimax and strtoq functions return the result of the conversion, unless the value
1493 would underflow or overflow. If no conversion could be performed, 0 is returned
1494 and the global variable errno is set to EINVAL (the last feature is not portable across all platforms). If
1495 an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped according to the following
1500 Function underflow overflow
1502 strtol LONG_MIN LONG_MAX
1503 strtoll LLONG_MIN LLONG_MAX
1504 strtoimax INTMAX_MIN INTMAX_MAX
1505 strtoq LLONG_MIN LLONG_MAX
1509 converts the string in nptr to a long
1511 The strtoll function
1512 converts the string in nptr to a long long
1514 The strtoimax function
1515 converts the string in nptr to an intmax_t
1518 converts the string in nptr to a quad_t
1520 The conversion is done according to the given base ,
1521 which must be between 2 and 36 inclusive,
1522 or be the special value 0.
1524 The string may begin with an arbitrary amount of white space
1525 (as determined by isspace )
1526 followed by a single optional "+"
1529 If base is zero or 16,
1530 the string may then include a "0x"
1532 and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is "0",
1533 in which case it is taken as 8 (octal).
1535 The remainder of the string is converted to a long , long long , intmax_t
1537 value in the obvious manner,
1538 stopping at the first character which is not a valid digit
1540 (In bases above 10, the letter "A"
1541 in either upper or lower case
1543 represents 11, and so forth, with "Z"
1546 If endptr is not NULL , strtol stores the address of the first invalid character in *endptr .
1547 If there were no digits at all, however, strtol stores the original value of nptr in *endptr .
1548 (Thus, if *nptr is not "\\0"
1549 but **endptr is "\\0"
1550 on return, the entire string was valid.)
1562 l = strtol("0x1236nvbi", &endpt;, 0);
1564 printf("{Expected: 4662} %ld
1566 printf("{Expected: \"nvbi\"} %s
1576 {Expected: 4662} 4662
1577 {Expected: "nvbi"} nvbi
1589 @externallyDefinedApi
1592 /** @fn strtold(const char *s, char **sp)
1596 Refer to strtod() for the documentation
1606 @externallyDefinedApi
1609 /** @fn strtoul(const char * nptr, char ** endptr, int base)
1616 This description also covers the following functions -
1617 strtoull() strtoumax() strtouq()
1619 @return The strtoul , strtoull , strtoumax and strtouq functions
1620 return either the result of the conversion
1621 or, if there was a leading minus sign,
1622 the negation of the result of the conversion,
1623 unless the original (non-negated) value would overflow;
1624 in the latter case, strtoul returns ULONG_MAX , strtoull returns ULLONG_MAX , strtoumax returns UINTMAX_MAX ,
1625 and strtouq returns ULLONG_MAX .
1626 In all cases, errno is set to ERANGE .
1627 If no conversion could be performed, 0 is returned and
1628 the global variable errno is set to EINVAL (the last feature is not portable across all platforms).
1630 The strtoul function
1631 converts the string in nptr to an unsigned long
1633 The strtoull function
1634 converts the string in nptr to an unsigned long long
1636 The strtoumax function
1637 converts the string in nptr to an uintmax_t
1639 The strtouq function
1640 converts the string in nptr to a u_quad_t
1642 The conversion is done according to the given base ,
1643 which must be between 2 and 36 inclusive,
1644 or be the special value 0.
1646 The string may begin with an arbitrary amount of white space
1647 (as determined by isspace )
1648 followed by a single optional "+"
1651 If base is zero or 16,
1652 the string may then include a "0x"
1654 and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is 'o',
1655 in which case it is taken as 8 (octal).
1657 The remainder of the string is converted to an unsigned long
1658 value in the obvious manner,
1659 stopping at the end of the string
1660 or at the first character that does not produce a valid digit
1662 (In bases above 10, the letter "A"
1663 in either upper or lower case
1665 represents 11, and so forth, with "Z"
1668 If endptr is not NULL , strtoul stores the address of the first invalid character in *endptr .
1669 If there were no digits at all, however, strtoul stores the original value of nptr in *endptr .
1670 (Thus, if *nptr is not "\\0"
1671 but **endptr is "\\0"
1672 on return, the entire string was valid.)
1682 unsigned long ul = 0;
1684 ul = strtoul("435435hmnb", &endpt;, 12);
1686 printf("{Expected: 1066793} %ld
1688 printf("{Expected: \"hmnb\"} %s
1698 {Expected: 1066793} 1066793
1699 {Expected: "hmnb"} hmnb
1707 @externallyDefinedApi
1710 /** @fn system(const char *cmd)
1712 @return The system function returns the exit status of the child process as returned by process exit reason or -1 if an error occurred when spawning a new process. It returns a non-zero value when NULL is passed as its argument.
1715 spawns another process with the argument cmd. The calling process waits for the child process
1716 to finish executing.
1718 If cmd is a NULL pointer, system will return non-zero to indicate that system is suporrted
1719 and zero if it is not supported.
1722 returns the exit status of the child process as returned by
1723 process' exit reason
1724 or -1 if an error occurred when spawning a new process.
1728 #include <stdlib.h> //system
1729 #include <stdio.h> //printf
1735 printf( "Calling system()...
1738 /* helloworld.exe is an executable that just prints
1739 * "Hello world!" and it should be created before
1740 * executing this example code.
1742 retVal = system("c:\sys\bin\helloworld.exe");
1744 /* Print the return value of system() */
1745 printf( "system() returned: %d", retVal );
1755 system() returned: -1
1762 @externallyDefinedApi
1765 /** @fn wctomb(char *s, wchar_t wchar)
1768 @return If s is a null pointer, wctomb returns a non-zero or 0 value according to the current locale. If s is not a null pointer, wctomb returns -1 if the value of src does not correspond to
1769 a valid character, or returns the number of bytes that constitute the character
1770 corresponding to the value of wchar . In no case is value returned greater than the value of the {MB_CUR_MAX} macro.
1772 The wctomb function converts a wide character wchar into a multibyte character and stores
1773 the result in mbchar .
1774 The object pointed to by mbchar must be large enough to accommodate the multibyte character, which
1775 may be up to MB_LEN_MAX bytes.
1777 A call with a null mbchar pointer returns nonzero if the current locale requires shift states,
1779 if shift states are required, the shift state is reset to the initial state.
1781 The behavior of the wctomb is affected by LC_CTYPE category of the current locale.
1787 /* Illustrates how to use wctomb API */
1788 int example_wctomb(wchar_t wc)
1790 char s[MAX_CUR_MAX];
1793 /* represent a wide-char in a single byte*/
1794 len = wctomb(s, wc);
1795 /* return the number of bytes */
1803 The current implementation of wctomb is not affected by the LC_CTYPE category of the current locale.
1804 It works only for UTF8 character set.
1813 @externallyDefinedApi
1816 /** @fn wcstombs(char * s, const wchar_t * pwcs, size_t n)
1820 @return The wcstombs function returns the number of bytes converted (not including
1821 any terminating null), if successful, otherwise it returns ( size_t)(-1) .
1823 The wcstombs function converts a wide character string wcstring into a multibyte character string, mbstring ,
1824 beginning in the initial conversion state.
1825 Up to n bytes are stored in mbstring .
1826 Partial multibyte characters at the end of the string are not stored.
1827 The multibyte character string is null terminated if there is room.
1829 The behavior of the wcstombs is affected by LC_CTYPE category of the current locale.
1835 /* Illustrates how to use wcstombs API */
1836 size_t example_wcstombs(wchar_t *wcs, char *s, size_t n)
1839 /* converting multibyte string to a wide-char string */
1840 len = wcstombs(s, (const wchar_t *)wcs, n);
1841 /* returning no of bytes that make up the multibyte sequence */
1849 The current implementation of wcstombs is not affected by the LC_CTYPE category of the current locale.
1850 It works only for UTF8 character set.
1858 @externallyDefinedApi
1861 /** @fn atoll(str) const char *str
1864 Refer to atol() for the documentation
1873 @externallyDefinedApi
1876 /** @fn llabs(long long j)
1878 @return The llabs function
1879 returns the absolue value.
1881 The llabs function returns the absolute value of j.
1893 long lx = -41567L, ly;
1894 long long llx = -5343546758, lly;
1897 printf( "The abs value of %d is %d
1901 printf( "The abs value of %ld is %ld
1905 printf( "The abs value of %lld is %lld
1915 The absolute value of -4 is 4
1916 The absolute value of -41567 is 41567
1917 The absolute value of -5343546758 is 5343546758
1928 @externallyDefinedApi
1931 /** @fn lldiv(long long numer, long long denom)
1935 The lldiv function computes the value of numer divided by denom and returns the stored result in the form of the lldiv_t type.
1937 The lldiv_t type is defined as:
1940 long long quot; /* Quotient. */
1941 long long rem; /* Remainder. */
1949 #include <math.h> /* link to the math lib -libm */
1953 long long numer = pow(2, 40);
1954 long long denom = 3;
1957 lldiv_t x = lldiv(-numer, denom);
1961 Remainder = %ld", x.quot, x.rem);
1963 long long exp_numer = (denom * x.quot) + x.rem;
1965 if( exp_numer == (-numer) )
1969 printf("Unexpected output");
1979 Quotient = -366503875925
1991 @externallyDefinedApi
1994 /** @fn strtoll(const char * nptr, char ** endptr, int base)
1999 Refer to strtol() for the documentation
2009 @externallyDefinedApi
2012 /** @fn strtoull(const char * nptr, char ** endptr, int base)
2017 Refer to strtoul() for the documentation
2023 @externallyDefinedApi
2026 /** @fn _Exit(int code)
2029 Refer to _exit() for the documentation
2032 @externallyDefinedApi
2035 /** @fn setenv(const char *name, const char *value, int overwrite)
2040 Refer to getenv() for the documentation
2045 @externallyDefinedApi
2048 /** @fn unsetenv(const char *name)
2051 Refer to getenv() for the documentation
2056 @externallyDefinedApi
2060 /** @fn mkstemp(char *template)
2062 @return The mkstemp function
2063 returns -1 if no suitable file could be created
2064 and an error code is placed in the global variable. errno.
2066 The mkstemp function
2067 takes the given file name template and overwrites a portion of it
2068 to create a file with that name and returns a file descriptor
2069 opened for reading and writing.
2070 This file name is guaranteed not to exist at the time of function invocation
2071 and is suitable for use
2073 The template may be any file name with some number of "X s"
2079 The trailing "X s" are replaced with a
2080 unique alphanumeric combination.
2081 The number of unique file names mkstemp can return depends on the number of "X s"
2084 result in mkstemp selecting one of 56800235584 (62 ** 6) possible temporary file names.
2090 #include <stdio.h> //printf, SEEK_SET
2095 char arr[] = "c:\someXXXXXXXX";
2098 //create a temporary file using mkstemp()
2099 int fd = mkstemp(arr);
2104 write(fd, "hello", 5);
2105 //seek to the beginning of the file
2106 lseek(fd, 0, SEEK_SET); //beg of the file
2107 //read from the file
2114 printf("buf read: %s", buf);
2128 A common problem that results in a core dump is that the programmer passes
2129 in a read-only string to mkstemp. This is particulary so with programs that were developed before -isoC compilers were common. For example, calling mkstemp with an argument of "/tmp/tempfile.XXXXXX" will result in a core dump due to mkstemp attempting to modify the string constant that was given. If
2130 the program in question makes heavy use of that type of function call, you do
2131 have the option of compiling the program so that it will store string constants
2132 in a writable segment of memory.
2141 @externallyDefinedApi
2144 /** @fn mkstemp64(char *template)
2146 @return The mkstemp64 function
2147 returns -1 if no suitable file could be created
2148 and an error code is placed in the global variable. errno.
2150 The mkstemp64() function generates a unique temporary file name from template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique.
2151 The mkstemp64() function is a 64-bit version of mkstemp.
2152 This function can be used to create tmp files which can grow more than 32 bit sizes
2157 @externallyDefinedApi
2160 /** @fn putenv(const char *string)
2163 Refer to getenv() for the documentation
2168 @externallyDefinedApi
2171 /** @fn random(void)
2175 This description also covers the following functions -
2176 srandom() srandomdev() initstate() setstate()
2179 uses a non-linear additive feedback random number generator employing a
2180 default table of size 31 long integers to return successive pseudo-random
2181 numbers in the range from 0 to
2182 (2**(310) -1). The period of this random number generator is very large, approximately
2185 The random and srandom functions have (almost) the same calling sequence and initialization properties as the rand and srand functions.
2186 The difference is that rand produces a much less random sequence in fact, the low dozen bits
2187 generated by rand go through a cyclic pattern.
2188 All the bits generated by random are usable.
2189 For example, 'random()\&01'
2190 will produce a random binary
2193 Like rand random will by default produce a sequence of numbers that can be duplicated
2194 by calling srandom with "1"
2197 The srandomdev routine initializes a state array using the random random number device which returns good random numbers,
2198 suitable for cryptographic use.
2199 Note that this particular seeding
2200 procedure can generate states which are impossible to reproduce by
2201 calling srandom with any value, since the succeeding terms in the
2202 state buffer are no longer derived from the LC algorithm applied to
2205 The initstate routine allows a state array, passed in as an argument, to be initialized
2207 The size of the state array (in bytes) is used by initstate to decide how sophisticated a random number generator it should use the
2208 more state, the better the random numbers will be.
2209 (Current "optimal" values for the amount of state information are
2210 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
2211 the nearest known amount.
2212 Using less than 8 bytes will cause an error.)
2213 The seed for the initialization (which specifies a starting point for
2214 the random number sequence, and provides for restarting at the same
2215 point) is also an argument.
2216 The initstate function
2217 returns a pointer to the previous state information array.
2219 Once a state has been initialized, the setstate routine provides for rapid switching between states.
2220 The setstate function
2221 returns a pointer to the previous state array; its
2222 argument state array is used for further random number generation
2223 until the next call to initstate or setstate.
2225 Once a state array has been initialized, it may be restarted at a different
2226 point either by calling initstate (with the desired seed, the state array, and its size) or
2227 by calling both setstate (with the state array) and srandom (with the desired seed). The advantage of calling both setstate and srandom is that the size of the state array does not have to be remembered
2228 after it is initialized.
2230 With 256 bytes of state information, the period of the random number generator
2231 is greater than 2**(690) , which should be sufficient for most purposes.
2235 //Illustrates how to use srandom API.
2240 // srandom function call sets its argument as the seed for the new sequence of random integers //generated by random
2242 // Function call to generate the random number, which will generate the random based on the //argument passed to the srandom function.
2244 //print the random number generated.
2245 printf("random number is %d",randNum);
2252 random number is 1505335290.
2257 If initstate is called with less than 8 bytes of state information, or if setstate detects that the state information has been garbled, error
2258 messages are printed on the standard error output.
2266 About 2/3 the speed of rand The historical implementation used to have a very weak seeding; the
2267 random sequence did not vary much with the seed.
2268 The current implementation employs a better pseudo-random number
2269 generator for the initial state calculation. Applications requiring cryptographic quality randomness should use arc4random .
2274 @externallyDefinedApi
2277 /** @fn srandom(unsigned long seed)
2280 Refer to random() for the documentation
2288 @externallyDefinedApi
2291 /** @fn realpath(const char *pathname, char resolved_path[])
2293 @param resolved_path[]
2294 @return The realpath function returns resolved_path on success.
2295 If an error occurs, realpath returns NULL, and resolved_path contains the pathname which caused the problem.
2297 The realpath function resolves all symbolic links, extra "/"
2298 characters and references to /./ and /../ in pathname, and copies the resulting absolute pathname into
2299 the memory referenced by resolved_path. The resolved_path argument must refer to a buffer capable of storing at least PATH_MAX characters.
2301 The realpath function will resolve both absolute and relative paths
2302 and return the absolute pathname corresponding to pathname. All but the last component of pathname must exist when realpath is called.
2307 #include<stdio.h> //printf
2308 #include<sys/stat.h> //S_IWUSR
2309 #include<sys/syslimits.h> //PATH_MAX
2310 #include<unistd.h> //chdir
2314 char resolvepath[PATH_MAX];
2319 fp = fopen("c:\xyz.txt", "w");
2322 printf("fopen failed!!");
2326 mkdir("c:\tmdir", S_IWUSR);
2328 int c = chdir("c:\");
2331 printf("chdir failed!!");
2335 rpath = realpath(".\tmdir\..\xyz.txt", resolvepath);
2336 printf("resolvepath: %s", resolvepath);
2338 printf("rpath: %s", rpath);
2342 unlink("c:\xyz.txt");
2344 mkdir("c:\tdir", S_IWUSR);
2346 fp = fopen("c:\tdir\xyz.txt", "w");
2349 printf("fopen failed!!");
2355 unlink("c:\linkname.txt");
2357 isymlink = symlink("c:\tdir\xyz.txt", "c:\linkname.txt");
2360 printf("symlink failed!!");
2364 rpath = realpath("c:\linkname.txt", resolvepath);
2366 printf("resolvepath: %s", resolvepath);
2368 printf("rpath: %s", rpath);
2370 unlink("c:\tdir\xyz.txt");
2380 resolvepath: C:\xyz.txt
2382 resolvepath: c: dir\xyz.txt
2383 rpath: c: dir\xyz.txt
2389 @externallyDefinedApi
2392 /** @fn setstate(char *state)
2395 Refer to random() for the documentation
2403 @externallyDefinedApi
2406 /** @fn initstate(unsigned long seed, char *state, long n)
2411 Refer to random() for the documentation
2419 @externallyDefinedApi
2423 /** @fn getprogname(void)
2425 Refer to setprogname() for the documentation
2428 @externallyDefinedApi
2431 /** @fn reallocf(void *ptr, size_t size)
2435 Refer to malloc() for the documentation
2442 @externallyDefinedApi
2445 /** @fn setprogname(const char *programname)
2448 These utility functions get and set the current program's name as used by
2449 various error-reporting functions.getprogname() returns the name of the current program.
2450 This function is typically useful when generating error messages or other diagnostic out-put.
2451 If the program name has not been set, getprogname() will return NULL.
2452 setprogname() sets the name of the current program to be the last path-name component of the programname argument.
2453 It should be invoked at the start of the program, using the argv[0] passed into the program's main() function.
2454 A pointer into the string pointed to by the programname argument is kept as the program name.
2455 Therefore, the string pointed to by programname should not be modified during the rest of the program's operation.
2456 A program's name can only be set once, and in NetBSD that is actually done by program start-up code that is run before main() is called.
2457 Therefore, in NetBSD, calling setprogname() from main() has no effect.
2458 However, it does serve to increase the portability of the program:
2459 on other operating systems, getprogname() and setprogname() may be implemented by a portability library,
2460 and a call to setprogname() allows that library to know the program name without modifications to that system's program start-up code.
2462 @externallyDefinedApi
2465 /** @fn strtoq(const char *nptr, char **endptr, int base)
2470 Refer to strtol() for the documentation
2480 @externallyDefinedApi
2483 /** @fn strtouq(const char *nptr, char **endptr, int base)
2488 Refer to strtoul() for the documentation
2494 @externallyDefinedApi
2500 Contains the following members
2503 @externallyDefinedApi
2506 /** @var div_t::quot
2517 Contains the following members
2520 @externallyDefinedApi
2523 /** @var ldiv_t::quot
2527 /** @var ldiv_t::rem
2534 Contains the following members
2537 @externallyDefinedApi
2540 /** @var lldiv_t::quot
2544 /** @var lldiv_t::rem
2549 /** @def EXIT_FAILURE
2551 These are arguments for the exit and _exit functions and the return values for the atexit and _onexit functions.
2554 @externallyDefinedApi
2558 /** @def EXIT_SUCCESS
2560 These are arguments for the exit and _exit functions and the return values for the atexit and _onexit functions.
2563 @externallyDefinedApi
2569 The constant RAND_MAX is the maximum value that can be returned by the rand function.
2572 @externallyDefinedApi
2579 The value of MB_CUR_MAX is the maximum number of bytes in a multibyte character for the current locale.
2582 @externallyDefinedApi