blob: fcfd40bc1589e9d386393b406c809052041ac789 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
int skip;
int length;
static void
cat(char const *filename, FILE *out)
{
FILE *in;
int c;
if (strcmp (filename, "-") == 0)
in = stdin;
else {
in = fopen(filename, "r");
if (!in) {
perror(filename);
exit(1);
}
}
while ((c = fgetc(in)) != EOF) {
if (skip) {
skip--;
continue;
}
fputc(c, out);
if (length && --length == 0)
break;
}
if (strcmp (filename, "-") == 0 && !feof(in)) {
/* drain input */
while (fgetc(in) != EOF)
;
}
fclose(in);
}
int
main(int argc, char **argv)
{
if (argc == 1)
cat("-", stdout);
else {
int c;
while ((c = getopt(argc, argv, "o:e:w:s:l:")) != EOF) {
switch (c) {
case 'o':
cat(optarg, stdout);
break;
case 'e':
cat(optarg, stderr);
break;
case 'w':
sleep(atoi(optarg));
break;
case 's':
skip = atoi(optarg);
assert(skip >= 0);
break;
case 'l':
length = atoi(optarg);
assert(length >= 0);
break;
default:
return 1;
}
}
argc -= optind;
argv += optind;
while (argc--)
cat(*argv++, stdout);
}
return 0;
}
|