Fixes compile failure if REENTRANT_SYSCALLS_PROVIDED and MISSING_SYSCALL_NAMES defined
[newlib-cygwin.git] / libgloss / arc / hl / hl_read.c
blob584df1cce723da8a3b64c70da8f43560a4ef5d00
1 /*
2 * hl_read.c -- provide _read().
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 <stdint.h>
20 #include <stddef.h>
21 #include <unistd.h>
23 #include "hl_toolchain.h"
24 #include "hl_api.h"
27 /* Read one chunk. Implements HL_SYSCALL_READ. */
28 static ssize_t
29 _hl_read (int fd, void *buf, size_t count)
31 ssize_t ret;
32 int32_t hl_n;
33 uint32_t host_errno;
34 volatile __uncached char *p;
36 p = _hl_message (HL_SYSCALL_READ, "ii:i",
37 (uint32_t) fd, /* i */
38 (uint32_t) count, /* i */
39 (uint32_t *) &hl_n /* :i */);
41 if (p == NULL)
43 errno = ETIMEDOUT;
44 ret = -1;
46 else if (hl_n < 0)
48 p = _hl_unpack_int (p, &host_errno);
49 errno = p == NULL ? EIO : host_errno;
50 ret = -1;
52 else
54 uint32_t n;
56 p = _hl_unpack_ptr (p, buf, &n);
57 ret = n;
59 if (p == NULL || n != (uint32_t) hl_n)
61 errno = EIO;
62 ret = -1;
66 _hl_delete ();
68 return ret;
71 ssize_t
72 _read (int fd, void *buf, size_t count)
74 const uint32_t hl_iochunk = _hl_iochunk_size ();
75 size_t to_read = count;
76 size_t offset = 0;
77 ssize_t ret = 0;
79 while (to_read > hl_iochunk)
81 ret = _hl_read (fd, (char *) buf + offset, hl_iochunk);
83 if (ret < 0)
84 return ret;
86 offset += ret;
88 if (ret != (ssize_t) hl_iochunk)
89 return offset;
91 to_read -= hl_iochunk;
94 if (to_read)
96 ret = _hl_read (fd, (char *) buf + offset, to_read);
98 if (ret < 0)
99 return ret;
101 ret += offset;
104 return ret;