main.c 634 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. int main() {
  5. int fd = open("/history_commands.txt", O_RDONLY);
  6. if (fd < 0) {
  7. perror("Failed to open file");
  8. return 1;
  9. }
  10. int new_fd = 777;
  11. int rt = dup3(fd, new_fd, O_CLOEXEC);
  12. if (rt < 0) {
  13. perror("Failed to duplicate file descriptor with flags");
  14. }
  15. char buffer[100];
  16. int bytes_read = read(new_fd, buffer, sizeof(buffer));
  17. if (bytes_read < 0) {
  18. perror("Failed to read data");
  19. return 1;
  20. }
  21. printf("Data:\n %.*s\n", bytes_read, buffer);
  22. close(fd);
  23. close(new_fd);
  24. return 0;
  25. }