Changes for 4.5.0 snapshot
[newlib-cygwin.git] / libgloss / arc / hl / hl_open.c
blob62cbe9db46b20c1f4f0e8c7ea8aaa33a3b988a26
1 /*
2 * hl_open.c -- provide _open().
4 * Copyright (c) 2024 Synopsys Inc.
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice is included verbatim in any distributions. No written agreement,
10 * license, or royalty fee is required for any of the authorized uses.
11 * Modifications to this software may be copyrighted by their authors
12 * and need not follow the licensing terms described here, provided that
13 * the new terms are clearly indicated on the first page of each file where
14 * they apply.
18 #include <errno.h>
19 #include <stdarg.h>
20 #include <stdint.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
25 #include "hl_toolchain.h"
26 #include "hl_api.h"
28 /* Map newlib open flags into Hostlink IO ones. */
29 static __always_inline uint32_t
30 _hl_open_flags_map (int flags)
32 uint32_t hl_flags = 0;
34 hl_flags |= (flags & O_RDONLY) ? 0x0000 : 0;
35 hl_flags |= (flags & O_WRONLY) ? 0x0001 : 0;
36 hl_flags |= (flags & O_RDWR) ? 0x0002 : 0;
37 hl_flags |= (flags & O_APPEND) ? 0x0008 : 0;
38 hl_flags |= (flags & O_CREAT) ? 0x0100 : 0;
39 hl_flags |= (flags & O_TRUNC) ? 0x0200 : 0;
40 hl_flags |= (flags & O_EXCL) ? 0x0400 : 0;
42 return hl_flags;
45 /* Open file on host. Implements HL_SYSCALL_OPEN. */
46 static __always_inline int
47 _hl_open (const char *path, int flags, mode_t mode)
49 int32_t fd;
50 uint32_t host_errno;
51 uint32_t hl_flags = _hl_open_flags_map (flags);
52 volatile __uncached char *p;
54 p = _hl_message (HL_SYSCALL_OPEN, "sii:ii",
55 path, /* s */
56 (uint32_t) hl_flags, /* i */
57 (uint32_t) mode, /* i */
58 (uint32_t *) &fd, /* :i */
59 (uint32_t *) &host_errno /* :i */);
61 if (p == NULL)
63 errno = ETIMEDOUT;
64 fd = -1;
66 else if (fd < 0)
68 errno = host_errno;
69 fd = -1;
72 _hl_delete ();
74 return fd;
77 int
78 _open (const char *path, int flags, ...)
80 va_list ap;
81 mode_t mode = 0;
83 va_start (ap, flags);
85 if (flags & O_CREAT)
86 mode = va_arg (ap, mode_t);
88 return _hl_open (path, flags, mode);