2
0

rename.c 568 B

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