]> cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/doc/res/app2.c
Import fatfs R0.12b
[z180-stamp.git] / fatfs / doc / res / app2.c
1 /*------------------------------------------------------------/
2 / Remove all contents of a directory
3 / This function works regardless of _FS_RPATH.
4 /------------------------------------------------------------*/
5
6
7 FRESULT empty_directory (
8 char* path /* Working buffer filled with start directory */
9 )
10 {
11 UINT i, j;
12 FRESULT fr;
13 DIR dir;
14 FILINFO fno;
15
16 #if _USE_LFN
17 fno.lfname = 0; /* Disable LFN output */
18 #endif
19 fr = f_opendir(&dir, path);
20 if (fr == FR_OK) {
21 for (i = 0; path[i]; i++) ;
22 path[i++] = '/';
23 for (;;) {
24 fr = f_readdir(&dir, &fno);
25 if (fr != FR_OK || !fno.fname[0]) break;
26 if (_FS_RPATH && fno.fname[0] == '.') continue;
27 j = 0;
28 do
29 path[i+j] = fno.fname[j];
30 while (fno.fname[j++]);
31 if (fno.fattrib & AM_DIR) {
32 fr = empty_directory(path);
33 if (fr != FR_OK) break;
34 }
35 fr = f_unlink(path);
36 if (fr != FR_OK) break;
37 }
38 path[--i] = '\0';
39 closedir(&dir);
40 }
41
42 return fr;
43 }
44
45
46
47 int main (void)
48 {
49 FRESULT fr;
50 FATFS fs;
51 char buff[64]; /* Working buffer */
52
53
54
55 f_mount(&fs, "", 0);
56
57 strcpy(buff, "/"); /* Directory to be emptied */
58 fr = empty_directory(buff);
59
60 if (fr) {
61 printf("Function failed. (%u)\n", fr);
62 return fr;
63 } else {
64 printf("All contents in the %s are successfully removed.\n", buff);
65 return 0;
66 }
67 }
68
69
70