/// The cd builtin. Changes the current directory to the one specified or to $HOME if none is /// specified. The directory can be relative to any directory in the CDPATH variable. /// The cd builtin. Changes the current directory to the one specified or to $HOME if none is /// specified. The directory can be relative to any directory in the CDPATH variable. int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; int optind; int retval = parse_help_only_cmd_opts(opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { builtin_print_help(parser, streams, cmd, streams.out); return STATUS_CMD_OK; } env_var_t dir_in; wcstring dir; if (argv[optind]) { dir_in = env_var_t(L"", argv[optind]); // unamed var } else { auto maybe_dir_in = env_get(L"HOME"); if (maybe_dir_in.missing_or_empty()) { streams.err.append_format(_(L"%ls: Could not find home directory\n"), cmd); return STATUS_CMD_ERROR; } dir_in = std::move(*maybe_dir_in); } if (!path_get_cdpath(dir_in, &dir)) { if (errno == ENOTDIR) { streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd, dir_in.as_string().c_str()); } else if (errno == ENOENT) { streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"), cmd, dir_in.as_string().c_str()); } else if (errno == EROTTEN) { streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), cmd, dir_in.as_string().c_str()); } else { streams.err.append_format(_(L"%ls: Unknown error trying to locate directory '%ls'\n"), cmd, dir_in.as_string().c_str()); } if (!shell_is_interactive()) streams.err.append(parser.current_line()); return STATUS_CMD_ERROR; } if (wchdir(dir) != 0) { struct stat buffer; int status; status = wstat(dir, &buffer); if (!status && S_ISDIR(buffer.st_mode)) { streams.err.append_format(_(L"%ls: Permission denied: '%ls'\n"), cmd, dir.c_str()); } else { streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd, dir.c_str()); } if (!shell_is_interactive()) { streams.err.append(parser.current_line()); } return STATUS_CMD_ERROR; } if (!env_set_pwd()) { streams.err.append_format(_(L"%ls: Could not set PWD variable\n"), cmd); return STATUS_CMD_ERROR; } return STATUS_CMD_OK; }
/** @SYMTestCaseID SYSLIB-STDLIB-CT-1092 @SYMTestCaseDesc Tests for operations on pipes @SYMTestPriority High @SYMTestActions Tests for command line arguments,directory operations,environment variables. @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ int do_main(int argc, wchar_t* argv[]) { test_Data; int i; wchar_t* var; wchar_t* varname; wchar_t cwd[MAXPATHLEN]; char buf[200]; char buf1[200]; test_Title("PIPE"); test_Next("Command line arguments"); test(argc>0); test(argv!=0); printf(" argc=%d\r\n", argc); for (i=0; i<argc; i++) { test(argv[i]!=0); test(-1 != wcstombs(buf, argv[i], sizeof(buf))); printf(" argv[%d]=\"%s\" length %d\r\n", i, buf, strlen(buf)); } printf("\r\n"); test_Next("Current working directory"); var=wgetcwd(cwd,sizeof(cwd)/2); test(var==cwd); test(-1 != wcstombs(buf, cwd, sizeof(buf))); printf(" %s\r\n\n", buf); test_Next("Change directory"); i=wchdir((wchar_t*)L"z:/system"); test(i==0); var=wgetcwd(cwd,sizeof(cwd)/2); test(var==cwd); test(-1 != wcstombs(buf, cwd, sizeof(buf))); printf(" %s\r\n\n", buf); test_Next("Environment variables"); varname=(wchar_t*)L"CLASSPATH"; var=wgetenv(varname); test(var!=0); test(-1 != wcstombs(buf, var, sizeof(buf))); test(-1 != wcstombs(buf1, varname, sizeof(buf1))); printf(" %s=%s\r\n", buf1, buf); varname=(wchar_t*)"VARIABLE2"; var=wgetenv(varname); if (var!=0) { test(-1 != wcstombs(buf, var, sizeof(buf))); test(-1 != wcstombs(buf1, varname, sizeof(buf1))); printf(" %s=%s\r\n", buf1, buf); wunsetenv((wchar_t*)"VARIABLE2"); } varname=(wchar_t*)L"USER"; var=wgetenv(varname); test(var!=0); test(-1 != wcstombs(buf, var, sizeof(buf))); test(-1 != wcstombs(buf1, varname, sizeof(buf1))); printf(" %s=%s\r\n", buf1, buf); sleep(5); test_Close(); return 0; }