fstat_cwd.c 657 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. int main (int argc, char** argv) {
  11. int r, fd;
  12. struct stat buf;
  13. fd = open(".", O_DIRECTORY);
  14. if (fd == -1) {
  15. printf("Opening CWD returned error %d\n", errno);
  16. return -1;
  17. }
  18. r = fstat(fd, &buf);
  19. if (r == -1) {
  20. printf("fstat on directory fd returned error %d\n", errno);
  21. return -1;
  22. }
  23. close(fd);
  24. if (S_ISDIR(buf.st_mode))
  25. printf("fstat returned the fd type as S_IFDIR\n");
  26. return 0;
  27. }