syscall_open.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <yfuns.h>
  12. #include <fcntl.h>
  13. #pragma module_name = "?__open"
  14. int __open(const char *filename, int mode)
  15. {
  16. #ifdef RT_LIBC_USING_FILEIO
  17. int handle;
  18. int open_mode = O_RDONLY;
  19. if (mode & _LLIO_CREAT)
  20. {
  21. open_mode |= O_CREAT;
  22. /* Check what we should do with it if it exists. */
  23. if (mode & _LLIO_APPEND)
  24. {
  25. /* Append to the existing file. */
  26. open_mode |= O_APPEND;
  27. }
  28. if (mode & _LLIO_TRUNC)
  29. {
  30. /* Truncate the existsing file. */
  31. open_mode |= O_TRUNC;
  32. }
  33. }
  34. if (mode & _LLIO_TEXT)
  35. {
  36. /* we didn't support text mode */
  37. }
  38. switch (mode & _LLIO_RDWRMASK)
  39. {
  40. case _LLIO_RDONLY:
  41. break;
  42. case _LLIO_WRONLY:
  43. open_mode |= O_WRONLY;
  44. break;
  45. case _LLIO_RDWR:
  46. /* The file should be opened for both reads and writes. */
  47. open_mode |= O_RDWR;
  48. break;
  49. default:
  50. return _LLIO_ERROR;
  51. }
  52. handle = open(filename, open_mode, 0);
  53. if (handle < 0)
  54. return _LLIO_ERROR;
  55. return handle;
  56. #else
  57. return _LLIO_ERROR;
  58. #endif /* RT_LIBC_USING_FILEIO */
  59. }