dfs_posix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * File : dfs_posix.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-05-27 Yi.qiu The first version
  23. * 2018-02-07 Bernard Change the 3rd parameter of open/fcntl/ioctl to '...'
  24. */
  25. #include <dfs.h>
  26. #include <dfs_posix.h>
  27. #include "dfs_private.h"
  28. /**
  29. * @addtogroup FsPosixApi
  30. */
  31. /*@{*/
  32. /**
  33. * this function is a POSIX compliant version, which will open a file and
  34. * return a file descriptor according specified flags.
  35. *
  36. * @param file the path name of file.
  37. * @param flags the file open flags.
  38. *
  39. * @return the non-negative integer on successful open, others for failed.
  40. */
  41. int open(const char *file, int flags, ...)
  42. {
  43. int fd, result;
  44. struct dfs_fd *d;
  45. /* allocate a fd */
  46. fd = fd_new();
  47. if (fd < 0)
  48. {
  49. rt_set_errno(-ENOMEM);
  50. return -1;
  51. }
  52. d = fd_get(fd);
  53. result = dfs_file_open(d, file, flags);
  54. if (result < 0)
  55. {
  56. /* release the ref-count of fd */
  57. fd_put(d);
  58. fd_put(d);
  59. rt_set_errno(result);
  60. return -1;
  61. }
  62. /* release the ref-count of fd */
  63. fd_put(d);
  64. return fd;
  65. }
  66. RTM_EXPORT(open);
  67. /**
  68. * this function is a POSIX compliant version, which will close the open
  69. * file descriptor.
  70. *
  71. * @param fd the file descriptor.
  72. *
  73. * @return 0 on successful, -1 on failed.
  74. */
  75. int close(int fd)
  76. {
  77. int result;
  78. struct dfs_fd *d;
  79. d = fd_get(fd);
  80. if (d == NULL)
  81. {
  82. rt_set_errno(-EBADF);
  83. return -1;
  84. }
  85. result = dfs_file_close(d);
  86. fd_put(d);
  87. if (result < 0)
  88. {
  89. rt_set_errno(result);
  90. return -1;
  91. }
  92. fd_put(d);
  93. return 0;
  94. }
  95. RTM_EXPORT(close);
  96. /**
  97. * this function is a POSIX compliant version, which will read specified data
  98. * buffer length for an open file descriptor.
  99. *
  100. * @param fd the file descriptor.
  101. * @param buf the buffer to save the read data.
  102. * @param len the maximal length of data buffer
  103. *
  104. * @return the actual read data buffer length. If the returned value is 0, it
  105. * may be reach the end of file, please check errno.
  106. */
  107. #ifdef RT_USING_NEWLIB
  108. _READ_WRITE_RETURN_TYPE _EXFUN(read, (int fd, void *buf, size_t len))
  109. #else
  110. int read(int fd, void *buf, size_t len)
  111. #endif
  112. {
  113. int result;
  114. struct dfs_fd *d;
  115. /* get the fd */
  116. d = fd_get(fd);
  117. if (d == NULL)
  118. {
  119. rt_set_errno(-EBADF);
  120. return -1;
  121. }
  122. result = dfs_file_read(d, buf, len);
  123. if (result < 0)
  124. {
  125. fd_put(d);
  126. rt_set_errno(result);
  127. return -1;
  128. }
  129. /* release the ref-count of fd */
  130. fd_put(d);
  131. return result;
  132. }
  133. RTM_EXPORT(read);
  134. /**
  135. * this function is a POSIX compliant version, which will write specified data
  136. * buffer length for an open file descriptor.
  137. *
  138. * @param fd the file descriptor
  139. * @param buf the data buffer to be written.
  140. * @param len the data buffer length.
  141. *
  142. * @return the actual written data buffer length.
  143. */
  144. #ifdef RT_USING_NEWLIB
  145. _READ_WRITE_RETURN_TYPE _EXFUN(write, (int fd, const void *buf, size_t len))
  146. #else
  147. int write(int fd, const void *buf, size_t len)
  148. #endif
  149. {
  150. int result;
  151. struct dfs_fd *d;
  152. /* get the fd */
  153. d = fd_get(fd);
  154. if (d == NULL)
  155. {
  156. rt_set_errno(-EBADF);
  157. return -1;
  158. }
  159. result = dfs_file_write(d, buf, len);
  160. if (result < 0)
  161. {
  162. fd_put(d);
  163. rt_set_errno(result);
  164. return -1;
  165. }
  166. /* release the ref-count of fd */
  167. fd_put(d);
  168. return result;
  169. }
  170. RTM_EXPORT(write);
  171. /**
  172. * this function is a POSIX compliant version, which will seek the offset for
  173. * an open file descriptor.
  174. *
  175. * @param fd the file descriptor.
  176. * @param offset the offset to be seeked.
  177. * @param whence the directory of seek.
  178. *
  179. * @return the current read/write position in the file, or -1 on failed.
  180. */
  181. off_t lseek(int fd, off_t offset, int whence)
  182. {
  183. int result;
  184. struct dfs_fd *d;
  185. d = fd_get(fd);
  186. if (d == NULL)
  187. {
  188. rt_set_errno(-EBADF);
  189. return -1;
  190. }
  191. switch (whence)
  192. {
  193. case SEEK_SET:
  194. break;
  195. case SEEK_CUR:
  196. offset += d->pos;
  197. break;
  198. case SEEK_END:
  199. offset += d->size;
  200. break;
  201. default:
  202. fd_put(d);
  203. rt_set_errno(-EINVAL);
  204. return -1;
  205. }
  206. if (offset < 0)
  207. {
  208. fd_put(d);
  209. rt_set_errno(-EINVAL);
  210. return -1;
  211. }
  212. result = dfs_file_lseek(d, offset);
  213. if (result < 0)
  214. {
  215. fd_put(d);
  216. rt_set_errno(result);
  217. return -1;
  218. }
  219. /* release the ref-count of fd */
  220. fd_put(d);
  221. return offset;
  222. }
  223. RTM_EXPORT(lseek);
  224. /**
  225. * this function is a POSIX compliant version, which will rename old file name
  226. * to new file name.
  227. *
  228. * @param old the old file name.
  229. * @param new the new file name.
  230. *
  231. * @return 0 on successful, -1 on failed.
  232. *
  233. * note: the old and new file name must be belong to a same file system.
  234. */
  235. int rename(const char *old, const char *new)
  236. {
  237. int result;
  238. result = dfs_file_rename(old, new);
  239. if (result < 0)
  240. {
  241. rt_set_errno(result);
  242. return -1;
  243. }
  244. return 0;
  245. }
  246. RTM_EXPORT(rename);
  247. /**
  248. * this function is a POSIX compliant version, which will unlink (remove) a
  249. * specified path file from file system.
  250. *
  251. * @param pathname the specified path name to be unlinked.
  252. *
  253. * @return 0 on successful, -1 on failed.
  254. */
  255. int unlink(const char *pathname)
  256. {
  257. int result;
  258. result = dfs_file_unlink(pathname);
  259. if (result < 0)
  260. {
  261. rt_set_errno(result);
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. RTM_EXPORT(unlink);
  267. #ifndef _WIN32 /* we can not implement these functions */
  268. /**
  269. * this function is a POSIX compliant version, which will get file information.
  270. *
  271. * @param file the file name
  272. * @param buf the data buffer to save stat description.
  273. *
  274. * @return 0 on successful, -1 on failed.
  275. */
  276. int stat(const char *file, struct stat *buf)
  277. {
  278. int result;
  279. result = dfs_file_stat(file, buf);
  280. if (result < 0)
  281. {
  282. rt_set_errno(result);
  283. return -1;
  284. }
  285. return result;
  286. }
  287. RTM_EXPORT(stat);
  288. /**
  289. * this function is a POSIX compliant version, which will get file status.
  290. *
  291. * @param fildes the file description
  292. * @param buf the data buffer to save stat description.
  293. *
  294. * @return 0 on successful, -1 on failed.
  295. */
  296. int fstat(int fildes, struct stat *buf)
  297. {
  298. struct dfs_fd *d;
  299. /* get the fd */
  300. d = fd_get(fildes);
  301. if (d == NULL)
  302. {
  303. rt_set_errno(-EBADF);
  304. return -1;
  305. }
  306. /* it's the root directory */
  307. buf->st_dev = 0;
  308. buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  309. S_IWUSR | S_IWGRP | S_IWOTH;
  310. if (d->type == FT_DIRECTORY)
  311. {
  312. buf->st_mode &= ~S_IFREG;
  313. buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  314. }
  315. buf->st_size = d->size;
  316. buf->st_mtime = 0;
  317. fd_put(d);
  318. return RT_EOK;
  319. }
  320. RTM_EXPORT(fstat);
  321. #endif
  322. /**
  323. * this function is a POSIX compliant version, which shall request that all data
  324. * for the open file descriptor named by fildes is to be transferred to the storage
  325. * device associated with the file described by fildes.
  326. *
  327. * @param fildes the file description
  328. *
  329. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  330. * set to indicate the error.
  331. */
  332. int fsync(int fildes)
  333. {
  334. int ret;
  335. struct dfs_fd *d;
  336. /* get the fd */
  337. d = fd_get(fildes);
  338. if (d == NULL)
  339. {
  340. rt_set_errno(-EBADF);
  341. return -1;
  342. }
  343. ret = dfs_file_flush(d);
  344. fd_put(d);
  345. return ret;
  346. }
  347. RTM_EXPORT(fsync);
  348. /**
  349. * this function is a POSIX compliant version, which shall perform a variety of
  350. * control functions on devices.
  351. *
  352. * @param fildes the file description
  353. * @param cmd the specified command
  354. * @param data represents the additional information that is needed by this
  355. * specific device to perform the requested function.
  356. *
  357. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  358. * set to indicate the error.
  359. */
  360. int fcntl(int fildes, int cmd, ...)
  361. {
  362. int ret = -1;
  363. struct dfs_fd *d;
  364. /* get the fd */
  365. d = fd_get(fildes);
  366. if (d)
  367. {
  368. void* arg;
  369. va_list ap;
  370. va_start(ap, cmd);
  371. arg = va_arg(ap, void*);
  372. va_end(ap);
  373. ret = dfs_file_ioctl(d, cmd, arg);
  374. fd_put(d);
  375. }
  376. else ret = -EBADF;
  377. if (ret != 0)
  378. {
  379. rt_set_errno(ret);
  380. ret = -1;
  381. }
  382. return 0;
  383. }
  384. RTM_EXPORT(fcntl);
  385. /**
  386. * this function is a POSIX compliant version, which shall perform a variety of
  387. * control functions on devices.
  388. *
  389. * @param fildes the file description
  390. * @param cmd the specified command
  391. * @param data represents the additional information that is needed by this
  392. * specific device to perform the requested function.
  393. *
  394. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  395. * set to indicate the error.
  396. */
  397. int ioctl(int fildes, int cmd, ...)
  398. {
  399. void* arg;
  400. va_list ap;
  401. va_start(ap, cmd);
  402. arg = va_arg(ap, void*);
  403. va_end(ap);
  404. /* we use fcntl for this API. */
  405. return fcntl(fildes, cmd, arg);
  406. }
  407. RTM_EXPORT(ioctl);
  408. /**
  409. * this function is a POSIX compliant version, which will return the
  410. * information about a mounted file system.
  411. *
  412. * @param path the path which mounted file system.
  413. * @param buf the buffer to save the returned information.
  414. *
  415. * @return 0 on successful, others on failed.
  416. */
  417. int statfs(const char *path, struct statfs *buf)
  418. {
  419. int result;
  420. result = dfs_statfs(path, buf);
  421. if (result < 0)
  422. {
  423. rt_set_errno(result);
  424. return -1;
  425. }
  426. return result;
  427. }
  428. RTM_EXPORT(statfs);
  429. /**
  430. * this function is a POSIX compliant version, which will make a directory
  431. *
  432. * @param path the directory path to be made.
  433. * @param mode
  434. *
  435. * @return 0 on successful, others on failed.
  436. */
  437. int mkdir(const char *path, mode_t mode)
  438. {
  439. int fd;
  440. struct dfs_fd *d;
  441. int result;
  442. fd = fd_new();
  443. if (fd == -1)
  444. {
  445. rt_set_errno(-ENOMEM);
  446. return -1;
  447. }
  448. d = fd_get(fd);
  449. result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
  450. if (result < 0)
  451. {
  452. fd_put(d);
  453. fd_put(d);
  454. rt_set_errno(result);
  455. return -1;
  456. }
  457. dfs_file_close(d);
  458. fd_put(d);
  459. fd_put(d);
  460. return 0;
  461. }
  462. RTM_EXPORT(mkdir);
  463. #ifdef RT_USING_FINSH
  464. #include <finsh.h>
  465. FINSH_FUNCTION_EXPORT(mkdir, create a directory);
  466. #endif
  467. /**
  468. * this function is a POSIX compliant version, which will remove a directory.
  469. *
  470. * @param pathname the path name to be removed.
  471. *
  472. * @return 0 on successful, others on failed.
  473. */
  474. int rmdir(const char *pathname)
  475. {
  476. int result;
  477. result = dfs_file_unlink(pathname);
  478. if (result < 0)
  479. {
  480. rt_set_errno(result);
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. RTM_EXPORT(rmdir);
  486. /**
  487. * this function is a POSIX compliant version, which will open a directory.
  488. *
  489. * @param name the path name to be open.
  490. *
  491. * @return the DIR pointer of directory, NULL on open directory failed.
  492. */
  493. DIR *opendir(const char *name)
  494. {
  495. struct dfs_fd *d;
  496. int fd, result;
  497. DIR *t;
  498. t = NULL;
  499. /* allocate a fd */
  500. fd = fd_new();
  501. if (fd == -1)
  502. {
  503. rt_set_errno(-ENOMEM);
  504. return NULL;
  505. }
  506. d = fd_get(fd);
  507. result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
  508. if (result >= 0)
  509. {
  510. /* open successfully */
  511. t = (DIR *) rt_malloc(sizeof(DIR));
  512. if (t == NULL)
  513. {
  514. dfs_file_close(d);
  515. fd_put(d);
  516. }
  517. else
  518. {
  519. memset(t, 0, sizeof(DIR));
  520. t->fd = fd;
  521. }
  522. fd_put(d);
  523. return t;
  524. }
  525. /* open failed */
  526. fd_put(d);
  527. fd_put(d);
  528. rt_set_errno(result);
  529. return NULL;
  530. }
  531. RTM_EXPORT(opendir);
  532. /**
  533. * this function is a POSIX compliant version, which will return a pointer
  534. * to a dirent structure representing the next directory entry in the
  535. * directory stream.
  536. *
  537. * @param d the directory stream pointer.
  538. *
  539. * @return the next directory entry, NULL on the end of directory or failed.
  540. */
  541. struct dirent *readdir(DIR *d)
  542. {
  543. int result;
  544. struct dfs_fd *fd;
  545. fd = fd_get(d->fd);
  546. if (fd == NULL)
  547. {
  548. rt_set_errno(-EBADF);
  549. return NULL;
  550. }
  551. if (d->num)
  552. {
  553. struct dirent* dirent_ptr;
  554. dirent_ptr = (struct dirent*)&d->buf[d->cur];
  555. d->cur += dirent_ptr->d_reclen;
  556. }
  557. if (!d->num || d->cur >= d->num)
  558. {
  559. /* get a new entry */
  560. result = dfs_file_getdents(fd,
  561. (struct dirent*)d->buf,
  562. sizeof(d->buf) - 1);
  563. if (result <= 0)
  564. {
  565. fd_put(fd);
  566. rt_set_errno(result);
  567. return NULL;
  568. }
  569. d->num = result;
  570. d->cur = 0; /* current entry index */
  571. }
  572. fd_put(fd);
  573. return (struct dirent *)(d->buf+d->cur);
  574. }
  575. RTM_EXPORT(readdir);
  576. /**
  577. * this function is a POSIX compliant version, which will return current
  578. * location in directory stream.
  579. *
  580. * @param d the directory stream pointer.
  581. *
  582. * @return the current location in directory stream.
  583. */
  584. long telldir(DIR *d)
  585. {
  586. struct dfs_fd *fd;
  587. long result;
  588. fd = fd_get(d->fd);
  589. if (fd == NULL)
  590. {
  591. rt_set_errno(-EBADF);
  592. return 0;
  593. }
  594. result = fd->pos - d->num + d->cur;
  595. fd_put(fd);
  596. return result;
  597. }
  598. RTM_EXPORT(telldir);
  599. /**
  600. * this function is a POSIX compliant version, which will set position of
  601. * next directory structure in the directory stream.
  602. *
  603. * @param d the directory stream.
  604. * @param offset the offset in directory stream.
  605. */
  606. void seekdir(DIR *d, off_t offset)
  607. {
  608. struct dfs_fd *fd;
  609. fd = fd_get(d->fd);
  610. if (fd == NULL)
  611. {
  612. rt_set_errno(-EBADF);
  613. return ;
  614. }
  615. /* seek to the offset position of directory */
  616. if (dfs_file_lseek(fd, offset) >= 0)
  617. d->num = d->cur = 0;
  618. fd_put(fd);
  619. }
  620. RTM_EXPORT(seekdir);
  621. /**
  622. * this function is a POSIX compliant version, which will reset directory
  623. * stream.
  624. *
  625. * @param d the directory stream.
  626. */
  627. void rewinddir(DIR *d)
  628. {
  629. struct dfs_fd *fd;
  630. fd = fd_get(d->fd);
  631. if (fd == NULL)
  632. {
  633. rt_set_errno(-EBADF);
  634. return ;
  635. }
  636. /* seek to the beginning of directory */
  637. if (dfs_file_lseek(fd, 0) >= 0)
  638. d->num = d->cur = 0;
  639. fd_put(fd);
  640. }
  641. RTM_EXPORT(rewinddir);
  642. /**
  643. * this function is a POSIX compliant version, which will close a directory
  644. * stream.
  645. *
  646. * @param d the directory stream.
  647. *
  648. * @return 0 on successful, -1 on failed.
  649. */
  650. int closedir(DIR *d)
  651. {
  652. int result;
  653. struct dfs_fd *fd;
  654. fd = fd_get(d->fd);
  655. if (fd == NULL)
  656. {
  657. rt_set_errno(-EBADF);
  658. return -1;
  659. }
  660. result = dfs_file_close(fd);
  661. fd_put(fd);
  662. fd_put(fd);
  663. rt_free(d);
  664. if (result < 0)
  665. {
  666. rt_set_errno(result);
  667. return -1;
  668. }
  669. else
  670. return 0;
  671. }
  672. RTM_EXPORT(closedir);
  673. #ifdef DFS_USING_WORKDIR
  674. /**
  675. * this function is a POSIX compliant version, which will change working
  676. * directory.
  677. *
  678. * @param path the path name to be changed to.
  679. *
  680. * @return 0 on successful, -1 on failed.
  681. */
  682. int chdir(const char *path)
  683. {
  684. char *fullpath;
  685. DIR *d;
  686. if (path == NULL)
  687. {
  688. dfs_lock();
  689. rt_kprintf("%s\n", working_directory);
  690. dfs_unlock();
  691. return 0;
  692. }
  693. if (strlen(path) > DFS_PATH_MAX)
  694. {
  695. rt_set_errno(-ENOTDIR);
  696. return -1;
  697. }
  698. fullpath = dfs_normalize_path(NULL, path);
  699. if (fullpath == NULL)
  700. {
  701. rt_set_errno(-ENOTDIR);
  702. return -1; /* build path failed */
  703. }
  704. dfs_lock();
  705. d = opendir(fullpath);
  706. if (d == NULL)
  707. {
  708. rt_free(fullpath);
  709. /* this is a not exist directory */
  710. dfs_unlock();
  711. return -1;
  712. }
  713. /* close directory stream */
  714. closedir(d);
  715. /* copy full path to working directory */
  716. strncpy(working_directory, fullpath, DFS_PATH_MAX);
  717. /* release normalize directory path name */
  718. rt_free(fullpath);
  719. dfs_unlock();
  720. return 0;
  721. }
  722. RTM_EXPORT(chdir);
  723. #ifdef RT_USING_FINSH
  724. FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
  725. #endif
  726. #endif
  727. /**
  728. * this function is a POSIX compliant version, which shall check the file named
  729. * by the pathname pointed to by the path argument for accessibility according
  730. * to the bit pattern contained in amode.
  731. *
  732. * @param path the specified file/dir path.
  733. * @param amode the value is either the bitwise-inclusive OR of the access
  734. * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).
  735. */
  736. int access(const char *path, int amode)
  737. {
  738. struct stat sb;
  739. if (stat(path, &sb) < 0)
  740. return -1; /* already sets errno */
  741. /* ignore R_OK,W_OK,X_OK condition */
  742. return 0;
  743. }
  744. /**
  745. * this function is a POSIX compliant version, which will return current
  746. * working directory.
  747. *
  748. * @param buf the returned current directory.
  749. * @param size the buffer size.
  750. *
  751. * @return the returned current directory.
  752. */
  753. char *getcwd(char *buf, size_t size)
  754. {
  755. #ifdef DFS_USING_WORKDIR
  756. rt_enter_critical();
  757. strncpy(buf, working_directory, size);
  758. rt_exit_critical();
  759. #else
  760. rt_kprintf(NO_WORKING_DIR);
  761. #endif
  762. return buf;
  763. }
  764. RTM_EXPORT(getcwd);
  765. /* @} */