|
Lines 43-48
Link Here
|
| 43 |
"$FreeBSD: src/usr.bin/cut/cut.c,v 1.12 2001/02/06 20:03:48 charnier Exp $"; |
43 |
"$FreeBSD: src/usr.bin/cut/cut.c,v 1.12 2001/02/06 20:03:48 charnier Exp $"; |
| 44 |
#endif /* not lint */ |
44 |
#endif /* not lint */ |
| 45 |
|
45 |
|
|
|
46 |
#include <assert.h> |
| 46 |
#include <ctype.h> |
47 |
#include <ctype.h> |
| 47 |
#include <err.h> |
48 |
#include <err.h> |
| 48 |
#include <limits.h> |
49 |
#include <limits.h> |
|
Lines 228-246
Link Here
|
| 228 |
int ch, field, isdelim; |
229 |
int ch, field, isdelim; |
| 229 |
char *pos, *p, sep; |
230 |
char *pos, *p, sep; |
| 230 |
int output; |
231 |
int output; |
| 231 |
char lbuf[_POSIX2_LINE_MAX + 1]; |
232 |
char *lbuf, *mlbuf = NULL; |
|
|
233 |
size_t lbuflen; |
| 232 |
|
234 |
|
| 233 |
for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) { |
235 |
for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) { |
|
|
236 |
/* Assert EOL has a newline. */ |
| 237 |
if (*(lbuf + lbuflen - 1) != '\n') { |
| 238 |
/* Can't have > 1 line with no trailing newline. */ |
| 239 |
assert(mlbuf == NULL); |
| 240 |
mlbuf = malloc(lbuflen + 1); |
| 241 |
if (mlbuf == NULL) |
| 242 |
err(1, "malloc"); |
| 243 |
memcpy(mlbuf, lbuf, lbuflen); |
| 244 |
*(mlbuf + lbuflen) = '\n'; |
| 245 |
lbuf = mlbuf; |
| 246 |
} |
| 234 |
output = 0; |
247 |
output = 0; |
| 235 |
for (isdelim = 0, p = lbuf;; ++p) { |
248 |
for (isdelim = 0, p = lbuf; p < lbuf + lbuflen; ++p) { |
| 236 |
if (!(ch = *p)) |
249 |
ch = *p; |
| 237 |
errx(1, "%s: line too long.", fname); |
|
|
| 238 |
/* this should work if newline is delimiter */ |
250 |
/* this should work if newline is delimiter */ |
| 239 |
if (ch == sep) |
251 |
if (ch == sep) |
| 240 |
isdelim = 1; |
252 |
isdelim = 1; |
| 241 |
if (ch == '\n') { |
253 |
if (ch == '\n') { |
| 242 |
if (!isdelim && !sflag) |
254 |
if (!isdelim && !sflag) |
| 243 |
(void)printf("%s", lbuf); |
255 |
(void)fwrite(lbuf, lbuflen, 1, stdout); |
| 244 |
break; |
256 |
break; |
| 245 |
} |
257 |
} |
| 246 |
} |
258 |
} |
|
Lines 272-277
Link Here
|
| 272 |
} |
284 |
} |
| 273 |
(void)putchar('\n'); |
285 |
(void)putchar('\n'); |
| 274 |
} |
286 |
} |
|
|
287 |
if (mlbuf != NULL) |
| 288 |
free(mlbuf); |
| 275 |
} |
289 |
} |
| 276 |
|
290 |
|
| 277 |
static void |
291 |
static void |