详见:嵌入式大讲堂
1 FRESULT f_chdir ( 2 const TCHAR *path /* Pointer to the directory path */ 3 ) 4 { 5 FRESULT res; 6 DIR dj; 7 DEF_NAMEBUF; 8 9 10 res = chk_mounted(&path, &dj.fs, 0); 11 if (res == FR_OK) { 12 INIT_BUF(dj); 13 res = follow_path(&dj, path); /* Follow the path */ 14 FREE_BUF(); 15 if (res == FR_OK) { /* Follow completed */ 16 if (!dj.dir) { 17 dj.fs->cdir = dj.sclust; /* Start directory itself */ 18 } else { 19 if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */ 20 dj.fs->cdir = LD_CLUST(dj.dir); 21 else 22 res = FR_NO_PATH; /* Reached but a file */ 23 } 24 } 25 if (res == FR_NO_FILE) res = FR_NO_PATH; 26 } 27 28 LEAVE_FF(dj.fs, res); 29 }