fchdir.c 776 B

123456789101112131415161718192021222324252627282930313233
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int main(void) {
  6. int fd = open("..", 0, 0);
  7. if (fd == -1) {
  8. perror("open");
  9. exit(EXIT_FAILURE);
  10. } else if (fd < 0) {
  11. printf("open returned %d, unexpected result\n", fd);
  12. exit(EXIT_FAILURE);
  13. }
  14. int status = fchdir(fd);
  15. if (status == -1) {
  16. perror("fchdir");
  17. exit(EXIT_FAILURE);
  18. } else if (status != 0) {
  19. printf("fchdir returned %d, unexpected result\n", status);
  20. exit(EXIT_FAILURE);
  21. }
  22. int c = close(fd);
  23. if (c == -1) {
  24. perror("close");
  25. exit(EXIT_FAILURE);
  26. } else if (c != 0) {
  27. printf("close returned %d, unexpected result\n", c);
  28. exit(EXIT_FAILURE);
  29. }
  30. }