Changes for 4.5.0 snapshot
[newlib-cygwin.git] / libgloss / arc / hl / hl_write.c
blobb00fc13e773261da05d8882598a0c1423739de52
1 /*
2 * hl_write.c -- provide _write().
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 /* Write one chunk to the host file. Implements HL_SYSCALL_WRITE. */
28 static ssize_t
29 _hl_write (int fd, const char *buf, size_t nbyte)
31 ssize_t ret;
32 int32_t n;
33 uint32_t host_errno;
34 volatile __uncached char *p;
36 p = _hl_message (HL_SYSCALL_WRITE, "ipi:ii",
37 (uint32_t) fd, /* i */
38 buf, (uint32_t) nbyte, /* p */
39 (uint32_t) nbyte, /* i */
40 (uint32_t *) &n, /* :i */
41 (uint32_t *) &host_errno /* :i */);
43 if (p == NULL)
45 errno = ETIMEDOUT;
46 ret = -1;
48 else if (n < 0)
50 errno = host_errno;
51 ret = -1;
53 else
55 ret = n;
58 _hl_delete ();
60 return ret;
63 ssize_t
64 _write (int fd, const char *buf, size_t nbyte)
66 const uint32_t hl_iochunk = _hl_iochunk_size ();
67 size_t to_write = nbyte;
68 size_t offset = 0;
69 ssize_t ret = 0;
71 while (to_write > hl_iochunk)
73 ret = _hl_write (fd, buf + offset, hl_iochunk);
75 if (ret < 0)
76 return ret;
78 offset += ret;
80 if (ret != (ssize_t) hl_iochunk)
81 return offset;
83 to_write -= hl_iochunk;
86 if (to_write)
88 ret = _hl_write (fd, buf + offset, to_write);
90 if (ret < 0)
91 return ret;
93 ret += offset;
96 return ret;