path.con commit Implement automatic fast-forward merge for submodules (68d03e4)
   1/*
   2 * I'm tired of doing "vsnprintf()" etc just to open a
   3 * file, so here's a "return static buffer with printf"
   4 * interface for paths.
   5 *
   6 * It's obviously not thread-safe. Sue me. But it's quite
   7 * useful for doing things like
   8 *
   9 *   f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
  10 *
  11 * which is what it's designed for.
  12 */
  13#include "cache.h"
  14#include "strbuf.h"
  15
  16static char bad_path[] = "/bad-path/";
  17
  18static char *get_pathname(void)
  19{
  20        static char pathname_array[4][PATH_MAX];
  21        static int index;
  22        return pathname_array[3 & ++index];
  23}
  24
  25static char *cleanup_path(char *path)
  26{
  27        /* Clean it up */
  28        if (!memcmp(path, "./", 2)) {
  29                path += 2;
  30                while (*path == '/')
  31                        path++;
  32        }
  33        return path;
  34}
  35
  36char *mksnpath(char *buf, size_t n, const char *fmt, ...)
  37{
  38        va_list args;
  39        unsigned len;
  40
  41        va_start(args, fmt);
  42        len = vsnprintf(buf, n, fmt, args);
  43        va_end(args);
  44        if (len >= n) {
  45                strlcpy(buf, bad_path, n);
  46                return buf;
  47        }
  48        return cleanup_path(buf);
  49}
  50
  51static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
  52{
  53        const char *git_dir = get_git_dir();
  54        size_t len;
  55
  56        len = strlen(git_dir);
  57        if (n < len + 1)
  58                goto bad;
  59        memcpy(buf, git_dir, len);
  60        if (len && !is_dir_sep(git_dir[len-1]))
  61                buf[len++] = '/';
  62        len += vsnprintf(buf + len, n - len, fmt, args);
  63        if (len >= n)
  64                goto bad;
  65        return cleanup_path(buf);
  66bad:
  67        strlcpy(buf, bad_path, n);
  68        return buf;
  69}
  70
  71char *git_snpath(char *buf, size_t n, const char *fmt, ...)
  72{
  73        va_list args;
  74        va_start(args, fmt);
  75        (void)git_vsnpath(buf, n, fmt, args);
  76        va_end(args);
  77        return buf;
  78}
  79
  80char *git_pathdup(const char *fmt, ...)
  81{
  82        char path[PATH_MAX];
  83        va_list args;
  84        va_start(args, fmt);
  85        (void)git_vsnpath(path, sizeof(path), fmt, args);
  86        va_end(args);
  87        return xstrdup(path);
  88}
  89
  90char *mkpath(const char *fmt, ...)
  91{
  92        va_list args;
  93        unsigned len;
  94        char *pathname = get_pathname();
  95
  96        va_start(args, fmt);
  97        len = vsnprintf(pathname, PATH_MAX, fmt, args);
  98        va_end(args);
  99        if (len >= PATH_MAX)
 100                return bad_path;
 101        return cleanup_path(pathname);
 102}
 103
 104char *git_path(const char *fmt, ...)
 105{
 106        const char *git_dir = get_git_dir();
 107        char *pathname = get_pathname();
 108        va_list args;
 109        unsigned len;
 110
 111        len = strlen(git_dir);
 112        if (len > PATH_MAX-100)
 113                return bad_path;
 114        memcpy(pathname, git_dir, len);
 115        if (len && git_dir[len-1] != '/')
 116                pathname[len++] = '/';
 117        va_start(args, fmt);
 118        len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
 119        va_end(args);
 120        if (len >= PATH_MAX)
 121                return bad_path;
 122        return cleanup_path(pathname);
 123}
 124
 125char *git_path_submodule(const char *path, const char *fmt, ...)
 126{
 127        char *pathname = get_pathname();
 128        struct strbuf buf = STRBUF_INIT;
 129        const char *git_dir;
 130        va_list args;
 131        unsigned len;
 132
 133        len = strlen(path);
 134        if (len > PATH_MAX-100)
 135                return bad_path;
 136
 137        strbuf_addstr(&buf, path);
 138        if (len && path[len-1] != '/')
 139                strbuf_addch(&buf, '/');
 140        strbuf_addstr(&buf, ".git");
 141
 142        git_dir = read_gitfile_gently(buf.buf);
 143        if (git_dir) {
 144                strbuf_reset(&buf);
 145                strbuf_addstr(&buf, git_dir);
 146        }
 147        strbuf_addch(&buf, '/');
 148
 149        if (buf.len >= PATH_MAX)
 150                return bad_path;
 151        memcpy(pathname, buf.buf, buf.len + 1);
 152
 153        strbuf_release(&buf);
 154        len = strlen(pathname);
 155
 156        va_start(args, fmt);
 157        len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
 158        va_end(args);
 159        if (len >= PATH_MAX)
 160                return bad_path;
 161        return cleanup_path(pathname);
 162}
 163
 164/* git_mkstemp() - create tmp file honoring TMPDIR variable */
 165int git_mkstemp(char *path, size_t len, const char *template)
 166{
 167        const char *tmp;
 168        size_t n;
 169
 170        tmp = getenv("TMPDIR");
 171        if (!tmp)
 172                tmp = "/tmp";
 173        n = snprintf(path, len, "%s/%s", tmp, template);
 174        if (len <= n) {
 175                errno = ENAMETOOLONG;
 176                return -1;
 177        }
 178        return mkstemp(path);
 179}
 180
 181/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
 182int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
 183{
 184        const char *tmp;
 185        size_t n;
 186
 187        tmp = getenv("TMPDIR");
 188        if (!tmp)
 189                tmp = "/tmp";
 190        n = snprintf(path, len, "%s/%s", tmp, template);
 191        if (len <= n) {
 192                errno = ENAMETOOLONG;
 193                return -1;
 194        }
 195        return mkstemps(path, suffix_len);
 196}
 197
 198/* Adapted from libiberty's mkstemp.c. */
 199
 200#undef TMP_MAX
 201#define TMP_MAX 16384
 202
 203int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
 204{
 205        static const char letters[] =
 206                "abcdefghijklmnopqrstuvwxyz"
 207                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 208                "0123456789";
 209        static const int num_letters = 62;
 210        uint64_t value;
 211        struct timeval tv;
 212        char *template;
 213        size_t len;
 214        int fd, count;
 215
 216        len = strlen(pattern);
 217
 218        if (len < 6 + suffix_len) {
 219                errno = EINVAL;
 220                return -1;
 221        }
 222
 223        if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
 224                errno = EINVAL;
 225                return -1;
 226        }
 227
 228        /*
 229         * Replace pattern's XXXXXX characters with randomness.
 230         * Try TMP_MAX different filenames.
 231         */
 232        gettimeofday(&tv, NULL);
 233        value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
 234        template = &pattern[len - 6 - suffix_len];
 235        for (count = 0; count < TMP_MAX; ++count) {
 236                uint64_t v = value;
 237                /* Fill in the random bits. */
 238                template[0] = letters[v % num_letters]; v /= num_letters;
 239                template[1] = letters[v % num_letters]; v /= num_letters;
 240                template[2] = letters[v % num_letters]; v /= num_letters;
 241                template[3] = letters[v % num_letters]; v /= num_letters;
 242                template[4] = letters[v % num_letters]; v /= num_letters;
 243                template[5] = letters[v % num_letters]; v /= num_letters;
 244
 245                fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
 246                if (fd > 0)
 247                        return fd;
 248                /*
 249                 * Fatal error (EPERM, ENOSPC etc).
 250                 * It doesn't make sense to loop.
 251                 */
 252                if (errno != EEXIST)
 253                        break;
 254                /*
 255                 * This is a random value.  It is only necessary that
 256                 * the next TMP_MAX values generated by adding 7777 to
 257                 * VALUE are different with (module 2^32).
 258                 */
 259                value += 7777;
 260        }
 261        /* We return the null string if we can't find a unique file name.  */
 262        pattern[0] = '\0';
 263        return -1;
 264}
 265
 266int git_mkstemp_mode(char *pattern, int mode)
 267{
 268        /* mkstemp is just mkstemps with no suffix */
 269        return git_mkstemps_mode(pattern, 0, mode);
 270}
 271
 272int gitmkstemps(char *pattern, int suffix_len)
 273{
 274        return git_mkstemps_mode(pattern, suffix_len, 0600);
 275}
 276
 277int validate_headref(const char *path)
 278{
 279        struct stat st;
 280        char *buf, buffer[256];
 281        unsigned char sha1[20];
 282        int fd;
 283        ssize_t len;
 284
 285        if (lstat(path, &st) < 0)
 286                return -1;
 287
 288        /* Make sure it is a "refs/.." symlink */
 289        if (S_ISLNK(st.st_mode)) {
 290                len = readlink(path, buffer, sizeof(buffer)-1);
 291                if (len >= 5 && !memcmp("refs/", buffer, 5))
 292                        return 0;
 293                return -1;
 294        }
 295
 296        /*
 297         * Anything else, just open it and try to see if it is a symbolic ref.
 298         */
 299        fd = open(path, O_RDONLY);
 300        if (fd < 0)
 301                return -1;
 302        len = read_in_full(fd, buffer, sizeof(buffer)-1);
 303        close(fd);
 304
 305        /*
 306         * Is it a symbolic ref?
 307         */
 308        if (len < 4)
 309                return -1;
 310        if (!memcmp("ref:", buffer, 4)) {
 311                buf = buffer + 4;
 312                len -= 4;
 313                while (len && isspace(*buf))
 314                        buf++, len--;
 315                if (len >= 5 && !memcmp("refs/", buf, 5))
 316                        return 0;
 317        }
 318
 319        /*
 320         * Is this a detached HEAD?
 321         */
 322        if (!get_sha1_hex(buffer, sha1))
 323                return 0;
 324
 325        return -1;
 326}
 327
 328static struct passwd *getpw_str(const char *username, size_t len)
 329{
 330        struct passwd *pw;
 331        char *username_z = xmalloc(len + 1);
 332        memcpy(username_z, username, len);
 333        username_z[len] = '\0';
 334        pw = getpwnam(username_z);
 335        free(username_z);
 336        return pw;
 337}
 338
 339/*
 340 * Return a string with ~ and ~user expanded via getpw*.  If buf != NULL,
 341 * then it is a newly allocated string. Returns NULL on getpw failure or
 342 * if path is NULL.
 343 */
 344char *expand_user_path(const char *path)
 345{
 346        struct strbuf user_path = STRBUF_INIT;
 347        const char *first_slash = strchrnul(path, '/');
 348        const char *to_copy = path;
 349
 350        if (path == NULL)
 351                goto return_null;
 352        if (path[0] == '~') {
 353                const char *username = path + 1;
 354                size_t username_len = first_slash - username;
 355                if (username_len == 0) {
 356                        const char *home = getenv("HOME");
 357                        strbuf_add(&user_path, home, strlen(home));
 358                } else {
 359                        struct passwd *pw = getpw_str(username, username_len);
 360                        if (!pw)
 361                                goto return_null;
 362                        strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
 363                }
 364                to_copy = first_slash;
 365        }
 366        strbuf_add(&user_path, to_copy, strlen(to_copy));
 367        return strbuf_detach(&user_path, NULL);
 368return_null:
 369        strbuf_release(&user_path);
 370        return NULL;
 371}
 372
 373/*
 374 * First, one directory to try is determined by the following algorithm.
 375 *
 376 * (0) If "strict" is given, the path is used as given and no DWIM is
 377 *     done. Otherwise:
 378 * (1) "~/path" to mean path under the running user's home directory;
 379 * (2) "~user/path" to mean path under named user's home directory;
 380 * (3) "relative/path" to mean cwd relative directory; or
 381 * (4) "/absolute/path" to mean absolute directory.
 382 *
 383 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
 384 * "%s/.git", "%s.git", "%s" in this order.  The first one that exists is
 385 * what we try.
 386 *
 387 * Second, we try chdir() to that.  Upon failure, we return NULL.
 388 *
 389 * Then, we try if the current directory is a valid git repository.
 390 * Upon failure, we return NULL.
 391 *
 392 * If all goes well, we return the directory we used to chdir() (but
 393 * before ~user is expanded), avoiding getcwd() resolving symbolic
 394 * links.  User relative paths are also returned as they are given,
 395 * except DWIM suffixing.
 396 */
 397char *enter_repo(char *path, int strict)
 398{
 399        static char used_path[PATH_MAX];
 400        static char validated_path[PATH_MAX];
 401
 402        if (!path)
 403                return NULL;
 404
 405        if (!strict) {
 406                static const char *suffix[] = {
 407                        ".git/.git", "/.git", ".git", "", NULL,
 408                };
 409                int len = strlen(path);
 410                int i;
 411                while ((1 < len) && (path[len-1] == '/')) {
 412                        path[len-1] = 0;
 413                        len--;
 414                }
 415                if (PATH_MAX <= len)
 416                        return NULL;
 417                if (path[0] == '~') {
 418                        char *newpath = expand_user_path(path);
 419                        if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
 420                                free(newpath);
 421                                return NULL;
 422                        }
 423                        /*
 424                         * Copy back into the static buffer. A pity
 425                         * since newpath was not bounded, but other
 426                         * branches of the if are limited by PATH_MAX
 427                         * anyway.
 428                         */
 429                        strcpy(used_path, newpath); free(newpath);
 430                        strcpy(validated_path, path);
 431                        path = used_path;
 432                }
 433                else if (PATH_MAX - 10 < len)
 434                        return NULL;
 435                else {
 436                        path = strcpy(used_path, path);
 437                        strcpy(validated_path, path);
 438                }
 439                len = strlen(path);
 440                for (i = 0; suffix[i]; i++) {
 441                        strcpy(path + len, suffix[i]);
 442                        if (!access(path, F_OK)) {
 443                                strcat(validated_path, suffix[i]);
 444                                break;
 445                        }
 446                }
 447                if (!suffix[i] || chdir(path))
 448                        return NULL;
 449                path = validated_path;
 450        }
 451        else if (chdir(path))
 452                return NULL;
 453
 454        if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
 455            validate_headref("HEAD") == 0) {
 456                set_git_dir(".");
 457                check_repository_format();
 458                return path;
 459        }
 460
 461        return NULL;
 462}
 463
 464int set_shared_perm(const char *path, int mode)
 465{
 466        struct stat st;
 467        int tweak, shared, orig_mode;
 468
 469        if (!shared_repository) {
 470                if (mode)
 471                        return chmod(path, mode & ~S_IFMT);
 472                return 0;
 473        }
 474        if (!mode) {
 475                if (lstat(path, &st) < 0)
 476                        return -1;
 477                mode = st.st_mode;
 478                orig_mode = mode;
 479        } else
 480                orig_mode = 0;
 481        if (shared_repository < 0)
 482                shared = -shared_repository;
 483        else
 484                shared = shared_repository;
 485        tweak = shared;
 486
 487        if (!(mode & S_IWUSR))
 488                tweak &= ~0222;
 489        if (mode & S_IXUSR)
 490                /* Copy read bits to execute bits */
 491                tweak |= (tweak & 0444) >> 2;
 492        if (shared_repository < 0)
 493                mode = (mode & ~0777) | tweak;
 494        else
 495                mode |= tweak;
 496
 497        if (S_ISDIR(mode)) {
 498                /* Copy read bits to execute bits */
 499                mode |= (shared & 0444) >> 2;
 500                mode |= FORCE_DIR_SET_GID;
 501        }
 502
 503        if (((shared_repository < 0
 504              ? (orig_mode & (FORCE_DIR_SET_GID | 0777))
 505              : (orig_mode & mode)) != mode) &&
 506            chmod(path, (mode & ~S_IFMT)) < 0)
 507                return -2;
 508        return 0;
 509}
 510
 511const char *make_relative_path(const char *abs, const char *base)
 512{
 513        static char buf[PATH_MAX + 1];
 514        int i = 0, j = 0;
 515
 516        if (!base || !base[0])
 517                return abs;
 518        while (base[i]) {
 519                if (is_dir_sep(base[i])) {
 520                        if (!is_dir_sep(abs[j]))
 521                                return abs;
 522                        while (is_dir_sep(base[i]))
 523                                i++;
 524                        while (is_dir_sep(abs[j]))
 525                                j++;
 526                        continue;
 527                } else if (abs[j] != base[i]) {
 528                        return abs;
 529                }
 530                i++;
 531                j++;
 532        }
 533        if (
 534            /* "/foo" is a prefix of "/foo" */
 535            abs[j] &&
 536            /* "/foo" is not a prefix of "/foobar" */
 537            !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
 538           )
 539                return abs;
 540        while (is_dir_sep(abs[j]))
 541                j++;
 542        if (!abs[j])
 543                strcpy(buf, ".");
 544        else
 545                strcpy(buf, abs + j);
 546        return buf;
 547}
 548
 549/*
 550 * It is okay if dst == src, but they should not overlap otherwise.
 551 *
 552 * Performs the following normalizations on src, storing the result in dst:
 553 * - Ensures that components are separated by '/' (Windows only)
 554 * - Squashes sequences of '/'.
 555 * - Removes "." components.
 556 * - Removes ".." components, and the components the precede them.
 557 * Returns failure (non-zero) if a ".." component appears as first path
 558 * component anytime during the normalization. Otherwise, returns success (0).
 559 *
 560 * Note that this function is purely textual.  It does not follow symlinks,
 561 * verify the existence of the path, or make any system calls.
 562 */
 563int normalize_path_copy(char *dst, const char *src)
 564{
 565        char *dst0;
 566
 567        if (has_dos_drive_prefix(src)) {
 568                *dst++ = *src++;
 569                *dst++ = *src++;
 570        }
 571        dst0 = dst;
 572
 573        if (is_dir_sep(*src)) {
 574                *dst++ = '/';
 575                while (is_dir_sep(*src))
 576                        src++;
 577        }
 578
 579        for (;;) {
 580                char c = *src;
 581
 582                /*
 583                 * A path component that begins with . could be
 584                 * special:
 585                 * (1) "." and ends   -- ignore and terminate.
 586                 * (2) "./"           -- ignore them, eat slash and continue.
 587                 * (3) ".." and ends  -- strip one and terminate.
 588                 * (4) "../"          -- strip one, eat slash and continue.
 589                 */
 590                if (c == '.') {
 591                        if (!src[1]) {
 592                                /* (1) */
 593                                src++;
 594                        } else if (is_dir_sep(src[1])) {
 595                                /* (2) */
 596                                src += 2;
 597                                while (is_dir_sep(*src))
 598                                        src++;
 599                                continue;
 600                        } else if (src[1] == '.') {
 601                                if (!src[2]) {
 602                                        /* (3) */
 603                                        src += 2;
 604                                        goto up_one;
 605                                } else if (is_dir_sep(src[2])) {
 606                                        /* (4) */
 607                                        src += 3;
 608                                        while (is_dir_sep(*src))
 609                                                src++;
 610                                        goto up_one;
 611                                }
 612                        }
 613                }
 614
 615                /* copy up to the next '/', and eat all '/' */
 616                while ((c = *src++) != '\0' && !is_dir_sep(c))
 617                        *dst++ = c;
 618                if (is_dir_sep(c)) {
 619                        *dst++ = '/';
 620                        while (is_dir_sep(c))
 621                                c = *src++;
 622                        src--;
 623                } else if (!c)
 624                        break;
 625                continue;
 626
 627        up_one:
 628                /*
 629                 * dst0..dst is prefix portion, and dst[-1] is '/';
 630                 * go up one level.
 631                 */
 632                dst--;  /* go to trailing '/' */
 633                if (dst <= dst0)
 634                        return -1;
 635                /* Windows: dst[-1] cannot be backslash anymore */
 636                while (dst0 < dst && dst[-1] != '/')
 637                        dst--;
 638        }
 639        *dst = '\0';
 640        return 0;
 641}
 642
 643/*
 644 * path = Canonical absolute path
 645 * prefix_list = Colon-separated list of absolute paths
 646 *
 647 * Determines, for each path in prefix_list, whether the "prefix" really
 648 * is an ancestor directory of path.  Returns the length of the longest
 649 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
 650 * is an ancestor.  (Note that this means 0 is returned if prefix_list is
 651 * "/".) "/foo" is not considered an ancestor of "/foobar".  Directories
 652 * are not considered to be their own ancestors.  path must be in a
 653 * canonical form: empty components, or "." or ".." components are not
 654 * allowed.  prefix_list may be null, which is like "".
 655 */
 656int longest_ancestor_length(const char *path, const char *prefix_list)
 657{
 658        char buf[PATH_MAX+1];
 659        const char *ceil, *colon;
 660        int len, max_len = -1;
 661
 662        if (prefix_list == NULL || !strcmp(path, "/"))
 663                return -1;
 664
 665        for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
 666                for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
 667                len = colon - ceil;
 668                if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
 669                        continue;
 670                strlcpy(buf, ceil, len+1);
 671                if (normalize_path_copy(buf, buf) < 0)
 672                        continue;
 673                len = strlen(buf);
 674                if (len > 0 && buf[len-1] == '/')
 675                        buf[--len] = '\0';
 676
 677                if (!strncmp(path, buf, len) &&
 678                    path[len] == '/' &&
 679                    len > max_len) {
 680                        max_len = len;
 681                }
 682        }
 683
 684        return max_len;
 685}
 686
 687/* strip arbitrary amount of directory separators at end of path */
 688static inline int chomp_trailing_dir_sep(const char *path, int len)
 689{
 690        while (len && is_dir_sep(path[len - 1]))
 691                len--;
 692        return len;
 693}
 694
 695/*
 696 * If path ends with suffix (complete path components), returns the
 697 * part before suffix (sans trailing directory separators).
 698 * Otherwise returns NULL.
 699 */
 700char *strip_path_suffix(const char *path, const char *suffix)
 701{
 702        int path_len = strlen(path), suffix_len = strlen(suffix);
 703
 704        while (suffix_len) {
 705                if (!path_len)
 706                        return NULL;
 707
 708                if (is_dir_sep(path[path_len - 1])) {
 709                        if (!is_dir_sep(suffix[suffix_len - 1]))
 710                                return NULL;
 711                        path_len = chomp_trailing_dir_sep(path, path_len);
 712                        suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
 713                }
 714                else if (path[--path_len] != suffix[--suffix_len])
 715                        return NULL;
 716        }
 717
 718        if (path_len && !is_dir_sep(path[path_len - 1]))
 719                return NULL;
 720        return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
 721}
 722
 723int daemon_avoid_alias(const char *p)
 724{
 725        int sl, ndot;
 726
 727        /*
 728         * This resurrects the belts and suspenders paranoia check by HPA
 729         * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
 730         * does not do getcwd() based path canonicalization.
 731         *
 732         * sl becomes true immediately after seeing '/' and continues to
 733         * be true as long as dots continue after that without intervening
 734         * non-dot character.
 735         */
 736        if (!p || (*p != '/' && *p != '~'))
 737                return -1;
 738        sl = 1; ndot = 0;
 739        p++;
 740
 741        while (1) {
 742                char ch = *p++;
 743                if (sl) {
 744                        if (ch == '.')
 745                                ndot++;
 746                        else if (ch == '/') {
 747                                if (ndot < 3)
 748                                        /* reject //, /./ and /../ */
 749                                        return -1;
 750                                ndot = 0;
 751                        }
 752                        else if (ch == 0) {
 753                                if (0 < ndot && ndot < 3)
 754                                        /* reject /.$ and /..$ */
 755                                        return -1;
 756                                return 0;
 757                        }
 758                        else
 759                                sl = ndot = 0;
 760                }
 761                else if (ch == 0)
 762                        return 0;
 763                else if (ch == '/') {
 764                        sl = 1;
 765                        ndot = 0;
 766                }
 767        }
 768}
 769
 770int offset_1st_component(const char *path)
 771{
 772        if (has_dos_drive_prefix(path))
 773                return 2 + is_dir_sep(path[2]);
 774        return is_dir_sep(path[0]);
 775}