unistd.c 19 KB

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