Annotation of researchv10no/cmd/ap/apnews/apnews3.c, revision 1.1

1.1     ! root        1: # include "stdio.h"
        !             2: # include "assert.h"
        !             3: # include "ctype.h"
        !             4: # include "sys/param.h"
        !             5: # include "sys/stat.h"
        !             6: # include "sys/dir.h"
        !             7: extern char pwdir[];
        !             8: 
        !             9: /* pwd command, modified to write in argument */
        !            10: 
        !            11: char   dot[]   = ".";
        !            12: char   dotdot[] = "..";
        !            13: char   name[512];
        !            14: char   output[512];
        !            15: int    file;
        !            16: int    off     = -1;
        !            17: struct stat    d, dd;
        !            18: struct direct  dir;
        !            19: 
        !            20: char *
        !            21: getpwd ()
        !            22: {
        !            23:        int rdev, rino;
        !            24: 
        !            25:        strcpy(output, "/");
        !            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:                        {
        !            33:                        prname();
        !            34:                        break;
        !            35:                        }
        !            36:                if ((file = open(dotdot,0)) < 0) {
        !            37:                        fprintf(stderr,"pwd: cannot open ..\n");
        !            38:                        exit(1);
        !            39:                }
        !            40:                fstat(file, &dd);
        !            41:                chdir(dotdot);
        !            42:                if(d.st_dev == dd.st_dev) {
        !            43:                        if(d.st_ino == dd.st_ino)
        !            44:                                {
        !            45:                                prname();
        !            46:                                break;
        !            47:                                }
        !            48:                        do
        !            49:                                if (read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
        !            50:                                        fprintf(stderr,"read error in ..\n");
        !            51:                                        exit(1);
        !            52:                                }
        !            53:                        while (dir.d_ino != d.st_ino);
        !            54:                }
        !            55:                else do {
        !            56:                                if(read(file, (char *)&dir, sizeof(dir)) < sizeof(dir)) {
        !            57:                                        fprintf(stderr,"read error in ..\n");
        !            58:                                        exit(1);
        !            59:                                }
        !            60:                                stat(dir.d_name, &dd);
        !            61:                        } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
        !            62:                close(file);
        !            63:                cat();
        !            64:        }
        !            65:        chdir (output);
        !            66:        return(output);
        !            67: }
        !            68: 
        !            69: prname()
        !            70: {
        !            71:        if (off<0)
        !            72:                off = 0;
        !            73:        name[off]='\0';
        !            74:        strcat (output, name);
        !            75:        return ;
        !            76: }
        !            77: 
        !            78: cat()
        !            79: {
        !            80:        register i, j;
        !            81: 
        !            82:        i = -1;
        !            83:        while (dir.d_name[++i] != 0);
        !            84:        if ((off+i+2) > 511)
        !            85:                {
        !            86:                fprintf(stderr, "Directory string exceeds 512 characters in length\n");
        !            87:                exit(1);
        !            88:                }
        !            89:        for(j=off+1; j>=0; --j)
        !            90:                name[j+i+1] = name[j];
        !            91:        off=i+off+1;
        !            92:        name[i] = '/';
        !            93:        for(--i; i>=0; --i)
        !            94:                name[i] = dir.d_name[i];
        !            95: }
        !            96: copyto( sdest, source)
        !            97:        char *sdest;
        !            98:        char *source;
        !            99: {
        !           100: char *s, target[100];
        !           101: FILE *f1, *f2;
        !           102: int k;
        !           103: struct stat stbuf;
        !           104: for(s=sdest; *s; s++);
        !           105: while (!isspace(*s) && s>sdest)
        !           106:        s--;
        !           107: if (!isspace(*s))
        !           108:        {
        !           109:        fprintf(stderr, "No destination '%s'\n", sdest);
        !           110:        return;
        !           111:        }
        !           112: if (*++s == '/')
        !           113:        sprintf(target, "%s", s);
        !           114: else
        !           115:        sprintf(target, "%s/%s", pwdir, s);
        !           116: k = stat (target, &stbuf);
        !           117: if (k==0 && (stbuf.st_mode & S_IFDIR)!=0)
        !           118:        {
        !           119:        strcat (target, "/");
        !           120:        strcat (target, source);
        !           121:        }
        !           122: f2 = fopen(target, "a");
        !           123: if (f2==NULL)
        !           124:        {
        !           125:        fprintf(stderr, "Can not open %s\n",target);
        !           126:        return;
        !           127:        }
        !           128: f1 = fopen(source, "r");
        !           129: while ( (k= getc(f1)) != EOF)
        !           130:        putc(k, f2);
        !           131: fclose(f1);
        !           132: fclose(f2);
        !           133: }

unix.superglobalmegacorp.com

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