]> git.sur5r.net Git - cc65/blob - testcode/lib/rename-test.c
Fixed _textcolor definition.
[cc65] / testcode / lib / rename-test.c
1 /* rename-test.c
2 **
3 **
4 ** A simple test of the rename function.
5 **
6 ** 2008-10-06, Greg King
7 */
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13
14 static FILE* file;
15 static int r;
16 static char name1[16], name2[16];
17
18 int main(void)
19 {
20     /* Generate two temporary file-names that have a random, unused spelling. */
21     _randomize();
22     for (;;) {
23         r = rand();
24         sprintf(name1, "r%04.4u.1", (unsigned)r);
25         sprintf(name2, "r%04.4u.2", (unsigned)r);
26
27         /* Ensure that neither file-name exists. */
28         errno = 0;
29         file = fopen(name1, "r");
30         if (file != NULL) {
31             fclose(file);
32             continue;                   /* try a different spelling */
33         }
34
35         /* Make sure that fopen() failed for the right reason. */
36         if (errno != ENOENT) {
37             perror("Disk error with first name");
38             return EXIT_FAILURE;
39         }
40
41         errno = 0;
42         file = fopen(name2, "r");
43         if (file != NULL) {
44             fclose(file);
45             continue;
46         }
47         if (errno != ENOENT) {
48             perror("Disk error with second name");
49             return EXIT_FAILURE;
50         }
51
52         break;                          /* neither one exists; do next step */
53     }
54
55     /* Create the first file.
56     ** Close it without writing anything because only its name is important.
57     */
58     printf("Creating file: %s\n", name1);
59     file = fopen(name1, "w");
60     if (file == NULL) {
61         _poserror("Disk error making first file");
62         return EXIT_FAILURE;
63     }
64     fclose(file);
65
66     /* Verify that the file-name exists now. */
67     file = fopen(name1, "r");
68     if (file == NULL) {
69         _poserror("Cannot find first name");
70         return EXIT_FAILURE;
71     }
72     fclose(file);
73
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
76     ** file-name.
77     */
78     printf("Renaming %s to %s\n", name1, name2);
79     r = rename(name1, name2);
80     if (r < 0) {
81         _poserror("rename() failed");
82         return EXIT_FAILURE;
83     }
84
85     /* Verify that the first file-name no longer exists. */
86     file = fopen(name1, "r");
87     if (file != NULL) {
88         fclose(file);
89         _poserror("First name still exists");
90         return EXIT_FAILURE;
91     }
92
93     /* Verify that the second file-name exists now. */
94     file = fopen(name2, "r");
95     if (file == NULL) {
96         _poserror("Cannot find second name");
97         return EXIT_FAILURE;
98     }
99     fclose(file);
100
101     printf("Success!\n");
102
103     /* Delete the second (temporary) name. */
104     printf("Removing %s\n", name2);
105     r = remove(name2);
106     if (r < 0) {
107         _poserror("remove() failed");
108         return EXIT_FAILURE;
109     }
110
111     printf("rename() passed the test.\n");
112     return EXIT_SUCCESS;
113 }
114
115