* this function is a POSIX compliant version, which will open a file and * return a file descriptor according specified flags. * * @param file the path name of file. * @param flags the file open flags. Common values include: * - Access modes (mutually exclusive): * - `O_RDONLY`: Open for read-only access. * - `O_WRONLY`: Open for write-only access. * - `O_RDWR`:
| 46 | * @return the non-negative integer on successful open, others for failed. |
| 47 | */ |
| 48 | int open(const char *file, int flags, ...) |
| 49 | { |
| 50 | int fd, result; |
| 51 | struct dfs_file *df = RT_NULL; |
| 52 | mode_t mode = 0; |
| 53 | |
| 54 | if (file == NULL) |
| 55 | { |
| 56 | rt_set_errno(-EBADF); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) |
| 61 | { |
| 62 | va_list ap; |
| 63 | va_start(ap, flags); |
| 64 | mode = va_arg(ap, mode_t); |
| 65 | va_end(ap); |
| 66 | } |
| 67 | |
| 68 | fd = fd_new(); |
| 69 | if (fd >= 0) |
| 70 | { |
| 71 | df = fd_get(fd); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | rt_set_errno(-RT_ERROR); |
| 76 | return RT_NULL; |
| 77 | } |
| 78 | |
| 79 | result = dfs_file_open(df, file, flags, mode); |
| 80 | if (result < 0) |
| 81 | { |
| 82 | fd_release(fd); |
| 83 | rt_set_errno(result); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | return fd; |
| 88 | } |
| 89 | RTM_EXPORT(open); |
| 90 | |
| 91 | #ifndef AT_FDCWD |
no test coverage detected