* make_absolute_path * * If the given pathname isn't already absolute, make it so, interpreting * it relative to the current working directory. * * Also canonicalizes the path. The result is always a malloc'd copy. * * In backend, failure cases result in ereport(ERROR); in frontend, * we write a complaint on stderr and return NULL. * * Note: interpretation of relative-path arguments dur
| 605 | * not like the results. |
| 606 | */ |
| 607 | char * |
| 608 | make_absolute_path(const char *path) |
| 609 | { |
| 610 | char *new; |
| 611 | |
| 612 | /* Returning null for null input is convenient for some callers */ |
| 613 | if (path == NULL) |
| 614 | return NULL; |
| 615 | |
| 616 | if (!is_absolute_path(path)) |
| 617 | { |
| 618 | char *buf; |
| 619 | size_t buflen; |
| 620 | |
| 621 | buflen = MAXPGPATH; |
| 622 | for (;;) |
| 623 | { |
| 624 | buf = malloc(buflen); |
| 625 | if (!buf) |
| 626 | { |
| 627 | #ifndef FRONTEND |
| 628 | ereport(ERROR, |
| 629 | (errcode(ERRCODE_OUT_OF_MEMORY), |
| 630 | errmsg("out of memory"))); |
| 631 | #else |
| 632 | fprintf(stderr, _("out of memory\n")); |
| 633 | return NULL; |
| 634 | #endif |
| 635 | } |
| 636 | |
| 637 | if (getcwd(buf, buflen)) |
| 638 | break; |
| 639 | else if (errno == ERANGE) |
| 640 | { |
| 641 | free(buf); |
| 642 | buflen *= 2; |
| 643 | continue; |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | int save_errno = errno; |
| 648 | |
| 649 | free(buf); |
| 650 | errno = save_errno; |
| 651 | #ifndef FRONTEND |
| 652 | elog(ERROR, "could not get current working directory: %m"); |
| 653 | #else |
| 654 | fprintf(stderr, _("could not get current working directory: %s\n"), |
| 655 | strerror(errno)); |
| 656 | return NULL; |
| 657 | #endif |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | new = malloc(strlen(buf) + strlen(path) + 2); |
| 662 | if (!new) |
| 663 | { |
| 664 | free(buf); |