Annotation of 41BSD/lib/libjobs/getwd.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Getwd
        !             3:  */
        !             4: #include       <sys/param.h>
        !             5: #include       <sys/stat.h>
        !             6: #include       <sys/dir.h>
        !             7: 
        !             8: #define        dot     "."
        !             9: #define        dotdot  ".."
        !            10: 
        !            11: static char    *name;
        !            12: 
        !            13: static int     file;
        !            14: static int     off     = -1;
        !            15: static struct  stat    d, dd;
        !            16: static struct  direct  dir;
        !            17: 
        !            18: char *
        !            19: getwd(np)
        !            20: char *np;
        !            21: {
        !            22:        int rdev, rino;
        !            23: 
        !            24:        *np++ = '/';
        !            25:        name = np;
        !            26:        stat("/", &d);
        !            27:        rdev = d.st_dev;
        !            28:        rino = d.st_ino;
        !            29:        for (;;) {
        !            30:                stat(dot, &d);
        !            31:                if (d.st_ino==rino && d.st_dev==rdev)
        !            32:                        goto done;
        !            33:                if ((file = open(dotdot,0)) < 0)
        !            34:                        prexit("getwd: cannot open ..\n");
        !            35:                fstat(file, &dd);
        !            36:                chdir(dotdot);
        !            37:                if(d.st_dev == dd.st_dev) {
        !            38:                        if(d.st_ino == dd.st_ino)
        !            39:                                goto done;
        !            40:                        do
        !            41:                                if (read(file, (char *)&dir, sizeof(dir)) < sizeof(dir))
        !            42:                                        prexit("getwd: read error in ..\n");
        !            43:                        while (dir.d_ino != d.st_ino);
        !            44:                }
        !            45:                else do {
        !            46:                                if(read(file, (char *)&dir, sizeof(dir)) < sizeof(dir))
        !            47:                                        prexit("getwd: read error in ..\n");
        !            48:                                stat(dir.d_name, &dd);
        !            49:                        } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
        !            50:                close(file);
        !            51:                cat();
        !            52:        }
        !            53: done:
        !            54:        name--;
        !            55:        if (chdir(name) < 0)
        !            56:                prexit("getwd: can't change back\n");
        !            57:        return (name);
        !            58: }
        !            59: 
        !            60: cat()
        !            61: {
        !            62:        register i, j;
        !            63: 
        !            64:        i = -1;
        !            65:        while (dir.d_name[++i] != 0);
        !            66:        if ((off+i+2) > 1024-1)
        !            67:                return;
        !            68:        for(j=off+1; j>=0; --j)
        !            69:                name[j+i+1] = name[j];
        !            70:        if (off >= 0)
        !            71:                name[i] = '/';
        !            72:        off=i+off+1;
        !            73:        name[off] = 0;
        !            74:        for(--i; i>=0; --i)
        !            75:                name[i] = dir.d_name[i];
        !            76: }
        !            77: 
        !            78: prexit(cp)
        !            79: char *cp;
        !            80: {
        !            81:        write(2, cp, strlen(cp));
        !            82:        exit(1);
        !            83: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.