1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved
|
---|
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 <str.h>
|
---|
32 | #include <stdarg.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <stdarg.h>
|
---|
35 |
|
---|
36 | #include "config.h"
|
---|
37 | #include "errors.h"
|
---|
38 | #include "util.h"
|
---|
39 |
|
---|
40 | extern volatile int cli_errno;
|
---|
41 |
|
---|
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 |
|
---|
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->cwd = (char *) realloc(usr->cwd, PATH_MAX);
|
---|
57 | if (NULL == usr->cwd) {
|
---|
58 | cli_error(CL_ENOMEM, "Can not allocate cwd");
|
---|
59 | cli_errno = CL_ENOMEM;
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 | if (!getcwd(usr->cwd, PATH_MAX))
|
---|
63 | snprintf(usr->cwd, PATH_MAX, "(unknown)");
|
---|
64 |
|
---|
65 | if (usr->prompt)
|
---|
66 | free(usr->prompt);
|
---|
67 | asprintf(&usr->prompt, "%s # ", usr->cwd);
|
---|
68 |
|
---|
69 | return 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|