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

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

Change to previous directory with `cd -' in bdsh

  • Property mode set to 100644
File size: 3.8 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
43void help_cmd_cd(unsigned int level)
44{
45 if (level == HELP_SHORT) {
46 printf("`%s' changes the current working directory.\n", cmdname);
47 } else {
48 printf(
49 " %s <directory>\n"
50 " Change directory to <directory>, e.g `%s /sbin'\n",
51 cmdname, cmdname);
52 }
53
54 return;
55}
56
57/* This is a very rudamentary 'cd' command. It is not 'link smart' (yet) */
58
59int cmd_cd(char **argv, cliuser_t *usr)
60{
61 int argc, rc = 0;
62
63 argc = cli_count_args(argv);
64
65 /* Handle cd -- -. Override to switch to a directory named '-' */
66 bool hyphen_override = false;
67 if (argc == 3) {
68 if(!str_cmp(argv[1], "--")) {
69 hyphen_override = true;
70 argc--;
71 }
72 }
73
74 /* We don't yet play nice with whitespace, a getopt implementation should
75 * protect "quoted\ destination" as a single argument. Its not our job to
76 * look for && || or redirection as the tokenizer should have done that
77 * (currently, it does not) */
78
79 if (argc > 2) {
80 cli_error(CL_EFAIL, "Too many arguments to `%s'", cmdname);
81 return CMD_FAILURE;
82 }
83
84 if (argc < 2) {
85 printf("%s - no directory specified. Try `help %s extended'\n",
86 cmdname, cmdname);
87 return CMD_FAILURE;
88 }
89
90 /* We have the correct # of arguments */
91 // TODO: handle tidle (~) expansion? */
92
93 /* Handle 'cd -' first. */
94 if (!str_cmp(argv[1], "-") && !hyphen_override) {
95 char *buffer = (char *) malloc(PATH_MAX);
96 if (!buffer) {
97 cli_error(CL_ENOMEM, "Cannot switch to previous directory");
98 return CMD_FAILURE;
99 }
100 memset(buffer, 0, PATH_MAX);
101 getprevwd(buffer, PATH_MAX);
102 if (*buffer == '\0') {
103 cli_error(CL_EFAIL, "No previous directory to switch to");
104 free(buffer);
105 return CMD_FAILURE;
106 } else {
107 rc = chdir(buffer);
108 free(buffer);
109 }
110 } else if (hyphen_override) {
111 /* Handles 'cd -- <dirname>'.
112 * Override for directory named '-'.
113 */
114 rc = chdir(argv[2]);
115 } else {
116 rc = chdir(argv[1]);
117 }
118
119 if (rc == 0) {
120 cli_set_prompt(usr);
121 return CMD_SUCCESS;
122 } else {
123 switch (rc) {
124 case ENOMEM:
125 cli_error(CL_EFAIL, "Destination path too long");
126 break;
127 case ENOENT:
128 cli_error(CL_ENOENT, "Invalid directory `%s'", argv[1]);
129 break;
130 default:
131 cli_error(CL_EFAIL, "Unable to change to `%s'", argv[1]);
132 break;
133 }
134 }
135
136 return CMD_FAILURE;
137}
Note: See TracBrowser for help on using the repository browser.