1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <fcntl.h> 5 #include <errno.h> 6 #include <string.h> 7 8 9 int 10 main(int argc,char **argv) 11 { 12 int file; 13 14 // clean up (may not be necessary) 15 remove("__directory/1"); 16 remove("__directory"); 17 remove("__file"); 18 19 file = open("__file", O_CREAT | O_TRUNC | O_WRONLY); 20 if (file < 0) 21 return -1; 22 close(file); 23 24 if (chmod("__file", 0644) != 0) 25 printf("chmod fails: %s\n", strerror(errno)); 26 27 if (mkdir("__directory", 0755) != 0) 28 return -1; 29 30 // create a file in that directory 31 file = open("__directory/1", O_CREAT | O_WRONLY); 32 close(file); 33 34 errno = 0; 35 36 puts("rename test: Overwrite a directory with files in it"); 37 printf(" %s\n",rename("__file", "__directory") ? "Could not rename file!" : "Rename succeeded."); 38 printf(" errno = %d (%s)\n", errno, strerror(errno)); 39 40 remove("__directory/1"); 41 errno = 0; 42 43 // rename the file and try to remove the directory 44 puts("rename test: Overwrite an empty directory"); 45 printf(" %s\n", rename("__file","__directory") ? "Could not rename file!" : "Rename succeeded."); 46 printf(" errno = %d (%s)\n", errno, strerror(errno)); 47 48 remove("__directory"); 49 remove("__file"); 50 51 return 0; 52 } 53