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_private.h"
19 #include "apr_arch_file_io.h"
20 #include "apr_strings.h"
30 /* WinNT accepts several odd forms of a 'root' path. Under Unicode
31 * calls (ApiFunctionW) the //?/C:/foo or //?/UNC/mach/share/foo forms
32 * are accepted. Ansi and Unicode functions both accept the //./C:/foo
33 * form under WinNT/2K. Since these forms are handled in the utf-8 to
34 * unicode translation phase, we don't want the user confused by them, so
35 * we will accept them but always return the canonical C:/ or //mach/share/
37 * OS2 appears immune from the nonsense :)
40 APR_DECLARE(apr_status_t) apr_filepath_root(const char **rootpath,
45 const char *testpath = *inpath;
48 char seperator[2] = { 0, 0};
49 char server[APR_PATH_MAX+1];
50 char volume[APR_PATH_MAX+1];
51 char file[APR_PATH_MAX+1];
55 if (inpath && *inpath)
56 volsep = strchr (*inpath, ':');
60 if (strlen(*inpath) > APR_PATH_MAX) {
64 seperator[0] = (flags & APR_FILEPATH_NATIVE) ? '\\' : '/';
66 /* Allocate and initialize each of the segment buffers
68 server[0] = volume[0] = file[0] = '\0';
70 /* If we don't have a volume separator then don't bother deconstructing
71 the path since we won't use the deconstructed information anyway.
74 /* Split the inpath into its separate parts. */
75 deconstruct(testpath, server, volume, NULL, file, NULL, &elements, PATH_UNDEF);
77 /* If we got a volume part then continue splitting out the root.
78 Otherwise we either have an incomplete or relative path
80 if (volume && strlen(volume) > 0) {
81 newpath = apr_pcalloc(p, strlen(server)+strlen(volume)+5);
82 construct(newpath, server, volume, NULL, NULL, NULL, PATH_NETWARE);
84 /* NetWare doesn't add the root slash so we need to add it manually.
86 strcat(newpath, seperator);
89 /* Skip the inpath pointer down to the first non-root character
94 } while (*newpath && ((*newpath == '/') || (*newpath == '\\')));
97 /* Need to handle APR_FILEPATH_TRUENAME checking here. */
104 else if ((**inpath == '/') || (**inpath == '\\')) {
105 /* if we have a root path without a volume then just split
106 in same manner as unix although this path will be
109 *rootpath = apr_pstrdup(p, seperator);
112 } while ((**inpath == '/') || (**inpath == '\\'));
115 return APR_ERELATIVE;
117 return APR_EINCOMPLETE;
119 #else /* ndef(NETWARE) */
125 seperator[0] = (flags & APR_FILEPATH_NATIVE) ? '\\' : '/';
128 if (testpath[0] == '/' || testpath[0] == '\\') {
129 if (testpath[1] == '/' || testpath[1] == '\\') {
131 #ifdef WIN32 /* //server/share isn't the only // delimited syntax */
132 if ((testpath[2] == '?' || testpath[2] == '.')
133 && (testpath[3] == '/' || testpath[3] == '\\')) {
134 if (IS_FNCHAR(testpath[4]) && testpath[5] == ':')
138 /* given '//?/C: or //./C: let us try this
139 * all over again from the drive designator
141 rv = apr_filepath_root(rootpath, &testpath, flags, p);
142 if (!rv || rv == APR_EINCOMPLETE)
146 else if (strncasecmp(testpath + 4, "UNC", 3) == 0
147 && (testpath[7] == '/' || testpath[7] == '\\')
148 && (testpath[2] == '?')) {
149 /* given '//?/UNC/machine/share, a little magic
150 * at the end makes this all work out by using
151 * 'C/machine' as the starting point and replacing
152 * the UNC delimiters with \'s, including the 'C'
157 /* This must not be a path to a file, but rather
158 * a volume or device. Die for now.
162 #endif /* WIN32 (non - //server/share syntax) */
164 /* Evaluate path of '//[machine/[share[/]]]' */
165 delim1 = testpath + 2;
167 /* Protect against //X/ where X is illegal */
168 if (*delim1 && !IS_FNCHAR(*(delim1++)))
170 } while (*delim1 && *delim1 != '/' && *delim1 != '\\');
175 while (*delim2 && *delim2 != '/' && *delim2 != '\\') {
176 /* Protect against //machine/X/ where X is illegal */
177 if (!IS_FNCHAR(*(delim2++)))
181 /* Copy the '//machine/[share[/]]' path, always providing
182 * an extra byte for the trailing slash.
184 newpath = apr_pstrmemdup(p, testpath, delim2 - testpath + 1);
186 if (delim2 == delim1 + 1) {
187 /* We found simply \\machine\, so give up already
191 return APR_EINCOMPLETE;
194 if (flags & APR_FILEPATH_TRUENAME) {
195 /* Validate the \\Machine\Share\ designation,
196 * Win32 will argue about slashed in UNC paths,
197 * so use backslashes till we finish testing,
198 * and add the trailing backslash [required].
199 * apr_pstrmemdup above guarentees us the new
200 * trailing null character.
204 newpath[delim1 - testpath] = '\\';
205 newpath[delim2 - testpath] = '\\';
207 rv = filepath_root_test(newpath, p);
210 rv = filepath_root_case(&newpath, newpath, p);
213 newpath[0] = seperator[0];
214 newpath[1] = seperator[0];
215 newpath[delim1 - testpath] = seperator[0];
216 newpath[delim2 - testpath] = (*delim2 ? seperator[0] : '\0');
219 /* Give back the caller's own choice of delimiters
221 newpath[0] = testpath[0];
222 newpath[1] = testpath[1];
223 newpath[delim1 - testpath] = *delim1;
224 newpath[delim2 - testpath] = *delim2;
227 /* If this root included the trailing / or \ designation
228 * then lop off multiple trailing slashes and give back
229 * appropriate delimiters.
232 *inpath = delim2 + 1;
233 while (**inpath == '/' || **inpath == '\\')
244 /* Have path of '\\[machine]', if the machine is given,
245 * append same trailing slash as the leading slash
247 delim1 = strchr(testpath, '\0');
248 if (delim1 > testpath + 2) {
249 newpath = apr_pstrndup(p, testpath, delim1 - testpath + 1);
250 if (flags & APR_FILEPATH_TRUENAME)
251 newpath[delim1 - testpath] = seperator[0];
253 newpath[delim1 - testpath] = newpath[0];
254 newpath[delim1 - testpath + 1] = '\0';
257 newpath = apr_pstrndup(p, testpath, delim1 - testpath);
259 if (flags & APR_FILEPATH_TRUENAME) {
260 newpath[0] = seperator[0];
261 newpath[1] = seperator[0];
265 return APR_EINCOMPLETE;
268 /* Left with a path of '/', what drive are we asking about?
270 *inpath = testpath + 1;
271 newpath = apr_palloc(p, 2);
272 if (flags & APR_FILEPATH_TRUENAME)
273 newpath[0] = seperator[0];
275 newpath[0] = testpath[0];
278 return APR_EINCOMPLETE;
281 /* Evaluate path of 'd:[/]' */
282 if (IS_FNCHAR(*testpath) && testpath[1] == ':')
285 /* Validate that D:\ drive exists, test must be rooted
286 * Note that posix/win32 insists a drive letter is upper case,
287 * so who are we to argue with a 'feature'.
288 * It is a safe fold, since only A-Z is legal, and has no
289 * side effects of legal mis-mapped non-us-ascii codes.
291 newpath = apr_palloc(p, 4);
292 newpath[0] = testpath[0];
293 newpath[1] = testpath[1];
294 newpath[2] = seperator[0];
296 if (flags & APR_FILEPATH_TRUENAME) {
297 newpath[0] = apr_toupper(newpath[0]);
298 rv = filepath_root_test(newpath, p);
302 /* Just give back the root the user handed to us.
304 if (testpath[2] != '/' && testpath[2] != '\\') {
307 *inpath = testpath + 2;
308 return APR_EINCOMPLETE;
311 /* strip off remaining slashes that designate the root,
312 * give the caller back their original choice of slash
313 * unless this is TRUENAME'ed
315 *inpath = testpath + 3;
316 while (**inpath == '/' || **inpath == '\\')
318 if (!(flags & APR_FILEPATH_TRUENAME))
319 newpath[2] = testpath[2];
324 /* Nothing interesting */
325 return APR_ERELATIVE;
327 #endif /* ndef(NETWARE) */
330 #if !defined(NETWARE)
331 static int same_drive(const char *path1, const char *path2)
333 char drive1 = path1[0];
334 char drive2 = path2[0];
336 if (!drive1 || !drive2 || path1[1] != ':' || path2[1] != ':')
339 if (drive1 == drive2)
342 if (drive1 >= 'a' && drive1 <= 'z')
345 if (drive2 >= 'a' && drive2 <= 'z')
348 return (drive1 == drive2);
352 APR_DECLARE(apr_status_t) apr_filepath_merge(char **newpath,
353 const char *basepath,
358 char path[APR_PATH_MAX]; /* isn't null term */
359 const char *baseroot = NULL;
361 apr_size_t rootlen; /* the length of the root portion of path, d:/ is 3 */
362 apr_size_t baselen; /* the length of basepath (excluding baseroot) */
363 apr_size_t keptlen; /* the length of the retained basepath (incl root) */
364 apr_size_t pathlen; /* the length of the result path */
365 apr_size_t segend; /* the end of the current segment */
366 apr_size_t seglen; /* the length of the segment (excl trailing chars) */
367 apr_status_t basetype = 0; /* from parsing the basepath's baseroot */
368 apr_status_t addtype; /* from parsing the addpath's addroot */
371 int fixunc = 0; /* flag to complete an incomplete UNC basepath */
374 /* Treat null as an empty path, otherwise split addroot from the addpath
377 addpath = addroot = "";
378 addtype = APR_ERELATIVE;
381 /* This call _should_ test the path
383 addtype = apr_filepath_root(&addroot, &addpath,
384 APR_FILEPATH_TRUENAME
385 | (flags & APR_FILEPATH_NATIVE),
387 if (addtype == APR_SUCCESS) {
388 addtype = APR_EABSOLUTE;
390 else if (addtype == APR_ERELATIVE) {
393 else if (addtype != APR_EINCOMPLETE) {
394 /* apr_filepath_root was incomprehensible so fail already
400 /* If addpath is (even partially) rooted, then basepath is
401 * unused. Ths violates any APR_FILEPATH_SECUREROOTTEST
402 * and APR_FILEPATH_NOTABSOLUTE flags specified.
404 if (addtype == APR_EABSOLUTE || addtype == APR_EINCOMPLETE)
406 if (flags & APR_FILEPATH_SECUREROOTTEST)
407 return APR_EABOVEROOT;
408 if (flags & APR_FILEPATH_NOTABSOLUTE)
412 /* Optimized tests before we query the current working path
416 /* If APR_FILEPATH_NOTABOVEROOT wasn't specified,
417 * we won't test the root again, it's ignored.
418 * Waste no CPU retrieving the working path.
420 if (addtype == APR_EABSOLUTE && !(flags & APR_FILEPATH_NOTABOVEROOT)) {
421 basepath = baseroot = "";
422 basetype = APR_ERELATIVE;
425 /* If APR_FILEPATH_NOTABSOLUTE is specified, the caller
426 * requires an absolutely relative result, So do not retrieve
429 if (addtype == APR_ERELATIVE && (flags & APR_FILEPATH_NOTABSOLUTE)) {
430 basepath = baseroot = "";
431 basetype = APR_ERELATIVE;
437 /* Start with the current working path. This is bass akwards,
438 * but required since the compiler (at least vc) doesn't like
439 * passing the address of a char const* for a char** arg.
440 * We must grab the current path of the designated drive
441 * if addroot is given in drive-relative form (e.g. d:foo)
445 if (addtype == APR_EINCOMPLETE && addroot[1] == ':')
446 rv = filepath_drive_get(&getpath, addroot[0], flags, p);
449 rv = apr_filepath_get(&getpath, flags, p);
450 if (rv != APR_SUCCESS)
456 /* This call should _not_ test the path
458 basetype = apr_filepath_root(&baseroot, &basepath,
459 (flags & APR_FILEPATH_NATIVE), p);
460 if (basetype == APR_SUCCESS) {
461 basetype = APR_EABSOLUTE;
463 else if (basetype == APR_ERELATIVE) {
466 else if (basetype != APR_EINCOMPLETE) {
467 /* apr_filepath_root was incomprehensible so fail already
472 baselen = strlen(basepath);
474 /* If APR_FILEPATH_NOTABSOLUTE is specified, the caller
475 * requires an absolutely relative result. If the given
476 * basepath is not relative then fail.
478 if ((flags & APR_FILEPATH_NOTABSOLUTE) && basetype != APR_ERELATIVE)
481 /* The Win32 nightmare on unc street... start combining for
482 * many possible root combinations.
484 if (addtype == APR_EABSOLUTE)
486 /* Ignore the given root path, and start with the addroot
488 if ((flags & APR_FILEPATH_NOTABOVEROOT)
489 && strncmp(baseroot, addroot, strlen(baseroot)))
490 return APR_EABOVEROOT;
492 rootlen = pathlen = strlen(addroot);
493 memcpy(path, addroot, pathlen);
495 else if (addtype == APR_EINCOMPLETE)
497 /* There are several types of incomplete paths,
498 * incomplete UNC paths (//foo/ or //),
499 * drives without rooted paths (d: as in d:foo),
500 * and simple roots (/ as in /foo).
501 * Deal with these in significantly different manners...
504 if ((addroot[0] == '/' || addroot[0] == '\\') &&
505 (addroot[1] == '/' || addroot[1] == '\\'))
507 /* Ignore the given root path if the incomplete addpath is UNC,
508 * (note that the final result will be incomplete).
510 if (flags & APR_FILEPATH_NOTRELATIVE)
512 if ((flags & APR_FILEPATH_NOTABOVEROOT)
513 && strncmp(baseroot, addroot, strlen(baseroot)))
514 return APR_EABOVEROOT;
517 rootlen = pathlen = strlen(addroot);
518 memcpy(path, addroot, pathlen);
522 if ((addroot[0] == '/' || addroot[0] == '\\') && !addroot[1])
524 /* Bring together the drive or UNC root from the baseroot
525 * if the addpath is a simple root and basepath is rooted,
526 * otherwise disregard the basepath entirely.
528 if (basetype != APR_EABSOLUTE && (flags & APR_FILEPATH_NOTRELATIVE))
530 if (basetype != APR_ERELATIVE) {
532 if (basetype == APR_INCOMPLETE
533 && (baseroot[0] == '/' || baseroot[0] == '\\')
534 && (baseroot[1] == '/' || baseroot[1] == '\\'))
537 keptlen = rootlen = pathlen = strlen(baseroot);
538 memcpy(path, baseroot, pathlen);
541 if (flags & APR_FILEPATH_NOTABOVEROOT)
542 return APR_EABOVEROOT;
544 rootlen = pathlen = strlen(addroot);
545 memcpy(path, addroot, pathlen);
549 else if (filepath_has_drive(addroot, DRIVE_ONLY, p))
551 /* If the addroot is a drive (without a volume root)
552 * use the basepath _if_ it matches this drive letter!
553 * Otherwise we must discard the basepath.
555 if (!filepath_compare_drive(addroot, baseroot, p) &&
556 filepath_has_drive(baseroot, 0, p)) {
558 else if (addroot[0] && addroot[1] == ':' && !addroot[2])
560 /* If the addroot is a drive (without a volume root)
561 * use the basepath _if_ it matches this drive letter!
562 * Otherwise we must discard the basepath.
564 if (same_drive(addroot, baseroot)) {
566 /* Base the result path on the basepath
568 if (basetype != APR_EABSOLUTE && (flags & APR_FILEPATH_NOTRELATIVE))
570 rootlen = strlen(baseroot);
571 keptlen = pathlen = rootlen + baselen;
572 if (keptlen >= sizeof(path))
573 return APR_ENAMETOOLONG;
574 memcpy(path, baseroot, rootlen);
575 memcpy(path + rootlen, basepath, baselen);
578 if (flags & APR_FILEPATH_NOTRELATIVE)
580 if (flags & APR_FILEPATH_NOTABOVEROOT)
581 return APR_EABOVEROOT;
583 rootlen = pathlen = strlen(addroot);
584 memcpy(path, addroot, pathlen);
588 /* Now this is unexpected, we aren't aware of any other
589 * incomplete path forms! Fail now.
594 else { /* addtype == APR_ERELATIVE */
595 /* If both paths are relative, fail early
597 if (basetype != APR_EABSOLUTE && (flags & APR_FILEPATH_NOTRELATIVE))
601 /* An incomplete UNC path must be completed
603 if (basetype == APR_INCOMPLETE
604 && (baseroot[0] == '/' || baseroot[0] == '\\')
605 && (baseroot[1] == '/' || baseroot[1] == '\\'))
609 /* Base the result path on the basepath
611 rootlen = strlen(baseroot);
612 keptlen = pathlen = rootlen + baselen;
613 if (keptlen >= sizeof(path))
614 return APR_ENAMETOOLONG;
615 memcpy(path, baseroot, rootlen);
616 memcpy(path + rootlen, basepath, baselen);
619 /* '/' terminate the given root path unless it's already terminated
620 * or is an incomplete drive root. Correct the trailing slash unless
621 * we have an incomplete UNC path still to fix.
623 if (pathlen && path[pathlen - 1] != ':') {
624 if (path[pathlen - 1] != '/' && path[pathlen - 1] != '\\') {
625 if (pathlen + 1 >= sizeof(path))
626 return APR_ENAMETOOLONG;
628 path[pathlen++] = ((flags & APR_FILEPATH_NATIVE) ? '\\' : '/');
630 /* XXX: wrong, but gotta figure out what I intended;
632 * path[pathlen++] = ((flags & APR_FILEPATH_NATIVE) ? '\\' : '/');
638 /* Parse each segment, find the closing '/'
641 while (addpath[seglen] && addpath[seglen] != '/'
642 && addpath[seglen] != '\\')
645 /* Truncate all trailing spaces and all but the first two dots */
647 while (seglen && (addpath[seglen - 1] == ' '
648 || addpath[seglen - 1] == '.')) {
649 if (seglen > 2 || addpath[seglen - 1] != '.' || addpath[0] != '.')
655 if (seglen == 0 || (seglen == 1 && addpath[0] == '.'))
657 /* NOTE: win32 _hates_ '/ /' and '/. /' (yes, with spaces in there)
658 * so eliminate all preconceptions that it is valid.
664 /* This isn't legal unless the unc path is completed
670 /* Otherwise, this is a noop segment (/ or ./) so ignore it
673 else if (seglen == 2 && addpath[0] == '.' && addpath[1] == '.')
675 /* NOTE: win32 _hates_ '/.. /' (yes, with a space in there)
676 * and '/..../', some functions treat it as ".", and some
677 * fail! Eliminate all preconceptions that they are valid.
679 if (seglen < segend && (seglen != 3 || addpath[2] != '.'))
683 /* This isn't legal unless the unc path is completed
689 /* backpath (../) when an absolute path is given */
690 if (rootlen && (pathlen <= rootlen))
692 /* Attempt to move above root. Always die if the
693 * APR_FILEPATH_SECUREROOTTEST flag is specified.
695 if (flags & APR_FILEPATH_SECUREROOTTEST)
696 return APR_EABOVEROOT;
698 /* Otherwise this is simply a noop, above root is root.
701 else if (pathlen == 0
704 || path[pathlen - 4] == ':'
705 || path[pathlen - 4] == '/'
706 || path[pathlen - 4] == '\\')
707 && path[pathlen - 3] == '.'
708 && path[pathlen - 2] == '.'
709 && (path[pathlen - 1] == '/'
710 || path[pathlen - 1] == '\\')))
712 /* Verified path is empty, exactly "..[/\]", or ends
713 * in "[:/\]..[/\]" - these patterns we will not back
714 * over since they aren't 'prior segements'.
716 * If APR_FILEPATH_SECUREROOTTEST.was given, die now.
718 if (flags & APR_FILEPATH_SECUREROOTTEST)
719 return APR_EABOVEROOT;
721 /* Otherwise append another backpath.
723 if (pathlen + 3 >= sizeof(path))
724 return APR_ENAMETOOLONG;
725 path[pathlen++] = '.';
726 path[pathlen++] = '.';
727 if (addpath[segend]) {
728 path[pathlen++] = ((flags & APR_FILEPATH_NATIVE)
729 ? '\\' : ((flags & APR_FILEPATH_TRUENAME)
730 ? '/' : addpath[segend]));
732 /* The 'root' part of this path now includes the ../ path,
733 * because that backpath will not be parsed by the truename
740 /* otherwise crop the prior segment
744 } while (pathlen && path[pathlen - 1] != '/'
745 && path[pathlen - 1] != '\\');
747 /* Now test if we are above where we started and back up
748 * the keptlen offset to reflect the added/altered path.
750 if (pathlen < keptlen)
752 if (flags & APR_FILEPATH_SECUREROOTTEST)
753 return APR_EABOVEROOT;
758 else /* not empty or dots */
762 const char *testpath = path;
763 const char *testroot;
764 apr_status_t testtype;
765 apr_size_t i = (addpath[segend] != '\0');
767 /* This isn't legal unless the unc path is complete!
771 if (pathlen + seglen + 1 >= sizeof(path))
772 return APR_ENAMETOOLONG;
773 memcpy(path + pathlen, addpath, seglen + i);
775 /* Always add the trailing slash to a UNC segment
777 path[pathlen + seglen] = ((flags & APR_FILEPATH_NATIVE)
779 pathlen += seglen + 1;
781 /* Recanonicalize the UNC root with the new UNC segment,
782 * and if we succeed, reset this test and the rootlen,
783 * and replace our path with the canonical UNC root path
785 path[pathlen] = '\0';
786 /* This call _should_ test the path
788 testtype = apr_filepath_root(&testroot, &testpath,
789 APR_FILEPATH_TRUENAME
790 | (flags & APR_FILEPATH_NATIVE),
792 if (testtype == APR_SUCCESS) {
793 rootlen = pathlen = (testpath - path);
794 memcpy(path, testroot, pathlen);
797 else if (testtype != APR_EINCOMPLETE) {
798 /* apr_filepath_root was very unexpected so fail already
806 /* An actual segment, append it to the destination path
808 apr_size_t i = (addpath[segend] != '\0');
809 if (pathlen + seglen + i >= sizeof(path))
810 return APR_ENAMETOOLONG;
811 memcpy(path + pathlen, addpath, seglen + i);
813 path[pathlen + seglen] = ((flags & APR_FILEPATH_NATIVE)
815 pathlen += seglen + i;
819 /* Skip over trailing slash to the next segment
827 /* keptlen will be the baselen unless the addpath contained
828 * backpath elements. If so, and APR_FILEPATH_NOTABOVEROOT
829 * is specified (APR_FILEPATH_SECUREROOTTEST was caught above),
830 * compare the string beyond the root to assure the result path
831 * is still within given basepath. Note that the root path
832 * segment is thoroughly tested prior to path parsing.
834 if ((flags & APR_FILEPATH_NOTABOVEROOT) && baselen) {
835 if (memcmp(basepath, path + rootlen, baselen) != 0)
836 return APR_EABOVEROOT;
838 /* Ahem... if we have a basepath without a trailing slash,
839 * we better be sure that /foo wasn't replaced with /foobar!
841 if (basepath[baselen - 1] != '/' && basepath[baselen - 1] != '\\'
842 && path[rootlen + baselen] && path[rootlen + baselen] != '/'
843 && path[rootlen + baselen] != '\\')
844 return APR_EABOVEROOT;
847 if (addpath && (flags & APR_FILEPATH_TRUENAME)) {
848 /* We can always skip the root, it's already true-named. */
849 if (rootlen > keptlen)
851 if ((path[keptlen] == '/') || (path[keptlen] == '\\')) {
852 /* By rights, keptlen may grown longer than pathlen.
853 * we wont' use it again (in that case) so we don't care.
857 /* Go through all the new segments */
858 while (keptlen < pathlen) {
862 /* find any slash and set it aside for a minute. */
863 for (seglen = 0; keptlen + seglen < pathlen; ++seglen) {
864 if ((path[keptlen + seglen] == '/') ||
865 (path[keptlen + seglen] == '\\')) {
866 saveslash = path[keptlen + seglen];
870 /* Null term for stat! */
871 path[keptlen + seglen] = '\0';
872 if ((rv = apr_stat(&finfo, path,
873 APR_FINFO_LINK | APR_FINFO_TYPE | APR_FINFO_NAME, p))
875 apr_size_t namelen = strlen(finfo.name);
877 #if defined(OS2) /* only has case folding, never aliases that change the length */
879 if (memcmp(finfo.name, path + keptlen, seglen) != 0) {
880 memcpy(path + keptlen, finfo.name, namelen);
882 #else /* WIN32 || NETWARE; here there be aliases that gire and gimble and change length */
884 if ((namelen != seglen) ||
885 (memcmp(finfo.name, path + keptlen, seglen) != 0))
887 if (namelen <= seglen) {
888 memcpy(path + keptlen, finfo.name, namelen);
889 if ((namelen < seglen) && saveslash) {
890 memmove(path + keptlen + namelen + 1,
891 path + keptlen + seglen + 1,
892 pathlen - keptlen - seglen);
893 pathlen += namelen - seglen;
897 else { /* namelen > seglen */
898 if (pathlen + namelen - seglen >= sizeof(path))
899 return APR_ENAMETOOLONG;
901 memmove(path + keptlen + namelen + 1,
902 path + keptlen + seglen + 1,
903 pathlen - keptlen - seglen);
905 memcpy(path + keptlen, finfo.name, namelen);
906 pathlen += namelen - seglen;
910 #endif /* !OS2 (Whatever that alias was we're over it) */
912 /* That's it, the rest is path info.
913 * I don't know how we aught to handle this. Should
914 * we define a new error to indicate 'more info'?
915 * Should we split out the rest of the path?
917 if ((finfo.filetype != APR_DIR) &&
918 (finfo.filetype != APR_LNK) && saveslash)
920 #ifdef XXX_FIGURE_THIS_OUT
922 /* the example inserts a null between the end of
923 * the filename and the next segment, and increments
924 * the path length so we would return both segments.
928 path[keptlen] = saveslash;
929 if (pathlen + 1 >= sizeof(path))
930 return APR_ENAMETOOLONG;
931 memmove(path + keptlen + 1,
934 path[keptlen] = '\0';
942 /* put back the '/' */
944 path[keptlen + seglen] = saveslash;
949 if (rv != APR_SUCCESS) {
950 if (APR_STATUS_IS_ENOENT(rv))
952 if (APR_STATUS_IS_EPATHWILD(rv))
953 /* This path included wildcards. The path elements
954 * that did not contain wildcards are canonicalized,
955 * so we will return the path, although later elements
956 * don't necessarily exist, and aren't canonical.
959 else if (APR_STATUS_IS_ENOTDIR(rv))
960 /* This is a little more serious, we just added a name
961 * onto a filename (think http's PATH_INFO)
962 * If the caller is foolish enough to do this, we expect
963 * the've already canonicalized the root) that they knew
964 * what they are doing :(
973 *newpath = apr_pstrmemdup(p, path, pathlen);
978 APR_DECLARE(apr_status_t) apr_filepath_list_split(apr_array_header_t **pathelts,
982 return apr_filepath_list_split_impl(pathelts, liststr, ';', p);
985 APR_DECLARE(apr_status_t) apr_filepath_list_merge(char **liststr,
986 apr_array_header_t *pathelts,
989 return apr_filepath_list_merge_impl(liststr, pathelts, ';', p);
993 APR_DECLARE(apr_status_t) apr_filepath_encoding(int *style, apr_pool_t *p)
995 #if APR_HAS_UNICODE_FS
998 *style = APR_FILEPATH_ENCODING_UTF8;
1003 *style = APR_FILEPATH_ENCODING_LOCALE;