fstat_cwd.c 637 B

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