Changeset a35b458 in mainline for uspace/lib/posix/src
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- uspace/lib/posix/src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/dlfcn.c
r3061bc1 ra35b458 42 42 fprintf(stderr, "dlopen() not implemented with non-zero flags (%s:%d), something will NOT work.\n", __FILE__, __LINE__); 43 43 } 44 44 45 45 return __helenos_libc_dlopen(filename, 0); 46 46 } -
uspace/lib/posix/src/fnmatch.c
r3061bc1 ra35b458 301 301 return COLL_ELM_INVALID; 302 302 } 303 303 304 304 *pattern = p + 1; 305 305 return _coll_elm_char(*p); … … 359 359 p++; 360 360 } 361 361 362 362 coll_elm_t current_elm = COLL_ELM_INVALID; 363 363 364 364 while (*p != ']') { 365 365 if (*p == '-' && *(p + 1) != ']' && … … 374 374 continue; 375 375 } 376 376 377 377 if (*p == '[' && *(p + 1) == ':') { 378 378 current_elm = COLL_ELM_INVALID; … … 380 380 continue; 381 381 } 382 382 383 383 current_elm = _next_coll_elm(&p, flags); 384 384 if (current_elm == COLL_ELM_INVALID) { … … 460 460 return false; 461 461 } 462 462 463 463 /* None of the above, match anything else. */ 464 464 p++; … … 496 496 497 497 /* Entire sub-pattern matched. */ 498 498 499 499 /* postconditions */ 500 500 assert(*p == '\0' || *p == '*'); 501 501 assert(*p != '\0' || *s == '\0' || (leading_dir && *s == '/')); 502 502 503 503 *pattern = p; 504 504 *string = s; … … 667 667 match("helen??", "helenos", 0); 668 668 match("****booo****", "booo", 0); 669 669 670 670 match("hello[[:space:]]world", "hello world", 0); 671 671 nomatch("hello[[:alpha:]]world", "hello world", 0); 672 672 673 673 match("/hoooo*", "/hooooooo/hooo", 0); 674 674 nomatch("/hoooo*", "/hooooooo/hooo", FNM_PATHNAME); … … 680 680 nomatch("/hoooo", "/hooooooo/hooo", FNM_LEADING_DIR); 681 681 match("/hooooooo", "/hooooooo/hooo", FNM_LEADING_DIR); 682 682 683 683 match("*", "hell", 0); 684 684 match("*?", "hell", 0); … … 687 687 match("??*??", "hell", 0); 688 688 nomatch("???*??", "hell", 0); 689 689 690 690 nomatch("", "hell", 0); 691 691 nomatch("?", "hell", 0); … … 693 693 nomatch("???", "hell", 0); 694 694 match("????", "hell", 0); 695 695 696 696 match("*", "h.ello", FNM_PERIOD); 697 697 match("*", "h.ello", FNM_PATHNAME | FNM_PERIOD); -
uspace/lib/posix/src/pwd.c
r3061bc1 ra35b458 120 120 assert(buffer != NULL); 121 121 assert(result != NULL); 122 122 123 123 if (strcmp(name, "user") != 0) { 124 124 *result = NULL; 125 125 return 0; 126 126 } 127 127 128 128 return getpwuid_r(0, pwd, buffer, bufsize, result); 129 129 } … … 161 161 assert(buffer != NULL); 162 162 assert(result != NULL); 163 163 164 164 static const char bf[] = { 'u', 's', 'e', 'r', '\0', 165 165 '/', '\0', 'b', 'd', 's', 'h', '\0' }; 166 166 167 167 if (uid != 0) { 168 168 *result = NULL; -
uspace/lib/posix/src/signal.c
r3061bc1 ra35b458 101 101 /* Commit suicide. */ 102 102 task_kill(task_get_id()); 103 103 104 104 /* Should not be reached. */ 105 105 abort(); … … 223 223 { 224 224 assert(set != NULL); 225 225 226 226 return (*set & (1 << signo)) != 0; 227 227 } … … 323 323 assert(signo >= 0 && signo <= _TOP_SIGNAL); 324 324 assert(siginfo != NULL); 325 325 326 326 signal_queue_item *item = malloc(sizeof(signal_queue_item)); 327 327 link_initialize(&(item->link)); … … 384 384 link_t *iterator = _signal_queue.head.next; 385 385 link_t *next; 386 386 387 387 while (iterator != &(_signal_queue).head) { 388 388 next = iterator->next; 389 389 390 390 signal_queue_item *item = 391 391 list_get_instance(iterator, signal_queue_item, link); 392 392 393 393 if (!sigismember(&_signal_mask, item->signo) && 394 394 _signal_actions[item->signo].sa_handler != SIG_HOLD) { … … 397 397 free(item); 398 398 } 399 399 400 400 iterator = next; 401 401 } … … 535 535 } 536 536 } 537 537 538 538 _dequeue_unblocked_signals(); 539 539 -
uspace/lib/posix/src/stdio.c
r3061bc1 ra35b458 277 277 size_t sz; 278 278 char buf[4]; 279 279 280 280 while (offset < size) { 281 281 sz = 0; … … 283 283 break; 284 284 } 285 285 286 286 const int fildes = *(int *) fd; 287 287 size_t nwr; 288 288 if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK) 289 289 break; 290 290 291 291 chars++; 292 292 offset += sizeof(wchar_t); 293 293 } 294 294 295 295 return chars; 296 296 } … … 311 311 .data = &fildes 312 312 }; 313 313 314 314 return printf_core(format, &spec, ap); 315 315 } … … 490 490 { 491 491 assert(L_tmpnam >= strlen("/tmp/tnXXXXXX")); 492 492 493 493 static char buffer[L_tmpnam + 1]; 494 494 if (s == NULL) { 495 495 s = buffer; 496 496 } 497 497 498 498 strcpy(s, "/tmp/tnXXXXXX"); 499 499 mktemp(s); 500 500 501 501 if (*s == '\0') { 502 502 /* Errno set by mktemp(). */ 503 503 return NULL; 504 504 } 505 505 506 506 return s; 507 507 } … … 518 518 /* Sequence number of the filename. */ 519 519 static int seq = 0; 520 520 521 521 size_t dir_len = strlen(dir); 522 522 if (dir[dir_len - 1] == '/') { 523 523 dir_len--; 524 524 } 525 525 526 526 size_t pfx_len = strlen(pfx); 527 527 if (pfx_len > 5) { 528 528 pfx_len = 5; 529 529 } 530 530 531 531 char *result = malloc(dir_len + /* slash*/ 1 + 532 532 pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1); 533 533 534 534 if (result == NULL) { 535 535 errno = ENOMEM; 536 536 return NULL; 537 537 } 538 538 539 539 char *res_ptr = result; 540 540 strncpy(res_ptr, dir, dir_len); … … 542 542 strncpy(res_ptr, pfx, pfx_len); 543 543 res_ptr += pfx_len; 544 544 545 545 for (; seq < 1000; ++seq) { 546 546 snprintf(res_ptr, 8, "%03d.tmp", seq); 547 547 548 548 int orig_errno = errno; 549 549 errno = EOK; … … 559 559 } 560 560 } 561 561 562 562 if (seq == 1000) { 563 563 free(result); … … 565 565 return NULL; 566 566 } 567 567 568 568 return result; 569 569 } … … 585 585 return NULL; 586 586 } 587 587 588 588 /* Unlink the created file, so that it's removed on close(). */ 589 589 unlink(filename); -
uspace/lib/posix/src/stdio/scanf.c
r3061bc1 ra35b458 459 459 { 460 460 assert(modifier); 461 461 462 462 switch (c) { 463 463 case 'S': … … 880 880 } else if (is_seq_conv(*fmt, &length_mod)) { 881 881 /* Character sequence conversion. */ 882 882 883 883 /* Check sanity of optional parts of conversion specifier. */ 884 884 if (length_mod != LMOD_NONE && … … 1074 1074 ++converted_cnt; 1075 1075 } 1076 1076 1077 1077 converting = false; 1078 1078 /* Format string pointer already incremented. */ … … 1102 1102 break; 1103 1103 } 1104 1104 1105 1105 } else { 1106 1106 … … 1152 1152 } 1153 1153 } 1154 1154 1155 1155 } 1156 1156 -
uspace/lib/posix/src/stdlib.c
r3061bc1 ra35b458 165 165 } 166 166 } 167 167 168 168 return NULL; 169 169 } … … 225 225 assert(resolved == NULL); 226 226 #endif 227 227 228 228 if (name == NULL) { 229 229 errno = EINVAL; 230 230 return NULL; 231 231 } 232 232 233 233 // TODO: symlink resolution 234 234 235 235 /* Function absolutize is implemented in libc and declared in vfs.h. 236 236 * No more processing is required as HelenOS doesn't have symlinks … … 239 239 */ 240 240 char* absolute = vfs_absolutize(name, NULL); 241 241 242 242 if (absolute == NULL) { 243 243 /* POSIX requires some specific errnos to be set … … 248 248 return NULL; 249 249 } 250 250 251 251 if (resolved == NULL) { 252 252 return absolute; … … 309 309 { 310 310 int fd = -1; 311 311 312 312 char *tptr = tmpl + strlen(tmpl) - 6; 313 313 314 314 while (fd < 0) { 315 315 if (*mktemp(tmpl) == '\0') { … … 317 317 return -1; 318 318 } 319 319 320 320 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 321 321 322 322 if (fd == -1) { 323 323 /* Restore template to it's original state. */ … … 325 325 } 326 326 } 327 327 328 328 return fd; 329 329 } … … 345 345 return tmpl; 346 346 } 347 347 348 348 char *tptr = tmpl + tmpl_len - 6; 349 349 if (strcmp(tptr, "XXXXXX") != 0) { … … 352 352 return tmpl; 353 353 } 354 354 355 355 static int seq = 0; 356 356 357 357 for (; seq < 1000000; ++seq) { 358 358 snprintf(tptr, 7, "%06d", seq); 359 359 360 360 int orig_errno = errno; 361 361 errno = 0; … … 372 372 } 373 373 } 374 374 375 375 if (seq == 10000000) { 376 376 errno = EEXIST; … … 378 378 return tmpl; 379 379 } 380 380 381 381 return tmpl; 382 382 } … … 392 392 { 393 393 assert(nelem > 0); 394 394 395 395 size_t count; 396 396 load_t *loads = stats_get_load(&count); 397 397 398 398 if (loads == NULL) { 399 399 return -1; 400 400 } 401 401 402 402 if (((size_t) nelem) < count) { 403 403 count = nelem; 404 404 } 405 405 406 406 for (size_t i = 0; i < count; ++i) { 407 407 loadavg[i] = (double) loads[i]; 408 408 } 409 409 410 410 free(loads); 411 411 return count; -
uspace/lib/posix/src/stdlib/strtold.c
r3061bc1 ra35b458 141 141 return mant; 142 142 } 143 143 144 144 if (abs(exp) >> (MAX_POW5 + 1) != 0) { 145 145 /* Too large exponent. */ … … 147 147 return exp < 0 ? LDBL_MIN : HUGE_VALL; 148 148 } 149 149 150 150 if (exp < 0) { 151 151 exp = abs(exp); … … 175 175 } 176 176 } 177 177 178 178 return mant; 179 179 } … … 191 191 return mant; 192 192 } 193 193 194 194 if (exp > LDBL_MAX_EXP || exp < LDBL_MIN_EXP) { 195 195 errno = ERANGE; 196 196 return exp < 0 ? LDBL_MIN : HUGE_VALL; 197 197 } 198 198 199 199 if (exp < 0) { 200 200 exp = abs(exp); … … 220 220 } 221 221 } 222 222 223 223 return mant; 224 224 } … … 242 242 assert(sptr != NULL); 243 243 assert (*sptr != NULL); 244 244 245 245 const int DEC_BASE = 10; 246 246 const char DECIMAL_POINT = '.'; 247 247 const char EXPONENT_MARK = 'e'; 248 248 249 249 const char *str = *sptr; 250 250 long double significand = 0; 251 251 long exponent = 0; 252 252 253 253 /* number of digits parsed so far */ 254 254 int parsed_digits = 0; 255 255 bool after_decimal = false; 256 256 257 257 while (isdigit(*str) || (!after_decimal && *str == DECIMAL_POINT)) { 258 258 if (*str == DECIMAL_POINT) { … … 261 261 continue; 262 262 } 263 263 264 264 if (parsed_digits == 0 && *str == '0') { 265 265 /* Nothing, just skip leading zeros. */ … … 270 270 exponent++; 271 271 } 272 272 273 273 if (after_decimal) { 274 274 /* Decrement exponent if we are parsing the fractional part. */ 275 275 exponent--; 276 276 } 277 277 278 278 str++; 279 279 } 280 280 281 281 /* exponent */ 282 282 if (tolower(*str) == EXPONENT_MARK) { 283 283 str++; 284 284 285 285 /* Returns MIN/MAX value on error, which is ok. */ 286 286 long exp = strtol(str, (char **) &str, DEC_BASE); 287 287 288 288 if (exponent > 0 && exp > LONG_MAX - exponent) { 289 289 exponent = LONG_MAX; … … 294 294 } 295 295 } 296 296 297 297 *sptr = str; 298 298 299 299 /* Return multiplied by a power of ten. */ 300 300 return mul_pow2(mul_pow5(significand, exponent), exponent); … … 330 330 { 331 331 assert(sptr != NULL && *sptr != NULL); 332 332 333 333 const int DEC_BASE = 10; 334 334 const int HEX_BASE = 16; 335 335 const char DECIMAL_POINT = '.'; 336 336 const char EXPONENT_MARK = 'p'; 337 337 338 338 const char *str = *sptr; 339 339 long double significand = 0; 340 340 long exponent = 0; 341 341 342 342 /* number of bits parsed so far */ 343 343 int parsed_bits = 0; 344 344 bool after_decimal = false; 345 345 346 346 while (isxdigit(*str) || (!after_decimal && *str == DECIMAL_POINT)) { 347 347 if (*str == DECIMAL_POINT) { … … 350 350 continue; 351 351 } 352 352 353 353 if (parsed_bits == 0 && *str == '0') { 354 354 /* Nothing, just skip leading zeros. */ … … 359 359 exponent += 4; 360 360 } 361 361 362 362 if (after_decimal) { 363 363 exponent -= 4; 364 364 } 365 365 366 366 str++; 367 367 } 368 368 369 369 /* exponent */ 370 370 if (tolower(*str) == EXPONENT_MARK) { 371 371 str++; 372 372 373 373 /* Returns MIN/MAX value on error, which is ok. */ 374 374 long exp = strtol(str, (char **) &str, DEC_BASE); 375 375 376 376 if (exponent > 0 && exp > LONG_MAX - exponent) { 377 377 exponent = LONG_MAX; … … 382 382 } 383 383 } 384 384 385 385 *sptr = str; 386 386 387 387 /* Return multiplied by a power of two. */ 388 388 return mul_pow2(significand, exponent); … … 407 407 { 408 408 assert(nptr != NULL); 409 409 410 410 const int RADIX = '.'; 411 411 412 412 /* minus sign */ 413 413 bool negative = false; 414 414 /* current position in the string */ 415 415 int i = 0; 416 416 417 417 /* skip whitespace */ 418 418 while (isspace(nptr[i])) { 419 419 i++; 420 420 } 421 421 422 422 /* parse sign */ 423 423 switch (nptr[i]) { … … 428 428 i++; 429 429 } 430 430 431 431 /* check for NaN */ 432 432 if (strncasecmp(&nptr[i], "nan", 3) == 0) { 433 433 // FIXME: return NaN 434 434 // TODO: handle the parenthesised case 435 435 436 436 if (endptr != NULL) { 437 437 *endptr = (char *) nptr; … … 440 440 return 0; 441 441 } 442 442 443 443 /* check for Infinity */ 444 444 if (strncasecmp(&nptr[i], "inf", 3) == 0) { … … 447 447 i += 5; 448 448 } 449 449 450 450 if (endptr != NULL) { 451 451 *endptr = (char *) &nptr[i]; … … 459 459 (nptr[i + 2] == RADIX && isxdigit(nptr[i + 3])))) { 460 460 i += 2; 461 461 462 462 const char *ptr = &nptr[i]; 463 463 /* this call sets errno if appropriate. */ … … 468 468 return negative ? -result : result; 469 469 } 470 470 471 471 /* check for a decimal number */ 472 472 if (isdigit(nptr[i]) || (nptr[i] == RADIX && isdigit(nptr[i + 1]))) { … … 479 479 return negative ? -result : result; 480 480 } 481 481 482 482 /* nothing to parse */ 483 483 if (endptr != NULL) { -
uspace/lib/posix/src/string.c
r3061bc1 ra35b458 62 62 ++s1; 63 63 } 64 64 65 65 return (char *) s1; 66 66 } … … 107 107 for (size_t i = 0; ; ++i) { 108 108 dest[i] = src[i]; 109 109 110 110 if (src[i] == '\0') { 111 111 /* pointer to the terminating nul character */ … … 113 113 } 114 114 } 115 115 116 116 /* unreachable */ 117 117 return NULL; … … 133 133 for (size_t i = 0; i < n; ++i) { 134 134 dest[i] = src[i]; 135 135 136 136 /* the standard requires that nul characters 137 137 * are appended to the length of n, in case src is shorter … … 145 145 } 146 146 } 147 147 148 148 return &dest[n]; 149 149 } … … 197 197 assert(dest != NULL); 198 198 assert(src != NULL); 199 199 200 200 unsigned char* bdest = dest; 201 201 const unsigned char* bsrc = src; 202 202 203 203 for (size_t i = 0; i < n; ++i) { 204 204 bdest[i] = bsrc[i]; 205 205 206 206 if (bsrc[i] == (unsigned char) c) { 207 207 /* pointer to the next byte */ … … 209 209 } 210 210 } 211 211 212 212 return NULL; 213 213 } … … 301 301 { 302 302 assert(mem != NULL); 303 303 304 304 const unsigned char *s = mem; 305 305 306 306 for (size_t i = 0; i < n; ++i) { 307 307 if (s[i] == (unsigned char) c) { … … 323 323 { 324 324 assert(s != NULL); 325 325 326 326 char *res = gnu_strchrnul(s, c); 327 327 return (*res == c) ? res : NULL; … … 339 339 { 340 340 assert(s != NULL); 341 341 342 342 const char *ptr = strchr(s, '\0'); 343 343 344 344 /* the same as in strchr, except it loops in reverse direction */ 345 345 while (*ptr != (char) c) { … … 365 365 { 366 366 assert(s != NULL); 367 367 368 368 while (*s != c && *s != '\0') { 369 369 s++; 370 370 } 371 371 372 372 return (char *) s; 373 373 } … … 439 439 assert(haystack != NULL); 440 440 assert(needle != NULL); 441 441 442 442 /* Special case - needle is an empty string. */ 443 443 if (needle[0] == '\0') { 444 444 return (char *) haystack; 445 445 } 446 446 447 447 /* Preprocess needle. */ 448 448 size_t nlen = strlen(needle); 449 449 size_t prefix_table[nlen + 1]; 450 450 451 451 { 452 452 size_t i = 0; 453 453 ssize_t j = -1; 454 454 455 455 prefix_table[i] = j; 456 456 457 457 while (i < nlen) { 458 458 while (j >= 0 && needle[i] != needle[j]) { … … 463 463 } 464 464 } 465 465 466 466 /* Search needle using the precomputed table. */ 467 467 size_t npos = 0; 468 468 469 469 for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) { 470 470 while (npos != 0 && haystack[hpos] != needle[npos]) { 471 471 npos = prefix_table[npos]; 472 472 } 473 473 474 474 if (haystack[hpos] == needle[npos]) { 475 475 npos++; 476 476 477 477 if (npos == nlen) { 478 478 return (char *) (haystack + hpos - nlen + 1); … … 480 480 } 481 481 } 482 482 483 483 return NULL; 484 484 } … … 604 604 { 605 605 assert(buf != NULL); 606 606 607 607 char *errstr = strerror(errnum); 608 608 609 609 if (strlen(errstr) + 1 > bufsz) { 610 610 return ERANGE; … … 625 625 { 626 626 assert(s != NULL); 627 627 628 628 return (size_t) (strchr(s, '\0') - s); 629 629 } … … 639 639 { 640 640 assert(s != NULL); 641 641 642 642 for (size_t sz = 0; sz < n; ++sz) { 643 643 644 644 if (s[sz] == '\0') { 645 645 return sz; 646 646 } 647 647 } 648 648 649 649 return n; 650 650 } -
uspace/lib/posix/src/strings.c
r3061bc1 ra35b458 110 110 return cmp; 111 111 } 112 112 113 113 if (s1[i] == 0) { 114 114 return 0; 115 115 } 116 116 } 117 117 118 118 return 0; 119 119 } -
uspace/lib/posix/src/sys/mman.c
r3061bc1 ra35b458 44 44 if (!start) 45 45 start = AS_AREA_ANY; 46 46 47 47 // if (!((flags & MAP_SHARED) ^ (flags & MAP_PRIVATE))) 48 48 // return MAP_FAILED; 49 49 50 50 if (!(flags & MAP_ANONYMOUS)) 51 51 return MAP_FAILED; 52 52 53 53 return as_area_create(start, length, prot, AS_AREA_UNPAGED); 54 54 } -
uspace/lib/posix/src/sys/stat.c
r3061bc1 ra35b458 52 52 { 53 53 memset(dest, 0, sizeof(struct stat)); 54 54 55 55 dest->st_dev = src->service; 56 56 dest->st_ino = src->index; 57 57 58 58 /* HelenOS doesn't support permissions, so we set them all */ 59 59 dest->st_mode = S_IRWXU | S_IRWXG | S_IRWXO; … … 64 64 dest->st_mode |= S_IFDIR; 65 65 } 66 66 67 67 dest->st_nlink = src->lnkcnt; 68 68 dest->st_size = src->size; -
uspace/lib/posix/src/sys/wait.c
r3061bc1 ra35b458 95 95 assert(stat_ptr != NULL); 96 96 assert(options == 0 /* None of the options are supported. */); 97 97 98 98 task_exit_t texit; 99 99 int retval; 100 100 101 101 if (failed(task_wait_task_id((task_id_t) pid, &texit, &retval))) { 102 102 /* Unable to retrieve status. */ 103 103 return (pid_t) -1; 104 104 } 105 105 106 106 if (texit == TASK_EXIT_NORMAL) { 107 107 // FIXME: relies on application not returning this value … … 112 112 *stat_ptr = INT_MIN; 113 113 } 114 114 115 115 return pid; 116 116 }
Note:
See TracChangeset
for help on using the changeset viewer.