syscall_open.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-01-28 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <yfuns.h>
  12. #ifdef RT_USING_DFS
  13. #include <dfs_posix.h>
  14. #endif
  15. #pragma module_name = "?__open"
  16. int __open(const char *filename, int mode)
  17. {
  18. #ifndef RT_USING_DFS
  19. return _LLIO_ERROR;
  20. #else
  21. int handle;
  22. int open_mode = O_RDONLY;
  23. if (mode & _LLIO_CREAT)
  24. {
  25. open_mode |= O_CREAT;
  26. /* Check what we should do with it if it exists. */
  27. if (mode & _LLIO_APPEND)
  28. {
  29. /* Append to the existing file. */
  30. open_mode |= O_APPEND;
  31. }
  32. if (mode & _LLIO_TRUNC)
  33. {
  34. /* Truncate the existsing file. */
  35. open_mode |= O_TRUNC;
  36. }
  37. }
  38. if (mode & _LLIO_TEXT)
  39. {
  40. /* we didn't support text mode */
  41. }
  42. switch (mode & _LLIO_RDWRMASK)
  43. {
  44. case _LLIO_RDONLY:
  45. break;
  46. case _LLIO_WRONLY:
  47. open_mode |= O_WRONLY;
  48. break;
  49. case _LLIO_RDWR:
  50. /* The file should be opened for both reads and writes. */
  51. open_mode |= O_RDWR;
  52. break;
  53. default:
  54. return _LLIO_ERROR;
  55. }
  56. handle = open(filename, open_mode, 0);
  57. if (handle < 0)
  58. return _LLIO_ERROR;
  59. return handle;
  60. #endif
  61. }