|
1 /** @file ../include/stdlib.h |
|
2 @internalComponent |
|
3 */ |
|
4 |
|
5 /** @fn abort(void) |
|
6 |
|
7 @return The abort function |
|
8 never returns. |
|
9 |
|
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. |
|
12 |
|
13 Any open streams are flushed and closed. |
|
14 |
|
15 Examples: |
|
16 @code |
|
17 #include <stdio.h> |
|
18 #include <stdlib.h> |
|
19 |
|
20 int main( void ) |
|
21 { |
|
22 FILE *stream; |
|
23 |
|
24 if( (stream = fopen( "notexist.file", "r" )) == NULL ) |
|
25 { |
|
26 perror( "Error occurred while opening file" ); |
|
27 abort(); |
|
28 } |
|
29 else |
|
30 fclose( stream ); |
|
31 return 0; |
|
32 } |
|
33 |
|
34 @endcode |
|
35 |
|
36 Output |
|
37 @code |
|
38 Error occurred while opening file: No such file or directory |
|
39 |
|
40 @endcode |
|
41 |
|
42 Implementation notes: |
|
43 |
|
44 The abort function is thread-safe. It is unknown whether it is async-cancel-safe. |
|
45 |
|
46 Limitations: |
|
47 |
|
48 The signal related functionalities are not applicable to the Symbian OS implementation |
|
49 as there is no support for signals from Symbian OS. |
|
50 |
|
51 @see exit() |
|
52 |
|
53 |
|
54 @publishedAll |
|
55 @externallyDefinedApi |
|
56 */ |
|
57 |
|
58 /** @fn abs(int j) |
|
59 @param j |
|
60 @return The abs function |
|
61 returns |
|
62 the absolute value. |
|
63 |
|
64 The abs function computes the absolute value of an integer j. The absolute value is always postive, it has size but not direction. |
|
65 |
|
66 |
|
67 |
|
68 Examples: |
|
69 @code |
|
70 #include <stdio.h> |
|
71 #include <stdlib.h> |
|
72 |
|
73 int main( void ) |
|
74 { |
|
75 int ix = -4, iy; |
|
76 long lx = -41567L, ly; |
|
77 long long llx = -5343546758, lly; |
|
78 |
|
79 iy = abs( ix ); |
|
80 printf( "The absolute value of %d is %d |
|
81 ", ix, iy); |
|
82 |
|
83 ly = labs( lx ); |
|
84 printf( "The absolute value of %ld is %ld |
|
85 ", lx, ly); |
|
86 |
|
87 lly = llabs( llx ); |
|
88 printf( "The absolute value of %lld is %lld |
|
89 ", llx, lly ); |
|
90 |
|
91 return 0; |
|
92 } |
|
93 |
|
94 @endcode |
|
95 |
|
96 @code |
|
97 Output |
|
98 |
|
99 The absolute value of -4 is 4 |
|
100 The absolute value of -41567 is 41567 |
|
101 The absolute value of -5343546758 is 5343546758 |
|
102 |
|
103 @endcode |
|
104 @see fabs() |
|
105 @see floor() |
|
106 @see hypot() |
|
107 @see labs() |
|
108 @see llabs() |
|
109 @see math() |
|
110 |
|
111 |
|
112 @publishedAll |
|
113 @externallyDefinedApi |
|
114 */ |
|
115 |
|
116 /** @fn atexit(void(*)(void) func) |
|
117 @param 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. |
|
120 |
|
121 The atexit function |
|
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. |
|
124 |
|
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 |
|
128 |
|
129 At least 32 functions can always be registered and more are allowed as long |
|
130 as sufficient memory can be allocated. |
|
131 |
|
132 In Symbian app with an entry point E32Main, the registered functions will not be called unless exit() is specified explicitly. |
|
133 |
|
134 Limitation: |
|
135 atexit() is not supported on emulator. |
|
136 |
|
137 Examples: |
|
138 @code |
|
139 #include <stdlib.h> |
|
140 #include <stdio.h> |
|
141 |
|
142 void fun1( void ), fun2( void ), fun3( void ), fun4( void ); |
|
143 |
|
144 int main( void ) |
|
145 { |
|
146 atexit( fun1 ); |
|
147 atexit( fun2 ); |
|
148 atexit( fun3 ); |
|
149 atexit( fun4 ); |
|
150 printf( "Before exiting...." ); |
|
151 } |
|
152 |
|
153 void fun1() |
|
154 { |
|
155 printf( " fun1 " ); |
|
156 } |
|
157 |
|
158 void fun2() |
|
159 { |
|
160 printf( " fun2" ); |
|
161 } |
|
162 |
|
163 void fun3() |
|
164 { |
|
165 printf( " fun3 " ); |
|
166 } |
|
167 |
|
168 void fun4() |
|
169 { |
|
170 printf( " fun4 " ); |
|
171 } |
|
172 |
|
173 @endcode |
|
174 @code |
|
175 Output |
|
176 |
|
177 Before exiting... |
|
178 fun4 |
|
179 fun3 |
|
180 fun2 |
|
181 fun1 |
|
182 |
|
183 @endcode |
|
184 |
|
185 @code |
|
186 #include <stdlib.h> |
|
187 #include <stdio.h> |
|
188 #include <e32base.h> |
|
189 |
|
190 void fun1(void),fun2(void),fun3(void); |
|
191 |
|
192 LOCAL_C int MainL() |
|
193 { |
|
194 atexit( fun1 ); |
|
195 atexit( fun2 ); |
|
196 atexit( fun3 ); |
|
197 printf( "Before exiting....\n" ); |
|
198 getchar(); |
|
199 exit(); |
|
200 } |
|
201 |
|
202 |
|
203 void fun1() |
|
204 { |
|
205 printf( " fun1\n " ); |
|
206 getchar(); |
|
207 } |
|
208 |
|
209 void fun2() |
|
210 { |
|
211 printf( " fun2\n " ); |
|
212 getchar(); |
|
213 } |
|
214 |
|
215 void fun3() |
|
216 { |
|
217 printf( " fun3\n " ); |
|
218 getchar(); |
|
219 } |
|
220 |
|
221 |
|
222 GLDEF_C TInt E32Main() |
|
223 { |
|
224 __UHEAP_MARK; |
|
225 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
226 if(cleanup == NULL) |
|
227 { |
|
228 return KErrNoMemory; |
|
229 } |
|
230 TInt ret; |
|
231 TRAPD(err,ret = MainL()); |
|
232 |
|
233 delete cleanup; |
|
234 __UHEAP_MARKEND; |
|
235 return KErrNone; |
|
236 } |
|
237 |
|
238 @encode |
|
239 |
|
240 @code |
|
241 Output |
|
242 |
|
243 Before exiting... |
|
244 |
|
245 fun3 |
|
246 fun2 |
|
247 fun1 |
|
248 |
|
249 @endcode |
|
250 |
|
251 @see exit() |
|
252 |
|
253 |
|
254 @publishedAll |
|
255 @externallyDefinedApi |
|
256 */ |
|
257 |
|
258 /** @fn atof(const char *nptr) |
|
259 @param nptr |
|
260 @return The atof function |
|
261 returns |
|
262 the converted value if the value can be represented. |
|
263 |
|
264 The atof function converts the initial portion of the string pointed to by nptr to double |
|
265 representation. |
|
266 |
|
267 @code |
|
268 It is equivalent to: strtod(nptr, (char **)NULL); |
|
269 |
|
270 The decimal point |
|
271 character is defined in the program's locale (category LC_NUMERIC). |
|
272 @endcode |
|
273 |
|
274 |
|
275 Examples: |
|
276 @code |
|
277 #include <stdlib.h> |
|
278 #include <stdio.h> |
|
279 |
|
280 void main( void ) |
|
281 { |
|
282 /* Test of atof */ |
|
283 double d = atof(" 86778E-2"); |
|
284 |
|
285 printf( "result of atof: %f |
|
286 ", d ); |
|
287 } |
|
288 |
|
289 @endcode |
|
290 |
|
291 @code |
|
292 Output |
|
293 result of atof: 867.78 |
|
294 |
|
295 @endcode |
|
296 |
|
297 Implementation notes: |
|
298 |
|
299 The atof function is not thread-safe and also not async-cancel-safe. The atof function has been deprecated. Use strtod instead. |
|
300 |
|
301 @see atoi() |
|
302 @see atol() |
|
303 @see strtod() |
|
304 @see strtol() |
|
305 @see strtoul() |
|
306 |
|
307 |
|
308 @publishedAll |
|
309 @externallyDefinedApi |
|
310 */ |
|
311 |
|
312 /** @fn atoi(const char *nptr) |
|
313 @param nptr |
|
314 @return The atoi function |
|
315 returns |
|
316 the converted value if the value can be represented. |
|
317 |
|
318 The atoi function converts the initial portion of the string pointed to by nptr to int |
|
319 representation. |
|
320 |
|
321 @code |
|
322 |
|
323 It is equivalent to: (int)strtol(nptr, (char **)NULL, 10); |
|
324 @endocde |
|
325 |
|
326 |
|
327 Examples: |
|
328 @code |
|
329 #include <stdlib.h> |
|
330 #include <stdio.h> |
|
331 |
|
332 void main( void ) |
|
333 { |
|
334 /* call to atoi */ |
|
335 char *str = " -56957jdhfjk"; |
|
336 int i = atoi( str ); |
|
337 printf( "result of atoi: %d |
|
338 ", i ); |
|
339 } |
|
340 |
|
341 @endcode |
|
342 |
|
343 @code |
|
344 Output |
|
345 result of atoi: -56957 |
|
346 |
|
347 @endcode |
|
348 |
|
349 Implementation notes: |
|
350 |
|
351 The atoi function is not thread-safe and also not async-cancel safe. The atoi function has been deprecated. Use strtol instead. |
|
352 |
|
353 @see atof() |
|
354 @see atol() |
|
355 @see strtod() |
|
356 @see strtol() |
|
357 @see strtoul() |
|
358 |
|
359 |
|
360 @publishedAll |
|
361 @externallyDefinedApi |
|
362 */ |
|
363 |
|
364 /** @fn atol(const char *nptr) |
|
365 @param nptr |
|
366 |
|
367 Note: |
|
368 |
|
369 This description also covers the following functions - |
|
370 atoll() |
|
371 |
|
372 @return The atol and atoll functions |
|
373 return |
|
374 the converted value if the value can be represented. |
|
375 |
|
376 The atol function converts the initial portion of the string pointed to by nptr to long |
|
377 integer |
|
378 representation. |
|
379 |
|
380 It is equivalent to: |
|
381 |
|
382 @code |
|
383 |
|
384 strtol(nptr, (char **)NULL, 10); |
|
385 @endcode |
|
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: |
|
388 @code |
|
389 strtoll(nptr, (char **)NULL, 10); |
|
390 @endcode |
|
391 |
|
392 Examples: |
|
393 @code |
|
394 #include <stdlib.h> |
|
395 #include <stdio.h> |
|
396 |
|
397 void main( void ) |
|
398 { |
|
399 /* call to atol */ |
|
400 long l = atol( "-000002344" ); |
|
401 |
|
402 printf( "result of atol: %ld |
|
403 ", l ); |
|
404 } |
|
405 @endcode |
|
406 |
|
407 @code |
|
408 Output |
|
409 |
|
410 result of atol: -2344 |
|
411 |
|
412 @endcode |
|
413 @code |
|
414 #include <stdlib.h> |
|
415 #include <stdio.h> |
|
416 |
|
417 void main( void ) |
|
418 { |
|
419 /* call to atoll */ |
|
420 long long l = atoll("454756356bs"); |
|
421 |
|
422 printf( "result of atoll: %ld |
|
423 ", l ); |
|
424 } |
|
425 |
|
426 @endcode |
|
427 |
|
428 @code |
|
429 Output |
|
430 |
|
431 result of atoll: 454756356 |
|
432 |
|
433 @endcode |
|
434 @see atof() |
|
435 @see atoi() |
|
436 @see strtod() |
|
437 @see strtol() |
|
438 @see strtoul() |
|
439 |
|
440 |
|
441 @publishedAll |
|
442 @externallyDefinedApi |
|
443 */ |
|
444 |
|
445 /** @fn bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *)) |
|
446 @param key |
|
447 @param base |
|
448 @param nmemb |
|
449 @param size |
|
450 @param compar |
|
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. |
|
454 |
|
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. |
|
457 |
|
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. |
|
462 |
|
463 Examples: |
|
464 @code |
|
465 #include<stdlib.h> |
|
466 #include<stdio.h> |
|
467 |
|
468 int search_function(const void* a,const void* b) |
|
469 { |
|
470 return(*(int *)a - *(int *)b); |
|
471 } |
|
472 |
|
473 int main() |
|
474 { |
|
475 int arr[] = {3,5,7,8,10}; |
|
476 int key = 7; |
|
477 int i = 5; |
|
478 |
|
479 int *ele; |
|
480 |
|
481 ele = ( int* ) bsearch(&key;, (void *)arr, i, sizeof(arr[0]), search_function); |
|
482 |
|
483 if (ele != NULL) |
|
484 printf(" |
|
485 element found: %d", *ele); |
|
486 else |
|
487 printf(" |
|
488 element not found"); |
|
489 return 0; |
|
490 } |
|
491 |
|
492 @endcode |
|
493 @code |
|
494 Output |
|
495 |
|
496 element found: 7 |
|
497 |
|
498 @endcode |
|
499 @see qsort() |
|
500 |
|
501 |
|
502 @publishedAll |
|
503 @externallyDefinedApi |
|
504 */ |
|
505 |
|
506 /** @fn calloc(size_t number, size_t size) |
|
507 @param number |
|
508 @param size |
|
509 |
|
510 Refer to malloc() for the documentation |
|
511 @see brk() |
|
512 @see mmap() |
|
513 @see getpagesize() |
|
514 |
|
515 |
|
516 @publishedAll |
|
517 @externallyDefinedApi |
|
518 */ |
|
519 |
|
520 /** @fn div(int num, int denom) |
|
521 @param num |
|
522 @param denom |
|
523 @return The div function |
|
524 returns a structure of type div_t that contains two int |
|
525 members named quot (quotient) |
|
526 and rem (remainder). |
|
527 |
|
528 The div function |
|
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. |
|
531 |
|
532 Examples: |
|
533 @code |
|
534 #include <stdlib.h> |
|
535 #include <stdio.h> |
|
536 |
|
537 void main( void ) |
|
538 { |
|
539 int numer = -14; |
|
540 int denom = -3; |
|
541 int exp_numer = 0; |
|
542 |
|
543 /* call to div */ |
|
544 div_t x = div(numer, denom); |
|
545 |
|
546 printf("Result-> |
|
547 Quotient = %d |
|
548 Remainder = %d |
|
549 ", x.quot, x.rem); |
|
550 |
|
551 exp_numer = (denom * x.quot) + x.rem; |
|
552 |
|
553 if( exp_numer == (numer) ) |
|
554 printf("Result is same as the expected output"); |
|
555 else |
|
556 printf("Unexpected output"); |
|
557 |
|
558 return 0; |
|
559 } |
|
560 |
|
561 @endcode |
|
562 @code |
|
563 Output |
|
564 |
|
565 Result-> |
|
566 Quotient = 4 |
|
567 Remainder = -2 |
|
568 Result is same as the expected output |
|
569 |
|
570 @endcode |
|
571 @see ldiv() |
|
572 @see lldiv() |
|
573 |
|
574 |
|
575 @publishedAll |
|
576 @externallyDefinedApi |
|
577 */ |
|
578 |
|
579 /** @fn exit(int status) |
|
580 @param status |
|
581 |
|
582 Note: |
|
583 |
|
584 This description also covers the following functions - |
|
585 _Exit() |
|
586 |
|
587 @return The exit and _Exit functions |
|
588 never return. |
|
589 |
|
590 The exit and _Exit functions terminate a process. |
|
591 |
|
592 Before termination, exit performs the following functions in the order listed: |
|
593 |
|
594 @code |
|
595 |
|
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. |
|
600 @endcode |
|
601 |
|
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. |
|
604 |
|
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. |
|
607 |
|
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). |
|
610 |
|
611 Examples: |
|
612 @code |
|
613 /* Detailed description : Sample usage of exit system call. */ |
|
614 #include <stdlib.h> |
|
615 |
|
616 void main() |
|
617 { |
|
618 exit(0) ; //Here 0 is exit status of the process |
|
619 } |
|
620 |
|
621 @endcode |
|
622 @see _exit() |
|
623 @see wait() |
|
624 @see atexit() |
|
625 @see tmpfile() |
|
626 |
|
627 |
|
628 @publishedAll |
|
629 @externallyDefinedApi |
|
630 */ |
|
631 |
|
632 /** @fn free(void *p) |
|
633 @param p |
|
634 |
|
635 Refer to malloc() for the documentation |
|
636 @see brk() |
|
637 @see mmap() |
|
638 @see getpagesize() |
|
639 |
|
640 |
|
641 @publishedAll |
|
642 @externallyDefinedApi |
|
643 */ |
|
644 |
|
645 /** @fn getenv(const char *name) |
|
646 @param name |
|
647 |
|
648 Note: |
|
649 |
|
650 This description also covers the following functions - |
|
651 setenv() putenv() unsetenv() |
|
652 |
|
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. |
|
655 |
|
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, |
|
660 respectively, |
|
661 with an equal sign "=" |
|
662 |
|
663 The getenv function obtains the current value of the environment variable name. |
|
664 |
|
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 |
|
668 zero, the |
|
669 variable is not reset, otherwise it is reset |
|
670 to the given value. |
|
671 |
|
672 The putenv function takes an argument of the form "name=value" and is |
|
673 equivalent to: setenv(name, value, 1); |
|
674 |
|
675 The unsetenv function |
|
676 deletes all instances of the variable name pointed to by name from the list. |
|
677 |
|
678 Examples: |
|
679 @code |
|
680 #include <stdlib.h> //getenv |
|
681 #include <stdio.h> //printf |
|
682 |
|
683 int main( void ) |
|
684 { |
|
685 int iret = putenv("PATH=c:\sys\bin;"); |
|
686 |
|
687 if( iret == -1) |
|
688 printf( "putenv() failed!! |
|
689 " ); |
|
690 |
|
691 /* fetch new value. */ |
|
692 char *psc = getenv("PATH"); |
|
693 |
|
694 if( psc != NULL ) |
|
695 printf( "new PATH variable is: %s |
|
696 ", psc ); |
|
697 |
|
698 return 0; |
|
699 } |
|
700 |
|
701 @endcode |
|
702 @code |
|
703 Output |
|
704 |
|
705 new PATH variable is: c:\sys\bin; |
|
706 |
|
707 @endcode |
|
708 @code |
|
709 #include <stdlib.h> //setenv, getenv |
|
710 #include <stdio.h> //printf |
|
711 |
|
712 int main( void ) |
|
713 { |
|
714 char *var; |
|
715 |
|
716 /* Unset the value of the HOME environment variable |
|
717 * to make sure that it is non-existing. |
|
718 */ |
|
719 unsetenv( "HOME" ); |
|
720 |
|
721 /* Set the value of HOME environment with no overwrite option */ |
|
722 int iret = setenv("HOME", "/home", 0); |
|
723 |
|
724 if(iret == -1) |
|
725 printf( "Setenv failed!!" ); |
|
726 |
|
727 /* Get new value. */ |
|
728 var = getenv( "HOME" ); |
|
729 |
|
730 if( var != NULL ) |
|
731 printf( "new HOME variable is: %s |
|
732 ", var ); |
|
733 |
|
734 /* Set the value of HOME environment with the overwrite option */ |
|
735 iret = setenv("HOME", "/check", 1); |
|
736 |
|
737 /* Get new value. */ |
|
738 var = getenv( "HOME" ); |
|
739 |
|
740 if( var != NULL ) |
|
741 printf( "new HOME variable is: %s |
|
742 ", var ); |
|
743 |
|
744 return 0; |
|
745 } |
|
746 |
|
747 @endcode |
|
748 @code |
|
749 Output |
|
750 |
|
751 new HOME variable is: \home |
|
752 new HOME variable is: \check |
|
753 |
|
754 @endcode |
|
755 |
|
756 |
|
757 @publishedAll |
|
758 @externallyDefinedApi |
|
759 */ |
|
760 |
|
761 /** @fn labs(long j) |
|
762 @param j |
|
763 @return The labs() function shall return the absolute value of the long integer operand. |
|
764 |
|
765 The labs function |
|
766 returns the absolute value of the long integer j. |
|
767 |
|
768 |
|
769 |
|
770 Examples: |
|
771 @code |
|
772 #include <stdio.h> |
|
773 #include <stdlib.h> |
|
774 |
|
775 int main( void ) |
|
776 { |
|
777 int ix = -4, iy; |
|
778 long lx = -41567L, ly; |
|
779 long long llx = -5343546758, lly; |
|
780 |
|
781 iy = abs( ix ); |
|
782 printf( "The abs value of %d is %d |
|
783 ", ix, iy); |
|
784 |
|
785 ly = labs( lx ); |
|
786 printf( "The abs value of %ld is %ld |
|
787 ", lx, ly); |
|
788 |
|
789 lly = llabs( llx ); |
|
790 printf( "The abs value of %lld is %lld |
|
791 ", llx, lly ); |
|
792 |
|
793 return 0; |
|
794 } |
|
795 |
|
796 @endcode |
|
797 @code |
|
798 Output |
|
799 |
|
800 The absolute value of -4 is 4 |
|
801 The absolute value of -41567 is 41567 |
|
802 The absolute value of -5343546758 is 5343546758 |
|
803 |
|
804 @endcode |
|
805 @see abs() |
|
806 @see floor() |
|
807 @see llabs() |
|
808 @see math() |
|
809 |
|
810 |
|
811 @publishedAll |
|
812 @externallyDefinedApi |
|
813 */ |
|
814 |
|
815 /** @fn ldiv(long num, long denom) |
|
816 @param num |
|
817 @param denom |
|
818 @return The ldiv function |
|
819 returns a structure of type ldiv_t that contains two long |
|
820 members named quot (quotient) |
|
821 and rem (remainder). |
|
822 |
|
823 The ldiv function |
|
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. |
|
827 |
|
828 Examples: |
|
829 @code |
|
830 #include <stdlib.h> |
|
831 #include <stdio.h> |
|
832 |
|
833 int main( void ) |
|
834 { |
|
835 long numer = 13; |
|
836 long denom = -3; |
|
837 |
|
838 /* call to ldiv */ |
|
839 ldiv_t x = ldiv(numer, denom); |
|
840 |
|
841 printf("Result-> |
|
842 Quotient = %ld |
|
843 Remainder = %ld", x.quot, x.rem); |
|
844 |
|
845 long exp_numer = (denom * x.quot) + x.rem; |
|
846 |
|
847 if( exp_numer == (numer) ) |
|
848 printf(" |
|
849 Result is same as the expected output"); |
|
850 else |
|
851 printf(" |
|
852 Unexpected output"); |
|
853 |
|
854 return 0; |
|
855 } |
|
856 |
|
857 @endcode |
|
858 @code |
|
859 Output |
|
860 |
|
861 Result-> |
|
862 Quotient = 4 |
|
863 Remainder = -1 |
|
864 Result is same as the expected output |
|
865 |
|
866 @endcode |
|
867 @see div() |
|
868 @see lldiv() |
|
869 |
|
870 |
|
871 @publishedAll |
|
872 @externallyDefinedApi |
|
873 */ |
|
874 |
|
875 /** @fn malloc(size_t nbytes) |
|
876 @param nbytes |
|
877 |
|
878 Note: |
|
879 |
|
880 This description also covers the following functions - |
|
881 calloc() realloc() reallocf() free() |
|
882 |
|
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. |
|
886 |
|
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. |
|
890 |
|
891 Note that malloc does NOT normally initialize the returned memory to zero bytes. |
|
892 |
|
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. |
|
895 |
|
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. |
|
904 |
|
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. |
|
909 |
|
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. |
|
912 |
|
913 Examples: |
|
914 @code |
|
915 #include <stdlib.h> //malloc |
|
916 #include <stdio.h> //printf |
|
917 |
|
918 int main( void ) |
|
919 { |
|
920 /* allocate memory */ |
|
921 char *pc = (char *)malloc( sizeof(char) * 3); |
|
922 |
|
923 if( pc == NULL ) |
|
924 printf( "memory insufficient |
|
925 " ); |
|
926 else |
|
927 { |
|
928 printf( "memory allocated |
|
929 " ); |
|
930 free( pc ); |
|
931 printf( "memory freed |
|
932 " ); |
|
933 } |
|
934 |
|
935 return 0; |
|
936 } |
|
937 |
|
938 @endcode |
|
939 @code |
|
940 Output |
|
941 |
|
942 memory allocated |
|
943 memory freed |
|
944 |
|
945 @endcode |
|
946 @code |
|
947 #include <stdio.h> //printf |
|
948 #include <stdlib.h> //calloc |
|
949 |
|
950 int main( void ) |
|
951 { |
|
952 int *pint = (int *)calloc(2, sizeof (int) * 2); |
|
953 if( pint != NULL ) |
|
954 printf( "allocated 2 blocks of memory, each of size - |
|
955 twice the size of an integer |
|
956 " ); |
|
957 else |
|
958 printf( "can't allocate memory |
|
959 " ); |
|
960 free( pint ); |
|
961 |
|
962 return 0; |
|
963 } |
|
964 |
|
965 @endcode |
|
966 @code |
|
967 Output |
|
968 |
|
969 allocated 2 blocks of memory, each of size - 2 integers |
|
970 |
|
971 @endcode |
|
972 @code |
|
973 #include <stdio.h> //printf |
|
974 #include <stdlib.h> //realloc |
|
975 |
|
976 int main( void ) |
|
977 { |
|
978 int *pint = (int *)calloc(2, sizeof (int) * 2); |
|
979 if (pint == NULL) |
|
980 { |
|
981 printf("calloc failed to allocate memory |
|
982 "); |
|
983 return -1; |
|
984 } |
|
985 else |
|
986 { |
|
987 printf("calloc allocated memory: 128 bytes |
|
988 "); |
|
989 } |
|
990 |
|
991 pint = (int *)realloc(pint, (sizeof (int) * 6) ); |
|
992 if (pint == NULL) |
|
993 { |
|
994 printf("realloc failed to allocate memory |
|
995 "); |
|
996 return -1; |
|
997 } |
|
998 else |
|
999 { |
|
1000 printf("realloc allocated memory: 192 bytes |
|
1001 "); |
|
1002 } |
|
1003 |
|
1004 free( pint ); |
|
1005 return 0; |
|
1006 } |
|
1007 |
|
1008 @endcode |
|
1009 @code |
|
1010 Output |
|
1011 |
|
1012 calloc allocated memory: 128 bytes |
|
1013 realloc allocated memory: 192 bytes |
|
1014 |
|
1015 @endcode |
|
1016 @see brk() |
|
1017 @see mmap() |
|
1018 @see getpagesize() |
|
1019 |
|
1020 |
|
1021 @publishedAll |
|
1022 @externallyDefinedApi |
|
1023 */ |
|
1024 |
|
1025 /** @fn mblen(const char *s, size_t n) |
|
1026 @param s |
|
1027 @param 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. |
|
1030 |
|
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. |
|
1034 |
|
1035 A call with a null s pointer returns nonzero if the current locale requires shift states, |
|
1036 zero otherwise; |
|
1037 if shift states are required, the shift state is reset to the initial state. |
|
1038 |
|
1039 The behavior of the mblen is affected by LC_CTYPE category of the current locale. |
|
1040 |
|
1041 Examples: |
|
1042 @code |
|
1043 #include <stdlib.h> |
|
1044 #include <wchar.h> |
|
1045 /* Illustrates how to use mblen API */ |
|
1046 int example_mblen(wchar_t wc) |
|
1047 { |
|
1048 int len; |
|
1049 |
|
1050 /* determine the number bytes in the multibyte char */ |
|
1051 len = mblen(wc, MB_CUR_MAX); |
|
1052 if(len == -1) |
|
1053 { |
|
1054 wprintf(L"mblen returned error!! |
|
1055 "); |
|
1056 } |
|
1057 /* return the no of bytes */ |
|
1058 return(len); |
|
1059 } |
|
1060 |
|
1061 @endcode |
|
1062 |
|
1063 Limitations: |
|
1064 |
|
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. |
|
1067 |
|
1068 @see mbrlen() |
|
1069 @see mbtowc() |
|
1070 |
|
1071 |
|
1072 @publishedAll |
|
1073 @externallyDefinedApi |
|
1074 */ |
|
1075 |
|
1076 /** @fn mbstowcs(wchar_t * pwcs, const char * s, size_t n) |
|
1077 @param pwcs |
|
1078 @param s |
|
1079 @param 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. |
|
1083 |
|
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. |
|
1087 |
|
1088 The behavior of the mbstowcs is affected by LC_CTYPE category of the current locale. |
|
1089 |
|
1090 |
|
1091 |
|
1092 Examples: |
|
1093 @code |
|
1094 #include <stdio.h> |
|
1095 #include <stdlib.h> |
|
1096 #include <wchar.h> |
|
1097 /* Illustrates how to use mbstowcs API */ |
|
1098 size_t example_mbstowcs(wchar_t *wc, char *s, size_t n) |
|
1099 { |
|
1100 size_t len; |
|
1101 /* converting multibyte string to a wide-char string */ |
|
1102 len = mbstowcs(wc, s, n); |
|
1103 /* checking for error */ |
|
1104 if(len < 0) |
|
1105 { |
|
1106 wprintf(L"mbstowcs returned error!! |
|
1107 "); |
|
1108 } |
|
1109 /* returning no of bytes consumed */ |
|
1110 return (len); |
|
1111 } |
|
1112 |
|
1113 @endcode |
|
1114 |
|
1115 Limitations: |
|
1116 |
|
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. |
|
1119 |
|
1120 @see mbsrtowcs() |
|
1121 @see mbtowc() |
|
1122 |
|
1123 |
|
1124 @publishedAll |
|
1125 @externallyDefinedApi |
|
1126 */ |
|
1127 |
|
1128 /** @fn mbtowc(wchar_t * pwc, const char * s, size_t n) |
|
1129 @param pwc |
|
1130 @param s |
|
1131 @param 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. |
|
1137 |
|
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. |
|
1140 |
|
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. |
|
1144 |
|
1145 The behavior of the mbtowc is affected by LC_CTYPE category of the current locale. |
|
1146 |
|
1147 |
|
1148 |
|
1149 Examples: |
|
1150 @code |
|
1151 #include <stdlib.h> |
|
1152 #include <wchar.h> |
|
1153 /* Illustrates how to use mbtowc API */ |
|
1154 int example_mbtowc(wchar_t *wc, char *s) |
|
1155 { |
|
1156 int len; |
|
1157 /* converting multibyte sequence to a wide-character */ |
|
1158 len = mbtowc(wc, s, MB_CUR_MAX); |
|
1159 /* checking for error */ |
|
1160 if(len < 0) |
|
1161 { |
|
1162 wprintf(L"mbtowc returned error!! |
|
1163 "); |
|
1164 } |
|
1165 /* returning no of bytes consumed */ |
|
1166 return (len;); |
|
1167 } |
|
1168 |
|
1169 @endcode |
|
1170 |
|
1171 Limitations: |
|
1172 |
|
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. |
|
1175 |
|
1176 @see btowc() |
|
1177 @see mblen() |
|
1178 @see mbrtowc() |
|
1179 @see mbstowcs() |
|
1180 @see wctomb() |
|
1181 |
|
1182 |
|
1183 @publishedAll |
|
1184 @externallyDefinedApi |
|
1185 */ |
|
1186 |
|
1187 /** @fn qsort(void *, size_t, size_t, int(*)(const void *, const void *)) |
|
1188 |
|
1189 @return The qsort function |
|
1190 returns no value. |
|
1191 |
|
1192 The qsort function is a modified partition-exchange sort, or quicksort. |
|
1193 |
|
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. |
|
1195 |
|
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 |
|
1198 compared. |
|
1199 |
|
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. |
|
1203 |
|
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. |
|
1206 |
|
1207 The qsort function is an implementation of C.A.R. |
|
1208 Hoare's "quicksort" |
|
1209 algorithm, |
|
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. |
|
1211 |
|
1212 @code |
|
1213 |
|
1214 This implementation uses median selection to avoid its O N**2 worst-case behavior. |
|
1215 @endcode |
|
1216 |
|
1217 Examples: |
|
1218 @code |
|
1219 #include <stdio.h> |
|
1220 #include <stdlib.h> |
|
1221 int sort_function( const void *a, const void *b); |
|
1222 |
|
1223 int main(void) |
|
1224 { |
|
1225 int x; |
|
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++) |
|
1229 printf("%d |
|
1230 ", list[x]); |
|
1231 |
|
1232 return 0; |
|
1233 } |
|
1234 int sort_function( const void *a, const void *b) |
|
1235 { |
|
1236 int *p1 = (int *)a; |
|
1237 int val1 = *p1; |
|
1238 int *p2 = (int *)b; |
|
1239 int val2 = *p2; |
|
1240 int x = -1; |
|
1241 if(val1 > val2 ) |
|
1242 x=1; |
|
1243 if(val1 == val2 ) |
|
1244 x=0; |
|
1245 |
|
1246 return x; |
|
1247 } |
|
1248 |
|
1249 @endcode |
|
1250 @code |
|
1251 Output |
|
1252 |
|
1253 1 |
|
1254 2 |
|
1255 3 |
|
1256 4 |
|
1257 5 |
|
1258 |
|
1259 @endcode |
|
1260 |
|
1261 |
|
1262 @publishedAll |
|
1263 @externallyDefinedApi |
|
1264 */ |
|
1265 |
|
1266 /** @fn rand(void) |
|
1267 |
|
1268 |
|
1269 Refer to srand() for the documentation |
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 @publishedAll |
|
1275 @externallyDefinedApi |
|
1276 */ |
|
1277 |
|
1278 /** @fn realloc(void *p, size_t nbytes) |
|
1279 @param p |
|
1280 @param nbytes |
|
1281 |
|
1282 Refer to malloc() for the documentation |
|
1283 @see brk() |
|
1284 @see mmap() |
|
1285 @see getpagesize() |
|
1286 |
|
1287 |
|
1288 @publishedAll |
|
1289 @externallyDefinedApi |
|
1290 */ |
|
1291 |
|
1292 /** @fn srand(unsigned seed) |
|
1293 @param seed |
|
1294 |
|
1295 Note: |
|
1296 |
|
1297 This description also covers the following functions - |
|
1298 rand() |
|
1299 |
|
1300 @return The rand function |
|
1301 never returns return the next pseudo-random number in the sequence. The srand function never returns. |
|
1302 |
|
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\> ). |
|
1305 |
|
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. |
|
1308 |
|
1309 If no seed value is provided, the functions are automatically |
|
1310 seeded with a value of 1. |
|
1311 |
|
1312 |
|
1313 |
|
1314 Examples: |
|
1315 @code |
|
1316 #include <stdlib.h> |
|
1317 #include <stdio.h> |
|
1318 |
|
1319 int main( void ) |
|
1320 { |
|
1321 int randArray[20]; |
|
1322 int i=0; |
|
1323 |
|
1324 while(i<20) |
|
1325 { |
|
1326 randArray[i]=rand(); |
|
1327 printf(" |
|
1328 %d", randArray[i]); |
|
1329 i++; |
|
1330 } |
|
1331 |
|
1332 return 0; |
|
1333 } |
|
1334 |
|
1335 @endcode |
|
1336 @code |
|
1337 Output |
|
1338 |
|
1339 16807 |
|
1340 282475249 |
|
1341 1622650073 |
|
1342 984943658 |
|
1343 1144108930 |
|
1344 470211272 |
|
1345 101027544 |
|
1346 1457850878 |
|
1347 1458777923 |
|
1348 2007237709 |
|
1349 823564440 |
|
1350 1115438165 |
|
1351 1784484492 |
|
1352 74243042 |
|
1353 114807987 |
|
1354 1137522503 |
|
1355 1441282327 |
|
1356 16531729 |
|
1357 823378840 |
|
1358 143542612 |
|
1359 |
|
1360 @endcode |
|
1361 @code |
|
1362 #include <stdlib.h> |
|
1363 #include <stdio.h> |
|
1364 int main( void ) |
|
1365 { |
|
1366 int seedVal = 45454652; |
|
1367 int randVal; |
|
1368 srand(seedVal); |
|
1369 randVal=rand(); |
|
1370 printf("Random Value returned is %d"), randVal); |
|
1371 } |
|
1372 |
|
1373 @endcode |
|
1374 @code |
|
1375 Output |
|
1376 |
|
1377 Random Value returned is 1599641479 |
|
1378 |
|
1379 @endcode |
|
1380 |
|
1381 |
|
1382 @publishedAll |
|
1383 @externallyDefinedApi |
|
1384 */ |
|
1385 |
|
1386 /** @fn strtod(const char * s, char ** tail) |
|
1387 @param s |
|
1388 @param tail |
|
1389 |
|
1390 Note: |
|
1391 |
|
1392 This description also covers the following functions - |
|
1393 strtof() strtold() |
|
1394 |
|
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 . |
|
1402 |
|
1403 These conversion functions convert the initial portion of the string pointed to by nptr to double , float , and long double representation, respectively. |
|
1404 |
|
1405 The expected form of the string is an optional plus ("+") or minus sign ("-") followed by either: |
|
1406 |
|
1407 @code |
|
1408 |
|
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. |
|
1411 @endcode |
|
1412 |
|
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. |
|
1416 |
|
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. |
|
1418 |
|
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). |
|
1420 |
|
1421 Examples: |
|
1422 @code |
|
1423 #include <stdlib.h> |
|
1424 #include <stdio.h> |
|
1425 |
|
1426 int main( void ) |
|
1427 { |
|
1428 char *endpt = NULL; |
|
1429 double d = 0.0; |
|
1430 |
|
1431 d = strtod("0x00e123bhduitri", &endpt;); |
|
1432 |
|
1433 printf("{Expected: 922171.0} %f |
|
1434 ", d); |
|
1435 printf("{Expected: \"hduitri\"} %s |
|
1436 ", endpt); |
|
1437 |
|
1438 return 0; |
|
1439 } |
|
1440 |
|
1441 @endcode |
|
1442 Output |
|
1443 @code |
|
1444 {Expected: 922171.0} 922171.0 |
|
1445 {Expected: "hduitri"} hduitri |
|
1446 |
|
1447 @endcode |
|
1448 |
|
1449 Limitations: |
|
1450 |
|
1451 All these functions don't support the long double length modifiers. |
|
1452 |
|
1453 @see atof() |
|
1454 @see atoi() |
|
1455 @see atol() |
|
1456 @see strtol() |
|
1457 @see strtoul() |
|
1458 @see wcstod() |
|
1459 |
|
1460 |
|
1461 @publishedAll |
|
1462 @externallyDefinedApi |
|
1463 */ |
|
1464 |
|
1465 /** @fn strtof(const char * s, char ** tail) |
|
1466 @param s |
|
1467 @param tail |
|
1468 |
|
1469 Refer to strtod() for the documentation |
|
1470 @see atof() |
|
1471 @see atoi() |
|
1472 @see atol() |
|
1473 @see strtol() |
|
1474 @see strtoul() |
|
1475 @see wcstod() |
|
1476 |
|
1477 |
|
1478 @publishedAll |
|
1479 @externallyDefinedApi |
|
1480 */ |
|
1481 |
|
1482 /** @fn strtol(const char * nptr, char ** endptr, int base) |
|
1483 @param nptr |
|
1484 @param endptr |
|
1485 @param base |
|
1486 |
|
1487 Note: |
|
1488 |
|
1489 This description also covers the following functions - |
|
1490 strtoll() strtoimax() strtoq() |
|
1491 |
|
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 |
|
1496 table: |
|
1497 |
|
1498 @code |
|
1499 |
|
1500 Function underflow overflow |
|
1501 |
|
1502 strtol LONG_MIN LONG_MAX |
|
1503 strtoll LLONG_MIN LLONG_MAX |
|
1504 strtoimax INTMAX_MIN INTMAX_MAX |
|
1505 strtoq LLONG_MIN LLONG_MAX |
|
1506 @endcode |
|
1507 |
|
1508 The strtol function |
|
1509 converts the string in nptr to a long |
|
1510 value. |
|
1511 The strtoll function |
|
1512 converts the string in nptr to a long long |
|
1513 value. |
|
1514 The strtoimax function |
|
1515 converts the string in nptr to an intmax_t |
|
1516 value. |
|
1517 The strtoq function |
|
1518 converts the string in nptr to a quad_t |
|
1519 value. |
|
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. |
|
1523 |
|
1524 The string may begin with an arbitrary amount of white space |
|
1525 (as determined by isspace ) |
|
1526 followed by a single optional "+" |
|
1527 or "-" |
|
1528 sign. |
|
1529 If base is zero or 16, |
|
1530 the string may then include a "0x" |
|
1531 prefix, |
|
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). |
|
1534 |
|
1535 The remainder of the string is converted to a long , long long , intmax_t |
|
1536 or quad_t |
|
1537 value in the obvious manner, |
|
1538 stopping at the first character which is not a valid digit |
|
1539 in the given base. |
|
1540 (In bases above 10, the letter "A" |
|
1541 in either upper or lower case |
|
1542 represents 10, "B" |
|
1543 represents 11, and so forth, with "Z" |
|
1544 representing 35.) |
|
1545 |
|
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.) |
|
1551 |
|
1552 Examples: |
|
1553 @code |
|
1554 #include <stdlib.h> |
|
1555 #include <stdio.h> |
|
1556 |
|
1557 int main( void ) |
|
1558 { |
|
1559 char *endpt = NULL; |
|
1560 long l = 0; |
|
1561 |
|
1562 l = strtol("0x1236nvbi", &endpt;, 0); |
|
1563 |
|
1564 printf("{Expected: 4662} %ld |
|
1565 ", l); |
|
1566 printf("{Expected: \"nvbi\"} %s |
|
1567 ", endpt); |
|
1568 |
|
1569 return 0; |
|
1570 } |
|
1571 |
|
1572 @endcode |
|
1573 @code |
|
1574 Output |
|
1575 |
|
1576 {Expected: 4662} 4662 |
|
1577 {Expected: "nvbi"} nvbi |
|
1578 |
|
1579 @endcode |
|
1580 @see atof() |
|
1581 @see atoi() |
|
1582 @see atol() |
|
1583 @see strtod() |
|
1584 @see strtoul() |
|
1585 @see wcstol() |
|
1586 |
|
1587 |
|
1588 @publishedAll |
|
1589 @externallyDefinedApi |
|
1590 */ |
|
1591 |
|
1592 /** @fn strtold(const char *s, char **sp) |
|
1593 @param s |
|
1594 @param sp |
|
1595 |
|
1596 Refer to strtod() for the documentation |
|
1597 @see atof() |
|
1598 @see atoi() |
|
1599 @see atol() |
|
1600 @see strtol() |
|
1601 @see strtoul() |
|
1602 @see wcstod() |
|
1603 |
|
1604 |
|
1605 @publishedAll |
|
1606 @externallyDefinedApi |
|
1607 */ |
|
1608 |
|
1609 /** @fn strtoul(const char * nptr, char ** endptr, int base) |
|
1610 @param nptr |
|
1611 @param endptr |
|
1612 @param base |
|
1613 |
|
1614 Note: |
|
1615 |
|
1616 This description also covers the following functions - |
|
1617 strtoull() strtoumax() strtouq() |
|
1618 |
|
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). |
|
1629 |
|
1630 The strtoul function |
|
1631 converts the string in nptr to an unsigned long |
|
1632 value. |
|
1633 The strtoull function |
|
1634 converts the string in nptr to an unsigned long long |
|
1635 value. |
|
1636 The strtoumax function |
|
1637 converts the string in nptr to an uintmax_t |
|
1638 value. |
|
1639 The strtouq function |
|
1640 converts the string in nptr to a u_quad_t |
|
1641 value. |
|
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. |
|
1645 |
|
1646 The string may begin with an arbitrary amount of white space |
|
1647 (as determined by isspace ) |
|
1648 followed by a single optional "+" |
|
1649 or "-" |
|
1650 sign. |
|
1651 If base is zero or 16, |
|
1652 the string may then include a "0x" |
|
1653 prefix, |
|
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). |
|
1656 |
|
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 |
|
1661 in the given base. |
|
1662 (In bases above 10, the letter "A" |
|
1663 in either upper or lower case |
|
1664 represents 10, "B" |
|
1665 represents 11, and so forth, with "Z" |
|
1666 representing 35.) |
|
1667 |
|
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.) |
|
1673 |
|
1674 Examples: |
|
1675 @code |
|
1676 #include <stdlib.h> |
|
1677 #include <stdio.h> |
|
1678 |
|
1679 int main( void ) |
|
1680 { |
|
1681 char *endpt = NULL; |
|
1682 unsigned long ul = 0; |
|
1683 |
|
1684 ul = strtoul("435435hmnb", &endpt;, 12); |
|
1685 |
|
1686 printf("{Expected: 1066793} %ld |
|
1687 ", ul); |
|
1688 printf("{Expected: \"hmnb\"} %s |
|
1689 ", endpt); |
|
1690 |
|
1691 return 0; |
|
1692 } |
|
1693 |
|
1694 @endcode |
|
1695 @code |
|
1696 Output |
|
1697 |
|
1698 {Expected: 1066793} 1066793 |
|
1699 {Expected: "hmnb"} hmnb |
|
1700 |
|
1701 @endcode |
|
1702 @see strtol() |
|
1703 @see wcstoul() |
|
1704 |
|
1705 |
|
1706 @publishedAll |
|
1707 @externallyDefinedApi |
|
1708 */ |
|
1709 |
|
1710 /** @fn system(const char *cmd) |
|
1711 @param 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. |
|
1713 |
|
1714 The system function |
|
1715 spawns another process with the argument cmd. The calling process waits for the child process |
|
1716 to finish executing. |
|
1717 |
|
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. |
|
1720 |
|
1721 The system function |
|
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. |
|
1725 |
|
1726 Examples: |
|
1727 @code |
|
1728 #include <stdlib.h> //system |
|
1729 #include <stdio.h> //printf |
|
1730 |
|
1731 int main( void ) |
|
1732 { |
|
1733 int retVal = -1; |
|
1734 |
|
1735 printf( "Calling system()... |
|
1736 " ); |
|
1737 |
|
1738 /* helloworld.exe is an executable that just prints |
|
1739 * "Hello world!" and it should be created before |
|
1740 * executing this example code. |
|
1741 */ |
|
1742 retVal = system("c:\sys\bin\helloworld.exe"); |
|
1743 |
|
1744 /* Print the return value of system() */ |
|
1745 printf( "system() returned: %d", retVal ); |
|
1746 |
|
1747 return 0; |
|
1748 } |
|
1749 |
|
1750 @endcode |
|
1751 @code |
|
1752 Output |
|
1753 |
|
1754 Calling system()... |
|
1755 system() returned: -1 |
|
1756 |
|
1757 @endcode |
|
1758 @see popen() |
|
1759 |
|
1760 |
|
1761 @publishedAll |
|
1762 @externallyDefinedApi |
|
1763 */ |
|
1764 |
|
1765 /** @fn wctomb(char *s, wchar_t wchar) |
|
1766 @param s |
|
1767 @param 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. |
|
1771 |
|
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. |
|
1776 |
|
1777 A call with a null mbchar pointer returns nonzero if the current locale requires shift states, |
|
1778 zero otherwise; |
|
1779 if shift states are required, the shift state is reset to the initial state. |
|
1780 |
|
1781 The behavior of the wctomb is affected by LC_CTYPE category of the current locale. |
|
1782 |
|
1783 Examples: |
|
1784 @code |
|
1785 #include <stdlib.h> |
|
1786 #include <wchar.h> |
|
1787 /* Illustrates how to use wctomb API */ |
|
1788 int example_wctomb(wchar_t wc) |
|
1789 { |
|
1790 char s[MAX_CUR_MAX]; |
|
1791 int len; |
|
1792 |
|
1793 /* represent a wide-char in a single byte*/ |
|
1794 len = wctomb(s, wc); |
|
1795 /* return the number of bytes */ |
|
1796 return(len); |
|
1797 } |
|
1798 |
|
1799 @endcode |
|
1800 |
|
1801 Limitations: |
|
1802 |
|
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. |
|
1805 |
|
1806 @see mbtowc() |
|
1807 @see wcrtomb() |
|
1808 @see wcstombs() |
|
1809 @see wctob() |
|
1810 |
|
1811 |
|
1812 @publishedAll |
|
1813 @externallyDefinedApi |
|
1814 */ |
|
1815 |
|
1816 /** @fn wcstombs(char * s, const wchar_t * pwcs, size_t n) |
|
1817 @param s |
|
1818 @param pwcs |
|
1819 @param 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) . |
|
1822 |
|
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. |
|
1828 |
|
1829 The behavior of the wcstombs is affected by LC_CTYPE category of the current locale. |
|
1830 |
|
1831 Examples: |
|
1832 @code |
|
1833 #include <stdlib.h> |
|
1834 #include <wchar.h> |
|
1835 /* Illustrates how to use wcstombs API */ |
|
1836 size_t example_wcstombs(wchar_t *wcs, char *s, size_t n) |
|
1837 { |
|
1838 size_t len; |
|
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 */ |
|
1842 return (len;); |
|
1843 } |
|
1844 |
|
1845 @endcode |
|
1846 |
|
1847 Limitations: |
|
1848 |
|
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. |
|
1851 |
|
1852 @see mbstowcs() |
|
1853 @see wcsrtombs() |
|
1854 @see wctomb() |
|
1855 |
|
1856 |
|
1857 @publishedAll |
|
1858 @externallyDefinedApi |
|
1859 */ |
|
1860 |
|
1861 /** @fn atoll(str) const char *str |
|
1862 @param str |
|
1863 |
|
1864 Refer to atol() for the documentation |
|
1865 @see atof() |
|
1866 @see atoi() |
|
1867 @see strtod() |
|
1868 @see strtol() |
|
1869 @see strtoul() |
|
1870 |
|
1871 |
|
1872 @publishedAll |
|
1873 @externallyDefinedApi |
|
1874 */ |
|
1875 |
|
1876 /** @fn llabs(long long j) |
|
1877 @param j |
|
1878 @return The llabs function |
|
1879 returns the absolue value. |
|
1880 |
|
1881 The llabs function returns the absolute value of j. |
|
1882 |
|
1883 |
|
1884 |
|
1885 Examples: |
|
1886 @code |
|
1887 #include <stdio.h> |
|
1888 #include <stdlib.h> |
|
1889 |
|
1890 int main( void ) |
|
1891 { |
|
1892 int ix = -4, iy; |
|
1893 long lx = -41567L, ly; |
|
1894 long long llx = -5343546758, lly; |
|
1895 |
|
1896 iy = abs( ix ); |
|
1897 printf( "The abs value of %d is %d |
|
1898 ", ix, iy); |
|
1899 |
|
1900 ly = labs( lx ); |
|
1901 printf( "The abs value of %ld is %ld |
|
1902 ", lx, ly); |
|
1903 |
|
1904 lly = llabs( llx ); |
|
1905 printf( "The abs value of %lld is %lld |
|
1906 ", llx, lly ); |
|
1907 |
|
1908 return 0; |
|
1909 } |
|
1910 |
|
1911 @endcode |
|
1912 @code |
|
1913 Output |
|
1914 |
|
1915 The absolute value of -4 is 4 |
|
1916 The absolute value of -41567 is 41567 |
|
1917 The absolute value of -5343546758 is 5343546758 |
|
1918 |
|
1919 @endcode |
|
1920 @see abs() |
|
1921 @see fabs() |
|
1922 @see hypot() |
|
1923 @see labs() |
|
1924 @see math() |
|
1925 |
|
1926 |
|
1927 @publishedAll |
|
1928 @externallyDefinedApi |
|
1929 */ |
|
1930 |
|
1931 /** @fn lldiv(long long numer, long long denom) |
|
1932 @param numer |
|
1933 @param denom |
|
1934 |
|
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. |
|
1936 |
|
1937 The lldiv_t type is defined as: |
|
1938 @code |
|
1939 typedef struct { |
|
1940 long long quot; /* Quotient. */ |
|
1941 long long rem; /* Remainder. */ |
|
1942 } lldiv_t; |
|
1943 @endcode |
|
1944 |
|
1945 Examples: |
|
1946 @code |
|
1947 #include <stdlib.h> |
|
1948 #include <stdio.h> |
|
1949 #include <math.h> /* link to the math lib -libm */ |
|
1950 |
|
1951 int main( void ) |
|
1952 { |
|
1953 long long numer = pow(2, 40); |
|
1954 long long denom = 3; |
|
1955 |
|
1956 /* call to lldiv */ |
|
1957 lldiv_t x = lldiv(-numer, denom); |
|
1958 |
|
1959 printf("Result-> |
|
1960 Quotient = %ld |
|
1961 Remainder = %ld", x.quot, x.rem); |
|
1962 |
|
1963 long long exp_numer = (denom * x.quot) + x.rem; |
|
1964 |
|
1965 if( exp_numer == (-numer) ) |
|
1966 printf(" |
|
1967 Expected output"); |
|
1968 else |
|
1969 printf("Unexpected output"); |
|
1970 |
|
1971 return 0; |
|
1972 } |
|
1973 |
|
1974 @endcode |
|
1975 @code |
|
1976 Output |
|
1977 |
|
1978 Result-> |
|
1979 Quotient = -366503875925 |
|
1980 Remainder = -1 |
|
1981 Expected output |
|
1982 |
|
1983 |
|
1984 @endcode |
|
1985 @see div() |
|
1986 @see ldiv() |
|
1987 @see math() |
|
1988 |
|
1989 |
|
1990 @publishedAll |
|
1991 @externallyDefinedApi |
|
1992 */ |
|
1993 |
|
1994 /** @fn strtoll(const char * nptr, char ** endptr, int base) |
|
1995 @param nptr |
|
1996 @param endptr |
|
1997 @param base |
|
1998 |
|
1999 Refer to strtol() for the documentation |
|
2000 @see atof() |
|
2001 @see atoi() |
|
2002 @see atol() |
|
2003 @see strtod() |
|
2004 @see strtoul() |
|
2005 @see wcstol() |
|
2006 |
|
2007 |
|
2008 @publishedAll |
|
2009 @externallyDefinedApi |
|
2010 */ |
|
2011 |
|
2012 /** @fn strtoull(const char * nptr, char ** endptr, int base) |
|
2013 @param nptr |
|
2014 @param endptr |
|
2015 @param base |
|
2016 |
|
2017 Refer to strtoul() for the documentation |
|
2018 @see strtol() |
|
2019 @see wcstoul() |
|
2020 |
|
2021 |
|
2022 @publishedAll |
|
2023 @externallyDefinedApi |
|
2024 */ |
|
2025 |
|
2026 /** @fn _Exit(int code) |
|
2027 @param code |
|
2028 |
|
2029 Refer to _exit() for the documentation |
|
2030 |
|
2031 @publishedAll |
|
2032 @externallyDefinedApi |
|
2033 */ |
|
2034 |
|
2035 /** @fn setenv(const char *name, const char *value, int overwrite) |
|
2036 @param name |
|
2037 @param value |
|
2038 @param overwrite |
|
2039 |
|
2040 Refer to getenv() for the documentation |
|
2041 |
|
2042 |
|
2043 |
|
2044 @publishedAll |
|
2045 @externallyDefinedApi |
|
2046 */ |
|
2047 |
|
2048 /** @fn unsetenv(const char *name) |
|
2049 @param name |
|
2050 |
|
2051 Refer to getenv() for the documentation |
|
2052 |
|
2053 |
|
2054 |
|
2055 @publishedAll |
|
2056 @externallyDefinedApi |
|
2057 */ |
|
2058 |
|
2059 |
|
2060 /** @fn mkstemp(char *template) |
|
2061 @param 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. |
|
2065 |
|
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 |
|
2072 by the application. |
|
2073 The template may be any file name with some number of "X s" |
|
2074 appended |
|
2075 to it, for example |
|
2076 @code |
|
2077 /tmp/temp.XXXXXX. |
|
2078 @endcode |
|
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" |
|
2082 provided; six "X s" |
|
2083 will |
|
2084 result in mkstemp selecting one of 56800235584 (62 ** 6) possible temporary file names. |
|
2085 |
|
2086 |
|
2087 Examples: |
|
2088 @code |
|
2089 #include <stdlib.h> |
|
2090 #include <stdio.h> //printf, SEEK_SET |
|
2091 #include <unistd.h> |
|
2092 |
|
2093 int main( void ) |
|
2094 { |
|
2095 char arr[] = "c:\someXXXXXXXX"; |
|
2096 char buf[10]; |
|
2097 |
|
2098 //create a temporary file using mkstemp() |
|
2099 int fd = mkstemp(arr); |
|
2100 |
|
2101 if(fd != -1) |
|
2102 { |
|
2103 //write to the file |
|
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 |
|
2108 read(fd, buf, 5); |
|
2109 buf[5] = '\0'; |
|
2110 //close the file |
|
2111 close(fd); |
|
2112 } |
|
2113 |
|
2114 printf("buf read: %s", buf); |
|
2115 return 0; |
|
2116 } |
|
2117 |
|
2118 @endcode |
|
2119 @code |
|
2120 Output |
|
2121 |
|
2122 buf read: hello |
|
2123 |
|
2124 @endcode |
|
2125 |
|
2126 Notes: |
|
2127 |
|
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. |
|
2133 @see chmod() |
|
2134 @see getpid() |
|
2135 @see mkdir() |
|
2136 @see open() |
|
2137 @see stat() |
|
2138 |
|
2139 |
|
2140 @publishedAll |
|
2141 @externallyDefinedApi |
|
2142 */ |
|
2143 |
|
2144 /** @fn mkstemp64(char *template) |
|
2145 @param 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. |
|
2149 |
|
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 |
|
2153 |
|
2154 @see mkstemp() |
|
2155 |
|
2156 @publishedAll |
|
2157 @externallyDefinedApi |
|
2158 */ |
|
2159 |
|
2160 /** @fn putenv(const char *string) |
|
2161 @param string |
|
2162 |
|
2163 Refer to getenv() for the documentation |
|
2164 |
|
2165 |
|
2166 |
|
2167 @publishedAll |
|
2168 @externallyDefinedApi |
|
2169 */ |
|
2170 |
|
2171 /** @fn random(void) |
|
2172 |
|
2173 Note: |
|
2174 |
|
2175 This description also covers the following functions - |
|
2176 srandom() srandomdev() initstate() setstate() |
|
2177 |
|
2178 The random function |
|
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 |
|
2183 16*(2**(310) -1). |
|
2184 |
|
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 |
|
2191 value. |
|
2192 |
|
2193 Like rand random will by default produce a sequence of numbers that can be duplicated |
|
2194 by calling srandom with "1" |
|
2195 as the seed. |
|
2196 |
|
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 |
|
2203 a fixed seed. |
|
2204 |
|
2205 The initstate routine allows a state array, passed in as an argument, to be initialized |
|
2206 for future use. |
|
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. |
|
2218 |
|
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. |
|
2224 |
|
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. |
|
2229 |
|
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. |
|
2232 |
|
2233 Examples: |
|
2234 @code |
|
2235 //Illustrates how to use srandom API. |
|
2236 #include <stdlib.h> |
|
2237 void srandomTest() |
|
2238 { |
|
2239 long randNum; |
|
2240 // srandom function call sets its argument as the seed for the new sequence of random integers //generated by random |
|
2241 srandom(2); |
|
2242 // Function call to generate the random number, which will generate the random based on the //argument passed to the srandom function. |
|
2243 randNum = random(); |
|
2244 //print the random number generated. |
|
2245 printf("random number is %d",randNum); |
|
2246 } |
|
2247 |
|
2248 @endcode |
|
2249 @code |
|
2250 Output |
|
2251 |
|
2252 random number is 1505335290. |
|
2253 |
|
2254 @endcode |
|
2255 Diagnostics: |
|
2256 |
|
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. |
|
2259 @see arc4random() |
|
2260 @see rand() |
|
2261 @see srand() |
|
2262 @see random() |
|
2263 |
|
2264 Bugs: |
|
2265 |
|
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 . |
|
2270 |
|
2271 |
|
2272 |
|
2273 @publishedAll |
|
2274 @externallyDefinedApi |
|
2275 */ |
|
2276 |
|
2277 /** @fn srandom(unsigned long seed) |
|
2278 @param seed |
|
2279 |
|
2280 Refer to random() for the documentation |
|
2281 @see arc4random() |
|
2282 @see rand() |
|
2283 @see srand() |
|
2284 @see random() |
|
2285 |
|
2286 |
|
2287 @publishedAll |
|
2288 @externallyDefinedApi |
|
2289 */ |
|
2290 |
|
2291 /** @fn realpath(const char *pathname, char resolved_path[]) |
|
2292 @param pathname |
|
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. |
|
2296 |
|
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. |
|
2300 |
|
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. |
|
2303 |
|
2304 Examples: |
|
2305 @code |
|
2306 #include<stdlib.h> |
|
2307 #include<stdio.h> //printf |
|
2308 #include<sys/stat.h> //S_IWUSR |
|
2309 #include<sys/syslimits.h> //PATH_MAX |
|
2310 #include<unistd.h> //chdir |
|
2311 |
|
2312 int main() |
|
2313 { |
|
2314 char resolvepath[PATH_MAX]; |
|
2315 FILE *fp = NULL; |
|
2316 char *rpath = NULL; |
|
2317 int isymlink = 0; |
|
2318 |
|
2319 fp = fopen("c:\xyz.txt", "w"); |
|
2320 if(!fp) |
|
2321 { |
|
2322 printf("fopen failed!!"); |
|
2323 return -1; |
|
2324 } |
|
2325 |
|
2326 mkdir("c:\tmdir", S_IWUSR); |
|
2327 |
|
2328 int c = chdir("c:\"); |
|
2329 if(c == -1) |
|
2330 { |
|
2331 printf("chdir failed!!"); |
|
2332 return -1; |
|
2333 } |
|
2334 |
|
2335 rpath = realpath(".\tmdir\..\xyz.txt", resolvepath); |
|
2336 printf("resolvepath: %s", resolvepath); |
|
2337 if(rpath != NULL) |
|
2338 printf("rpath: %s", rpath); |
|
2339 |
|
2340 fclose(fp); |
|
2341 rmdir("c:\tmdir"); |
|
2342 unlink("c:\xyz.txt"); |
|
2343 |
|
2344 mkdir("c:\tdir", S_IWUSR); |
|
2345 |
|
2346 fp = fopen("c:\tdir\xyz.txt", "w"); |
|
2347 if(!fp) |
|
2348 { |
|
2349 printf("fopen failed!!"); |
|
2350 return -1; |
|
2351 } |
|
2352 |
|
2353 fclose(fp); |
|
2354 |
|
2355 unlink("c:\linkname.txt"); |
|
2356 |
|
2357 isymlink = symlink("c:\tdir\xyz.txt", "c:\linkname.txt"); |
|
2358 if (isymlink == -1) |
|
2359 { |
|
2360 printf("symlink failed!!"); |
|
2361 return -1; |
|
2362 } |
|
2363 |
|
2364 rpath = realpath("c:\linkname.txt", resolvepath); |
|
2365 |
|
2366 printf("resolvepath: %s", resolvepath); |
|
2367 if(rpath != NULL) |
|
2368 printf("rpath: %s", rpath); |
|
2369 |
|
2370 unlink("c:\tdir\xyz.txt"); |
|
2371 rmdir("c:\tdir"); |
|
2372 |
|
2373 return 0; |
|
2374 } |
|
2375 |
|
2376 @endcode |
|
2377 @code |
|
2378 Output |
|
2379 |
|
2380 resolvepath: C:\xyz.txt |
|
2381 rpath: C:\xyz.txt |
|
2382 resolvepath: c: dir\xyz.txt |
|
2383 rpath: c: dir\xyz.txt |
|
2384 |
|
2385 @endcode |
|
2386 |
|
2387 |
|
2388 @publishedAll |
|
2389 @externallyDefinedApi |
|
2390 */ |
|
2391 |
|
2392 /** @fn setstate(char *state) |
|
2393 @param state |
|
2394 |
|
2395 Refer to random() for the documentation |
|
2396 @see arc4random() |
|
2397 @see rand() |
|
2398 @see srand() |
|
2399 @see random() |
|
2400 |
|
2401 |
|
2402 @publishedAll |
|
2403 @externallyDefinedApi |
|
2404 */ |
|
2405 |
|
2406 /** @fn initstate(unsigned long seed, char *state, long n) |
|
2407 @param seed |
|
2408 @param state |
|
2409 @param n |
|
2410 |
|
2411 Refer to random() for the documentation |
|
2412 @see arc4random() |
|
2413 @see rand() |
|
2414 @see srand() |
|
2415 @see random() |
|
2416 |
|
2417 |
|
2418 @publishedAll |
|
2419 @externallyDefinedApi |
|
2420 */ |
|
2421 |
|
2422 |
|
2423 /** @fn getprogname(void) |
|
2424 |
|
2425 Refer to setprogname() for the documentation |
|
2426 |
|
2427 @publishedAll |
|
2428 @externallyDefinedApi |
|
2429 */ |
|
2430 |
|
2431 /** @fn reallocf(void *ptr, size_t size) |
|
2432 @param ptr |
|
2433 @param size |
|
2434 |
|
2435 Refer to malloc() for the documentation |
|
2436 @see brk() |
|
2437 @see mmap() |
|
2438 @see getpagesize() |
|
2439 |
|
2440 |
|
2441 @publishedAll |
|
2442 @externallyDefinedApi |
|
2443 */ |
|
2444 |
|
2445 /** @fn setprogname(const char *programname) |
|
2446 @param programname |
|
2447 |
|
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. |
|
2461 @publishedAll |
|
2462 @externallyDefinedApi |
|
2463 */ |
|
2464 |
|
2465 /** @fn strtoq(const char *nptr, char **endptr, int base) |
|
2466 @param nptr |
|
2467 @param endptr |
|
2468 @param base |
|
2469 |
|
2470 Refer to strtol() for the documentation |
|
2471 @see atof() |
|
2472 @see atoi() |
|
2473 @see atol() |
|
2474 @see strtod() |
|
2475 @see strtoul() |
|
2476 @see wcstol() |
|
2477 |
|
2478 |
|
2479 @publishedAll |
|
2480 @externallyDefinedApi |
|
2481 */ |
|
2482 |
|
2483 /** @fn strtouq(const char *nptr, char **endptr, int base) |
|
2484 @param nptr |
|
2485 @param endptr |
|
2486 @param base |
|
2487 |
|
2488 Refer to strtoul() for the documentation |
|
2489 @see strtol() |
|
2490 @see wcstoul() |
|
2491 |
|
2492 |
|
2493 @publishedAll |
|
2494 @externallyDefinedApi |
|
2495 */ |
|
2496 |
|
2497 |
|
2498 /** @struct div_t |
|
2499 |
|
2500 Contains the following members |
|
2501 |
|
2502 @publishedAll |
|
2503 @externallyDefinedApi |
|
2504 */ |
|
2505 |
|
2506 /** @var div_t::quot |
|
2507 quotient |
|
2508 */ |
|
2509 |
|
2510 /** @var div_t::rem |
|
2511 remainder |
|
2512 */ |
|
2513 |
|
2514 |
|
2515 /** @struct ldiv_t |
|
2516 |
|
2517 Contains the following members |
|
2518 |
|
2519 @publishedAll |
|
2520 @externallyDefinedApi |
|
2521 */ |
|
2522 |
|
2523 /** @var ldiv_t::quot |
|
2524 quotient |
|
2525 */ |
|
2526 |
|
2527 /** @var ldiv_t::rem |
|
2528 remainder |
|
2529 */ |
|
2530 |
|
2531 |
|
2532 /** @struct lldiv_t |
|
2533 |
|
2534 Contains the following members |
|
2535 |
|
2536 @publishedAll |
|
2537 @externallyDefinedApi |
|
2538 */ |
|
2539 |
|
2540 /** @var lldiv_t::quot |
|
2541 quotient |
|
2542 */ |
|
2543 |
|
2544 /** @var lldiv_t::rem |
|
2545 remainder |
|
2546 */ |
|
2547 |
|
2548 |
|
2549 /** @def EXIT_FAILURE |
|
2550 |
|
2551 These are arguments for the exit and _exit functions and the return values for the atexit and _onexit functions. |
|
2552 |
|
2553 @publishedAll |
|
2554 @externallyDefinedApi |
|
2555 */ |
|
2556 |
|
2557 |
|
2558 /** @def EXIT_SUCCESS |
|
2559 |
|
2560 These are arguments for the exit and _exit functions and the return values for the atexit and _onexit functions. |
|
2561 |
|
2562 @publishedAll |
|
2563 @externallyDefinedApi |
|
2564 */ |
|
2565 |
|
2566 |
|
2567 /** @def RAND_MAX |
|
2568 |
|
2569 The constant RAND_MAX is the maximum value that can be returned by the rand function. |
|
2570 |
|
2571 @publishedAll |
|
2572 @externallyDefinedApi |
|
2573 */ |
|
2574 |
|
2575 |
|
2576 |
|
2577 /** @def MB_CUR_MAX |
|
2578 |
|
2579 The value of MB_CUR_MAX is the maximum number of bytes in a multibyte character for the current locale. |
|
2580 |
|
2581 @publishedAll |
|
2582 @externallyDefinedApi |
|
2583 */ |
|
2584 |
|
2585 |
|
2586 |
|
2587 |
|
2588 |