|
Lines 60-67
Link Here
|
| 60 |
* Bill Joy UCB August 24, 1977 |
60 |
* Bill Joy UCB August 24, 1977 |
| 61 |
*/ |
61 |
*/ |
| 62 |
|
62 |
|
| 63 |
void head __P((FILE *, int)); |
63 |
void head __P((FILE *, int, int)); |
| 64 |
void head_bytes __P((FILE *, int)); |
64 |
void head_bytes __P((FILE *, int, int)); |
| 65 |
void obsolete __P((char *[])); |
65 |
void obsolete __P((char *[])); |
| 66 |
void usage __P((void)); |
66 |
void usage __P((void)); |
| 67 |
|
67 |
|
|
Lines 74-84
Link Here
|
| 74 |
{ |
74 |
{ |
| 75 |
register int ch; |
75 |
register int ch; |
| 76 |
FILE *fp; |
76 |
FILE *fp; |
| 77 |
int first, linecnt = -1, bytecnt = -1; |
77 |
int first, linecnt = -1, bytecnt = -1, skipcnt = 0; |
| 78 |
char *ep; |
78 |
char *ep; |
| 79 |
|
79 |
|
| 80 |
obsolete(argv); |
80 |
obsolete(argv); |
| 81 |
while ((ch = getopt(argc, argv, "n:c:")) != -1) |
81 |
while ((ch = getopt(argc, argv, "n:c:s:")) != -1) |
| 82 |
switch(ch) { |
82 |
switch(ch) { |
| 83 |
case 'c': |
83 |
case 'c': |
| 84 |
bytecnt = strtol(optarg, &ep, 10); |
84 |
bytecnt = strtol(optarg, &ep, 10); |
|
Lines 90-95
Link Here
|
| 90 |
if (*ep || linecnt <= 0) |
90 |
if (*ep || linecnt <= 0) |
| 91 |
errx(1, "illegal line count -- %s", optarg); |
91 |
errx(1, "illegal line count -- %s", optarg); |
| 92 |
break; |
92 |
break; |
|
|
93 |
case 's': |
| 94 |
skipcnt = strtol(optarg, &ep, 10); |
| 95 |
if (*ep || skipcnt <= 0) |
| 96 |
errx(1, "illegal skip count -- %s", optarg); |
| 97 |
break; |
| 93 |
case '?': |
98 |
case '?': |
| 94 |
default: |
99 |
default: |
| 95 |
usage(); |
100 |
usage(); |
|
Lines 113-152
Link Here
|
| 113 |
first ? "" : "\n", *argv); |
118 |
first ? "" : "\n", *argv); |
| 114 |
first = 0; |
119 |
first = 0; |
| 115 |
} |
120 |
} |
| 116 |
if (bytecnt == -1) |
121 |
if (bytecnt == -1) { |
| 117 |
head(fp, linecnt); |
122 |
if (skipcnt) head(fp, skipcnt, 0); |
| 118 |
else |
123 |
head(fp, linecnt, 1); |
| 119 |
head_bytes(fp, bytecnt); |
124 |
} else { |
|
|
125 |
if (skipcnt) head_bytes(fp, skipcnt, 0); |
| 126 |
head_bytes(fp, bytecnt, 1); |
| 127 |
} |
| 120 |
(void)fclose(fp); |
128 |
(void)fclose(fp); |
| 121 |
} |
129 |
} |
| 122 |
} |
130 |
} |
| 123 |
else if (bytecnt == -1) |
131 |
else if (bytecnt == -1) { |
| 124 |
head(stdin, linecnt); |
132 |
if (skipcnt) head(stdin, skipcnt, 0); |
| 125 |
else |
133 |
head(stdin, linecnt, 1); |
| 126 |
head_bytes(stdin, bytecnt); |
134 |
} else { |
|
|
135 |
if (skipcnt) head_bytes(stdin, skipcnt, 0); |
| 136 |
head_bytes(stdin, bytecnt, 1); |
| 137 |
} |
| 127 |
|
138 |
|
| 128 |
exit(eval); |
139 |
exit(eval); |
| 129 |
} |
140 |
} |
| 130 |
|
141 |
|
| 131 |
void |
142 |
void |
| 132 |
head(fp, cnt) |
143 |
head(fp, cnt, out) |
| 133 |
FILE *fp; |
144 |
FILE *fp; |
| 134 |
register int cnt; |
145 |
register int cnt; |
|
|
146 |
int out; |
| 135 |
{ |
147 |
{ |
| 136 |
register int ch; |
148 |
register int ch; |
| 137 |
|
149 |
|
| 138 |
while (cnt && (ch = getc(fp)) != EOF) { |
150 |
while (cnt && (ch = getc(fp)) != EOF) { |
|
|
151 |
if (out) |
| 139 |
if (putchar(ch) == EOF) |
152 |
if (putchar(ch) == EOF) |
| 140 |
err(1, "stdout"); |
153 |
err(1, "stdout"); |
| 141 |
if (ch == '\n') |
154 |
if (ch == '\n') |
| 142 |
cnt--; |
155 |
cnt--; |
| 143 |
} |
156 |
} |
| 144 |
} |
157 |
} |
| 145 |
|
158 |
|
| 146 |
void |
159 |
void |
| 147 |
head_bytes(fp, cnt) |
160 |
head_bytes(fp, cnt, out) |
| 148 |
FILE *fp; |
161 |
FILE *fp; |
| 149 |
register int cnt; |
162 |
register int cnt; |
|
|
163 |
int out; |
| 150 |
{ |
164 |
{ |
| 151 |
char buf[4096]; |
165 |
char buf[4096]; |
| 152 |
register int readlen; |
166 |
register int readlen; |
|
Lines 159-166
Link Here
|
| 159 |
readlen = fread(buf, sizeof(char), readlen, fp); |
173 |
readlen = fread(buf, sizeof(char), readlen, fp); |
| 160 |
if (readlen == 0) |
174 |
if (readlen == 0) |
| 161 |
break; |
175 |
break; |
| 162 |
if (fwrite(buf, sizeof(char), readlen, stdout) != readlen) |
176 |
if (out) |
| 163 |
err(1, "stdout"); |
177 |
if (fwrite(buf, sizeof(char), readlen, stdout) != readlen) |
|
|
178 |
err(1, "stdout"); |
| 164 |
cnt -= readlen; |
179 |
cnt -= readlen; |
| 165 |
} |
180 |
} |
| 166 |
} |
181 |
} |