-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcat.c
More file actions
44 lines (37 loc) · 889 Bytes
/
cat.c
File metadata and controls
44 lines (37 loc) · 889 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
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#define BUFFER_SIZ 512
int main(int argc, char *argv[]){
int fd, ret;
char buf[BUFFER_SIZ];
struct stat statbuf;
char *file = argv[1];
if(argc < 2){
// fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
fd = STDIN_FILENO;
}else{
ret = stat(file, &statbuf);
if(ret){
perror("open");
return 1;
}
if(S_ISDIR(statbuf.st_mode)){
fprintf(stderr, "%s is a directory\n", file);
return 1;
}
fd = open(file, O_RDONLY);
if(fd < 0){
perror("open");
return 1;
}
}
while((ret = read(fd, buf, BUFFER_SIZ * sizeof(char))) > 0){
write(STDOUT_FILENO, buf, ret);
}
if(ret < 0){
perror("read");
}
return 0;
}