-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
53 lines (43 loc) · 713 Bytes
/
cat.c
File metadata and controls
53 lines (43 loc) · 713 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
unsigned long bufsiz = BUFSIZ;
static void parse_options(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "z:")) != -1) {
switch(c) {
case 'z':
bufsiz = atol(optarg);
break;
default:
break;
}
}
}
static int cat(FILE *input, FILE *output)
{
while (1) {
int c;
c = fgetc(input);
if (c == EOF)
return -1;
c = fputc(c, stdout);
if (c == EOF)
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
char *buf;
int ret;
parse_options(argc, argv);
buf = malloc(bufsiz);
if (!buf)
return -1;
ret = setvbuf(stdin, buf, _IOLBF, bufsiz);
if (ret)
return -1;
return cat(stdin, stdout);
}