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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b00255a was 35a35651, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Previous work-dir handling only in Bdsh, not in libc

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