Annotation of researchv9/cmd/cat.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Concatenate files.
        !             3:  */
        !             4: 
        !             5: #include <stdio.h>
        !             6: #include <sys/types.h>
        !             7: #include <sys/stat.h>
        !             8: #define BLOCK 4096
        !             9: 
        !            10: char   buf[BLOCK];
        !            11: 
        !            12: main (argc, argv)
        !            13:        int argc;
        !            14:        char **argv;
        !            15: {
        !            16:        register int fflg = 0;
        !            17:        register int fi, fo;
        !            18:        register int n;
        !            19:        register int dev, ino = -1;
        !            20:        struct stat statb;
        !            21:        int retcode=0;
        !            22: 
        !            23:        if (fstat (1, &statb) < 0) {
        !            24:                perror ("cat stdout");
        !            25:                return 1;
        !            26:        }
        !            27: 
        !            28:        statb.st_mode &= S_IFMT;
        !            29:        if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
        !            30:                dev = statb.st_dev;
        !            31:                ino = statb.st_ino;
        !            32:        }
        !            33:        if (argc < 2) {
        !            34:                argc = 2;
        !            35:                fflg++;
        !            36:        }
        !            37:        while (--argc > 0) {
        !            38:                if (fflg || **++argv == '-' && (*argv)[1] == '\0')
        !            39:                        fi = 0;
        !            40:                else {
        !            41:                        if ((fi = open (*argv, 0)) < 0) {
        !            42:                                fprintf(stderr, "cat: ");
        !            43:                                perror (*argv);
        !            44:                                retcode = 1;
        !            45:                                continue;
        !            46:                        }
        !            47:                }
        !            48:                fstat (fi, &statb);
        !            49:                if (fstat (fi, &statb) >= 0
        !            50:                    && statb.st_dev == dev
        !            51:                    && statb.st_ino == ino) {
        !            52:                        fprintf (stderr, "cat: input %s is output\n",
        !            53:                           fflg? "-": *argv);
        !            54:                        close (fi);
        !            55:                        retcode = 1;
        !            56:                        continue;
        !            57:                }
        !            58:                while ((n = read (fi, buf, sizeof buf)) > 0)
        !            59:                        if (write (1, buf, n) != n){
        !            60:                                perror ("cat output");
        !            61:                                return 1;
        !            62:                        }
        !            63:                if (fi != 0)
        !            64:                        close (fi);
        !            65:        }
        !            66:        return retcode;
        !            67: }

unix.superglobalmegacorp.com

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