dfs_posix.c 18 KB

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