4 ** A simple test of the rename function.
6 ** 2008-10-06, Greg King
16 static char name1[16], name2[16];
20 /* Generate two temporary file-names that have a random, unused spelling. */
24 sprintf(name1, "r%04.4u.1", (unsigned)r);
25 sprintf(name2, "r%04.4u.2", (unsigned)r);
27 /* Ensure that neither file-name exists. */
29 file = fopen(name1, "r");
32 continue; /* try a different spelling */
35 /* Make sure that fopen() failed for the right reason. */
36 if (errno != ENOENT) {
37 perror("Disk error with first name");
42 file = fopen(name2, "r");
47 if (errno != ENOENT) {
48 perror("Disk error with second name");
52 break; /* neither one exists; do next step */
55 /* Create the first file.
56 ** Close it without writing anything because only its name is important.
58 printf("Creating file: %s\n", name1);
59 file = fopen(name1, "w");
61 _poserror("Disk error making first file");
66 /* Verify that the file-name exists now. */
67 file = fopen(name1, "r");
69 _poserror("Cannot find first name");
74 /* Whew! Finally, we get to the reason why this program exists:
75 ** Confirm that the first file-name can be changed into the second
78 printf("Renaming %s to %s\n", name1, name2);
79 r = rename(name1, name2);
81 _poserror("rename() failed");
85 /* Verify that the first file-name no longer exists. */
86 file = fopen(name1, "r");
89 _poserror("First name still exists");
93 /* Verify that the second file-name exists now. */
94 file = fopen(name2, "r");
96 _poserror("Cannot find second name");
101 printf("Success!\n");
103 /* Delete the second (temporary) name. */
104 printf("Removing %s\n", name2);
107 _poserror("remove() failed");
111 printf("rename() passed the test.\n");