Rev 3345 | Rev 3366 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3345 | Rev 3346 | ||
---|---|---|---|
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 | #include <stdarg.h> |
50 | 50 | ||
51 | #include "config.h" |
51 | #include "config.h" |
52 | #include "errors.h" |
52 | #include "errors.h" |
53 | #include "util.h" |
53 | #include "util.h" |
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 | * Returns a pointer to an allocated string or NULL on failure */ |
57 | char * cli_strdup(const char *s1) |
57 | char * cli_strdup(const char *s1) |
58 | { |
58 | { |
59 | size_t len = strlen(s1) + 1; |
59 | size_t len = strlen(s1) + 1; |
60 | void *ret = malloc(len); |
60 | void *ret = malloc(len); |
61 | 61 | ||
62 | if (ret == NULL) |
62 | if (ret == NULL) |
63 | return (char *) NULL; |
63 | return (char *) NULL; |
64 | 64 | ||
65 | return (char *) memcpy(ret, s1, len); |
65 | return (char *) memcpy(ret, s1, len); |
66 | } |
66 | } |
67 | 67 | ||
68 | /* |
68 | /* |
69 | * Take a previously allocated string (s1), re-size it to accept s2 and copy |
69 | * Take a previously allocated string (s1), re-size it to accept s2 and copy |
70 | * the contents of s2 into s1. |
70 | * the contents of s2 into s1. |
71 | * Return -1 on failure, or the length of the copied string on success. |
71 | * Return -1 on failure, or the length of the copied string on success. |
72 | */ |
72 | */ |
73 | int cli_redup(char **s1, const char *s2) |
73 | int cli_redup(char **s1, const char *s2) |
74 | { |
74 | { |
75 | size_t len = strlen(s2) + 1; |
75 | size_t len = strlen(s2) + 1; |
76 | 76 | ||
77 | if (! len) |
77 | if (! len) |
78 | return -1; |
78 | return -1; |
79 | 79 | ||
80 | *s1 = realloc(*s1, len); |
80 | *s1 = realloc(*s1, len); |
81 | 81 | ||
82 | if (*s1 == NULL) |
82 | if (*s1 == NULL) |
83 | return -1; |
83 | return -1; |
84 | 84 | ||
85 | memset(*s1, 0, sizeof(*s1)); |
85 | memset(*s1, 0, sizeof(*s1)); |
86 | memcpy(*s1, s2, len); |
86 | memcpy(*s1, s2, len); |
87 | return (int) len; |
87 | return (int) len; |
88 | } |
88 | } |
89 | 89 | ||
90 | /* An asprintf() for concantenating paths. Allocates the system PATH_MAX value, |
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. |
91 | * expands the formatted string and re-sizes the block s1 points to accordingly. |
92 | * |
92 | * |
93 | * Returns the length of the new s1 on success, -1 on failure. On failure, an |
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. |
94 | * attempt is made to return s1 unmodified for sanity, in this case 0 is returned. |
95 | * to indicate that s1 was not modified. |
95 | * to indicate that s1 was not modified. |
96 | * |
96 | * |
97 | * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */ |
97 | * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */ |
98 | int cli_psprintf(char **s1, const char *fmt, ...) |
98 | int cli_psprintf(char **s1, const char *fmt, ...) |
99 | { |
99 | { |
100 | va_list ap; |
100 | va_list ap; |
101 | size_t needed, base = PATH_MAX + 1; |
101 | size_t needed, base = PATH_MAX + 1; |
102 | char *tmp = (char *) malloc(base); |
102 | char *tmp = (char *) malloc(base); |
103 | 103 | ||
104 | if (NULL == tmp) |
104 | if (NULL == tmp) |
105 | return -1; |
105 | return -1; |
106 | 106 | ||
107 | char *orig = *s1; |
107 | char *orig = *s1; |
108 | 108 | ||
109 | memset(tmp, 0, sizeof(tmp)); |
109 | memset(tmp, 0, sizeof(tmp)); |
110 | va_start(ap, fmt); |
110 | va_start(ap, fmt); |
111 | vsnprintf(tmp, base, fmt, ap); |
111 | vsnprintf(tmp, base, fmt, ap); |
112 | va_end(ap); |
112 | va_end(ap); |
113 | needed = strlen(tmp) + 1; |
113 | needed = strlen(tmp) + 1; |
114 | *s1 = realloc(*s1, needed); |
114 | *s1 = realloc(*s1, needed); |
115 | if (NULL == *s1) { |
115 | if (NULL == *s1) { |
116 | *s1 = realloc(*s1, strlen(orig) + 1); |
116 | *s1 = realloc(*s1, strlen(orig) + 1); |
117 | if (NULL == *s1) { |
117 | if (NULL == *s1) { |
118 | free(tmp); |
118 | free(tmp); |
119 | return -1; |
119 | return -1; |
120 | } |
120 | } |
121 | memset(*s1, 0, sizeof(*s1)); |
121 | memset(*s1, 0, sizeof(*s1)); |
122 | memcpy(*s1, orig, strlen(orig) + 1); |
122 | memcpy(*s1, orig, strlen(orig) + 1); |
123 | free(tmp); |
123 | free(tmp); |
124 | return 0; |
124 | return 0; |
125 | } |
125 | } |
126 | memset(*s1, 0, sizeof(*s1)); |
126 | memset(*s1, 0, sizeof(*s1)); |
127 | memcpy(*s1, tmp, needed); |
127 | memcpy(*s1, tmp, needed); |
128 | free(tmp); |
128 | free(tmp); |
129 | 129 | ||
130 | return (int) needed; |
130 | return (int) needed; |
131 | } |
131 | } |
132 | 132 | ||
133 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
133 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
134 | char * cli_strtok_r(char *s, const char *delim, char **last) |
134 | char * cli_strtok_r(char *s, const char *delim, char **last) |
135 | { |
135 | { |
136 | char *spanp, *tok; |
136 | char *spanp, *tok; |
137 | int c, sc; |
137 | int c, sc; |
138 | 138 | ||
139 | if (s == NULL && (s = *last) == NULL) |
139 | if (s == NULL && (s = *last) == NULL) |
140 | return (NULL); |
140 | return (NULL); |
141 | 141 | ||
142 | cont: |
142 | cont: |
143 | c = *s++; |
143 | c = *s++; |
144 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
144 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
145 | if (c == sc) |
145 | if (c == sc) |
146 | goto cont; |
146 | goto cont; |
147 | } |
147 | } |
148 | 148 | ||
149 | if (c == 0) { /* no non-delimiter characters */ |
149 | if (c == 0) { /* no non-delimiter characters */ |
150 | *last = NULL; |
150 | *last = NULL; |
151 | return (NULL); |
151 | return (NULL); |
152 | } |
152 | } |
153 | 153 | ||
154 | tok = s - 1; |
154 | tok = s - 1; |
155 | 155 | ||
156 | for (;;) { |
156 | for (;;) { |
157 | c = *s++; |
157 | c = *s++; |
158 | spanp = (char *)delim; |
158 | spanp = (char *)delim; |
159 | do { |
159 | do { |
160 | if ((sc = *spanp++) == c) { |
160 | if ((sc = *spanp++) == c) { |
161 | if (c == 0) |
161 | if (c == 0) |
162 | s = NULL; |
162 | s = NULL; |
163 | else |
163 | else |
164 | s[-1] = '\0'; |
164 | s[-1] = '\0'; |
165 | *last = s; |
165 | *last = s; |
166 | return (tok); |
166 | return (tok); |
167 | } |
167 | } |
168 | } while (sc != 0); |
168 | } while (sc != 0); |
169 | } |
169 | } |
170 | } |
170 | } |
171 | 171 | ||
172 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
172 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
173 | char * cli_strtok(char *s, const char *delim) |
173 | char * cli_strtok(char *s, const char *delim) |
174 | { |
174 | { |
175 | static char *last; |
175 | static char *last; |
176 | 176 | ||
177 | return (cli_strtok_r(s, delim, &last)); |
177 | return (cli_strtok_r(s, delim, &last)); |
178 | } |
178 | } |
179 | 179 |