blob: 09cc7c38015590d87669affad5b0ef52a0aee839 [file] [log] [blame]
Thorsten Glaser811a5752013-07-25 14:24:45 +00001/* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002
3/*-
Thorsten Glaser811a5752013-07-25 14:24:45 +00004 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005 * 2012, 2013, 2015, 2016, 2017
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07007 *
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
13 *
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
Geremy Condra03ebf062011-10-12 18:17:24 -070022 *-
23 * Use %zX instead of %p and floating point isn't supported at all.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070024 */
25
26#include "sh.h"
27
Elliott Hughesa3c3f962017-04-12 16:52:30 -070028__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.79 2017/04/12 17:08:49 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070029
30/* flags to shf_emptybuf() */
31#define EB_READSW 0x01 /* about to switch to reading */
32#define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
33
34/*
35 * Replacement stdio routines. Stdio is too flakey on too many machines
36 * to be useful when you have multiple processes using the same underlying
37 * file descriptors.
38 */
39
40static int shf_fillbuf(struct shf *);
41static int shf_emptybuf(struct shf *, int);
42
Geremy Condra03ebf062011-10-12 18:17:24 -070043/*
44 * Open a file. First three args are for open(), last arg is flags for
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070045 * this package. Returns NULL if file could not be opened, or if a dup
46 * fails.
47 */
48struct shf *
49shf_open(const char *name, int oflags, int mode, int sflags)
50{
51 struct shf *shf;
Geremy Condra03ebf062011-10-12 18:17:24 -070052 ssize_t bsize =
53 /* at most 512 */
54 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
Thorsten Glaser811a5752013-07-25 14:24:45 +000055 int fd, eno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070056
57 /* Done before open so if alloca fails, fd won't be lost. */
58 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
59 shf->areap = ATEMP;
60 shf->buf = (unsigned char *)&shf[1];
61 shf->bsize = bsize;
62 shf->flags = SHF_ALLOCS;
63 /* Rest filled in by reopen. */
64
Elliott Hughes96b43632015-07-17 11:39:41 -070065 fd = binopen3(name, oflags, mode);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070066 if (fd < 0) {
Thorsten Glaser811a5752013-07-25 14:24:45 +000067 eno = errno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070068 afree(shf, shf->areap);
Thorsten Glaser811a5752013-07-25 14:24:45 +000069 errno = eno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070070 return (NULL);
71 }
72 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
73 int nfd;
74
75 nfd = fcntl(fd, F_DUPFD, FDBASE);
Thorsten Glaser811a5752013-07-25 14:24:45 +000076 eno = errno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070077 close(fd);
78 if (nfd < 0) {
79 afree(shf, shf->areap);
Thorsten Glaser811a5752013-07-25 14:24:45 +000080 errno = eno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070081 return (NULL);
82 }
83 fd = nfd;
84 }
85 sflags &= ~SHF_ACCMODE;
86 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
87 ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
88
89 return (shf_reopen(fd, sflags, shf));
90}
91
Geremy Condra03ebf062011-10-12 18:17:24 -070092/* helper function for shf_fdopen and shf_reopen */
93static void
94shf_open_hlp(int fd, int *sflagsp, const char *where)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070095{
Geremy Condra03ebf062011-10-12 18:17:24 -070096 int sflags = *sflagsp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070097
98 /* use fcntl() to figure out correct read/write flags */
99 if (sflags & SHF_GETFL) {
100 int flags = fcntl(fd, F_GETFL, 0);
101
102 if (flags < 0)
103 /* will get an error on first read/write */
104 sflags |= SHF_RDWR;
105 else {
106 switch (flags & O_ACCMODE) {
107 case O_RDONLY:
108 sflags |= SHF_RD;
109 break;
110 case O_WRONLY:
111 sflags |= SHF_WR;
112 break;
113 case O_RDWR:
114 sflags |= SHF_RDWR;
115 break;
116 }
117 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700118 *sflagsp = sflags;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700119 }
120
121 if (!(sflags & (SHF_RD | SHF_WR)))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700122 internal_errorf(Tf_sD_s, where, "missing read/write");
Geremy Condra03ebf062011-10-12 18:17:24 -0700123}
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700124
Geremy Condra03ebf062011-10-12 18:17:24 -0700125/* Set up the shf structure for a file descriptor. Doesn't fail. */
126struct shf *
127shf_fdopen(int fd, int sflags, struct shf *shf)
128{
129 ssize_t bsize =
130 /* at most 512 */
131 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
132
133 shf_open_hlp(fd, &sflags, "shf_fdopen");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700134 if (shf) {
135 if (bsize) {
136 shf->buf = alloc(bsize, ATEMP);
137 sflags |= SHF_ALLOCB;
138 } else
139 shf->buf = NULL;
140 } else {
141 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
142 shf->buf = (unsigned char *)&shf[1];
143 sflags |= SHF_ALLOCS;
144 }
145 shf->areap = ATEMP;
146 shf->fd = fd;
147 shf->rp = shf->wp = shf->buf;
148 shf->rnleft = 0;
149 shf->rbsize = bsize;
150 shf->wnleft = 0; /* force call to shf_emptybuf() */
151 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
152 shf->flags = sflags;
Geremy Condra03ebf062011-10-12 18:17:24 -0700153 shf->errnosv = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700154 shf->bsize = bsize;
155 if (sflags & SHF_CLEXEC)
156 fcntl(fd, F_SETFD, FD_CLOEXEC);
157 return (shf);
158}
159
160/* Set up an existing shf (and buffer) to use the given fd */
161struct shf *
162shf_reopen(int fd, int sflags, struct shf *shf)
163{
Geremy Condra03ebf062011-10-12 18:17:24 -0700164 ssize_t bsize =
165 /* at most 512 */
166 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700167
Geremy Condra03ebf062011-10-12 18:17:24 -0700168 shf_open_hlp(fd, &sflags, "shf_reopen");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700169 if (!shf || !shf->buf || shf->bsize < bsize)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700170 internal_errorf(Tf_sD_s, "shf_reopen", Tbad_bsize);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700171
172 /* assumes shf->buf and shf->bsize already set up */
173 shf->fd = fd;
174 shf->rp = shf->wp = shf->buf;
175 shf->rnleft = 0;
176 shf->rbsize = bsize;
177 shf->wnleft = 0; /* force call to shf_emptybuf() */
178 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
179 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
Geremy Condra03ebf062011-10-12 18:17:24 -0700180 shf->errnosv = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700181 if (sflags & SHF_CLEXEC)
182 fcntl(fd, F_SETFD, FD_CLOEXEC);
183 return (shf);
184}
185
Geremy Condra03ebf062011-10-12 18:17:24 -0700186/*
187 * Open a string for reading or writing. If reading, bsize is the number
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700188 * of bytes that can be read. If writing, bsize is the maximum number of
Geremy Condra03ebf062011-10-12 18:17:24 -0700189 * bytes that can be written. If shf is not NULL, it is filled in and
190 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700191 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
192 * used for the initial size). Doesn't fail.
Geremy Condra03ebf062011-10-12 18:17:24 -0700193 * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700194 */
195struct shf *
Geremy Condra03ebf062011-10-12 18:17:24 -0700196shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700197{
198 /* can't have a read+write string */
199 if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700200 internal_errorf(Tf_flags, "shf_sopen",
201 (unsigned int)sflags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700202
203 if (!shf) {
204 shf = alloc(sizeof(struct shf), ATEMP);
205 sflags |= SHF_ALLOCS;
206 }
207 shf->areap = ATEMP;
208 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
209 if (bsize <= 0)
210 bsize = 64;
211 sflags |= SHF_ALLOCB;
212 buf = alloc(bsize, shf->areap);
213 }
214 shf->fd = -1;
215 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
216 shf->rnleft = bsize;
217 shf->rbsize = bsize;
218 shf->wnleft = bsize - 1; /* space for a '\0' */
219 shf->wbsize = bsize;
220 shf->flags = sflags | SHF_STRING;
Geremy Condra03ebf062011-10-12 18:17:24 -0700221 shf->errnosv = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700222 shf->bsize = bsize;
223
224 return (shf);
225}
226
227/* Flush and close file descriptor, free the shf structure */
228int
229shf_close(struct shf *shf)
230{
231 int ret = 0;
232
233 if (shf->fd >= 0) {
234 ret = shf_flush(shf);
235 if (close(shf->fd) < 0)
Elliott Hughes50012062015-03-10 22:22:24 -0700236 ret = -1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700237 }
238 if (shf->flags & SHF_ALLOCS)
239 afree(shf, shf->areap);
240 else if (shf->flags & SHF_ALLOCB)
241 afree(shf->buf, shf->areap);
242
243 return (ret);
244}
245
246/* Flush and close file descriptor, don't free file structure */
247int
248shf_fdclose(struct shf *shf)
249{
250 int ret = 0;
251
252 if (shf->fd >= 0) {
253 ret = shf_flush(shf);
254 if (close(shf->fd) < 0)
Elliott Hughes50012062015-03-10 22:22:24 -0700255 ret = -1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700256 shf->rnleft = 0;
257 shf->rp = shf->buf;
258 shf->wnleft = 0;
259 shf->fd = -1;
260 }
261
262 return (ret);
263}
264
Geremy Condra03ebf062011-10-12 18:17:24 -0700265/*
266 * Close a string - if it was opened for writing, it is NUL terminated;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700267 * returns a pointer to the string and frees shf if it was allocated
268 * (does not free string if it was allocated).
269 */
270char *
271shf_sclose(struct shf *shf)
272{
273 unsigned char *s = shf->buf;
274
Geremy Condra03ebf062011-10-12 18:17:24 -0700275 /* NUL terminate */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700276 if (shf->flags & SHF_WR) {
277 shf->wnleft++;
278 shf_putc('\0', shf);
279 }
280 if (shf->flags & SHF_ALLOCS)
281 afree(shf, shf->areap);
282 return ((char *)s);
283}
284
Geremy Condra03ebf062011-10-12 18:17:24 -0700285/*
286 * Un-read what has been read but not examined, or write what has been
Elliott Hughes50012062015-03-10 22:22:24 -0700287 * buffered. Returns 0 for success, -1 for (write) error.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700288 */
289int
290shf_flush(struct shf *shf)
291{
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700292 int rv = 0;
293
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700294 if (shf->flags & SHF_STRING)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700295 rv = (shf->flags & SHF_WR) ? -1 : 0;
296 else if (shf->fd < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700297 internal_errorf(Tf_sD_s, "shf_flush", "no fd");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700298 else if (shf->flags & SHF_ERROR) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700299 errno = shf->errnosv;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700300 rv = -1;
301 } else if (shf->flags & SHF_READING) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700302 shf->flags &= ~(SHF_EOF | SHF_READING);
303 if (shf->rnleft > 0) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700304 if (lseek(shf->fd, (off_t)-shf->rnleft,
305 SEEK_CUR) == -1) {
306 shf->flags |= SHF_ERROR;
307 shf->errnosv = errno;
308 rv = -1;
309 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700310 shf->rnleft = 0;
311 shf->rp = shf->buf;
312 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700313 } else if (shf->flags & SHF_WRITING)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700314 rv = shf_emptybuf(shf, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700315
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700316 return (rv);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700317}
318
Geremy Condra03ebf062011-10-12 18:17:24 -0700319/*
320 * Write out any buffered data. If currently reading, flushes the read
Elliott Hughes50012062015-03-10 22:22:24 -0700321 * buffer. Returns 0 for success, -1 for (write) error.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700322 */
323static int
324shf_emptybuf(struct shf *shf, int flags)
325{
326 int ret = 0;
327
328 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700329 internal_errorf(Tf_sD_s, "shf_emptybuf", "no fd");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700330
331 if (shf->flags & SHF_ERROR) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700332 errno = shf->errnosv;
Elliott Hughes50012062015-03-10 22:22:24 -0700333 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700334 }
335
336 if (shf->flags & SHF_READING) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700337 if (flags & EB_READSW)
338 /* doesn't happen */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700339 return (0);
340 ret = shf_flush(shf);
341 shf->flags &= ~SHF_READING;
342 }
343 if (shf->flags & SHF_STRING) {
344 unsigned char *nbuf;
345
Geremy Condra03ebf062011-10-12 18:17:24 -0700346 /*
347 * Note that we assume SHF_ALLOCS is not set if
348 * SHF_ALLOCB is set... (changing the shf pointer could
349 * cause problems)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700350 */
351 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
352 !(shf->flags & SHF_ALLOCB))
Elliott Hughes50012062015-03-10 22:22:24 -0700353 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700354 /* allocate more space for buffer */
Geremy Condra03ebf062011-10-12 18:17:24 -0700355 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700356 shf->rp = nbuf + (shf->rp - shf->buf);
357 shf->wp = nbuf + (shf->wp - shf->buf);
358 shf->rbsize += shf->wbsize;
359 shf->wnleft += shf->wbsize;
Geremy Condra03ebf062011-10-12 18:17:24 -0700360 shf->wbsize <<= 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700361 shf->buf = nbuf;
362 } else {
363 if (shf->flags & SHF_WRITING) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700364 ssize_t n, ntowrite = shf->wp - shf->buf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700365 unsigned char *buf = shf->buf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700366
367 while (ntowrite > 0) {
368 n = write(shf->fd, buf, ntowrite);
369 if (n < 0) {
370 if (errno == EINTR &&
371 !(shf->flags & SHF_INTERRUPT))
372 continue;
373 shf->flags |= SHF_ERROR;
Geremy Condra03ebf062011-10-12 18:17:24 -0700374 shf->errnosv = errno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700375 shf->wnleft = 0;
376 if (buf != shf->buf) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700377 /*
378 * allow a second flush
379 * to work
380 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700381 memmove(shf->buf, buf,
382 ntowrite);
383 shf->wp = shf->buf + ntowrite;
384 }
Elliott Hughes50012062015-03-10 22:22:24 -0700385 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700386 }
387 buf += n;
388 ntowrite -= n;
389 }
390 if (flags & EB_READSW) {
391 shf->wp = shf->buf;
392 shf->wnleft = 0;
393 shf->flags &= ~SHF_WRITING;
394 return (0);
395 }
396 }
397 shf->wp = shf->buf;
398 shf->wnleft = shf->wbsize;
399 }
400 shf->flags |= SHF_WRITING;
401
402 return (ret);
403}
404
Elliott Hughes50012062015-03-10 22:22:24 -0700405/* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700406static int
407shf_fillbuf(struct shf *shf)
408{
Geremy Condra03ebf062011-10-12 18:17:24 -0700409 ssize_t n;
410
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700411 if (shf->flags & SHF_STRING)
412 return (0);
413
414 if (shf->fd < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700415 internal_errorf(Tf_sD_s, "shf_fillbuf", "no fd");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700416
417 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
418 if (shf->flags & SHF_ERROR)
Geremy Condra03ebf062011-10-12 18:17:24 -0700419 errno = shf->errnosv;
Elliott Hughes50012062015-03-10 22:22:24 -0700420 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700421 }
422
Elliott Hughes50012062015-03-10 22:22:24 -0700423 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
424 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700425
426 shf->flags |= SHF_READING;
427
428 shf->rp = shf->buf;
Geremy Condra03ebf062011-10-12 18:17:24 -0700429 while (/* CONSTCOND */ 1) {
430 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
431 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700432 continue;
433 break;
434 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700435 if (n < 0) {
436 shf->flags |= SHF_ERROR;
437 shf->errnosv = errno;
438 shf->rnleft = 0;
439 shf->rp = shf->buf;
Elliott Hughes50012062015-03-10 22:22:24 -0700440 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700441 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700442 if ((shf->rnleft = n) == 0)
443 shf->flags |= SHF_EOF;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700444 return (0);
445}
446
Geremy Condra03ebf062011-10-12 18:17:24 -0700447/*
448 * Read a buffer from shf. Returns the number of bytes read into buf, if
Elliott Hughes50012062015-03-10 22:22:24 -0700449 * no bytes were read, returns 0 if end of file was seen, -1 if a read
Geremy Condra03ebf062011-10-12 18:17:24 -0700450 * error occurred.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700451 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700452ssize_t
453shf_read(char *buf, ssize_t bsize, struct shf *shf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700454{
Geremy Condra03ebf062011-10-12 18:17:24 -0700455 ssize_t ncopy, orig_bsize = bsize;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700456
457 if (!(shf->flags & SHF_RD))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700458 internal_errorf(Tf_flags, Tshf_read,
459 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700460
461 if (bsize <= 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700462 internal_errorf(Tf_szs, Tshf_read, bsize, Tbsize);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700463
464 while (bsize > 0) {
465 if (shf->rnleft == 0 &&
Elliott Hughes50012062015-03-10 22:22:24 -0700466 (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700467 break;
468 ncopy = shf->rnleft;
469 if (ncopy > bsize)
470 ncopy = bsize;
471 memcpy(buf, shf->rp, ncopy);
472 buf += ncopy;
473 bsize -= ncopy;
474 shf->rp += ncopy;
475 shf->rnleft -= ncopy;
476 }
477 /* Note: fread(3S) returns 0 for errors - this doesn't */
Elliott Hughes50012062015-03-10 22:22:24 -0700478 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700479 orig_bsize - bsize);
480}
481
Geremy Condra03ebf062011-10-12 18:17:24 -0700482/*
Elliott Hughes50012062015-03-10 22:22:24 -0700483 * Read up to a newline or -1. The newline is put in buf; buf is always
Geremy Condra03ebf062011-10-12 18:17:24 -0700484 * NUL terminated. Returns NULL on read error or if nothing was read
485 * before end of file, returns a pointer to the NUL byte in buf
486 * otherwise.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700487 */
488char *
Geremy Condra03ebf062011-10-12 18:17:24 -0700489shf_getse(char *buf, ssize_t bsize, struct shf *shf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700490{
491 unsigned char *end;
Geremy Condra03ebf062011-10-12 18:17:24 -0700492 ssize_t ncopy;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700493 char *orig_buf = buf;
494
495 if (!(shf->flags & SHF_RD))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700496 internal_errorf(Tf_flags, "shf_getse",
497 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700498
499 if (bsize <= 0)
500 return (NULL);
501
Geremy Condra03ebf062011-10-12 18:17:24 -0700502 /* save room for NUL */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000503 --bsize;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700504 do {
505 if (shf->rnleft == 0) {
Elliott Hughes50012062015-03-10 22:22:24 -0700506 if (shf_fillbuf(shf) == -1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700507 return (NULL);
508 if (shf->rnleft == 0) {
509 *buf = '\0';
510 return (buf == orig_buf ? NULL : buf);
511 }
512 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700513 end = (unsigned char *)memchr((char *)shf->rp, '\n',
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700514 shf->rnleft);
515 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
516 if (ncopy > bsize)
517 ncopy = bsize;
518 memcpy(buf, (char *) shf->rp, ncopy);
519 shf->rp += ncopy;
520 shf->rnleft -= ncopy;
521 buf += ncopy;
522 bsize -= ncopy;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700523#ifdef MKSH_WITH_TEXTMODE
524 if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
525 buf--;
526 bsize++;
527 buf[-1] = '\n';
528 }
529#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700530 } while (!end && bsize);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700531#ifdef MKSH_WITH_TEXTMODE
532 if (!bsize && buf[-1] == '\r') {
533 int c = shf_getc(shf);
534 if (c == '\n')
535 buf[-1] = '\n';
536 else if (c != -1)
537 shf_ungetc(c, shf);
538 }
539#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700540 *buf = '\0';
541 return (buf);
542}
543
Elliott Hughes50012062015-03-10 22:22:24 -0700544/* Returns the char read. Returns -1 for error and end of file. */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700545int
546shf_getchar(struct shf *shf)
547{
548 if (!(shf->flags & SHF_RD))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700549 internal_errorf(Tf_flags, "shf_getchar",
550 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700551
Elliott Hughes50012062015-03-10 22:22:24 -0700552 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
553 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700554 --shf->rnleft;
555 return (*shf->rp++);
556}
557
Geremy Condra03ebf062011-10-12 18:17:24 -0700558/*
559 * Put a character back in the input stream. Returns the character if
Elliott Hughes50012062015-03-10 22:22:24 -0700560 * successful, -1 if there is no room.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700561 */
562int
563shf_ungetc(int c, struct shf *shf)
564{
565 if (!(shf->flags & SHF_RD))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700566 internal_errorf(Tf_flags, "shf_ungetc",
567 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700568
Elliott Hughes50012062015-03-10 22:22:24 -0700569 if ((shf->flags & SHF_ERROR) || c == -1 ||
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700570 (shf->rp == shf->buf && shf->rnleft))
Elliott Hughes50012062015-03-10 22:22:24 -0700571 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700572
Elliott Hughes50012062015-03-10 22:22:24 -0700573 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
574 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700575
576 if (shf->rp == shf->buf)
577 shf->rp = shf->buf + shf->rbsize;
578 if (shf->flags & SHF_STRING) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700579 /*
580 * Can unget what was read, but not something different;
581 * we don't want to modify a string.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700582 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000583 if ((int)(shf->rp[-1]) != c)
Elliott Hughes50012062015-03-10 22:22:24 -0700584 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700585 shf->flags &= ~SHF_EOF;
586 shf->rp--;
587 shf->rnleft++;
588 return (c);
589 }
590 shf->flags &= ~SHF_EOF;
591 *--(shf->rp) = c;
592 shf->rnleft++;
593 return (c);
594}
595
Geremy Condra03ebf062011-10-12 18:17:24 -0700596/*
Elliott Hughes50012062015-03-10 22:22:24 -0700597 * Write a character. Returns the character if successful, -1 if the
Geremy Condra03ebf062011-10-12 18:17:24 -0700598 * char could not be written.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700599 */
600int
601shf_putchar(int c, struct shf *shf)
602{
603 if (!(shf->flags & SHF_WR))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700604 internal_errorf(Tf_flags, "shf_putchar",
605 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700606
Elliott Hughes50012062015-03-10 22:22:24 -0700607 if (c == -1)
608 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700609
610 if (shf->flags & SHF_UNBUF) {
611 unsigned char cc = (unsigned char)c;
Geremy Condra03ebf062011-10-12 18:17:24 -0700612 ssize_t n;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700613
614 if (shf->fd < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700615 internal_errorf(Tf_sD_s, "shf_putchar", "no fd");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700616 if (shf->flags & SHF_ERROR) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700617 errno = shf->errnosv;
Elliott Hughes50012062015-03-10 22:22:24 -0700618 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700619 }
620 while ((n = write(shf->fd, &cc, 1)) != 1)
621 if (n < 0) {
622 if (errno == EINTR &&
623 !(shf->flags & SHF_INTERRUPT))
624 continue;
625 shf->flags |= SHF_ERROR;
Geremy Condra03ebf062011-10-12 18:17:24 -0700626 shf->errnosv = errno;
Elliott Hughes50012062015-03-10 22:22:24 -0700627 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700628 }
629 } else {
630 /* Flush deals with strings and sticky errors */
Elliott Hughes50012062015-03-10 22:22:24 -0700631 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
632 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700633 shf->wnleft--;
634 *shf->wp++ = c;
635 }
636
637 return (c);
638}
639
Geremy Condra03ebf062011-10-12 18:17:24 -0700640/*
Elliott Hughes50012062015-03-10 22:22:24 -0700641 * Write a string. Returns the length of the string if successful, -1
Geremy Condra03ebf062011-10-12 18:17:24 -0700642 * if the string could not be written.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700643 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700644ssize_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700645shf_puts(const char *s, struct shf *shf)
646{
647 if (!s)
Elliott Hughes50012062015-03-10 22:22:24 -0700648 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700649
650 return (shf_write(s, strlen(s), shf));
651}
652
Elliott Hughes50012062015-03-10 22:22:24 -0700653/* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
Geremy Condra03ebf062011-10-12 18:17:24 -0700654ssize_t
655shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700656{
Geremy Condra03ebf062011-10-12 18:17:24 -0700657 ssize_t n, ncopy, orig_nbytes = nbytes;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700658
659 if (!(shf->flags & SHF_WR))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700660 internal_errorf(Tf_flags, Tshf_write,
661 (unsigned int)shf->flags);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700662
663 if (nbytes < 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700664 internal_errorf(Tf_szs, Tshf_write, nbytes, Tbytes);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700665
666 /* Don't buffer if buffer is empty and we're writting a large amount. */
667 if ((ncopy = shf->wnleft) &&
668 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
669 if (ncopy > nbytes)
670 ncopy = nbytes;
671 memcpy(shf->wp, buf, ncopy);
672 nbytes -= ncopy;
673 buf += ncopy;
674 shf->wp += ncopy;
675 shf->wnleft -= ncopy;
676 }
677 if (nbytes > 0) {
678 if (shf->flags & SHF_STRING) {
679 /* resize buffer until there's enough space left */
680 while (nbytes > shf->wnleft)
Elliott Hughes50012062015-03-10 22:22:24 -0700681 if (shf_emptybuf(shf, EB_GROW) == -1)
682 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700683 /* then write everything into the buffer */
684 } else {
685 /* flush deals with sticky errors */
Elliott Hughes50012062015-03-10 22:22:24 -0700686 if (shf_emptybuf(shf, EB_GROW) == -1)
687 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700688 /* write chunks larger than window size directly */
689 if (nbytes > shf->wbsize) {
690 ncopy = nbytes;
691 if (shf->wbsize)
692 ncopy -= nbytes % shf->wbsize;
693 nbytes -= ncopy;
694 while (ncopy > 0) {
695 n = write(shf->fd, buf, ncopy);
696 if (n < 0) {
697 if (errno == EINTR &&
698 !(shf->flags & SHF_INTERRUPT))
699 continue;
700 shf->flags |= SHF_ERROR;
Geremy Condra03ebf062011-10-12 18:17:24 -0700701 shf->errnosv = errno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700702 shf->wnleft = 0;
703 /*
704 * Note: fwrite(3) returns 0
705 * for errors - this doesn't
706 */
Elliott Hughes50012062015-03-10 22:22:24 -0700707 return (-1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700708 }
709 buf += n;
710 ncopy -= n;
711 }
712 }
713 /* ... and buffer the rest */
714 }
715 if (nbytes > 0) {
716 /* write remaining bytes to buffer */
717 memcpy(shf->wp, buf, nbytes);
718 shf->wp += nbytes;
719 shf->wnleft -= nbytes;
720 }
721 }
722
723 return (orig_nbytes);
724}
725
Geremy Condra03ebf062011-10-12 18:17:24 -0700726ssize_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700727shf_fprintf(struct shf *shf, const char *fmt, ...)
728{
729 va_list args;
Geremy Condra03ebf062011-10-12 18:17:24 -0700730 ssize_t n;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700731
732 va_start(args, fmt);
733 n = shf_vfprintf(shf, fmt, args);
734 va_end(args);
735
736 return (n);
737}
738
Geremy Condra03ebf062011-10-12 18:17:24 -0700739ssize_t
740shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700741{
742 struct shf shf;
743 va_list args;
Geremy Condra03ebf062011-10-12 18:17:24 -0700744 ssize_t n;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700745
746 if (!buf || bsize <= 0)
Geremy Condra03ebf062011-10-12 18:17:24 -0700747 internal_errorf("shf_snprintf: buf %zX, bsize %zd",
748 (size_t)buf, bsize);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700749
750 shf_sopen(buf, bsize, SHF_WR, &shf);
751 va_start(args, fmt);
752 n = shf_vfprintf(&shf, fmt, args);
753 va_end(args);
Geremy Condra03ebf062011-10-12 18:17:24 -0700754 /* NUL terminates */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000755 shf_sclose(&shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700756 return (n);
757}
758
759char *
760shf_smprintf(const char *fmt, ...)
761{
762 struct shf shf;
763 va_list args;
764
765 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
766 va_start(args, fmt);
767 shf_vfprintf(&shf, fmt, args);
768 va_end(args);
Geremy Condra03ebf062011-10-12 18:17:24 -0700769 /* NUL terminates */
770 return (shf_sclose(&shf));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700771}
772
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700773#define FL_HASH 0x001 /* '#' seen */
774#define FL_PLUS 0x002 /* '+' seen */
775#define FL_RIGHT 0x004 /* '-' seen */
776#define FL_BLANK 0x008 /* ' ' seen */
777#define FL_SHORT 0x010 /* 'h' seen */
778#define FL_LONG 0x020 /* 'l' seen */
779#define FL_ZERO 0x040 /* '0' seen */
780#define FL_DOT 0x080 /* '.' seen */
781#define FL_UPPER 0x100 /* format character was uppercase */
782#define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
Geremy Condra03ebf062011-10-12 18:17:24 -0700783#define FL_SIZET 0x400 /* 'z' seen */
784#define FM_SIZES 0x430 /* h/l/z mask */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700785
Geremy Condra03ebf062011-10-12 18:17:24 -0700786ssize_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700787shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
788{
789 const char *s;
790 char c, *cp;
Geremy Condra03ebf062011-10-12 18:17:24 -0700791 int tmp = 0, flags;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700792 size_t field, precision, len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700793 unsigned long lnum;
794 /* %#o produces the longest output */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700795 char numbuf[(8 * sizeof(long) + 2) / 3 + 1 + /* NUL */ 1];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700796 /* this stuff for dealing with the buffer */
Geremy Condra03ebf062011-10-12 18:17:24 -0700797 ssize_t nwritten = 0;
798
799#define VA(type) va_arg(args, type)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700800
801 if (!fmt)
802 return (0);
803
804 while ((c = *fmt++)) {
805 if (c != '%') {
806 shf_putc(c, shf);
807 nwritten++;
808 continue;
809 }
810 /*
Geremy Condra03ebf062011-10-12 18:17:24 -0700811 * This will accept flags/fields in any order - not just
812 * the order specified in printf(3), but this is the way
813 * _doprnt() seems to work (on BSD and SYSV). The only
814 * restriction is that the format character must come
815 * last :-).
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700816 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700817 flags = 0;
818 field = precision = 0;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700819 while ((c = *fmt++)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700820 switch (c) {
821 case '#':
822 flags |= FL_HASH;
823 continue;
824
825 case '+':
826 flags |= FL_PLUS;
827 continue;
828
829 case '-':
830 flags |= FL_RIGHT;
831 continue;
832
833 case ' ':
834 flags |= FL_BLANK;
835 continue;
836
837 case '0':
838 if (!(flags & FL_DOT))
839 flags |= FL_ZERO;
840 continue;
841
842 case '.':
843 flags |= FL_DOT;
844 precision = 0;
845 continue;
846
847 case '*':
Geremy Condra03ebf062011-10-12 18:17:24 -0700848 tmp = VA(int);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700849 if (tmp < 0) {
850 if (flags & FL_DOT)
851 precision = 0;
852 else {
853 field = (unsigned int)-tmp;
854 flags |= FL_RIGHT;
855 }
856 } else if (flags & FL_DOT)
857 precision = (unsigned int)tmp;
858 else
859 field = (unsigned int)tmp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700860 continue;
861
862 case 'l':
Geremy Condra03ebf062011-10-12 18:17:24 -0700863 flags &= ~FM_SIZES;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700864 flags |= FL_LONG;
865 continue;
866
867 case 'h':
Geremy Condra03ebf062011-10-12 18:17:24 -0700868 flags &= ~FM_SIZES;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700869 flags |= FL_SHORT;
870 continue;
Geremy Condra03ebf062011-10-12 18:17:24 -0700871
872 case 'z':
873 flags &= ~FM_SIZES;
874 flags |= FL_SIZET;
875 continue;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700876 }
877 if (ksh_isdigit(c)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000878 bool overflowed = false;
879
Elliott Hughes96b43632015-07-17 11:39:41 -0700880 tmp = ksh_numdig(c);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700881 while (c = *fmt++, ksh_isdigit(c))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000882 if (notok2mul(2147483647, tmp, 10))
883 overflowed = true;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700884 else
885 tmp = tmp * 10 + ksh_numdig(c);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700886 --fmt;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000887 if (overflowed)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700888 tmp = 0;
889 if (flags & FL_DOT)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700890 precision = (unsigned int)tmp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700891 else
Elliott Hughes77740fc2016-08-12 15:06:53 -0700892 field = (unsigned int)tmp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700893 continue;
894 }
895 break;
896 }
897
Geremy Condra03ebf062011-10-12 18:17:24 -0700898 if (!c)
899 /* nasty format */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700900 break;
901
Elliott Hughes96b43632015-07-17 11:39:41 -0700902 if (ksh_isupper(c)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700903 flags |= FL_UPPER;
904 c = ksh_tolower(c);
905 }
906
907 switch (c) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700908 case 'd':
909 case 'i':
Geremy Condra03ebf062011-10-12 18:17:24 -0700910 if (flags & FL_SIZET)
911 lnum = (long)VA(ssize_t);
912 else if (flags & FL_LONG)
913 lnum = VA(long);
914 else if (flags & FL_SHORT)
915 lnum = (long)(short)VA(int);
916 else
917 lnum = (long)VA(int);
918 goto integral;
919
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700920 case 'o':
921 case 'u':
922 case 'x':
Geremy Condra03ebf062011-10-12 18:17:24 -0700923 if (flags & FL_SIZET)
924 lnum = VA(size_t);
925 else if (flags & FL_LONG)
926 lnum = VA(unsigned long);
927 else if (flags & FL_SHORT)
928 lnum = (unsigned long)(unsigned short)VA(int);
929 else
930 lnum = (unsigned long)VA(unsigned int);
931
932 integral:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700933 flags |= FL_NUMBER;
934 cp = numbuf + sizeof(numbuf);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700935 *--cp = '\0';
Geremy Condra03ebf062011-10-12 18:17:24 -0700936
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700937 switch (c) {
938 case 'd':
939 case 'i':
940 if (0 > (long)lnum) {
941 lnum = -(long)lnum;
942 tmp = 1;
943 } else
944 tmp = 0;
945 /* FALLTHROUGH */
946 case 'u':
947 do {
Elliott Hughes96b43632015-07-17 11:39:41 -0700948 *--cp = digits_lc[lnum % 10];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700949 lnum /= 10;
950 } while (lnum);
951
952 if (c != 'u') {
953 if (tmp)
954 *--cp = '-';
955 else if (flags & FL_PLUS)
956 *--cp = '+';
957 else if (flags & FL_BLANK)
958 *--cp = ' ';
959 }
960 break;
961
962 case 'o':
963 do {
Elliott Hughes96b43632015-07-17 11:39:41 -0700964 *--cp = digits_lc[lnum & 0x7];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700965 lnum >>= 3;
966 } while (lnum);
967
968 if ((flags & FL_HASH) && *cp != '0')
969 *--cp = '0';
970 break;
971
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700972 case 'x': {
973 const char *digits = (flags & FL_UPPER) ?
974 digits_uc : digits_lc;
975 do {
Elliott Hughes96b43632015-07-17 11:39:41 -0700976 *--cp = digits[lnum & 0xF];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700977 lnum >>= 4;
978 } while (lnum);
979
980 if (flags & FL_HASH) {
981 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
982 *--cp = '0';
983 }
Elliott Hughes77740fc2016-08-12 15:06:53 -0700984 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700985 }
Elliott Hughes77740fc2016-08-12 15:06:53 -0700986 len = numbuf + sizeof(numbuf) - 1 - (s = cp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700987 if (flags & FL_DOT) {
988 if (precision > len) {
989 field = precision;
990 flags |= FL_ZERO;
991 } else
Geremy Condra03ebf062011-10-12 18:17:24 -0700992 /* no loss */
993 precision = len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700994 }
995 break;
996
997 case 's':
Geremy Condra03ebf062011-10-12 18:17:24 -0700998 if ((s = VA(const char *)) == NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700999 s = "(null)";
Thorsten Glaser811a5752013-07-25 14:24:45 +00001000 else if (flags & FL_HASH) {
1001 print_value_quoted(shf, s);
1002 continue;
1003 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001004 len = utf_mbswidth(s);
1005 break;
1006
1007 case 'c':
1008 flags &= ~FL_DOT;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001009 c = (char)(VA(int));
1010 /* FALLTHROUGH */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001011
1012 case '%':
1013 default:
1014 numbuf[0] = c;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001015 numbuf[1] = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001016 s = numbuf;
1017 len = 1;
1018 break;
1019 }
1020
1021 /*
1022 * At this point s should point to a string that is to be
1023 * formatted, and len should be the length of the string.
1024 */
1025 if (!(flags & FL_DOT) || len < precision)
1026 precision = len;
1027 if (field > precision) {
1028 field -= precision;
1029 if (!(flags & FL_RIGHT)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001030 /* skip past sign or 0x when padding with 0 */
1031 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1032 if (*s == '+' || *s == '-' ||
1033 *s == ' ') {
1034 shf_putc(*s, shf);
1035 s++;
1036 precision--;
1037 nwritten++;
1038 } else if (*s == '0') {
1039 shf_putc(*s, shf);
1040 s++;
1041 nwritten++;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001042 if (--precision &&
Elliott Hughes96b43632015-07-17 11:39:41 -07001043 ksh_eq(*s, 'X', 'x')) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001044 shf_putc(*s, shf);
1045 s++;
1046 precision--;
1047 nwritten++;
1048 }
1049 }
1050 c = '0';
1051 } else
1052 c = flags & FL_ZERO ? '0' : ' ';
Elliott Hughes77740fc2016-08-12 15:06:53 -07001053 nwritten += field;
1054 while (field--)
1055 shf_putc(c, shf);
1056 field = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001057 } else
1058 c = ' ';
1059 } else
1060 field = 0;
1061
Elliott Hughes77740fc2016-08-12 15:06:53 -07001062 nwritten += precision;
1063 precision = utf_skipcols(s, precision, &tmp) - s;
1064 while (precision--)
1065 shf_putc(*s++, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001066
Elliott Hughes77740fc2016-08-12 15:06:53 -07001067 nwritten += field;
1068 while (field--)
1069 shf_putc(c, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001070 }
1071
Elliott Hughes50012062015-03-10 22:22:24 -07001072 return (shf_error(shf) ? -1 : nwritten);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001073}
1074
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001075#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001076int
1077shf_getc(struct shf *shf)
1078{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001079 return (shf_getc_i(shf));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001080}
1081
1082int
1083shf_putc(int c, struct shf *shf)
1084{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001085 return (shf_putc_i(c, shf));
1086}
1087#endif
1088
1089#ifdef DEBUG
1090const char *
1091cstrerror(int errnum)
1092{
1093#undef strerror
1094 return (strerror(errnum));
1095#define strerror dontuse_strerror /* poisoned */
1096}
1097#elif !HAVE_STRERROR
1098
1099#if HAVE_SYS_ERRLIST
1100#if !HAVE_SYS_ERRLIST_DECL
1101extern const int sys_nerr;
1102extern const char * const sys_errlist[];
1103#endif
1104#endif
1105
1106const char *
1107cstrerror(int errnum)
1108{
1109 /* "Unknown error: " + sign + rough estimate + NUL */
1110 static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
1111
1112#if HAVE_SYS_ERRLIST
1113 if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
1114 return (sys_errlist[errnum]);
1115#endif
1116
1117 switch (errnum) {
1118 case 0:
1119 return ("Undefined error: 0");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001120 case EPERM:
1121 return ("Operation not permitted");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001122 case ENOENT:
1123 return ("No such file or directory");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001124#ifdef ESRCH
1125 case ESRCH:
1126 return ("No such process");
1127#endif
1128#ifdef E2BIG
1129 case E2BIG:
1130 return ("Argument list too long");
1131#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001132 case ENOEXEC:
1133 return ("Exec format error");
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001134 case EBADF:
1135 return ("Bad file descriptor");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001136#ifdef ENOMEM
1137 case ENOMEM:
1138 return ("Cannot allocate memory");
1139#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001140 case EACCES:
1141 return ("Permission denied");
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001142 case EEXIST:
1143 return ("File exists");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001144 case ENOTDIR:
1145 return ("Not a directory");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001146#ifdef EINVAL
1147 case EINVAL:
1148 return ("Invalid argument");
1149#endif
1150#ifdef ELOOP
1151 case ELOOP:
1152 return ("Too many levels of symbolic links");
1153#endif
1154 default:
1155 shf_snprintf(errbuf, sizeof(errbuf),
1156 "Unknown error: %d", errnum);
1157 return (errbuf);
1158 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001159}
1160#endif