Rev 4341 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4341 | Rev 4691 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved |
2 | * |
2 | * |
3 | * Redistribution and use in source and binary forms, with or without |
3 | * Redistribution and use in source and binary forms, with or without |
4 | * modification, are permitted provided that the following conditions are met: |
4 | * modification, are permitted provided that the following conditions are met: |
5 | * |
5 | * |
6 | * Redistributions of source code must retain the above copyright notice, this |
6 | * Redistributions of source code must retain the above copyright notice, this |
7 | * list of conditions and the following disclaimer. |
7 | * list of conditions and the following disclaimer. |
8 | * |
8 | * |
9 | * Redistributions in binary form must reproduce the above copyright notice, |
9 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * this list of conditions and the following disclaimer in the documentation |
10 | * this list of conditions and the following disclaimer in the documentation |
11 | * and/or other materials provided with the distribution. |
11 | * and/or other materials provided with the distribution. |
12 | * |
12 | * |
13 | * Neither the name of the original program's authors nor the names of its |
13 | * Neither the name of the original program's authors nor the names of its |
14 | * contributors may be used to endorse or promote products derived from this |
14 | * contributors may be used to endorse or promote products derived from this |
15 | * software without specific prior written permission. |
15 | * software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * POSSIBILITY OF SUCH DAMAGE. |
27 | * POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #include <stdio.h> |
30 | #include <stdio.h> |
31 | #include <string.h> |
31 | #include <string.h> |
32 | #include <stdarg.h> |
32 | #include <stdarg.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <stdarg.h> |
34 | #include <stdarg.h> |
35 | 35 | ||
36 | #include "config.h" |
36 | #include "config.h" |
37 | #include "errors.h" |
37 | #include "errors.h" |
38 | #include "util.h" |
38 | #include "util.h" |
39 | 39 | ||
40 | extern volatile int cli_errno; |
40 | extern volatile int cli_errno; |
41 | 41 | ||
42 | /* Count and return the # of elements in an array */ |
42 | /* Count and return the # of elements in an array */ |
43 | unsigned int cli_count_args(char **args) |
43 | unsigned int cli_count_args(char **args) |
44 | { |
44 | { |
45 | unsigned int i; |
45 | unsigned int i; |
46 | 46 | ||
47 | for (i=0; args[i] != NULL; i++); |
47 | for (i=0; args[i] != NULL; i++); |
48 | return i; |
48 | return i; |
49 | } |
49 | } |
50 | 50 | ||
51 | /* (re)allocates memory to store the current working directory, gets |
51 | /* (re)allocates memory to store the current working directory, gets |
52 | * and updates the current working directory, formats the prompt |
52 | * and updates the current working directory, formats the prompt |
53 | * string */ |
53 | * string */ |
54 | unsigned int cli_set_prompt(cliuser_t *usr) |
54 | unsigned int cli_set_prompt(cliuser_t *usr) |
55 | { |
55 | { |
56 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
- | |
57 | if (NULL == usr->prompt) { |
- | |
58 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
- | |
59 | cli_errno = CL_ENOMEM; |
- | |
60 | return 1; |
- | |
61 | } |
- | |
62 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
- | |
63 | - | ||
64 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
56 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
65 | if (NULL == usr->cwd) { |
57 | if (NULL == usr->cwd) { |
66 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
58 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
67 | cli_errno = CL_ENOMEM; |
59 | cli_errno = CL_ENOMEM; |
68 | return 1; |
60 | return 1; |
69 | } |
61 | } |
70 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
- | |
71 | - | ||
72 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
62 | if (!getcwd(usr->cwd, PATH_MAX)) |
73 | - | ||
74 | if (NULL == usr->cwd) |
- | |
75 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
63 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
76 | 64 | ||
- | 65 | if (usr->prompt) |
|
- | 66 | free(usr->prompt); |
|
77 | asprintf(&usr->prompt, "%s # ", usr->cwd); |
67 | asprintf(&usr->prompt, "%s # ", usr->cwd); |
78 | 68 | ||
79 | return 0; |
69 | return 0; |
80 | } |
70 | } |
81 | 71 | ||
82 | 72 | ||
83 | 73 |