Advertisement
STANAANDREY

Untitled

Mar 22nd, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #define NAME_MAX 101
  7.  
  8.  
  9. int getFD(const char *const fname, int flags, int perm) {
  10. int fd = open(fname, flags, perm);
  11. if (fd < 0) {
  12. perror("");
  13. exit(1);
  14. }
  15. return fd;
  16. }
  17.  
  18. void closeFD(int fd) {
  19. if (close(fd) < 0) {
  20. perror("");
  21. exit(1);
  22. }
  23. }
  24.  
  25. int main(int argc, char *argv[]) {
  26. if (argc != 2) {
  27. fprintf(stderr, "Wrong usage!\n");
  28. exit(1);
  29. }
  30. int fd = getFD(argv[1], O_RDONLY, 0);
  31.  
  32. static char name[NAME_MAX];
  33. read(fd, name, NAME_MAX);
  34. printf("name: %s\n", name);
  35.  
  36. int fsize;
  37. lseek(fd, 124, SEEK_SET);
  38. read(fd, &fsize, 12);
  39. printf("size: %d\n", 3);
  40.  
  41. closeFD(fd);
  42. return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement