Annotation of 3BSD/libI77/pwd.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Print working (current) directory
                      3:  */
                      4: #include       <sys/types.h>
                      5: #include       <sys/stat.h>
                      6: #include       <sys/dir.h>
                      7: 
                      8: char   dot[]   ".";
                      9: char   dotdot[]        "..";
                     10: char   root[]  "/";
                     11: char   name[512];
                     12: int    file;
                     13: int    off     -1;
                     14: struct stat    x;
                     15: struct direct  y;
                     16: 
                     17: main()
                     18: {
                     19:        for (;;) {
                     20:                stat(dot, &x);
                     21:                if ((file = open(dotdot,0)) < 0) prname();
                     22:                do {
                     23:                        if (read(file, &y, sizeof(y)) < sizeof(y))
                     24:                                prname();
                     25:                } while (y.d_ino != x.st_ino);
                     26:                close(file);
                     27:                if (y.d_ino == 2)
                     28:                        ckroot();
                     29:                cat();
                     30:                chdir(dotdot);
                     31:        }
                     32: }
                     33: 
                     34: ckroot()
                     35: {
                     36:        register i;
                     37: 
                     38:        if (stat(y.d_name,&x)<0 || chdir(root)<0 || (file=open(root,0))<0)
                     39:                prname();
                     40:        i = x.st_dev;
                     41:        do {
                     42:                if (read(file,&y,sizeof(y)) < sizeof(y))
                     43:                        prname();
                     44:                if (y.d_ino == 0)
                     45:                        continue;
                     46:                if (stat(y.d_name,&x) < 0)
                     47:                        prname();
                     48:        } while (x.st_dev!=i || (x.st_mode&S_IFMT)!=S_IFDIR);
                     49:        if (strcmp(dot, y.d_name) || strcmp(dotdot, y.d_name))
                     50:                cat();
                     51:        write(1, root, 1);
                     52:        prname();
                     53: }
                     54: 
                     55: prname()
                     56: {
                     57:        if (off<0)
                     58:                off = 0;
                     59:        name[off] = '\n';
                     60:        write(1, name, off+1);
                     61:        exit(0);
                     62: }
                     63: 
                     64: cat()
                     65: {
                     66:        register i, j;
                     67: 
                     68:        i = -1;
                     69:        while (y.d_name[++i] != 0);
                     70:        if ((off+i+2) > 511)
                     71:                prname();
                     72:        for(j=off+1; j>=0; --j)
                     73:                name[j+i+1] = name[j];
                     74:        off=i+off+1;
                     75:        name[i] = root[0];
                     76:        for(--i; i>=0; --i)
                     77:                name[i] = y.d_name[i];
                     78: }

unix.superglobalmegacorp.com

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