rename.c 609 B

12345678910111213141516171819202122232425262728
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. static char oldpath[] = "old-name.out";
  7. static char newpath[] = "new-name.out";
  8. static char str[] = "Hello, World!";
  9. int str_len = 13;
  10. int main(void) {
  11. char buf[14];
  12. buf[13] = 0x00;
  13. int fd = creat(oldpath, 0777);
  14. write(fd, str, str_len);
  15. close(fd);
  16. rename(oldpath, newpath);
  17. fd = open(newpath, O_RDONLY);
  18. read(fd, buf, str_len);
  19. close(fd);
  20. remove(newpath);
  21. if (strcmp(str, buf) == 0) {
  22. return EXIT_SUCCESS;
  23. } else {
  24. return EXIT_FAILURE;
  25. }
  26. }