2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Code for implementing buffered I/O on my own terms.
44 void bc_file_ultoa(unsigned long long val, char buf[BC_FILE_ULL_LENGTH]) {
46 char buf2[BC_FILE_ULL_LENGTH];
49 memset(buf2, 0, BC_FILE_ULL_LENGTH);
51 // The i = 1 is to ensure that there is a null byte at the end.
52 for (i = 1; val; ++i) {
53 unsigned long long mod = val % 10;
54 buf2[i] = ((char) mod) + '0';
60 for (i = 0; i < len; ++i) buf[i] = buf2[len - i - 1];
63 static BcStatus bc_file_output(int fd, const char *buf, size_t n) {
72 ssize_t written = write(fd, buf + bytes, n - bytes);
74 if (BC_ERR(written == -1))
75 return errno == EPIPE ? BC_STATUS_EOF : BC_STATUS_ERROR_FATAL;
77 bytes += (size_t) written;
80 BC_SIG_TRYUNLOCK(lock);
82 return BC_STATUS_SUCCESS;
85 BcStatus bc_file_flushErr(BcFile *restrict f) {
90 s = bc_file_output(f->fd, f->buf, f->len);
93 else s = BC_STATUS_SUCCESS;
98 void bc_file_flush(BcFile *restrict f) {
100 BcStatus s = bc_file_flushErr(f);
104 if (s == BC_STATUS_EOF) {
105 vm.status = (sig_atomic_t) s;
108 else bc_vm_err(BC_ERROR_FATAL_IO_ERR);
112 void bc_file_write(BcFile *restrict f, const char *buf, size_t n) {
114 if (n > f->cap - f->len) {
119 if (BC_UNLIKELY(n > f->cap - f->len)) bc_file_output(f->fd, buf, n);
121 memcpy(f->buf + f->len, buf, n);
126 void bc_file_printf(BcFile *restrict f, const char *fmt, ...) {
131 bc_file_vprintf(f, fmt, args);
135 void bc_file_vprintf(BcFile *restrict f, const char *fmt, va_list args) {
138 const char *ptr = fmt;
139 char buf[BC_FILE_ULL_LENGTH];
141 while ((percent = strchr(ptr, '%')) != NULL) {
145 if (percent != ptr) {
146 size_t len = (size_t) (percent - ptr);
147 bc_file_write(f, ptr, len);
154 uchar uc = (uchar) va_arg(args, int);
156 bc_file_putchar(f, uc);
160 char *s = va_arg(args, char*);
167 int d = va_arg(args, int);
170 bc_file_putchar(f, '-');
174 if (!d) bc_file_putchar(f, '0');
176 bc_file_ultoa((unsigned long long) d, buf);
177 bc_file_puts(f, buf);
180 #endif // BC_DEBUG_CODE
183 unsigned long long ull;
185 assert((c == 'l' || c == 'z') && percent[2] == 'u');
187 if (c == 'z') ull = (unsigned long long) va_arg(args, size_t);
188 else ull = (unsigned long long) va_arg(args, unsigned long);
190 if (!ull) bc_file_putchar(f, '0');
192 bc_file_ultoa(ull, buf);
193 bc_file_puts(f, buf);
197 ptr = percent + 2 + (c == 'l' || c == 'z');
200 if (ptr[0]) bc_file_puts(f, ptr);
203 void bc_file_puts(BcFile *restrict f, const char *str) {
204 bc_file_write(f, str, strlen(str));
207 void bc_file_putchar(BcFile *restrict f, uchar c) {
208 if (f->len == f->cap) bc_file_flush(f);
209 assert(f->len < f->cap);
210 f->buf[f->len] = (char) c;
214 void bc_file_init(BcFile *f, int fd, char *buf, size_t cap) {
215 BC_SIG_ASSERT_LOCKED;
222 void bc_file_free(BcFile *f) {
223 BC_SIG_ASSERT_LOCKED;