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 |
|
---|
41 | static const char *cmdname = "cd";
|
---|
42 |
|
---|
43 | void 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 |
|
---|
59 | int cmd_cd(char **argv, cliuser_t *usr)
|
---|
60 | {
|
---|
61 | int argc, rc = 0;
|
---|
62 |
|
---|
63 | argc = cli_count_args(argv);
|
---|
64 |
|
---|
65 | /* We don't yet play nice with whitespace, a getopt implementation should
|
---|
66 | * protect "quoted\ destination" as a single argument. Its not our job to
|
---|
67 | * look for && || or redirection as the tokenizer should have done that
|
---|
68 | * (currently, it does not) */
|
---|
69 |
|
---|
70 | if (argc > 2) {
|
---|
71 | cli_error(CL_EFAIL, "Too many arguments to `%s'", cmdname);
|
---|
72 | return CMD_FAILURE;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (argc < 2) {
|
---|
76 | printf("%s - no directory specified. Try `help %s extended'\n",
|
---|
77 | cmdname, cmdname);
|
---|
78 | return CMD_FAILURE;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* We have the correct # of arguments
|
---|
82 | * TODO: handle tidle (~) expansion? */
|
---|
83 |
|
---|
84 | rc = chdir(argv[1]);
|
---|
85 |
|
---|
86 | if (rc == 0) {
|
---|
87 | cli_set_prompt(usr);
|
---|
88 | return CMD_SUCCESS;
|
---|
89 | } else {
|
---|
90 | switch (rc) {
|
---|
91 | case ENOMEM:
|
---|
92 | cli_error(CL_EFAIL, "Destination path too long");
|
---|
93 | break;
|
---|
94 | case ENOENT:
|
---|
95 | cli_error(CL_ENOENT, "Invalid directory `%s'", argv[1]);
|
---|
96 | break;
|
---|
97 | default:
|
---|
98 | cli_error(CL_EFAIL, "Unable to change to `%s'", argv[1]);
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return CMD_FAILURE;
|
---|
104 | }
|
---|