syscall_open.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * 2015-01-28 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <LowLevelIOInterface.h>
  12. #include <fcntl.h>
  13. #include <compiler_private.h>
  14. #define DBG_TAG "dlib.syscall.open"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. /*
  18. * The "__open" function opens the file named "filename" as specified
  19. * by "mode".
  20. */
  21. #pragma module_name = "?__open"
  22. int __open(const char *filename, int mode)
  23. {
  24. #ifdef DFS_USING_POSIX
  25. int handle;
  26. int open_mode = O_RDONLY;
  27. if (mode & _LLIO_CREAT)
  28. {
  29. open_mode |= O_CREAT;
  30. /* Check what we should do with it if it exists. */
  31. if (mode & _LLIO_APPEND)
  32. {
  33. /* Append to the existing file. */
  34. open_mode |= O_APPEND;
  35. }
  36. if (mode & _LLIO_TRUNC)
  37. {
  38. /* Truncate the existsing file. */
  39. open_mode |= O_TRUNC;
  40. }
  41. }
  42. if (mode & _LLIO_TEXT)
  43. {
  44. /* we didn't support text mode */
  45. }
  46. switch (mode & _LLIO_RDWRMASK)
  47. {
  48. case _LLIO_RDONLY:
  49. break;
  50. case _LLIO_WRONLY:
  51. open_mode |= O_WRONLY;
  52. break;
  53. case _LLIO_RDWR:
  54. /* The file should be opened for both reads and writes. */
  55. open_mode |= O_RDWR;
  56. break;
  57. default:
  58. return _LLIO_ERROR;
  59. }
  60. handle = open(filename, open_mode, 0);
  61. if (handle < 0)
  62. {
  63. return _LLIO_ERROR;
  64. }
  65. return handle;
  66. #else
  67. LOG_W(_WARNING_WITHOUT_FS);
  68. return _LLIO_ERROR;
  69. #endif /* DFS_USING_POSIX */
  70. }