Rev 3316 | Rev 3346 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3316 | Rev 3345 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
2 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
3 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
3 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
4 | * All rights reserved by all copyright holders. |
4 | * All rights reserved by all copyright holders. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are met: |
7 | * modification, are permitted provided that the following conditions are met: |
8 | * |
8 | * |
9 | * Redistributions of source code must retain the above copyright notice, this |
9 | * Redistributions of source code must retain the above copyright notice, this |
10 | * list of conditions and the following disclaimer. |
10 | * list of conditions and the following disclaimer. |
11 | * |
11 | * |
12 | * Redistributions in binary form must reproduce the above copyright notice, |
12 | * Redistributions in binary form must reproduce the above copyright notice, |
13 | * this list of conditions and the following disclaimer in the documentation |
13 | * this list of conditions and the following disclaimer in the documentation |
14 | * and/or other materials provided with the distribution. |
14 | * and/or other materials provided with the distribution. |
15 | * |
15 | * |
16 | * Neither the name of the original program's authors nor the names of its |
16 | * Neither the name of the original program's authors nor the names of its |
17 | * contributors may be used to endorse or promote products derived from this |
17 | * contributors may be used to endorse or promote products derived from this |
18 | * software without specific prior written permission. |
18 | * software without specific prior written permission. |
19 | * |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | * POSSIBILITY OF SUCH DAMAGE. |
30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /* NOTES: |
33 | /* NOTES: |
34 | * 1 - Various functions were adapted from FreeBSD (copyright holders noted above) |
34 | * 1 - Various functions were adapted from FreeBSD (copyright holders noted above) |
35 | * these functions are identified with comments. |
35 | * these functions are identified with comments. |
36 | * |
36 | * |
37 | * 2 - Some of these have since appeared in libc. They remain here for various |
37 | * 2 - Some of these have since appeared in libc. They remain here for various |
38 | * reasons, such as the eventual integration of garbage collection for things |
38 | * reasons, such as the eventual integration of garbage collection for things |
39 | * that allocate memory and don't automatically free it. |
39 | * that allocate memory and don't automatically free it. |
40 | * |
40 | * |
41 | * 3 - Things that expect a pointer to an allocated string do _no_ sanity checking |
41 | * 3 - Things that expect a pointer to an allocated string do _no_ sanity checking |
42 | * if developing on a simulator with no debugger, take care :) |
42 | * if developing on a simulator with no debugger, take care :) |
43 | */ |
43 | */ |
44 | 44 | ||
45 | #include <stdio.h> |
45 | #include <stdio.h> |
46 | #include <string.h> |
46 | #include <string.h> |
47 | #include <stdarg.h> |
47 | #include <stdarg.h> |
48 | #include <stdlib.h> |
48 | #include <stdlib.h> |
- | 49 | #include <stdarg.h> |
|
49 | 50 | ||
50 | #include "config.h" |
51 | #include "config.h" |
51 | #include "errors.h" |
52 | #include "errors.h" |
52 | #include "util.h" |
53 | #include "util.h" |
53 | 54 | ||
54 | - | ||
55 | /* some platforms do not have strdup, implement it here */ |
55 | /* some platforms do not have strdup, implement it here. |
- | 56 | * Returns a pointer to an allocated string or NULL on failure */ |
|
56 | char * cli_strdup(const char *s1) |
57 | char * cli_strdup(const char *s1) |
57 | { |
58 | { |
58 | size_t len = strlen(s1) + 1; |
59 | size_t len = strlen(s1) + 1; |
59 | void *ret = malloc(len); |
60 | void *ret = malloc(len); |
60 | 61 | ||
61 | if (ret == NULL) |
62 | if (ret == NULL) |
62 | return (char *) NULL; |
63 | return (char *) NULL; |
63 | 64 | ||
64 | return (char *) memcpy(ret, s1, len); |
65 | return (char *) memcpy(ret, s1, len); |
65 | } |
66 | } |
66 | 67 | ||
- | 68 | /* |
|
- | 69 | * Take a previously allocated string (s1), re-size it to accept s2 and copy |
|
- | 70 | * the contents of s2 into s1. |
|
- | 71 | * Return -1 on failure, or the length of the copied string on success. |
|
- | 72 | */ |
|
- | 73 | int cli_redup(char **s1, const char *s2) |
|
- | 74 | { |
|
- | 75 | size_t len = strlen(s2) + 1; |
|
- | 76 | ||
- | 77 | if (! len) |
|
- | 78 | return -1; |
|
- | 79 | ||
- | 80 | *s1 = realloc(*s1, len); |
|
- | 81 | ||
- | 82 | if (*s1 == NULL) |
|
- | 83 | return -1; |
|
- | 84 | ||
- | 85 | memset(*s1, 0, sizeof(*s1)); |
|
- | 86 | memcpy(*s1, s2, len); |
|
- | 87 | return (int) len; |
|
- | 88 | } |
|
- | 89 | ||
- | 90 | /* An asprintf() for concantenating paths. Allocates the system PATH_MAX value, |
|
- | 91 | * expands the formatted string and re-sizes the block s1 points to accordingly. |
|
- | 92 | * |
|
- | 93 | * Returns the length of the new s1 on success, -1 on failure. On failure, an |
|
- | 94 | * attempt is made to return s1 unmodified for sanity, in this case 0 is returned. |
|
- | 95 | * to indicate that s1 was not modified. |
|
- | 96 | * |
|
- | 97 | * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */ |
|
- | 98 | int cli_psprintf(char **s1, const char *fmt, ...) |
|
- | 99 | { |
|
- | 100 | va_list ap; |
|
- | 101 | size_t needed, base = PATH_MAX + 1; |
|
- | 102 | char *tmp = (char *) malloc(base); |
|
- | 103 | ||
- | 104 | if (NULL == tmp) |
|
- | 105 | return -1; |
|
- | 106 | ||
- | 107 | char *orig = *s1; |
|
- | 108 | ||
- | 109 | memset(tmp, 0, sizeof(tmp)); |
|
- | 110 | va_start(ap, fmt); |
|
- | 111 | vsnprintf(tmp, base, fmt, ap); |
|
- | 112 | va_end(ap); |
|
- | 113 | needed = strlen(tmp) + 1; |
|
- | 114 | *s1 = realloc(*s1, needed); |
|
- | 115 | if (NULL == *s1) { |
|
- | 116 | *s1 = realloc(*s1, strlen(orig) + 1); |
|
- | 117 | if (NULL == *s1) { |
|
- | 118 | free(tmp); |
|
- | 119 | return -1; |
|
- | 120 | } |
|
- | 121 | memset(*s1, 0, sizeof(*s1)); |
|
- | 122 | memcpy(*s1, orig, strlen(orig) + 1); |
|
- | 123 | free(tmp); |
|
- | 124 | return 0; |
|
- | 125 | } |
|
- | 126 | memset(*s1, 0, sizeof(*s1)); |
|
- | 127 | memcpy(*s1, tmp, needed); |
|
- | 128 | free(tmp); |
|
- | 129 | ||
- | 130 | return (int) needed; |
|
- | 131 | } |
|
- | 132 | ||
67 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
133 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
68 | char * cli_strtok_r(char *s, const char *delim, char **last) |
134 | char * cli_strtok_r(char *s, const char *delim, char **last) |
69 | { |
135 | { |
70 | char *spanp, *tok; |
136 | char *spanp, *tok; |
71 | int c, sc; |
137 | int c, sc; |
72 | 138 | ||
73 | if (s == NULL && (s = *last) == NULL) |
139 | if (s == NULL && (s = *last) == NULL) |
74 | return (NULL); |
140 | return (NULL); |
75 | 141 | ||
76 | cont: |
142 | cont: |
77 | c = *s++; |
143 | c = *s++; |
78 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
144 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
79 | if (c == sc) |
145 | if (c == sc) |
80 | goto cont; |
146 | goto cont; |
81 | } |
147 | } |
82 | 148 | ||
83 | if (c == 0) { /* no non-delimiter characters */ |
149 | if (c == 0) { /* no non-delimiter characters */ |
84 | *last = NULL; |
150 | *last = NULL; |
85 | return (NULL); |
151 | return (NULL); |
86 | } |
152 | } |
87 | 153 | ||
88 | tok = s - 1; |
154 | tok = s - 1; |
89 | 155 | ||
90 | for (;;) { |
156 | for (;;) { |
91 | c = *s++; |
157 | c = *s++; |
92 | spanp = (char *)delim; |
158 | spanp = (char *)delim; |
93 | do { |
159 | do { |
94 | if ((sc = *spanp++) == c) { |
160 | if ((sc = *spanp++) == c) { |
95 | if (c == 0) |
161 | if (c == 0) |
96 | s = NULL; |
162 | s = NULL; |
97 | else |
163 | else |
98 | s[-1] = '\0'; |
164 | s[-1] = '\0'; |
99 | *last = s; |
165 | *last = s; |
100 | return (tok); |
166 | return (tok); |
101 | } |
167 | } |
102 | } while (sc != 0); |
168 | } while (sc != 0); |
103 | } |
169 | } |
104 | } |
170 | } |
105 | 171 | ||
106 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
172 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
107 | char * cli_strtok(char *s, const char *delim) |
173 | char * cli_strtok(char *s, const char *delim) |
108 | { |
174 | { |
109 | static char *last; |
175 | static char *last; |
110 | 176 | ||
111 | return (cli_strtok_r(s, delim, &last)); |
177 | return (cli_strtok_r(s, delim, &last)); |
112 | } |
178 | } |
113 | 179 |