Use `errno_t` in all uspace and kernel code.
[helenos.git] / uspace / app / bdsh / cmds / builtins / cd / cd.c
blob673d6f65ab214b188f4e6e663890c67f71ebf545
1 /*
2 * Copyright (c) 2008 Tim Post
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <str.h>
32 #include <errno.h>
33 #include <vfs/vfs.h>
35 #include "util.h"
36 #include "errors.h"
37 #include "entry.h"
38 #include "cmds.h"
39 #include "cd.h"
41 static const char *cmdname = "cd";
43 /* Previous directory variables.
45 * Declaring them static to avoid many "== NULL" checks.
46 * PATH_MAX is not that big to cause any problems with memory overhead.
48 static char previous_directory[PATH_MAX] = "";
49 static char previous_directory_tmp[PATH_MAX];
50 static bool previous_directory_valid = true;
51 static bool previous_directory_set = false;
53 static errno_t chdir_and_remember(const char *new_dir)
56 errno_t rc = vfs_cwd_get(previous_directory_tmp, PATH_MAX);
57 previous_directory_valid = (rc == EOK);
58 previous_directory_set = true;
60 rc = vfs_cwd_set(new_dir);
61 if (rc != EOK)
62 return rc;
64 str_cpy(previous_directory, PATH_MAX, previous_directory_tmp);
65 return EOK;
68 void help_cmd_cd(unsigned int level)
70 if (level == HELP_SHORT) {
71 printf("`%s' changes the current working directory.\n", cmdname);
72 } else {
73 printf(
74 " %s <directory>\n"
75 " Change directory to <directory>, e.g `%s /sbin'\n",
76 cmdname, cmdname);
79 return;
83 /* This is a very rudamentary 'cd' command. It is not 'link smart' (yet) */
85 int cmd_cd(char **argv, cliuser_t *usr)
87 int argc;
88 errno_t rc = EOK;
90 argc = cli_count_args(argv);
92 /* Handle cd -- -. Override to switch to a directory named '-' */
93 bool hyphen_override = false;
94 char *target_directory = argv[1];
95 if (argc == 3) {
96 if (!str_cmp(argv[1], "--")) {
97 hyphen_override = true;
98 argc--;
99 target_directory = argv[2];
103 /* We don't yet play nice with whitespace, a getopt implementation should
104 * protect "quoted\ destination" as a single argument. Its not our job to
105 * look for && || or redirection as the tokenizer should have done that
106 * (currently, it does not) */
108 if (argc > 2) {
109 cli_error(CL_EFAIL, "Too many arguments to `%s'", cmdname);
110 return CMD_FAILURE;
113 if (argc < 2) {
114 printf("%s - no directory specified. Try `help %s extended'\n",
115 cmdname, cmdname);
116 return CMD_FAILURE;
119 /* We have the correct # of arguments */
120 // TODO: handle tidle (~) expansion? */
122 /* Handle 'cd -' first. */
123 if (!str_cmp(target_directory, "-") && !hyphen_override) {
124 if (!previous_directory_valid) {
125 cli_error(CL_EFAIL, "Cannot switch to previous directory");
126 return CMD_FAILURE;
128 if (!previous_directory_set) {
129 cli_error(CL_EFAIL, "No previous directory to switch to");
130 return CMD_FAILURE;
132 char *prev_dup = str_dup(previous_directory);
133 if (prev_dup == NULL) {
134 cli_error(CL_ENOMEM, "Cannot switch to previous directory");
135 return CMD_FAILURE;
137 rc = chdir_and_remember(prev_dup);
138 free(prev_dup);
139 } else {
140 rc = chdir_and_remember(target_directory);
143 if (rc == 0) {
144 cli_set_prompt(usr);
145 return CMD_SUCCESS;
146 } else {
147 switch (rc) {
148 case ENOMEM:
149 cli_error(CL_EFAIL, "Destination path too long");
150 break;
151 case ENOENT:
152 cli_error(CL_ENOENT, "Invalid directory `%s'", target_directory);
153 break;
154 default:
155 cli_error(CL_EFAIL, "Unable to change to `%s'", target_directory);
156 break;
160 return CMD_FAILURE;