* \cd -- change directory */
| 538 | * \cd -- change directory |
| 539 | */ |
| 540 | static backslashResult |
| 541 | exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd) |
| 542 | { |
| 543 | bool success = true; |
| 544 | |
| 545 | if (active_branch) |
| 546 | { |
| 547 | char *opt = psql_scan_slash_option(scan_state, |
| 548 | OT_NORMAL, NULL, true); |
| 549 | char *dir; |
| 550 | |
| 551 | if (opt) |
| 552 | dir = opt; |
| 553 | else |
| 554 | { |
| 555 | #ifndef WIN32 |
| 556 | struct passwd *pw; |
| 557 | uid_t user_id = geteuid(); |
| 558 | |
| 559 | errno = 0; /* clear errno before call */ |
| 560 | pw = getpwuid(user_id); |
| 561 | if (!pw) |
| 562 | { |
| 563 | pg_log_error("could not get home directory for user ID %ld: %s", |
| 564 | (long) user_id, |
| 565 | errno ? strerror(errno) : _("user does not exist")); |
| 566 | exit(EXIT_FAILURE); |
| 567 | } |
| 568 | dir = pw->pw_dir; |
| 569 | #else /* WIN32 */ |
| 570 | |
| 571 | /* |
| 572 | * On Windows, 'cd' without arguments prints the current |
| 573 | * directory, so if someone wants to code this here instead... |
| 574 | */ |
| 575 | dir = "/"; |
| 576 | #endif /* WIN32 */ |
| 577 | } |
| 578 | |
| 579 | if (chdir(dir) == -1) |
| 580 | { |
| 581 | pg_log_error("\\%s: could not change directory to \"%s\": %m", |
| 582 | cmd, dir); |
| 583 | success = false; |
| 584 | } |
| 585 | |
| 586 | if (opt) |
| 587 | free(opt); |
| 588 | } |
| 589 | else |
| 590 | ignore_slash_options(scan_state); |
| 591 | |
| 592 | return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * \conninfo -- display information about the current connection |
no test coverage detected