xref: /freebsd/contrib/bc/src/file.c (revision e17f5b1d)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
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.
17  *
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.
29  *
30  * *****************************************************************************
31  *
32  * Code for implementing buffered I/O on my own terms.
33  *
34  */
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <file.h>
42 #include <vm.h>
43 
44 void bc_file_ultoa(unsigned long long val, char buf[BC_FILE_ULL_LENGTH]) {
45 
46 	char buf2[BC_FILE_ULL_LENGTH];
47 	size_t i, len;
48 
49 	memset(buf2, 0, BC_FILE_ULL_LENGTH);
50 
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';
55 		val /= 10;
56 	}
57 
58 	len = i;
59 
60 	for (i = 0; i < len; ++i) buf[i] = buf2[len - i - 1];
61 }
62 
63 static BcStatus bc_file_output(int fd, const char *buf, size_t n) {
64 
65 	size_t bytes = 0;
66 	sig_atomic_t lock;
67 
68 	BC_SIG_TRYLOCK(lock);
69 
70 	while (bytes < n) {
71 
72 		ssize_t written = write(fd, buf + bytes, n - bytes);
73 
74 		if (BC_ERR(written == -1))
75 			return errno == EPIPE ? BC_STATUS_EOF : BC_STATUS_ERROR_FATAL;
76 
77 		bytes += (size_t) written;
78 	}
79 
80 	BC_SIG_TRYUNLOCK(lock);
81 
82 	return BC_STATUS_SUCCESS;
83 }
84 
85 BcStatus bc_file_flushErr(BcFile *restrict f) {
86 
87 	BcStatus s;
88 
89 	if (f->len) {
90 		s = bc_file_output(f->fd, f->buf, f->len);
91 		f->len = 0;
92 	}
93 	else s = BC_STATUS_SUCCESS;
94 
95 	return s;
96 }
97 
98 void bc_file_flush(BcFile *restrict f) {
99 
100 	BcStatus s = bc_file_flushErr(f);
101 
102 	if (BC_ERR(s)) {
103 
104 		if (s == BC_STATUS_EOF) {
105 			vm.status = (sig_atomic_t) s;
106 			BC_VM_JMP;
107 		}
108 		else bc_vm_err(BC_ERROR_FATAL_IO_ERR);
109 	}
110 }
111 
112 void bc_file_write(BcFile *restrict f, const char *buf, size_t n) {
113 
114 	if (n > f->cap - f->len) {
115 		bc_file_flush(f);
116 		assert(!f->len);
117 	}
118 
119 	if (BC_UNLIKELY(n > f->cap - f->len)) bc_file_output(f->fd, buf, n);
120 	else {
121 		memcpy(f->buf + f->len, buf, n);
122 		f->len += n;
123 	}
124 }
125 
126 void bc_file_printf(BcFile *restrict f, const char *fmt, ...) {
127 
128 	va_list args;
129 
130 	va_start(args, fmt);
131 	bc_file_vprintf(f, fmt, args);
132 	va_end(args);
133 }
134 
135 void bc_file_vprintf(BcFile *restrict f, const char *fmt, va_list args) {
136 
137 	char *percent;
138 	const char *ptr = fmt;
139 	char buf[BC_FILE_ULL_LENGTH];
140 
141 	while ((percent = strchr(ptr, '%')) != NULL) {
142 
143 		char c;
144 
145 		if (percent != ptr) {
146 			size_t len = (size_t) (percent - ptr);
147 			bc_file_write(f, ptr, len);
148 		}
149 
150 		c = percent[1];
151 
152 		if (c == 'c') {
153 
154 			uchar uc = (uchar) va_arg(args, int);
155 
156 			bc_file_putchar(f, uc);
157 		}
158 		else if (c == 's') {
159 
160 			char *s = va_arg(args, char*);
161 
162 			bc_file_puts(f, s);
163 		}
164 #if BC_DEBUG_CODE
165 		else if (c == 'd') {
166 
167 			int d = va_arg(args, int);
168 
169 			if (d < 0) {
170 				bc_file_putchar(f, '-');
171 				d = -d;
172 			}
173 
174 			if (!d) bc_file_putchar(f, '0');
175 			else {
176 				bc_file_ultoa((unsigned long long) d, buf);
177 				bc_file_puts(f, buf);
178 			}
179 		}
180 #endif // BC_DEBUG_CODE
181 		else {
182 
183 			unsigned long long ull;
184 
185 			assert((c == 'l' || c == 'z') && percent[2] == 'u');
186 
187 			if (c == 'z') ull = (unsigned long long) va_arg(args, size_t);
188 			else ull = (unsigned long long) va_arg(args, unsigned long);
189 
190 			if (!ull) bc_file_putchar(f, '0');
191 			else {
192 				bc_file_ultoa(ull, buf);
193 				bc_file_puts(f, buf);
194 			}
195 		}
196 
197 		ptr = percent + 2 + (c == 'l' || c == 'z');
198 	}
199 
200 	if (ptr[0]) bc_file_puts(f, ptr);
201 }
202 
203 void bc_file_puts(BcFile *restrict f, const char *str) {
204 	bc_file_write(f, str, strlen(str));
205 }
206 
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;
211 	f->len += 1;
212 }
213 
214 void bc_file_init(BcFile *f, int fd, char *buf, size_t cap) {
215 	BC_SIG_ASSERT_LOCKED;
216 	f->fd = fd;
217 	f->buf = buf;
218 	f->len = 0;
219 	f->cap = cap;
220 }
221 
222 void bc_file_free(BcFile *f) {
223 	BC_SIG_ASSERT_LOCKED;
224 	bc_file_flush(f);
225 }
226