source: mainline/uspace/app/bdsh/cmds/builtins/cd/cd.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[36ab7c7]1/*
2 * Copyright (c) 2008 Tim Post
[216d6fc]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
[36ab7c7]6 * modification, are permitted provided that the following conditions
7 * are met:
[216d6fc]8 *
[36ab7c7]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.
[216d6fc]16 *
[36ab7c7]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.
[216d6fc]27 */
28
29#include <stdio.h>
30#include <stdlib.h>
[19f857a]31#include <str.h>
[216d6fc]32#include <errno.h>
[d96d9bc]33#include <vfs/vfs.h>
[216d6fc]34
35#include "util.h"
36#include "errors.h"
37#include "entry.h"
38#include "cmds.h"
39#include "cd.h"
40
[a000878c]41static const char *cmdname = "cd";
[216d6fc]42
[7c3fb9b]43/*
44 * Previous directory variables.
[35a35651]45 *
46 * Declaring them static to avoid many "== NULL" checks.
47 * PATH_MAX is not that big to cause any problems with memory overhead.
48 */
49static char previous_directory[PATH_MAX] = "";
50static char previous_directory_tmp[PATH_MAX];
51static bool previous_directory_valid = true;
52static bool previous_directory_set = false;
53
[b7fd2a0]54static errno_t chdir_and_remember(const char *new_dir)
[d96d9bc]55{
[35a35651]56
[b7fd2a0]57 errno_t rc = vfs_cwd_get(previous_directory_tmp, PATH_MAX);
[d96d9bc]58 previous_directory_valid = (rc == EOK);
[35a35651]59 previous_directory_set = true;
60
[d96d9bc]61 rc = vfs_cwd_set(new_dir);
62 if (rc != EOK)
63 return rc;
[35a35651]64
65 str_cpy(previous_directory, PATH_MAX, previous_directory_tmp);
66 return EOK;
67}
68
[809813d]69void help_cmd_cd(unsigned int level)
[216d6fc]70{
71 if (level == HELP_SHORT) {
72 printf("`%s' changes the current working directory.\n", cmdname);
73 } else {
74 printf(
[1433ecda]75 " %s <directory>\n"
76 " Change directory to <directory>, e.g `%s /sbin'\n",
77 cmdname, cmdname);
[216d6fc]78 }
79
[809813d]80 return;
[216d6fc]81}
82
83/* This is a very rudamentary 'cd' command. It is not 'link smart' (yet) */
84
[809813d]85int cmd_cd(char **argv, cliuser_t *usr)
[216d6fc]86{
[d5c1051]87 int argc;
[b7fd2a0]88 errno_t rc = EOK;
[216d6fc]89
[43e02a6]90 argc = cli_count_args(argv);
[216d6fc]91
[089385e8]92 /* Handle cd -- -. Override to switch to a directory named '-' */
93 bool hyphen_override = false;
[35a35651]94 char *target_directory = argv[1];
[089385e8]95 if (argc == 3) {
[35a35651]96 if (!str_cmp(argv[1], "--")) {
[089385e8]97 hyphen_override = true;
98 argc--;
[35a35651]99 target_directory = argv[2];
[089385e8]100 }
101 }
102
[7c3fb9b]103 /*
104 * We don't yet play nice with whitespace, a getopt implementation should
[216d6fc]105 * protect "quoted\ destination" as a single argument. Its not our job to
106 * look for && || or redirection as the tokenizer should have done that
[7c3fb9b]107 * (currently, it does not)
108 */
[216d6fc]109
110 if (argc > 2) {
111 cli_error(CL_EFAIL, "Too many arguments to `%s'", cmdname);
112 return CMD_FAILURE;
113 }
114
115 if (argc < 2) {
116 printf("%s - no directory specified. Try `help %s extended'\n",
[1433ecda]117 cmdname, cmdname);
[216d6fc]118 return CMD_FAILURE;
119 }
120
[089385e8]121 /* We have the correct # of arguments */
122 // TODO: handle tidle (~) expansion? */
[216d6fc]123
[089385e8]124 /* Handle 'cd -' first. */
[35a35651]125 if (!str_cmp(target_directory, "-") && !hyphen_override) {
126 if (!previous_directory_valid) {
127 cli_error(CL_EFAIL, "Cannot switch to previous directory");
[089385e8]128 return CMD_FAILURE;
129 }
[35a35651]130 if (!previous_directory_set) {
[089385e8]131 cli_error(CL_EFAIL, "No previous directory to switch to");
132 return CMD_FAILURE;
133 }
[35a35651]134 char *prev_dup = str_dup(previous_directory);
135 if (prev_dup == NULL) {
136 cli_error(CL_ENOMEM, "Cannot switch to previous directory");
137 return CMD_FAILURE;
138 }
139 rc = chdir_and_remember(prev_dup);
140 free(prev_dup);
[089385e8]141 } else {
[35a35651]142 rc = chdir_and_remember(target_directory);
[089385e8]143 }
[216d6fc]144
145 if (rc == 0) {
[0c4f46a]146 cli_set_prompt(usr);
[216d6fc]147 return CMD_SUCCESS;
148 } else {
149 switch (rc) {
150 case ENOMEM:
151 cli_error(CL_EFAIL, "Destination path too long");
152 break;
153 case ENOENT:
[35a35651]154 cli_error(CL_ENOENT, "Invalid directory `%s'", target_directory);
[216d6fc]155 break;
156 default:
[35a35651]157 cli_error(CL_EFAIL, "Unable to change to `%s'", target_directory);
[216d6fc]158 break;
159 }
160 }
161
162 return CMD_FAILURE;
163}
Note: See TracBrowser for help on using the repository browser.