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
| #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <string.h>
#define READ_BUFSIZE 256
int main(void) { int dir_fd = open(".", O_RDONLY); if (dir_fd < 0) { perror("dir open failed"); exit(1); } int data_fd = openat(dir_fd, "data.txt", O_RDONLY); if (data_fd < 0) { perror("data.txt open failed"); close(dir_fd); exit(1); }
printf("dir_fd: %d, data_fd: %d\n", dir_fd, data_fd);
char buf[READ_BUFSIZE]; ssize_t bytes, total = 0; while ((bytes = read(data_fd, buf, READ_BUFSIZE)) > 0) { printf("byted read: %d\n", (int)bytes); total += bytes; } close(data_fd); close(dir_fd); if (bytes == -1) { perror("read error"); exit(1); }
printf("read from data.txt: %d bytes\n", (int)total);
int not_exist_fd = openat(AT_FDCWD, "no_such_file.txt", O_RDONLY); if (not_exist_fd == -1) { perror("open \"no_such_file.txt\""); } else { close(not_exist_fd); } exit(0); }
|