Rev 3813 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3813 | post | 1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved |
3265 | post | 2 | * |
3 | * Redistribution and use in source and binary forms, with or without |
||
4 | * modification, are permitted provided that the following conditions are met: |
||
5 | * |
||
6 | * Redistributions of source code must retain the above copyright notice, this |
||
7 | * list of conditions and the following disclaimer. |
||
8 | * |
||
9 | * Redistributions in binary form must reproduce the above copyright notice, |
||
10 | * this list of conditions and the following disclaimer in the documentation |
||
11 | * and/or other materials provided with the distribution. |
||
12 | * |
||
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 |
||
15 | * software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
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 |
||
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 |
||
27 | * POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdio.h> |
||
31 | #include <string.h> |
||
32 | #include <stdarg.h> |
||
33 | #include <stdlib.h> |
||
3345 | post | 34 | #include <stdarg.h> |
3265 | post | 35 | |
36 | #include "config.h" |
||
37 | #include "errors.h" |
||
38 | #include "util.h" |
||
39 | |||
3366 | post | 40 | extern volatile int cli_errno; |
41 | |||
3376 | post | 42 | /* Count and return the # of elements in an array */ |
43 | unsigned int cli_count_args(char **args) |
||
44 | { |
||
45 | unsigned int i; |
||
46 | |||
47 | for (i=0; args[i] != NULL; i++); |
||
48 | return i; |
||
49 | } |
||
50 | |||
3377 | post | 51 | /* (re)allocates memory to store the current working directory, gets |
52 | * and updates the current working directory, formats the prompt |
||
53 | * string */ |
||
54 | unsigned int cli_set_prompt(cliuser_t *usr) |
||
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); |
||
65 | if (NULL == usr->cwd) { |
||
66 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
||
67 | cli_errno = CL_ENOMEM; |
||
68 | return 1; |
||
69 | } |
||
70 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
||
71 | |||
72 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
||
73 | |||
74 | if (NULL == usr->cwd) |
||
75 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
||
76 | |||
3481 | post | 77 | asprintf(&usr->prompt, "%s # ", usr->cwd); |
3377 | post | 78 | |
79 | return 0; |
||
80 | } |
||
81 | |||
82 |