blob: 1c3e40a3d79da664f6fd93cf6617546d4a6193d3 (
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
|
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
void fileoutput(FILE *, const char *);
bool isdir(const char *);
/* concatinate files to a file stream */
int main(int argc, char **argv)
{
FILE *fp;
if (argc == 1) {
fileoutput(stdin, "stdin");
} else {
while (--argc > 0 && !ferror(stdout)) {
if (isdir(*++argv)) {
perror("cat");
continue;
}
if ((fp = fopen(*argv, "r")) == NULL) {
perror("cat");
continue;
}
fileoutput(fp, *argv);
}
}
if (ferror(stdout)) {
perror("cat");
return -1;
}
return 0;
}
/* fileoutput: output a file to stdout */
void fileoutput(FILE *fp, const char *name)
{
int c;
while ((c = getc(fp)) != EOF)
putc(c, stdout);
if (ferror(fp)) {
perror("cat");
}
fclose(fp);
}
/* isdir: return true if the file as the path is a directory */
bool isdir(const char *path)
{
struct stat sbuf;
stat(path, &sbuf);
return S_ISDIR(sbuf.st_mode);
}
|