1 static char *sccsid = "@(#)cat.c 4.1 (Berkeley) 10/01/80"; 2 /* 3 * Concatenate files. 4 */ 5 6 #include <stdio.h> 7 #include <sys/types.h> 8 #include <sys/stat.h> 9 10 char stdbuf[BUFSIZ]; 11 12 main(argc, argv) 13 char **argv; 14 { 15 int fflg = 0; 16 register FILE *fi; 17 register c; 18 int dev, ino = -1; 19 struct stat statb; 20 21 setbuf(stdout, stdbuf); 22 for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) { 23 switch(argv[1][1]) { 24 case 0: 25 break; 26 case 'u': 27 setbuf(stdout, (char *)NULL); 28 continue; 29 } 30 break; 31 } 32 fstat(fileno(stdout), &statb); 33 statb.st_mode &= S_IFMT; 34 if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) { 35 dev = statb.st_dev; 36 ino = statb.st_ino; 37 } 38 if (argc < 2) { 39 argc = 2; 40 fflg++; 41 } 42 while (--argc > 0) { 43 if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0') 44 fi = stdin; 45 else { 46 if ((fi = fopen(*argv, "r")) == NULL) { 47 fprintf(stderr, "cat: can't open %s\n", *argv); 48 continue; 49 } 50 } 51 fstat(fileno(fi), &statb); 52 if (statb.st_dev==dev && statb.st_ino==ino) { 53 fprintf(stderr, "cat: input %s is output\n", 54 fflg?"-": *argv); 55 fclose(fi); 56 continue; 57 } 58 while ((c = getc(fi)) != EOF) 59 putchar(c); 60 if (fi!=stdin) 61 fclose(fi); 62 } 63 return(0); 64 } 65