Annotation of researchv9/jtools/src/pi/v9wd.c, revision 1.1

1.1     ! root        1: #include "univ.h"
        !             2: #include <CC/sys/types.h>
        !             3: #include <sys/param.h>
        !             4: #include <CC/sys/stat.h>
        !             5: #include <sys/dir.h>
        !             6: #include "wd.h"
        !             7: SRCFILE("wd.c")
        !             8: 
        !             9: void NewWd() { new Wd; }
        !            10: 
        !            11: Wd::Wd()
        !            12: {
        !            13:        pad = new Pad((PadRcv*) this);
        !            14:        pad->name("pwd/cd");
        !            15:        pad->banner("Working Directory:");
        !            16:        prevwd = 0;
        !            17:        pwd();
        !            18: }
        !            19: 
        !            20: Index Wd::carte()
        !            21: {
        !            22:        struct direct dirbuf;
        !            23:        struct stat stbuf;
        !            24:        Menu m;
        !            25: 
        !            26:        trace( "%d.carte()", this );    OK(ZIndex);
        !            27:        int  fd = ::open(".", 0);
        !            28:        if( fd >= 0 ) {
        !            29:                while( read(fd, (char*) &dirbuf, sizeof dirbuf) > 0 ){
        !            30:                        char dn[16];
        !            31:                        sprintf(dn, "%0.14s", dirbuf.d_name);
        !            32:                        int len = strlen(dn);
        !            33:                        if( len>=3 && dn[len-2]=='.' ) continue;
        !            34:                        if( dirbuf.d_ino == 0 
        !            35:                         || ::stat(dn, &stbuf)== -1
        !            36:                         || (stbuf.st_mode&S_IFMT)!=S_IFDIR )
        !            37:                                continue;
        !            38:                        long opand = (long) sf("%s/%s", getwd, dn);
        !            39:                        m.sort(sf("%s\240", dn), (Action)&Wd::kbd, opand);
        !            40:                }
        !            41:                ::close(fd);
        !            42:        }
        !            43:        return m.index();
        !            44: }
        !            45: 
        !            46: char *Getwd()
        !            47: {
        !            48:        char    *dot = ".", *dotdot = "..", *name;
        !            49:        int     file, off = -1;
        !            50:        struct  stat    d, dd;
        !            51:        struct  direct  dir;
        !            52:        int rdev, rino;
        !            53: 
        !            54:        name = "";
        !            55:        ::stat("/", &d);
        !            56:        rdev = d.st_dev;
        !            57:        rino = d.st_ino;
        !            58:        for (;;) {
        !            59:                ::stat(dot, &d);
        !            60:                if(d.st_ino==rino && d.st_dev==rdev) goto ChDir;
        !            61:                if((file = open(dotdot,0)) < 0)
        !            62:                        return "can't get working directory";
        !            63:                fstat(file, &dd);
        !            64:                chdir(dotdot);
        !            65:                if(d.st_dev == dd.st_dev) {
        !            66:                        if(d.st_ino == dd.st_ino) goto ChDir;
        !            67:                        do
        !            68:                                if (read(file,(char*)&dir,sizeof(dir))<sizeof(dir))
        !            69:                                        return "can't get working directory";
        !            70:                        while (dir.d_ino != d.st_ino);
        !            71:                }
        !            72:                else do {
        !            73:                                if(read(file,(char*)&dir,sizeof(dir))<sizeof(dir))
        !            74:                                        return "can't get working directory";
        !            75:                                ::stat(dir.d_name, &dd);        
        !            76:                        } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
        !            77:                close(file);
        !            78:                name = sf( name[0]?"%s/%s":"%s", dir.d_name, name );
        !            79:        }
        !            80: ChDir:
        !            81:        name =  sf("/%s",name);
        !            82:        if( chdir(name) == -1 ) return "chdir error";
        !            83:        return name;
        !            84: }
        !            85: 
        !            86: char *Wd::help()
        !            87: {
        !            88:        trace( "%d.help()", this );
        !            89:        return "[cd] <path> {change working directory}";
        !            90: }
        !            91: 
        !            92: char *Wd::kbd(char *s)
        !            93: {
        !            94:        trace( "%d.kbd(%s)", this, s );         OK("kbd");
        !            95:        if( s[0]=='c' && s[1]=='d' && (s[2]==' '||s[2]==0) ) s += 2;
        !            96:        while( *s == ' ' ) ++s;
        !            97:        if( *s==0 ){
        !            98:                char *getenv(char*), *e = getenv("HOME");
        !            99:                if( e ) s = e;
        !           100:        }
        !           101:        if( chdir(s) == -1 ){
        !           102:                pad->insert(key++, "cannot cd %s", s);
        !           103:                prevwd = 0;
        !           104:        }
        !           105:        pwd(SELECTLINE);
        !           106:        return 0;
        !           107: }
        !           108: 
        !           109: void Wd::pwd(Attrib a)
        !           110: {
        !           111:        if( prevwd )
        !           112:                pad->insert(key, a, (PadRcv*)this, ix, "%s", getwd);
        !           113:        getwd = Getwd();
        !           114:        ix = carte();
        !           115:        pad->menu(ix);
        !           116:        pad->insert(++key, a|DONT_CUT, (PadRcv*)this, ix, "wd=%s", getwd);
        !           117:        prevwd = getwd;
        !           118: }

unix.superglobalmegacorp.com

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