syscall_open.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /*
  14. * The "__open" function opens the file named "filename" as specified
  15. * by "mode".
  16. */
  17. #pragma module_name = "?__open"
  18. int __open(const char *filename, int mode)
  19. {
  20. #ifdef RT_USING_POSIX
  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. #else
  61. return _LLIO_ERROR;
  62. #endif /* RT_USING_POSIX */
  63. }