Changeset 7f9df7b9 in mainline for uspace/lib/posix/src/stdlib.c
- Timestamp:
- 2018-01-22T22:42:57Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7a08c70
- Parents:
- e0f47f5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/stdlib.c
re0f47f5 r7f9df7b9 34 34 */ 35 35 36 #define LIBPOSIX_INTERNAL37 #define __POSIX_DEF__(x) posix_##x38 39 36 #include "internal/common.h" 40 37 #include "posix/stdlib.h" … … 60 57 * @param compare 61 58 */ 62 int posix_atexit(void (*func)(void))59 int atexit(void (*func)(void)) 63 60 { 64 61 // TODO: low priority, just a compile-time dependency of binutils … … 73 70 * @return Absolute value of the parameter. 74 71 */ 75 int posix_abs(int i)72 int abs(int i) 76 73 { 77 74 return i < 0 ? -i : i; … … 84 81 * @return Absolute value of the parameter. 85 82 */ 86 long posix_labs(long i)83 long labs(long i) 87 84 { 88 85 return i < 0 ? -i : i; … … 95 92 * @return Absolute value of the parameter. 96 93 */ 97 long long posix_llabs(long long i)94 long long llabs(long long i) 98 95 { 99 96 return i < 0 ? -i : i; … … 107 104 * @return Quotient and remainder packed into structure. 108 105 */ 109 posix_div_t posix_div(int numer, int denom)110 { 111 return ( posix_div_t) { .quot = numer / denom, .rem = numer % denom };106 div_t div(int numer, int denom) 107 { 108 return (div_t) { .quot = numer / denom, .rem = numer % denom }; 112 109 } 113 110 … … 119 116 * @return Quotient and remainder packed into structure. 120 117 */ 121 posix_ldiv_t posix_ldiv(long numer, long denom)122 { 123 return ( posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom };118 ldiv_t ldiv(long numer, long denom) 119 { 120 return (ldiv_t) { .quot = numer / denom, .rem = numer % denom }; 124 121 } 125 122 … … 131 128 * @return Quotient and remainder packed into structure. 132 129 */ 133 posix_lldiv_t posix_lldiv(long long numer, long long denom) 134 { 135 return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom }; 136 } 137 138 /** 139 * Array sorting utilizing the quicksort algorithm. 140 * 141 * @param array Array of elements to sort. 142 * @param count Number of elements in the array. 143 * @param size Width of each element. 144 * @param compare Decides relative ordering of two elements. 145 */ 146 void posix_qsort(void *array, size_t count, size_t size, 147 int (*compare)(const void *, const void *)) 148 { 149 qsort(array, count, size, compare); 130 lldiv_t lldiv(long long numer, long long denom) 131 { 132 return (lldiv_t) { .quot = numer / denom, .rem = numer % denom }; 150 133 } 151 134 … … 160 143 * @return Pointer to a matching element, or NULL if none can be found. 161 144 */ 162 void * posix_bsearch(const void *key, const void *base,145 void *bsearch(const void *key, const void *base, 163 146 size_t nmemb, size_t size, int (*compar)(const void *, const void *)) 164 147 { … … 195 178 * @return Value of the variable or NULL if such variable does not exist. 196 179 */ 197 char * posix_getenv(const char *name)180 char *getenv(const char *name) 198 181 { 199 182 return NULL; … … 206 189 * @return 207 190 */ 208 int p osix_putenv(char *string)191 int putenv(char *string) 209 192 { 210 193 // TODO: low priority, just a compile-time dependency of binutils … … 221 204 * or not (zero). 222 205 */ 223 int posix_system(const char *string) {206 int system(const char *string) { 224 207 // TODO: does nothing at the moment 225 208 not_implemented(); … … 237 220 * 238 221 */ 239 char * posix_realpath(const char *restrict name, char *restrict resolved)222 char *realpath(const char *restrict name, char *restrict resolved) 240 223 { 241 224 #ifndef PATH_MAX … … 279 262 /** 280 263 * Converts a string representation of a floating-point number to 281 * its native representation. See posix_strtold().264 * its native representation. See strtold(). 282 265 * 283 266 * @param nptr String representation of a floating-point number. 284 267 * @return Double-precision number resulting from the string conversion. 285 268 */ 286 double posix_atof(const char *nptr)287 { 288 return posix_strtod(nptr, NULL);269 double atof(const char *nptr) 270 { 271 return strtod(nptr, NULL); 289 272 } 290 273 291 274 /** 292 275 * Converts a string representation of a floating-point number to 293 * its native representation. See posix_strtold().276 * its native representation. See strtold(). 294 277 * 295 278 * @param nptr String representation of a floating-point number. … … 298 281 * @return Single-precision number resulting from the string conversion. 299 282 */ 300 float posix_strtof(const char *restrict nptr, char **restrict endptr)301 { 302 return (float) posix_strtold(nptr, endptr);283 float strtof(const char *restrict nptr, char **restrict endptr) 284 { 285 return (float) strtold(nptr, endptr); 303 286 } 304 287 305 288 /** 306 289 * Converts a string representation of a floating-point number to 307 * its native representation. See posix_strtold().290 * its native representation. See strtold(). 308 291 * 309 292 * @param nptr String representation of a floating-point number. … … 312 295 * @return Double-precision number resulting from the string conversion. 313 296 */ 314 double posix_strtod(const char *restrict nptr, char **restrict endptr) 315 { 316 return (double) posix_strtold(nptr, endptr); 317 } 318 319 /** 320 * Allocate memory chunk. 321 * 322 * @param size Size of the chunk to allocate. 323 * @return Either pointer to the allocated chunk or NULL if not possible. 324 */ 325 void *posix_malloc(size_t size) 326 { 327 return malloc(size); 328 } 329 330 /** 331 * Allocate memory for an array of elements. 332 * 333 * @param nelem Number of elements in the array. 334 * @param elsize Size of each element. 335 * @return Either pointer to the allocated array or NULL if not possible. 336 */ 337 void *posix_calloc(size_t nelem, size_t elsize) 338 { 339 return calloc(nelem, elsize); 340 } 341 342 /** 343 * Reallocate memory chunk to a new size. 344 * 345 * @param ptr Memory chunk to reallocate. Might be NULL. 346 * @param size Size of the reallocated chunk. Might be zero. 347 * @return Either NULL or the pointer to the newly reallocated chunk. 348 */ 349 void *posix_realloc(void *ptr, size_t size) 350 { 351 if (ptr != NULL && size == 0) { 352 /* Native realloc does not handle this special case. */ 353 free(ptr); 354 return NULL; 355 } else { 356 return realloc(ptr, size); 357 } 358 } 359 360 /** 361 * Free allocated memory chunk. 362 * 363 * @param ptr Memory chunk to be freed. 364 */ 365 void posix_free(void *ptr) 366 { 367 if (ptr) { 368 free(ptr); 369 } 370 } 371 372 /** 373 * Generate a pseudo random integer in the range 0 to RAND_MAX inclusive. 374 * 375 * @return The pseudo random integer. 376 */ 377 int posix_rand(void) 378 { 379 return (int) rand(); 380 } 381 382 /** 383 * Initialize a new sequence of pseudo-random integers. 384 * 385 * @param seed The seed of the new sequence. 386 */ 387 void posix_srand(unsigned int seed) 388 { 389 srand(seed); 297 double strtod(const char *restrict nptr, char **restrict endptr) 298 { 299 return (double) strtold(nptr, endptr); 390 300 } 391 301 … … 396 306 * @return The opened file descriptor or -1 on error. 397 307 */ 398 int posix_mkstemp(char *tmpl)308 int mkstemp(char *tmpl) 399 309 { 400 310 int fd = -1; 401 311 402 char *tptr = tmpl + posix_strlen(tmpl) - 6;312 char *tptr = tmpl + strlen(tmpl) - 6; 403 313 404 314 while (fd < 0) { 405 if (* posix_mktemp(tmpl) == '\0') {315 if (*mktemp(tmpl) == '\0') { 406 316 /* Errno set by mktemp(). */ 407 317 return -1; 408 318 } 409 319 410 fd = posix_open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);320 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 411 321 412 322 if (fd == -1) { … … 427 337 * reduced to an empty string. 428 338 */ 429 char * posix_mktemp(char *tmpl)430 { 431 int tmpl_len = posix_strlen(tmpl);339 char *mktemp(char *tmpl) 340 { 341 int tmpl_len = strlen(tmpl); 432 342 if (tmpl_len < 6) { 433 343 errno = EINVAL; … … 437 347 438 348 char *tptr = tmpl + tmpl_len - 6; 439 if ( posix_strcmp(tptr, "XXXXXX") != 0) {349 if (strcmp(tptr, "XXXXXX") != 0) { 440 350 errno = EINVAL; 441 351 *tmpl = '\0'; … … 451 361 errno = 0; 452 362 /* Check if the file exists. */ 453 if ( posix_access(tmpl, F_OK) == -1) {363 if (access(tmpl, F_OK) == -1) { 454 364 if (errno == ENOENT) { 455 365 errno = orig_errno;
Note:
See TracChangeset
for help on using the changeset viewer.