1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
19 #include "apr_private.h"
22 #if APR_HAVE_SYS_TYPES_H
23 #include <sys/types.h>
33 * Apache's "replacement" for the strncpy() function. We roll our
34 * own to implement these specific changes:
35 * (1) strncpy() doesn't always null terminate and we want it to.
36 * (2) strncpy() null fills, which is bogus, esp. when copy 8byte
37 * strings into 8k blocks.
38 * (3) Instead of returning the pointer to the beginning of
39 * the destination string, we return a pointer to the
40 * terminating '\0' to allow us to "check" for truncation
41 * (4) If src is NULL, null terminate dst (empty string copy)
43 * apr_cpystrn() follows the same call structure as strncpy().
46 APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
56 end = dst + dst_size - 1;
58 for (; d < end; ++d, ++src) {
65 *d = '\0'; /* always null terminate */
72 * This function provides a way to parse a generic argument string
73 * into a standard argv[] form of argument list. It respects the
74 * usual "whitespace" and quoteing rules. In the future this could
75 * be expanded to include support for the apr_call_exec command line
76 * string processing (including converting '+' to ' ' and doing the
77 * url processing. It does not currently support this function.
79 * token_context: Context from which pool allocations will occur.
80 * arg_str: Input argument string for conversion to argv[].
81 * argv_out: Output location. This is a pointer to an array
82 * of pointers to strings (ie. &(char *argv[]).
83 * This value will be allocated from the contexts
84 * pool and filled in with copies of the tokens
85 * found during parsing of the arg_str.
87 APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
89 apr_pool_t *token_context)
93 char *cleaned, *dirty;
95 int isquoted, numargs = 0, argnum;
97 #define SKIP_WHITESPACE(cp) \
98 for ( ; *cp == ' ' || *cp == '\t'; ) { \
102 #define CHECK_QUOTATION(cp,isquoted) \
108 else if (*cp == '\'') { \
113 /* DETERMINE_NEXTSTRING:
114 * At exit, cp will point to one of the following: NULL, SPACE, TAB or QUOTE.
115 * NULL implies the argument string has been fully traversed.
117 #define DETERMINE_NEXTSTRING(cp,isquoted) \
118 for ( ; *cp != '\0'; cp++) { \
119 if ( (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t' || \
120 *(cp+1) == '"' || *(cp+1) == '\''))) { \
124 if ( (!isquoted && (*cp == ' ' || *cp == '\t')) \
125 || (isquoted == 1 && *cp == '"') \
126 || (isquoted == 2 && *cp == '\'') ) { \
131 /* REMOVE_ESCAPE_CHARS:
132 * Compresses the arg string to remove all of the '\' escape chars.
133 * The final argv strings should not have any extra escape chars in it.
135 #define REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped) \
138 if (!escaped && *dirty == '\\') { \
143 *cleaned++ = *dirty; \
147 *cleaned = 0; /* last line of macro... */
153 /* This is ugly and expensive, but if anyone wants to figure a
154 * way to support any number of args without counting and
155 * allocating, please go ahead and change the code.
157 * Must account for the trailing NULL arg.
160 while (*ct != '\0') {
161 CHECK_QUOTATION(ct, isquoted);
162 DETERMINE_NEXTSTRING(ct, isquoted);
169 *argv_out = apr_palloc(token_context, numargs * sizeof(char*));
171 /* determine first argument */
172 for (argnum = 0; argnum < (numargs-1); argnum++) {
174 CHECK_QUOTATION(cp, isquoted);
176 DETERMINE_NEXTSTRING(cp, isquoted);
178 (*argv_out)[argnum] = apr_palloc(token_context, cp - ct);
179 apr_cpystrn((*argv_out)[argnum], ct, cp - ct);
180 cleaned = dirty = (*argv_out)[argnum];
181 REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped);
183 (*argv_out)[argnum] = NULL;
188 /* Filepath_name_get returns the final element of the pathname.
189 * Using the current platform's filename syntax.
190 * "/foo/bar/gum" -> "gum"
191 * "/foo/bar/gum/" -> ""
193 * "wi\\n32\\stuff" -> "stuff
195 * Corrected Win32 to accept "a/b\\stuff", "a:stuff"
198 APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname)
200 const char path_separator = '/';
201 const char *s = strrchr(pathname, path_separator);
204 const char path_separator_win = '\\';
205 const char drive_separator_win = ':';
206 const char *s2 = strrchr(pathname, path_separator_win);
210 if (!s) s = strrchr(pathname, drive_separator_win);
213 return s ? ++s : pathname;
216 /* length of dest assumed >= length of src
217 * collapse in place (src == dest) is legal.
218 * returns terminating null ptr to dest string.
220 APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src)
223 if (!apr_isspace(*src))
232 char *strdup(const char *str)
235 size_t len = strlen(str) + 1;
237 sdup = (char *) malloc(len);
238 memcpy(sdup, str, len);
244 /* The following two routines were donated for SVR4 by Andreas Vogel */
245 #if (!APR_HAVE_STRCASECMP && !APR_HAVE_STRICMP)
246 int strcasecmp(const char *a, const char *b)
250 for (p = a, q = b; *p && *q; p++, q++) {
251 int diff = apr_tolower(*p) - apr_tolower(*q);
256 return 1; /* p was longer than q */
258 return -1; /* p was shorter than q */
259 return 0; /* Exact match */
264 #if (!APR_HAVE_STRNCASECMP && !APR_HAVE_STRNICMP)
265 int strncasecmp(const char *a, const char *b, size_t n)
270 for (p = a, q = b; /*NOTHING */ ; p++, q++) {
273 return 0; /* Match up to n characters */
276 diff = apr_tolower(*p) - apr_tolower(*q);
284 /* The following routine was donated for UTS21 by dwd@bell-labs.com */
285 #if (!APR_HAVE_STRSTR)
286 char *strstr(char *s1, char *s2)
293 while((s1 = strchr(s1, *s2)) != NULL) {
294 /* found first character of s2, see if the rest matches */
297 while (*++p1 == *++p2) {
299 /* both strings ended together */
304 /* second string ended, a match */
307 /* didn't find a match here, try starting at next character in s1 */