cat.c (225736) | cat.c (240407) |
---|---|
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kevin Fall. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 30 unchanged lines hidden (view full) --- 39#endif 40 41#ifndef lint 42#if 0 43static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95"; 44#endif 45#endif /* not lint */ 46#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kevin Fall. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 30 unchanged lines hidden (view full) --- 39#endif 40 41#ifndef lint 42#if 0 43static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95"; 44#endif 45#endif /* not lint */ 46#include <sys/cdefs.h> |
47__FBSDID("$FreeBSD: stable/9/bin/cat/cat.c 184471 2008-10-30 14:05:57Z ivoras $"); | 47__FBSDID("$FreeBSD: stable/9/bin/cat/cat.c 240407 2012-09-12 15:59:03Z jh $"); |
48 49#include <sys/param.h> 50#include <sys/stat.h> 51#ifndef NO_UDOM_SUPPORT 52#include <sys/socket.h> 53#include <sys/un.h> 54#include <errno.h> 55#endif 56 57#include <ctype.h> 58#include <err.h> 59#include <fcntl.h> 60#include <locale.h> | 48 49#include <sys/param.h> 50#include <sys/stat.h> 51#ifndef NO_UDOM_SUPPORT 52#include <sys/socket.h> 53#include <sys/un.h> 54#include <errno.h> 55#endif 56 57#include <ctype.h> 58#include <err.h> 59#include <fcntl.h> 60#include <locale.h> |
61#include <stddef.h> |
|
61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> | 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <unistd.h> |
65#include <stddef.h> | |
66 67int bflag, eflag, nflag, sflag, tflag, vflag; 68int rval; 69const char *filename; 70 71static void usage(void); 72static void scanfiles(char *argv[], int cooked); 73static void cook_cat(FILE *); 74static void raw_cat(int); 75 76#ifndef NO_UDOM_SUPPORT 77static int udom_open(const char *path, int flags); 78#endif 79 | 66 67int bflag, eflag, nflag, sflag, tflag, vflag; 68int rval; 69const char *filename; 70 71static void usage(void); 72static void scanfiles(char *argv[], int cooked); 73static void cook_cat(FILE *); 74static void raw_cat(int); 75 76#ifndef NO_UDOM_SUPPORT 77static int udom_open(const char *path, int flags); 78#endif 79 |
80/* Memory strategy threshold, in pages: if physmem is larger then this, use a 81 * large buffer */ 82#define PHYSPAGES_THRESHOLD (32*1024) | 80/* 81 * Memory strategy threshold, in pages: if physmem is larger than this, 82 * use a large buffer. 83 */ 84#define PHYSPAGES_THRESHOLD (32 * 1024) |
83 | 85 |
84/* Maximum buffer size in bytes - do not allow it to grow larger than this */ 85#define BUFSIZE_MAX (2*1024*1024) | 86/* Maximum buffer size in bytes - do not allow it to grow larger than this. */ 87#define BUFSIZE_MAX (2 * 1024 * 1024) |
86 | 88 |
87/* Small (default) buffer size in bytes. It's inefficient for this to be 88 * smaller than MAXPHYS */ 89#define BUFSIZE_SMALL (MAXPHYS) | 89/* 90 * Small (default) buffer size in bytes. It's inefficient for this to be 91 * smaller than MAXPHYS. 92 */ 93#define BUFSIZE_SMALL (MAXPHYS) |
90 91int 92main(int argc, char *argv[]) 93{ 94 int ch; 95 96 setlocale(LC_CTYPE, ""); 97 --- 41 unchanged lines hidden (view full) --- 139 fprintf(stderr, "usage: cat [-benstuv] [file ...]\n"); 140 exit(1); 141 /* NOTREACHED */ 142} 143 144static void 145scanfiles(char *argv[], int cooked) 146{ | 94 95int 96main(int argc, char *argv[]) 97{ 98 int ch; 99 100 setlocale(LC_CTYPE, ""); 101 --- 41 unchanged lines hidden (view full) --- 143 fprintf(stderr, "usage: cat [-benstuv] [file ...]\n"); 144 exit(1); 145 /* NOTREACHED */ 146} 147 148static void 149scanfiles(char *argv[], int cooked) 150{ |
147 int i = 0; | 151 int fd, i; |
148 char *path; 149 FILE *fp; 150 | 152 char *path; 153 FILE *fp; 154 |
155 i = 0; |
|
151 while ((path = argv[i]) != NULL || i == 0) { | 156 while ((path = argv[i]) != NULL || i == 0) { |
152 int fd; 153 | |
154 if (path == NULL || strcmp(path, "-") == 0) { 155 filename = "stdin"; 156 fd = STDIN_FILENO; 157 } else { 158 filename = path; 159 fd = open(path, O_RDONLY); 160#ifndef NO_UDOM_SUPPORT 161 if (fd < 0 && errno == EOPNOTSUPP) --- 94 unchanged lines hidden (view full) --- 256 257 wfd = fileno(stdout); 258 if (buf == NULL) { 259 if (fstat(wfd, &sbuf)) 260 err(1, "%s", filename); 261 if (S_ISREG(sbuf.st_mode)) { 262 /* If there's plenty of RAM, use a large copy buffer */ 263 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD) | 157 if (path == NULL || strcmp(path, "-") == 0) { 158 filename = "stdin"; 159 fd = STDIN_FILENO; 160 } else { 161 filename = path; 162 fd = open(path, O_RDONLY); 163#ifndef NO_UDOM_SUPPORT 164 if (fd < 0 && errno == EOPNOTSUPP) --- 94 unchanged lines hidden (view full) --- 259 260 wfd = fileno(stdout); 261 if (buf == NULL) { 262 if (fstat(wfd, &sbuf)) 263 err(1, "%s", filename); 264 if (S_ISREG(sbuf.st_mode)) { 265 /* If there's plenty of RAM, use a large copy buffer */ 266 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD) |
264 bsize = MIN(BUFSIZE_MAX, MAXPHYS*8); | 267 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8); |
265 else 266 bsize = BUFSIZE_SMALL; 267 } else | 268 else 269 bsize = BUFSIZE_SMALL; 270 } else |
268 bsize = MAX(sbuf.st_blksize, 269 (blksize_t)sysconf(_SC_PAGESIZE)); | 271 bsize = MAX(sbuf.st_blksize, 272 (blksize_t)sysconf(_SC_PAGESIZE)); |
270 if ((buf = malloc(bsize)) == NULL) 271 err(1, "malloc() failure of IO buffer"); 272 } 273 while ((nr = read(rfd, buf, bsize)) > 0) 274 for (off = 0; nr; nr -= nw, off += nw) 275 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) 276 err(1, "stdout"); 277 if (nr < 0) { --- 44 unchanged lines hidden (view full) --- 322 case O_WRONLY: 323 if (shutdown(fd, SHUT_RD) == -1) 324 warn(NULL); 325 break; 326 default: 327 break; 328 } 329 } | 273 if ((buf = malloc(bsize)) == NULL) 274 err(1, "malloc() failure of IO buffer"); 275 } 276 while ((nr = read(rfd, buf, bsize)) > 0) 277 for (off = 0; nr; nr -= nw, off += nw) 278 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) 279 err(1, "stdout"); 280 if (nr < 0) { --- 44 unchanged lines hidden (view full) --- 325 case O_WRONLY: 326 if (shutdown(fd, SHUT_RD) == -1) 327 warn(NULL); 328 break; 329 default: 330 break; 331 } 332 } |
330 return(fd); | 333 return (fd); |
331} 332 333#endif | 334} 335 336#endif |