Deleted Added
full compact
head.c (92920) head.c (93441)
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

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
43#endif
44static const char rcsid[] =
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

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/usr.bin/head/head.c 92920 2002-03-22 01:22:50Z imp $";
45 "$FreeBSD: head/usr.bin/head/head.c 93441 2002-03-30 17:17:26Z dwmalone $";
46#endif /* not lint */
47
48#include <sys/types.h>
49
50#include <ctype.h>
51#include <err.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
46#endif /* not lint */
47
48#include <sys/types.h>
49
50#include <ctype.h>
51#include <err.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
63void head(FILE *, int);
64void head_bytes(FILE *, int);
65void obsolete(char *[]);
66void usage(void);
63static void head(FILE *, int);
64static void head_bytes(FILE *, size_t);
65static void obsolete(char *[]);
66static void usage(void);
67
68int
69main(argc, argv)
70 int argc;
71 char *argv[];
72{
67
68int
69main(argc, argv)
70 int argc;
71 char *argv[];
72{
73 register int ch;
73 int ch;
74 FILE *fp;
75 int first, linecnt = -1, bytecnt = -1, eval = 0;
76 char *ep;
77
78 obsolete(argv);
79 while ((ch = getopt(argc, argv, "n:c:")) != -1)
80 switch(ch) {
81 case 'c':

--- 38 unchanged lines hidden (view full) ---

120 } else if (bytecnt == -1)
121 head(stdin, linecnt);
122 else
123 head_bytes(stdin, bytecnt);
124
125 exit(eval);
126}
127
74 FILE *fp;
75 int first, linecnt = -1, bytecnt = -1, eval = 0;
76 char *ep;
77
78 obsolete(argv);
79 while ((ch = getopt(argc, argv, "n:c:")) != -1)
80 switch(ch) {
81 case 'c':

--- 38 unchanged lines hidden (view full) ---

120 } else if (bytecnt == -1)
121 head(stdin, linecnt);
122 else
123 head_bytes(stdin, bytecnt);
124
125 exit(eval);
126}
127
128void
128static void
129head(fp, cnt)
130 FILE *fp;
129head(fp, cnt)
130 FILE *fp;
131 register int cnt;
131 int cnt;
132{
133 char *cp;
132{
133 char *cp;
134 int error, readlen;
134 size_t error, readlen;
135
136 while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
137 error = fwrite(cp, sizeof(char), readlen, stdout);
138 if (error != readlen)
139 err(1, "stdout");
140 cnt--;
141 }
142}
143
135
136 while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
137 error = fwrite(cp, sizeof(char), readlen, stdout);
138 if (error != readlen)
139 err(1, "stdout");
140 cnt--;
141 }
142}
143
144void
144static void
145head_bytes(fp, cnt)
146 FILE *fp;
145head_bytes(fp, cnt)
146 FILE *fp;
147 register int cnt;
147 size_t cnt;
148{
149 char buf[4096];
148{
149 char buf[4096];
150 register int readlen;
150 size_t readlen;
151
152 while (cnt) {
153 if (cnt < sizeof(buf))
154 readlen = cnt;
155 else
156 readlen = sizeof(buf);
157 readlen = fread(buf, sizeof(char), readlen, fp);
158 if (readlen == 0)
159 break;
160 if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
161 err(1, "stdout");
162 cnt -= readlen;
163 }
164}
165
151
152 while (cnt) {
153 if (cnt < sizeof(buf))
154 readlen = cnt;
155 else
156 readlen = sizeof(buf);
157 readlen = fread(buf, sizeof(char), readlen, fp);
158 if (readlen == 0)
159 break;
160 if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
161 err(1, "stdout");
162 cnt -= readlen;
163 }
164}
165
166void
166static void
167obsolete(argv)
168 char *argv[];
169{
170 char *ap;
171
172 while ((ap = *++argv)) {
173 /* Return if "--" or not "-[0-9]*". */
174 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
175 return;
176 if ((ap = malloc(strlen(*argv) + 2)) == NULL)
177 err(1, NULL);
178 ap[0] = '-';
179 ap[1] = 'n';
180 (void)strcpy(ap + 2, *argv + 1);
181 *argv = ap;
182 }
183}
184
167obsolete(argv)
168 char *argv[];
169{
170 char *ap;
171
172 while ((ap = *++argv)) {
173 /* Return if "--" or not "-[0-9]*". */
174 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
175 return;
176 if ((ap = malloc(strlen(*argv) + 2)) == NULL)
177 err(1, NULL);
178 ap[0] = '-';
179 ap[1] = 'n';
180 (void)strcpy(ap + 2, *argv + 1);
181 *argv = ap;
182 }
183}
184
185void
185static void
186usage()
187{
188
189 (void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
190 exit(1);
191}
186usage()
187{
188
189 (void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
190 exit(1);
191}