Sync with v2.1.0-rc0
[git/jrn.git] / path.c
blobf570141a1fbaf482dfb8a12e6898b1681fa85596
1 /*
2 * Utilities for paths and pathnames
3 */
4 #include "cache.h"
5 #include "strbuf.h"
6 #include "string-list.h"
7 #include "dir.h"
9 static int get_st_mode_bits(const char *path, int *mode)
11 struct stat st;
12 if (lstat(path, &st) < 0)
13 return -1;
14 *mode = st.st_mode;
15 return 0;
18 static char bad_path[] = "/bad-path/";
20 static struct strbuf *get_pathname(void)
22 static struct strbuf pathname_array[4] = {
23 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
25 static int index;
26 struct strbuf *sb = &pathname_array[3 & ++index];
27 strbuf_reset(sb);
28 return sb;
31 static char *cleanup_path(char *path)
33 /* Clean it up */
34 if (!memcmp(path, "./", 2)) {
35 path += 2;
36 while (*path == '/')
37 path++;
39 return path;
42 static void strbuf_cleanup_path(struct strbuf *sb)
44 char *path = cleanup_path(sb->buf);
45 if (path > sb->buf)
46 strbuf_remove(sb, 0, path - sb->buf);
49 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
51 va_list args;
52 unsigned len;
54 va_start(args, fmt);
55 len = vsnprintf(buf, n, fmt, args);
56 va_end(args);
57 if (len >= n) {
58 strlcpy(buf, bad_path, n);
59 return buf;
61 return cleanup_path(buf);
64 static int dir_prefix(const char *buf, const char *dir)
66 int len = strlen(dir);
67 return !strncmp(buf, dir, len) &&
68 (is_dir_sep(buf[len]) || buf[len] == '\0');
71 /* $buf =~ m|$dir/+$file| but without regex */
72 static int is_dir_file(const char *buf, const char *dir, const char *file)
74 int len = strlen(dir);
75 if (strncmp(buf, dir, len) || !is_dir_sep(buf[len]))
76 return 0;
77 while (is_dir_sep(buf[len]))
78 len++;
79 return !strcmp(buf + len, file);
82 static void replace_dir(struct strbuf *buf, int len, const char *newdir)
84 int newlen = strlen(newdir);
85 int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) &&
86 !is_dir_sep(newdir[newlen - 1]);
87 if (need_sep)
88 len--; /* keep one char, to be replaced with '/' */
89 strbuf_splice(buf, 0, len, newdir, newlen);
90 if (need_sep)
91 buf->buf[newlen] = '/';
94 static const char *common_list[] = {
95 "/branches", "/hooks", "/info", "!/logs", "/lost-found", "/modules",
96 "/objects", "/refs", "/remotes", "/repos", "/rr-cache", "/svn",
97 "config", "!gc.pid", "packed-refs", "shallow",
98 NULL
101 static void update_common_dir(struct strbuf *buf, int git_dir_len)
103 char *base = buf->buf + git_dir_len;
104 const char **p;
106 if (is_dir_file(base, "logs", "HEAD") ||
107 is_dir_file(base, "info", "sparse-checkout"))
108 return; /* keep this in $GIT_DIR */
109 for (p = common_list; *p; p++) {
110 const char *path = *p;
111 int is_dir = 0;
112 if (*path == '!')
113 path++;
114 if (*path == '/') {
115 path++;
116 is_dir = 1;
118 if (is_dir && dir_prefix(base, path)) {
119 replace_dir(buf, git_dir_len, get_git_common_dir());
120 return;
122 if (!is_dir && !strcmp(base, path)) {
123 replace_dir(buf, git_dir_len, get_git_common_dir());
124 return;
129 void report_linked_checkout_garbage(void)
131 struct strbuf sb = STRBUF_INIT;
132 const char **p;
133 int len;
135 if (!git_common_dir_env)
136 return;
137 strbuf_addf(&sb, "%s/", get_git_dir());
138 len = sb.len;
139 for (p = common_list; *p; p++) {
140 const char *path = *p;
141 if (*path == '!')
142 continue;
143 strbuf_setlen(&sb, len);
144 strbuf_addstr(&sb, path);
145 if (file_exists(sb.buf))
146 report_garbage("unused in linked checkout", sb.buf);
148 strbuf_release(&sb);
151 static void adjust_git_path(struct strbuf *buf, int git_dir_len)
153 const char *base = buf->buf + git_dir_len;
154 if (git_graft_env && is_dir_file(base, "info", "grafts"))
155 strbuf_splice(buf, 0, buf->len,
156 get_graft_file(), strlen(get_graft_file()));
157 else if (git_index_env && !strcmp(base, "index"))
158 strbuf_splice(buf, 0, buf->len,
159 get_index_file(), strlen(get_index_file()));
160 else if (git_db_env && dir_prefix(base, "objects"))
161 replace_dir(buf, git_dir_len + 7, get_object_directory());
162 else if (git_common_dir_env)
163 update_common_dir(buf, git_dir_len);
166 static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
168 int gitdir_len;
169 strbuf_addstr(buf, get_git_dir());
170 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
171 strbuf_addch(buf, '/');
172 gitdir_len = buf->len;
173 strbuf_vaddf(buf, fmt, args);
174 adjust_git_path(buf, gitdir_len);
175 strbuf_cleanup_path(buf);
178 void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
180 va_list args;
181 va_start(args, fmt);
182 do_git_path(sb, fmt, args);
183 va_end(args);
186 const char *git_path(const char *fmt, ...)
188 struct strbuf *pathname = get_pathname();
189 va_list args;
190 va_start(args, fmt);
191 do_git_path(pathname, fmt, args);
192 va_end(args);
193 return pathname->buf;
196 char *git_pathdup(const char *fmt, ...)
198 struct strbuf path = STRBUF_INIT;
199 va_list args;
200 va_start(args, fmt);
201 do_git_path(&path, fmt, args);
202 va_end(args);
203 return strbuf_detach(&path, NULL);
206 char *mkpathdup(const char *fmt, ...)
208 struct strbuf sb = STRBUF_INIT;
209 va_list args;
210 va_start(args, fmt);
211 strbuf_vaddf(&sb, fmt, args);
212 va_end(args);
213 strbuf_cleanup_path(&sb);
214 return strbuf_detach(&sb, NULL);
217 const char *mkpath(const char *fmt, ...)
219 va_list args;
220 struct strbuf *pathname = get_pathname();
221 va_start(args, fmt);
222 strbuf_vaddf(pathname, fmt, args);
223 va_end(args);
224 return cleanup_path(pathname->buf);
227 void home_config_paths(char **global, char **xdg, char *file)
229 char *xdg_home = getenv("XDG_CONFIG_HOME");
230 char *home = getenv("HOME");
231 char *to_free = NULL;
233 if (!home) {
234 if (global)
235 *global = NULL;
236 } else {
237 if (!xdg_home) {
238 to_free = mkpathdup("%s/.config", home);
239 xdg_home = to_free;
241 if (global)
242 *global = mkpathdup("%s/.gitconfig", home);
245 if (!xdg_home)
246 *xdg = NULL;
247 else
248 *xdg = mkpathdup("%s/git/%s", xdg_home, file);
250 free(to_free);
253 const char *git_path_submodule(const char *path, const char *fmt, ...)
255 struct strbuf *buf = get_pathname();
256 const char *git_dir;
257 va_list args;
259 strbuf_addstr(buf, path);
260 if (buf->len && buf->buf[buf->len - 1] != '/')
261 strbuf_addch(buf, '/');
262 strbuf_addstr(buf, ".git");
264 git_dir = read_gitfile(buf->buf);
265 if (git_dir) {
266 strbuf_reset(buf);
267 strbuf_addstr(buf, git_dir);
269 strbuf_addch(buf, '/');
271 va_start(args, fmt);
272 strbuf_vaddf(buf, fmt, args);
273 va_end(args);
274 strbuf_cleanup_path(buf);
275 return buf->buf;
278 int validate_headref(const char *path)
280 struct stat st;
281 char *buf, buffer[256];
282 unsigned char sha1[20];
283 int fd;
284 ssize_t len;
286 if (lstat(path, &st) < 0)
287 return -1;
289 /* Make sure it is a "refs/.." symlink */
290 if (S_ISLNK(st.st_mode)) {
291 len = readlink(path, buffer, sizeof(buffer)-1);
292 if (len >= 5 && !memcmp("refs/", buffer, 5))
293 return 0;
294 return -1;
298 * Anything else, just open it and try to see if it is a symbolic ref.
300 fd = open(path, O_RDONLY);
301 if (fd < 0)
302 return -1;
303 len = read_in_full(fd, buffer, sizeof(buffer)-1);
304 close(fd);
307 * Is it a symbolic ref?
309 if (len < 4)
310 return -1;
311 if (!memcmp("ref:", buffer, 4)) {
312 buf = buffer + 4;
313 len -= 4;
314 while (len && isspace(*buf))
315 buf++, len--;
316 if (len >= 5 && !memcmp("refs/", buf, 5))
317 return 0;
321 * Is this a detached HEAD?
323 if (!get_sha1_hex(buffer, sha1))
324 return 0;
326 return -1;
329 static struct passwd *getpw_str(const char *username, size_t len)
331 struct passwd *pw;
332 char *username_z = xmemdupz(username, len);
333 pw = getpwnam(username_z);
334 free(username_z);
335 return pw;
339 * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
340 * then it is a newly allocated string. Returns NULL on getpw failure or
341 * if path is NULL.
343 char *expand_user_path(const char *path)
345 struct strbuf user_path = STRBUF_INIT;
346 const char *to_copy = path;
348 if (path == NULL)
349 goto return_null;
350 if (path[0] == '~') {
351 const char *first_slash = strchrnul(path, '/');
352 const char *username = path + 1;
353 size_t username_len = first_slash - username;
354 if (username_len == 0) {
355 const char *home = getenv("HOME");
356 if (!home)
357 goto return_null;
358 strbuf_addstr(&user_path, home);
359 } else {
360 struct passwd *pw = getpw_str(username, username_len);
361 if (!pw)
362 goto return_null;
363 strbuf_addstr(&user_path, pw->pw_dir);
365 to_copy = first_slash;
367 strbuf_addstr(&user_path, to_copy);
368 return strbuf_detach(&user_path, NULL);
369 return_null:
370 strbuf_release(&user_path);
371 return NULL;
375 * First, one directory to try is determined by the following algorithm.
377 * (0) If "strict" is given, the path is used as given and no DWIM is
378 * done. Otherwise:
379 * (1) "~/path" to mean path under the running user's home directory;
380 * (2) "~user/path" to mean path under named user's home directory;
381 * (3) "relative/path" to mean cwd relative directory; or
382 * (4) "/absolute/path" to mean absolute directory.
384 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
385 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
386 * what we try.
388 * Second, we try chdir() to that. Upon failure, we return NULL.
390 * Then, we try if the current directory is a valid git repository.
391 * Upon failure, we return NULL.
393 * If all goes well, we return the directory we used to chdir() (but
394 * before ~user is expanded), avoiding getcwd() resolving symbolic
395 * links. User relative paths are also returned as they are given,
396 * except DWIM suffixing.
398 const char *enter_repo(const char *path, int strict)
400 static char used_path[PATH_MAX];
401 static char validated_path[PATH_MAX];
403 if (!path)
404 return NULL;
406 if (!strict) {
407 static const char *suffix[] = {
408 "/.git", "", ".git/.git", ".git", NULL,
410 const char *gitfile;
411 int len = strlen(path);
412 int i;
413 while ((1 < len) && (path[len-1] == '/'))
414 len--;
416 if (PATH_MAX <= len)
417 return NULL;
418 strncpy(used_path, path, len); used_path[len] = 0 ;
419 strcpy(validated_path, used_path);
421 if (used_path[0] == '~') {
422 char *newpath = expand_user_path(used_path);
423 if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
424 free(newpath);
425 return NULL;
428 * Copy back into the static buffer. A pity
429 * since newpath was not bounded, but other
430 * branches of the if are limited by PATH_MAX
431 * anyway.
433 strcpy(used_path, newpath); free(newpath);
435 else if (PATH_MAX - 10 < len)
436 return NULL;
437 len = strlen(used_path);
438 for (i = 0; suffix[i]; i++) {
439 struct stat st;
440 strcpy(used_path + len, suffix[i]);
441 if (!stat(used_path, &st) &&
442 (S_ISREG(st.st_mode) ||
443 (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
444 strcat(validated_path, suffix[i]);
445 break;
448 if (!suffix[i])
449 return NULL;
450 gitfile = read_gitfile(used_path) ;
451 if (gitfile)
452 strcpy(used_path, gitfile);
453 if (chdir(used_path))
454 return NULL;
455 path = validated_path;
457 else if (chdir(path))
458 return NULL;
460 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
461 validate_headref("HEAD") == 0) {
462 set_git_dir(".");
463 check_repository_format();
464 return path;
467 return NULL;
470 static int calc_shared_perm(int mode)
472 int tweak;
474 if (shared_repository < 0)
475 tweak = -shared_repository;
476 else
477 tweak = shared_repository;
479 if (!(mode & S_IWUSR))
480 tweak &= ~0222;
481 if (mode & S_IXUSR)
482 /* Copy read bits to execute bits */
483 tweak |= (tweak & 0444) >> 2;
484 if (shared_repository < 0)
485 mode = (mode & ~0777) | tweak;
486 else
487 mode |= tweak;
489 return mode;
493 int adjust_shared_perm(const char *path)
495 int old_mode, new_mode;
497 if (!shared_repository)
498 return 0;
499 if (get_st_mode_bits(path, &old_mode) < 0)
500 return -1;
502 new_mode = calc_shared_perm(old_mode);
503 if (S_ISDIR(old_mode)) {
504 /* Copy read bits to execute bits */
505 new_mode |= (new_mode & 0444) >> 2;
506 new_mode |= FORCE_DIR_SET_GID;
509 if (((old_mode ^ new_mode) & ~S_IFMT) &&
510 chmod(path, (new_mode & ~S_IFMT)) < 0)
511 return -2;
512 return 0;
515 static int have_same_root(const char *path1, const char *path2)
517 int is_abs1, is_abs2;
519 is_abs1 = is_absolute_path(path1);
520 is_abs2 = is_absolute_path(path2);
521 return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) ||
522 (!is_abs1 && !is_abs2);
526 * Give path as relative to prefix.
528 * The strbuf may or may not be used, so do not assume it contains the
529 * returned path.
531 const char *relative_path(const char *in, const char *prefix,
532 struct strbuf *sb)
534 int in_len = in ? strlen(in) : 0;
535 int prefix_len = prefix ? strlen(prefix) : 0;
536 int in_off = 0;
537 int prefix_off = 0;
538 int i = 0, j = 0;
540 if (!in_len)
541 return "./";
542 else if (!prefix_len)
543 return in;
545 if (have_same_root(in, prefix)) {
546 /* bypass dos_drive, for "c:" is identical to "C:" */
547 if (has_dos_drive_prefix(in)) {
548 i = 2;
549 j = 2;
551 } else {
552 return in;
555 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
556 if (is_dir_sep(prefix[i])) {
557 while (is_dir_sep(prefix[i]))
558 i++;
559 while (is_dir_sep(in[j]))
560 j++;
561 prefix_off = i;
562 in_off = j;
563 } else {
564 i++;
565 j++;
569 if (
570 /* "prefix" seems like prefix of "in" */
571 i >= prefix_len &&
573 * but "/foo" is not a prefix of "/foobar"
574 * (i.e. prefix not end with '/')
576 prefix_off < prefix_len) {
577 if (j >= in_len) {
578 /* in="/a/b", prefix="/a/b" */
579 in_off = in_len;
580 } else if (is_dir_sep(in[j])) {
581 /* in="/a/b/c", prefix="/a/b" */
582 while (is_dir_sep(in[j]))
583 j++;
584 in_off = j;
585 } else {
586 /* in="/a/bbb/c", prefix="/a/b" */
587 i = prefix_off;
589 } else if (
590 /* "in" is short than "prefix" */
591 j >= in_len &&
592 /* "in" not end with '/' */
593 in_off < in_len) {
594 if (is_dir_sep(prefix[i])) {
595 /* in="/a/b", prefix="/a/b/c/" */
596 while (is_dir_sep(prefix[i]))
597 i++;
598 in_off = in_len;
601 in += in_off;
602 in_len -= in_off;
604 if (i >= prefix_len) {
605 if (!in_len)
606 return "./";
607 else
608 return in;
611 strbuf_reset(sb);
612 strbuf_grow(sb, in_len);
614 while (i < prefix_len) {
615 if (is_dir_sep(prefix[i])) {
616 strbuf_addstr(sb, "../");
617 while (is_dir_sep(prefix[i]))
618 i++;
619 continue;
621 i++;
623 if (!is_dir_sep(prefix[prefix_len - 1]))
624 strbuf_addstr(sb, "../");
626 strbuf_addstr(sb, in);
628 return sb->buf;
632 * A simpler implementation of relative_path
634 * Get relative path by removing "prefix" from "in". This function
635 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
636 * to increase performance when traversing the path to work_tree.
638 const char *remove_leading_path(const char *in, const char *prefix)
640 static char buf[PATH_MAX + 1];
641 int i = 0, j = 0;
643 if (!prefix || !prefix[0])
644 return in;
645 while (prefix[i]) {
646 if (is_dir_sep(prefix[i])) {
647 if (!is_dir_sep(in[j]))
648 return in;
649 while (is_dir_sep(prefix[i]))
650 i++;
651 while (is_dir_sep(in[j]))
652 j++;
653 continue;
654 } else if (in[j] != prefix[i]) {
655 return in;
657 i++;
658 j++;
660 if (
661 /* "/foo" is a prefix of "/foo" */
662 in[j] &&
663 /* "/foo" is not a prefix of "/foobar" */
664 !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
666 return in;
667 while (is_dir_sep(in[j]))
668 j++;
669 if (!in[j])
670 strcpy(buf, ".");
671 else
672 strcpy(buf, in + j);
673 return buf;
677 * It is okay if dst == src, but they should not overlap otherwise.
679 * Performs the following normalizations on src, storing the result in dst:
680 * - Ensures that components are separated by '/' (Windows only)
681 * - Squashes sequences of '/'.
682 * - Removes "." components.
683 * - Removes ".." components, and the components the precede them.
684 * Returns failure (non-zero) if a ".." component appears as first path
685 * component anytime during the normalization. Otherwise, returns success (0).
687 * Note that this function is purely textual. It does not follow symlinks,
688 * verify the existence of the path, or make any system calls.
690 * prefix_len != NULL is for a specific case of prefix_pathspec():
691 * assume that src == dst and src[0..prefix_len-1] is already
692 * normalized, any time "../" eats up to the prefix_len part,
693 * prefix_len is reduced. In the end prefix_len is the remaining
694 * prefix that has not been overridden by user pathspec.
696 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
698 char *dst0;
700 if (has_dos_drive_prefix(src)) {
701 *dst++ = *src++;
702 *dst++ = *src++;
704 dst0 = dst;
706 if (is_dir_sep(*src)) {
707 *dst++ = '/';
708 while (is_dir_sep(*src))
709 src++;
712 for (;;) {
713 char c = *src;
716 * A path component that begins with . could be
717 * special:
718 * (1) "." and ends -- ignore and terminate.
719 * (2) "./" -- ignore them, eat slash and continue.
720 * (3) ".." and ends -- strip one and terminate.
721 * (4) "../" -- strip one, eat slash and continue.
723 if (c == '.') {
724 if (!src[1]) {
725 /* (1) */
726 src++;
727 } else if (is_dir_sep(src[1])) {
728 /* (2) */
729 src += 2;
730 while (is_dir_sep(*src))
731 src++;
732 continue;
733 } else if (src[1] == '.') {
734 if (!src[2]) {
735 /* (3) */
736 src += 2;
737 goto up_one;
738 } else if (is_dir_sep(src[2])) {
739 /* (4) */
740 src += 3;
741 while (is_dir_sep(*src))
742 src++;
743 goto up_one;
748 /* copy up to the next '/', and eat all '/' */
749 while ((c = *src++) != '\0' && !is_dir_sep(c))
750 *dst++ = c;
751 if (is_dir_sep(c)) {
752 *dst++ = '/';
753 while (is_dir_sep(c))
754 c = *src++;
755 src--;
756 } else if (!c)
757 break;
758 continue;
760 up_one:
762 * dst0..dst is prefix portion, and dst[-1] is '/';
763 * go up one level.
765 dst--; /* go to trailing '/' */
766 if (dst <= dst0)
767 return -1;
768 /* Windows: dst[-1] cannot be backslash anymore */
769 while (dst0 < dst && dst[-1] != '/')
770 dst--;
771 if (prefix_len && *prefix_len > dst - dst0)
772 *prefix_len = dst - dst0;
774 *dst = '\0';
775 return 0;
778 int normalize_path_copy(char *dst, const char *src)
780 return normalize_path_copy_len(dst, src, NULL);
784 * path = Canonical absolute path
785 * prefixes = string_list containing normalized, absolute paths without
786 * trailing slashes (except for the root directory, which is denoted by "/").
788 * Determines, for each path in prefixes, whether the "prefix"
789 * is an ancestor directory of path. Returns the length of the longest
790 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
791 * is an ancestor. (Note that this means 0 is returned if prefixes is
792 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
793 * are not considered to be their own ancestors. path must be in a
794 * canonical form: empty components, or "." or ".." components are not
795 * allowed.
797 int longest_ancestor_length(const char *path, struct string_list *prefixes)
799 int i, max_len = -1;
801 if (!strcmp(path, "/"))
802 return -1;
804 for (i = 0; i < prefixes->nr; i++) {
805 const char *ceil = prefixes->items[i].string;
806 int len = strlen(ceil);
808 if (len == 1 && ceil[0] == '/')
809 len = 0; /* root matches anything, with length 0 */
810 else if (!strncmp(path, ceil, len) && path[len] == '/')
811 ; /* match of length len */
812 else
813 continue; /* no match */
815 if (len > max_len)
816 max_len = len;
819 return max_len;
822 /* strip arbitrary amount of directory separators at end of path */
823 static inline int chomp_trailing_dir_sep(const char *path, int len)
825 while (len && is_dir_sep(path[len - 1]))
826 len--;
827 return len;
831 * If path ends with suffix (complete path components), returns the
832 * part before suffix (sans trailing directory separators).
833 * Otherwise returns NULL.
835 char *strip_path_suffix(const char *path, const char *suffix)
837 int path_len = strlen(path), suffix_len = strlen(suffix);
839 while (suffix_len) {
840 if (!path_len)
841 return NULL;
843 if (is_dir_sep(path[path_len - 1])) {
844 if (!is_dir_sep(suffix[suffix_len - 1]))
845 return NULL;
846 path_len = chomp_trailing_dir_sep(path, path_len);
847 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
849 else if (path[--path_len] != suffix[--suffix_len])
850 return NULL;
853 if (path_len && !is_dir_sep(path[path_len - 1]))
854 return NULL;
855 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
858 int daemon_avoid_alias(const char *p)
860 int sl, ndot;
863 * This resurrects the belts and suspenders paranoia check by HPA
864 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
865 * does not do getcwd() based path canonicalization.
867 * sl becomes true immediately after seeing '/' and continues to
868 * be true as long as dots continue after that without intervening
869 * non-dot character.
871 if (!p || (*p != '/' && *p != '~'))
872 return -1;
873 sl = 1; ndot = 0;
874 p++;
876 while (1) {
877 char ch = *p++;
878 if (sl) {
879 if (ch == '.')
880 ndot++;
881 else if (ch == '/') {
882 if (ndot < 3)
883 /* reject //, /./ and /../ */
884 return -1;
885 ndot = 0;
887 else if (ch == 0) {
888 if (0 < ndot && ndot < 3)
889 /* reject /.$ and /..$ */
890 return -1;
891 return 0;
893 else
894 sl = ndot = 0;
896 else if (ch == 0)
897 return 0;
898 else if (ch == '/') {
899 sl = 1;
900 ndot = 0;