Annotation of researchv9/jtools/src/pi/wd.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 <dirent.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 stat stbuf;
        !            23:        Menu m;
        !            24:        struct dirent *dp;
        !            25:        DIR *dirp;
        !            26: 
        !            27:        dirp = ::opendir(".");
        !            28:        if (dirp == NULL)
        !            29:                return m.index();
        !            30:        for ( dp = ::readdir(dirp); dp!=NULL; dp=::readdir(dirp) ) {
        !            31:                char dn[25];
        !            32:                sprintf(dn, "%0.25s", dp->d_name);
        !            33:                int len = strlen(dn);
        !            34:                if( len>=3 && dn[len-2]=='.' ) continue;
        !            35:                if( dp->d_ino == 0 
        !            36:                 || ::stat(dn, &stbuf)== -1
        !            37:                 || (stbuf.st_mode&S_IFMT)!=S_IFDIR )
        !            38:                        continue;
        !            39:                long opand = (long) sf("%s/%s", getwd, dn);
        !            40:                m.sort(sf("%s\240", dn), (Action)&Wd::kbd, opand);
        !            41:        }
        !            42:        ::closedir(dirp);
        !            43:        return m.index();
        !            44: }
        !            45: 
        !            46: char *Getwd()
        !            47: {
        !            48:        char pathname[MAXPATHLEN];
        !            49:        char *getwd(char*);
        !            50: 
        !            51:        if (getwd(pathname) == 0)
        !            52:                return "getwd error";
        !            53:        return sf("%s",pathname);
        !            54: }
        !            55: 
        !            56: char *Wd::help()
        !            57: {
        !            58:        trace( "%d.help()", this );
        !            59:        return "[cd] <path> {change working directory}";
        !            60: }
        !            61: 
        !            62: char *Wd::kbd(char *s)
        !            63: {
        !            64:        trace( "%d.kbd(%s)", this, s );         OK("kbd");
        !            65:        if( s[0]=='c' && s[1]=='d' && (s[2]==' '||s[2]==0) ) s += 2;
        !            66:        while( *s == ' ' ) ++s;
        !            67:        if( *s==0 ){
        !            68:                char *getenv(char*), *e = getenv("HOME");
        !            69:                if( e ) s = e;
        !            70:        }
        !            71:        if( chdir(s) == -1 ){
        !            72:                pad->insert(key++, "cannot cd %s", s);
        !            73:                prevwd = 0;
        !            74:        }
        !            75:        pwd(SELECTLINE);
        !            76:        return 0;
        !            77: }
        !            78: 
        !            79: void Wd::pwd(Attrib a)
        !            80: {
        !            81:        if( prevwd )
        !            82:                pad->insert(key, a, (PadRcv*)this, ix, "%s", getwd);
        !            83:        getwd = Getwd();
        !            84:        ix = carte();
        !            85:        pad->menu(ix);
        !            86:        pad->insert(++key, a|DONT_CUT, (PadRcv*)this, ix, "wd=%s", getwd);
        !            87:        prevwd = getwd;
        !            88: }

unix.superglobalmegacorp.com

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