head.c (100575) | head.c (165940) |
---|---|
1/* 2 * Copyright (c) 1980, 1987, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 29 unchanged lines hidden (view full) --- 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95"; 43#endif 44#endif /* not lint */ 45#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 1980, 1987, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 29 unchanged lines hidden (view full) --- 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95"; 43#endif 44#endif /* not lint */ 45#include <sys/cdefs.h> |
46__FBSDID("$FreeBSD: head/usr.bin/head/head.c 100575 2002-07-23 14:39:20Z jmallett $"); | 46__FBSDID("$FreeBSD: head/usr.bin/head/head.c 165940 2007-01-11 17:03:51Z brooks $"); |
47 48#include <sys/types.h> 49 50#include <ctype.h> 51#include <err.h> | 47 48#include <sys/types.h> 49 50#include <ctype.h> 51#include <err.h> |
52#include <inttypes.h> |
|
52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <unistd.h> 56 57/* 58 * head - give the first few lines of a stream or of each of a set of files 59 * 60 * Bill Joy UCB August 24, 1977 61 */ 62 63static void head(FILE *, int); | 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <unistd.h> 57 58/* 59 * head - give the first few lines of a stream or of each of a set of files 60 * 61 * Bill Joy UCB August 24, 1977 62 */ 63 64static void head(FILE *, int); |
64static void head_bytes(FILE *, size_t); | 65static void head_bytes(FILE *, off_t); |
65static void obsolete(char *[]); 66static void usage(void); 67 68int 69main(int argc, char *argv[]) 70{ 71 int ch; 72 FILE *fp; | 66static void obsolete(char *[]); 67static void usage(void); 68 69int 70main(int argc, char *argv[]) 71{ 72 int ch; 73 FILE *fp; |
73 int first, linecnt = -1, bytecnt = -1, eval = 0; | 74 int first, linecnt = -1, eval = 0; 75 off_t bytecnt = -1; |
74 char *ep; 75 76 obsolete(argv); 77 while ((ch = getopt(argc, argv, "n:c:")) != -1) 78 switch(ch) { 79 case 'c': | 76 char *ep; 77 78 obsolete(argv); 79 while ((ch = getopt(argc, argv, "n:c:")) != -1) 80 switch(ch) { 81 case 'c': |
80 bytecnt = strtol(optarg, &ep, 10); | 82 bytecnt = strtoimax(optarg, &ep, 10); |
81 if (*ep || bytecnt <= 0) 82 errx(1, "illegal byte count -- %s", optarg); 83 break; 84 case 'n': 85 linecnt = strtol(optarg, &ep, 10); 86 if (*ep || linecnt <= 0) 87 errx(1, "illegal line count -- %s", optarg); 88 break; --- 44 unchanged lines hidden (view full) --- 133 error = fwrite(cp, sizeof(char), readlen, stdout); 134 if (error != readlen) 135 err(1, "stdout"); 136 cnt--; 137 } 138} 139 140static void | 83 if (*ep || bytecnt <= 0) 84 errx(1, "illegal byte count -- %s", optarg); 85 break; 86 case 'n': 87 linecnt = strtol(optarg, &ep, 10); 88 if (*ep || linecnt <= 0) 89 errx(1, "illegal line count -- %s", optarg); 90 break; --- 44 unchanged lines hidden (view full) --- 135 error = fwrite(cp, sizeof(char), readlen, stdout); 136 if (error != readlen) 137 err(1, "stdout"); 138 cnt--; 139 } 140} 141 142static void |
141head_bytes(FILE *fp, size_t cnt) | 143head_bytes(FILE *fp, off_t cnt) |
142{ 143 char buf[4096]; 144 size_t readlen; 145 146 while (cnt) { 147 if (cnt < sizeof(buf)) 148 readlen = cnt; 149 else --- 35 unchanged lines hidden --- | 144{ 145 char buf[4096]; 146 size_t readlen; 147 148 while (cnt) { 149 if (cnt < sizeof(buf)) 150 readlen = cnt; 151 else --- 35 unchanged lines hidden --- |