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