|
|
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: /* ! 30: * st_nlink == 0 for pipes and other magic files ! 31: */ ! 32: if (statb.st_nlink!=0 && statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) { ! 33: dev = statb.st_dev; ! 34: ino = statb.st_ino; ! 35: } ! 36: if (argc < 2) { ! 37: argc = 2; ! 38: fflg++; ! 39: } ! 40: while (--argc > 0) { ! 41: if (fflg || **++argv == '-' && (*argv)[1] == '\0') ! 42: fi = 0; ! 43: else { ! 44: if ((fi = open (*argv, 0)) < 0) { ! 45: fprintf(stderr, "cat: "); ! 46: perror (*argv); ! 47: retcode = 1; ! 48: continue; ! 49: } ! 50: } ! 51: if (fstat (fi, &statb) >= 0 ! 52: && statb.st_dev == dev ! 53: && statb.st_ino == ino) { ! 54: fprintf (stderr, "cat: input %s is output\n", ! 55: fflg? "-": *argv); ! 56: close (fi); ! 57: retcode = 1; ! 58: continue; ! 59: } ! 60: while ((n = read (fi, buf, sizeof buf)) > 0) ! 61: if (write (1, buf, n) != n){ ! 62: perror ("cat output"); ! 63: return 1; ! 64: } ! 65: if (fi != 0) ! 66: close (fi); ! 67: } ! 68: return retcode; ! 69: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.