xref: /nrf52832-nimble/rt-thread/components/libc/compilers/dlib/syscall_open.c (revision 104654410c56c573564690304ae786df310c91fc)
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 
11 #include <rtthread.h>
12 #include <yfuns.h>
13 #ifdef RT_USING_DFS
14 #include <dfs_posix.h>
15 #endif
16 
17 #pragma module_name = "?__open"
18 
__open(const char * filename,int mode)19 int __open(const char *filename, int mode)
20 {
21 #ifndef RT_USING_DFS
22   return _LLIO_ERROR;
23 #else
24   int handle;
25   int open_mode = O_RDONLY;
26 
27   if (mode & _LLIO_CREAT)
28   {
29     open_mode |= O_CREAT;
30 
31     /* Check what we should do with it if it exists. */
32     if (mode & _LLIO_APPEND)
33     {
34       /* Append to the existing file. */
35       open_mode |= O_APPEND;
36     }
37 
38     if (mode & _LLIO_TRUNC)
39     {
40       /* Truncate the existsing file. */
41       open_mode |= O_TRUNC;
42     }
43   }
44 
45   if (mode & _LLIO_TEXT)
46   {
47     /* we didn't support text mode */
48   }
49 
50   switch (mode & _LLIO_RDWRMASK)
51   {
52   case _LLIO_RDONLY:
53     break;
54 
55   case _LLIO_WRONLY:
56     open_mode |= O_WRONLY;
57     break;
58 
59   case _LLIO_RDWR:
60     /* The file should be opened for both reads and writes. */
61     open_mode |= O_RDWR;
62     break;
63 
64   default:
65     return _LLIO_ERROR;
66   }
67 
68   handle = open(filename, open_mode, 0);
69   if (handle < 0)
70     return _LLIO_ERROR;
71 
72   return handle;
73 #endif
74 }
75