fstat_cwd.c 611 B

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