Home | History | Annotate | Download | only in src
      1 /*	$OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
      5  *		 2012, 2013, 2015
      6  *	mirabilos <m (at) mirbsd.org>
      7  *
      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.
     22  *-
     23  * Use %zX instead of %p and floating point isn't supported at all.
     24  */
     25 
     26 #include "sh.h"
     27 
     28 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.69 2015/12/31 20:38:59 tg Exp $");
     29 
     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 
     40 static int shf_fillbuf(struct shf *);
     41 static int shf_emptybuf(struct shf *, int);
     42 
     43 /*
     44  * Open a file. First three args are for open(), last arg is flags for
     45  * this package. Returns NULL if file could not be opened, or if a dup
     46  * fails.
     47  */
     48 struct shf *
     49 shf_open(const char *name, int oflags, int mode, int sflags)
     50 {
     51 	struct shf *shf;
     52 	ssize_t bsize =
     53 	    /* at most 512 */
     54 	    sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
     55 	int fd, eno;
     56 
     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 
     65 	fd = binopen3(name, oflags, mode);
     66 	if (fd < 0) {
     67 		eno = errno;
     68 		afree(shf, shf->areap);
     69 		errno = eno;
     70 		return (NULL);
     71 	}
     72 	if ((sflags & SHF_MAPHI) && fd < FDBASE) {
     73 		int nfd;
     74 
     75 		nfd = fcntl(fd, F_DUPFD, FDBASE);
     76 		eno = errno;
     77 		close(fd);
     78 		if (nfd < 0) {
     79 			afree(shf, shf->areap);
     80 			errno = eno;
     81 			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 
     92 /* helper function for shf_fdopen and shf_reopen */
     93 static void
     94 shf_open_hlp(int fd, int *sflagsp, const char *where)
     95 {
     96 	int sflags = *sflagsp;
     97 
     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 		}
    118 		*sflagsp = sflags;
    119 	}
    120 
    121 	if (!(sflags & (SHF_RD | SHF_WR)))
    122 		internal_errorf("%s: %s", where, "missing read/write");
    123 }
    124 
    125 /* Set up the shf structure for a file descriptor. Doesn't fail. */
    126 struct shf *
    127 shf_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");
    134 	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;
    153 	shf->errnosv = 0;
    154 	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 */
    161 struct shf *
    162 shf_reopen(int fd, int sflags, struct shf *shf)
    163 {
    164 	ssize_t bsize =
    165 	    /* at most 512 */
    166 	    sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
    167 
    168 	shf_open_hlp(fd, &sflags, "shf_reopen");
    169 	if (!shf || !shf->buf || shf->bsize < bsize)
    170 		internal_errorf("%s: %s", "shf_reopen", "bad shf/buf/bsize");
    171 
    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;
    180 	shf->errnosv = 0;
    181 	if (sflags & SHF_CLEXEC)
    182 		fcntl(fd, F_SETFD, FD_CLOEXEC);
    183 	return (shf);
    184 }
    185 
    186 /*
    187  * Open a string for reading or writing. If reading, bsize is the number
    188  * of bytes that can be read. If writing, bsize is the maximum number of
    189  * 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
    191  * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
    192  * used for the initial size). Doesn't fail.
    193  * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
    194  */
    195 struct shf *
    196 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
    197 {
    198 	/* can't have a read+write string */
    199 	if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
    200 		internal_errorf("%s: flags 0x%X", "shf_sopen", sflags);
    201 
    202 	if (!shf) {
    203 		shf = alloc(sizeof(struct shf), ATEMP);
    204 		sflags |= SHF_ALLOCS;
    205 	}
    206 	shf->areap = ATEMP;
    207 	if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
    208 		if (bsize <= 0)
    209 			bsize = 64;
    210 		sflags |= SHF_ALLOCB;
    211 		buf = alloc(bsize, shf->areap);
    212 	}
    213 	shf->fd = -1;
    214 	shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
    215 	shf->rnleft = bsize;
    216 	shf->rbsize = bsize;
    217 	shf->wnleft = bsize - 1;	/* space for a '\0' */
    218 	shf->wbsize = bsize;
    219 	shf->flags = sflags | SHF_STRING;
    220 	shf->errnosv = 0;
    221 	shf->bsize = bsize;
    222 
    223 	return (shf);
    224 }
    225 
    226 /* Flush and close file descriptor, free the shf structure */
    227 int
    228 shf_close(struct shf *shf)
    229 {
    230 	int ret = 0;
    231 
    232 	if (shf->fd >= 0) {
    233 		ret = shf_flush(shf);
    234 		if (close(shf->fd) < 0)
    235 			ret = -1;
    236 	}
    237 	if (shf->flags & SHF_ALLOCS)
    238 		afree(shf, shf->areap);
    239 	else if (shf->flags & SHF_ALLOCB)
    240 		afree(shf->buf, shf->areap);
    241 
    242 	return (ret);
    243 }
    244 
    245 /* Flush and close file descriptor, don't free file structure */
    246 int
    247 shf_fdclose(struct shf *shf)
    248 {
    249 	int ret = 0;
    250 
    251 	if (shf->fd >= 0) {
    252 		ret = shf_flush(shf);
    253 		if (close(shf->fd) < 0)
    254 			ret = -1;
    255 		shf->rnleft = 0;
    256 		shf->rp = shf->buf;
    257 		shf->wnleft = 0;
    258 		shf->fd = -1;
    259 	}
    260 
    261 	return (ret);
    262 }
    263 
    264 /*
    265  * Close a string - if it was opened for writing, it is NUL terminated;
    266  * returns a pointer to the string and frees shf if it was allocated
    267  * (does not free string if it was allocated).
    268  */
    269 char *
    270 shf_sclose(struct shf *shf)
    271 {
    272 	unsigned char *s = shf->buf;
    273 
    274 	/* NUL terminate */
    275 	if (shf->flags & SHF_WR) {
    276 		shf->wnleft++;
    277 		shf_putc('\0', shf);
    278 	}
    279 	if (shf->flags & SHF_ALLOCS)
    280 		afree(shf, shf->areap);
    281 	return ((char *)s);
    282 }
    283 
    284 /*
    285  * Un-read what has been read but not examined, or write what has been
    286  * buffered. Returns 0 for success, -1 for (write) error.
    287  */
    288 int
    289 shf_flush(struct shf *shf)
    290 {
    291 	if (shf->flags & SHF_STRING)
    292 		return ((shf->flags & SHF_WR) ? -1 : 0);
    293 
    294 	if (shf->fd < 0)
    295 		internal_errorf("%s: %s", "shf_flush", "no fd");
    296 
    297 	if (shf->flags & SHF_ERROR) {
    298 		errno = shf->errnosv;
    299 		return (-1);
    300 	}
    301 
    302 	if (shf->flags & SHF_READING) {
    303 		shf->flags &= ~(SHF_EOF | SHF_READING);
    304 		if (shf->rnleft > 0) {
    305 			lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
    306 			shf->rnleft = 0;
    307 			shf->rp = shf->buf;
    308 		}
    309 		return (0);
    310 	} else if (shf->flags & SHF_WRITING)
    311 		return (shf_emptybuf(shf, 0));
    312 
    313 	return (0);
    314 }
    315 
    316 /*
    317  * Write out any buffered data. If currently reading, flushes the read
    318  * buffer. Returns 0 for success, -1 for (write) error.
    319  */
    320 static int
    321 shf_emptybuf(struct shf *shf, int flags)
    322 {
    323 	int ret = 0;
    324 
    325 	if (!(shf->flags & SHF_STRING) && shf->fd < 0)
    326 		internal_errorf("%s: %s", "shf_emptybuf", "no fd");
    327 
    328 	if (shf->flags & SHF_ERROR) {
    329 		errno = shf->errnosv;
    330 		return (-1);
    331 	}
    332 
    333 	if (shf->flags & SHF_READING) {
    334 		if (flags & EB_READSW)
    335 			/* doesn't happen */
    336 			return (0);
    337 		ret = shf_flush(shf);
    338 		shf->flags &= ~SHF_READING;
    339 	}
    340 	if (shf->flags & SHF_STRING) {
    341 		unsigned char *nbuf;
    342 
    343 		/*
    344 		 * Note that we assume SHF_ALLOCS is not set if
    345 		 * SHF_ALLOCB is set... (changing the shf pointer could
    346 		 * cause problems)
    347 		 */
    348 		if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
    349 		    !(shf->flags & SHF_ALLOCB))
    350 			return (-1);
    351 		/* allocate more space for buffer */
    352 		nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
    353 		shf->rp = nbuf + (shf->rp - shf->buf);
    354 		shf->wp = nbuf + (shf->wp - shf->buf);
    355 		shf->rbsize += shf->wbsize;
    356 		shf->wnleft += shf->wbsize;
    357 		shf->wbsize <<= 1;
    358 		shf->buf = nbuf;
    359 	} else {
    360 		if (shf->flags & SHF_WRITING) {
    361 			ssize_t n, ntowrite = shf->wp - shf->buf;
    362 			unsigned char *buf = shf->buf;
    363 
    364 			while (ntowrite > 0) {
    365 				n = write(shf->fd, buf, ntowrite);
    366 				if (n < 0) {
    367 					if (errno == EINTR &&
    368 					    !(shf->flags & SHF_INTERRUPT))
    369 						continue;
    370 					shf->flags |= SHF_ERROR;
    371 					shf->errnosv = errno;
    372 					shf->wnleft = 0;
    373 					if (buf != shf->buf) {
    374 						/*
    375 						 * allow a second flush
    376 						 * to work
    377 						 */
    378 						memmove(shf->buf, buf,
    379 						    ntowrite);
    380 						shf->wp = shf->buf + ntowrite;
    381 					}
    382 					return (-1);
    383 				}
    384 				buf += n;
    385 				ntowrite -= n;
    386 			}
    387 			if (flags & EB_READSW) {
    388 				shf->wp = shf->buf;
    389 				shf->wnleft = 0;
    390 				shf->flags &= ~SHF_WRITING;
    391 				return (0);
    392 			}
    393 		}
    394 		shf->wp = shf->buf;
    395 		shf->wnleft = shf->wbsize;
    396 	}
    397 	shf->flags |= SHF_WRITING;
    398 
    399 	return (ret);
    400 }
    401 
    402 /* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
    403 static int
    404 shf_fillbuf(struct shf *shf)
    405 {
    406 	ssize_t n;
    407 
    408 	if (shf->flags & SHF_STRING)
    409 		return (0);
    410 
    411 	if (shf->fd < 0)
    412 		internal_errorf("%s: %s", "shf_fillbuf", "no fd");
    413 
    414 	if (shf->flags & (SHF_EOF | SHF_ERROR)) {
    415 		if (shf->flags & SHF_ERROR)
    416 			errno = shf->errnosv;
    417 		return (-1);
    418 	}
    419 
    420 	if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
    421 		return (-1);
    422 
    423 	shf->flags |= SHF_READING;
    424 
    425 	shf->rp = shf->buf;
    426 	while (/* CONSTCOND */ 1) {
    427 		n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
    428 		if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
    429 			continue;
    430 		break;
    431 	}
    432 	if (n < 0) {
    433 		shf->flags |= SHF_ERROR;
    434 		shf->errnosv = errno;
    435 		shf->rnleft = 0;
    436 		shf->rp = shf->buf;
    437 		return (-1);
    438 	}
    439 	if ((shf->rnleft = n) == 0)
    440 		shf->flags |= SHF_EOF;
    441 	return (0);
    442 }
    443 
    444 /*
    445  * Read a buffer from shf. Returns the number of bytes read into buf, if
    446  * no bytes were read, returns 0 if end of file was seen, -1 if a read
    447  * error occurred.
    448  */
    449 ssize_t
    450 shf_read(char *buf, ssize_t bsize, struct shf *shf)
    451 {
    452 	ssize_t ncopy, orig_bsize = bsize;
    453 
    454 	if (!(shf->flags & SHF_RD))
    455 		internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
    456 
    457 	if (bsize <= 0)
    458 		internal_errorf("%s: %s %zd", "shf_write", "bsize", bsize);
    459 
    460 	while (bsize > 0) {
    461 		if (shf->rnleft == 0 &&
    462 		    (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
    463 			break;
    464 		ncopy = shf->rnleft;
    465 		if (ncopy > bsize)
    466 			ncopy = bsize;
    467 		memcpy(buf, shf->rp, ncopy);
    468 		buf += ncopy;
    469 		bsize -= ncopy;
    470 		shf->rp += ncopy;
    471 		shf->rnleft -= ncopy;
    472 	}
    473 	/* Note: fread(3S) returns 0 for errors - this doesn't */
    474 	return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
    475 	    orig_bsize - bsize);
    476 }
    477 
    478 /*
    479  * Read up to a newline or -1. The newline is put in buf; buf is always
    480  * NUL terminated. Returns NULL on read error or if nothing was read
    481  * before end of file, returns a pointer to the NUL byte in buf
    482  * otherwise.
    483  */
    484 char *
    485 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
    486 {
    487 	unsigned char *end;
    488 	ssize_t ncopy;
    489 	char *orig_buf = buf;
    490 
    491 	if (!(shf->flags & SHF_RD))
    492 		internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
    493 
    494 	if (bsize <= 0)
    495 		return (NULL);
    496 
    497 	/* save room for NUL */
    498 	--bsize;
    499 	do {
    500 		if (shf->rnleft == 0) {
    501 			if (shf_fillbuf(shf) == -1)
    502 				return (NULL);
    503 			if (shf->rnleft == 0) {
    504 				*buf = '\0';
    505 				return (buf == orig_buf ? NULL : buf);
    506 			}
    507 		}
    508 		end = (unsigned char *)memchr((char *)shf->rp, '\n',
    509 		    shf->rnleft);
    510 		ncopy = end ? end - shf->rp + 1 : shf->rnleft;
    511 		if (ncopy > bsize)
    512 			ncopy = bsize;
    513 		memcpy(buf, (char *) shf->rp, ncopy);
    514 		shf->rp += ncopy;
    515 		shf->rnleft -= ncopy;
    516 		buf += ncopy;
    517 		bsize -= ncopy;
    518 	} while (!end && bsize);
    519 	*buf = '\0';
    520 	return (buf);
    521 }
    522 
    523 /* Returns the char read. Returns -1 for error and end of file. */
    524 int
    525 shf_getchar(struct shf *shf)
    526 {
    527 	if (!(shf->flags & SHF_RD))
    528 		internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
    529 
    530 	if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
    531 		return (-1);
    532 	--shf->rnleft;
    533 	return (*shf->rp++);
    534 }
    535 
    536 /*
    537  * Put a character back in the input stream. Returns the character if
    538  * successful, -1 if there is no room.
    539  */
    540 int
    541 shf_ungetc(int c, struct shf *shf)
    542 {
    543 	if (!(shf->flags & SHF_RD))
    544 		internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
    545 
    546 	if ((shf->flags & SHF_ERROR) || c == -1 ||
    547 	    (shf->rp == shf->buf && shf->rnleft))
    548 		return (-1);
    549 
    550 	if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
    551 		return (-1);
    552 
    553 	if (shf->rp == shf->buf)
    554 		shf->rp = shf->buf + shf->rbsize;
    555 	if (shf->flags & SHF_STRING) {
    556 		/*
    557 		 * Can unget what was read, but not something different;
    558 		 * we don't want to modify a string.
    559 		 */
    560 		if ((int)(shf->rp[-1]) != c)
    561 			return (-1);
    562 		shf->flags &= ~SHF_EOF;
    563 		shf->rp--;
    564 		shf->rnleft++;
    565 		return (c);
    566 	}
    567 	shf->flags &= ~SHF_EOF;
    568 	*--(shf->rp) = c;
    569 	shf->rnleft++;
    570 	return (c);
    571 }
    572 
    573 /*
    574  * Write a character. Returns the character if successful, -1 if the
    575  * char could not be written.
    576  */
    577 int
    578 shf_putchar(int c, struct shf *shf)
    579 {
    580 	if (!(shf->flags & SHF_WR))
    581 		internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
    582 
    583 	if (c == -1)
    584 		return (-1);
    585 
    586 	if (shf->flags & SHF_UNBUF) {
    587 		unsigned char cc = (unsigned char)c;
    588 		ssize_t n;
    589 
    590 		if (shf->fd < 0)
    591 			internal_errorf("%s: %s", "shf_putchar", "no fd");
    592 		if (shf->flags & SHF_ERROR) {
    593 			errno = shf->errnosv;
    594 			return (-1);
    595 		}
    596 		while ((n = write(shf->fd, &cc, 1)) != 1)
    597 			if (n < 0) {
    598 				if (errno == EINTR &&
    599 				    !(shf->flags & SHF_INTERRUPT))
    600 					continue;
    601 				shf->flags |= SHF_ERROR;
    602 				shf->errnosv = errno;
    603 				return (-1);
    604 			}
    605 	} else {
    606 		/* Flush deals with strings and sticky errors */
    607 		if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
    608 			return (-1);
    609 		shf->wnleft--;
    610 		*shf->wp++ = c;
    611 	}
    612 
    613 	return (c);
    614 }
    615 
    616 /*
    617  * Write a string. Returns the length of the string if successful, -1
    618  * if the string could not be written.
    619  */
    620 ssize_t
    621 shf_puts(const char *s, struct shf *shf)
    622 {
    623 	if (!s)
    624 		return (-1);
    625 
    626 	return (shf_write(s, strlen(s), shf));
    627 }
    628 
    629 /* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
    630 ssize_t
    631 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
    632 {
    633 	ssize_t n, ncopy, orig_nbytes = nbytes;
    634 
    635 	if (!(shf->flags & SHF_WR))
    636 		internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
    637 
    638 	if (nbytes < 0)
    639 		internal_errorf("%s: %s %zd", "shf_write", "nbytes", nbytes);
    640 
    641 	/* Don't buffer if buffer is empty and we're writting a large amount. */
    642 	if ((ncopy = shf->wnleft) &&
    643 	    (shf->wp != shf->buf || nbytes < shf->wnleft)) {
    644 		if (ncopy > nbytes)
    645 			ncopy = nbytes;
    646 		memcpy(shf->wp, buf, ncopy);
    647 		nbytes -= ncopy;
    648 		buf += ncopy;
    649 		shf->wp += ncopy;
    650 		shf->wnleft -= ncopy;
    651 	}
    652 	if (nbytes > 0) {
    653 		if (shf->flags & SHF_STRING) {
    654 			/* resize buffer until there's enough space left */
    655 			while (nbytes > shf->wnleft)
    656 				if (shf_emptybuf(shf, EB_GROW) == -1)
    657 					return (-1);
    658 			/* then write everything into the buffer */
    659 		} else {
    660 			/* flush deals with sticky errors */
    661 			if (shf_emptybuf(shf, EB_GROW) == -1)
    662 				return (-1);
    663 			/* write chunks larger than window size directly */
    664 			if (nbytes > shf->wbsize) {
    665 				ncopy = nbytes;
    666 				if (shf->wbsize)
    667 					ncopy -= nbytes % shf->wbsize;
    668 				nbytes -= ncopy;
    669 				while (ncopy > 0) {
    670 					n = write(shf->fd, buf, ncopy);
    671 					if (n < 0) {
    672 						if (errno == EINTR &&
    673 						    !(shf->flags & SHF_INTERRUPT))
    674 							continue;
    675 						shf->flags |= SHF_ERROR;
    676 						shf->errnosv = errno;
    677 						shf->wnleft = 0;
    678 						/*
    679 						 * Note: fwrite(3) returns 0
    680 						 * for errors - this doesn't
    681 						 */
    682 						return (-1);
    683 					}
    684 					buf += n;
    685 					ncopy -= n;
    686 				}
    687 			}
    688 			/* ... and buffer the rest */
    689 		}
    690 		if (nbytes > 0) {
    691 			/* write remaining bytes to buffer */
    692 			memcpy(shf->wp, buf, nbytes);
    693 			shf->wp += nbytes;
    694 			shf->wnleft -= nbytes;
    695 		}
    696 	}
    697 
    698 	return (orig_nbytes);
    699 }
    700 
    701 ssize_t
    702 shf_fprintf(struct shf *shf, const char *fmt, ...)
    703 {
    704 	va_list args;
    705 	ssize_t n;
    706 
    707 	va_start(args, fmt);
    708 	n = shf_vfprintf(shf, fmt, args);
    709 	va_end(args);
    710 
    711 	return (n);
    712 }
    713 
    714 ssize_t
    715 shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
    716 {
    717 	struct shf shf;
    718 	va_list args;
    719 	ssize_t n;
    720 
    721 	if (!buf || bsize <= 0)
    722 		internal_errorf("shf_snprintf: buf %zX, bsize %zd",
    723 		    (size_t)buf, bsize);
    724 
    725 	shf_sopen(buf, bsize, SHF_WR, &shf);
    726 	va_start(args, fmt);
    727 	n = shf_vfprintf(&shf, fmt, args);
    728 	va_end(args);
    729 	/* NUL terminates */
    730 	shf_sclose(&shf);
    731 	return (n);
    732 }
    733 
    734 char *
    735 shf_smprintf(const char *fmt, ...)
    736 {
    737 	struct shf shf;
    738 	va_list args;
    739 
    740 	shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
    741 	va_start(args, fmt);
    742 	shf_vfprintf(&shf, fmt, args);
    743 	va_end(args);
    744 	/* NUL terminates */
    745 	return (shf_sclose(&shf));
    746 }
    747 
    748 #define	FL_HASH		0x001	/* '#' seen */
    749 #define FL_PLUS		0x002	/* '+' seen */
    750 #define FL_RIGHT	0x004	/* '-' seen */
    751 #define FL_BLANK	0x008	/* ' ' seen */
    752 #define FL_SHORT	0x010	/* 'h' seen */
    753 #define FL_LONG		0x020	/* 'l' seen */
    754 #define FL_ZERO		0x040	/* '0' seen */
    755 #define FL_DOT		0x080	/* '.' seen */
    756 #define FL_UPPER	0x100	/* format character was uppercase */
    757 #define FL_NUMBER	0x200	/* a number was formated %[douxefg] */
    758 #define FL_SIZET	0x400	/* 'z' seen */
    759 #define FM_SIZES	0x430	/* h/l/z mask */
    760 
    761 ssize_t
    762 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
    763 {
    764 	const char *s;
    765 	char c, *cp;
    766 	int tmp = 0, flags;
    767 	ssize_t field, precision, len;
    768 	unsigned long lnum;
    769 	/* %#o produces the longest output */
    770 	char numbuf[(8 * sizeof(long) + 2) / 3 + 1];
    771 	/* this stuff for dealing with the buffer */
    772 	ssize_t nwritten = 0;
    773 
    774 #define VA(type) va_arg(args, type)
    775 
    776 	if (!fmt)
    777 		return (0);
    778 
    779 	while ((c = *fmt++)) {
    780 		if (c != '%') {
    781 			shf_putc(c, shf);
    782 			nwritten++;
    783 			continue;
    784 		}
    785 		/*
    786 		 * This will accept flags/fields in any order - not just
    787 		 * the order specified in printf(3), but this is the way
    788 		 * _doprnt() seems to work (on BSD and SYSV). The only
    789 		 * restriction is that the format character must come
    790 		 * last :-).
    791 		 */
    792 		flags = 0;
    793 		field = precision = 0;
    794 		for ( ; (c = *fmt++) ; ) {
    795 			switch (c) {
    796 			case '#':
    797 				flags |= FL_HASH;
    798 				continue;
    799 
    800 			case '+':
    801 				flags |= FL_PLUS;
    802 				continue;
    803 
    804 			case '-':
    805 				flags |= FL_RIGHT;
    806 				continue;
    807 
    808 			case ' ':
    809 				flags |= FL_BLANK;
    810 				continue;
    811 
    812 			case '0':
    813 				if (!(flags & FL_DOT))
    814 					flags |= FL_ZERO;
    815 				continue;
    816 
    817 			case '.':
    818 				flags |= FL_DOT;
    819 				precision = 0;
    820 				continue;
    821 
    822 			case '*':
    823 				tmp = VA(int);
    824 				if (flags & FL_DOT)
    825 					precision = tmp;
    826 				else if ((field = tmp) < 0) {
    827 					field = -field;
    828 					flags |= FL_RIGHT;
    829 				}
    830 				continue;
    831 
    832 			case 'l':
    833 				flags &= ~FM_SIZES;
    834 				flags |= FL_LONG;
    835 				continue;
    836 
    837 			case 'h':
    838 				flags &= ~FM_SIZES;
    839 				flags |= FL_SHORT;
    840 				continue;
    841 
    842 			case 'z':
    843 				flags &= ~FM_SIZES;
    844 				flags |= FL_SIZET;
    845 				continue;
    846 			}
    847 			if (ksh_isdigit(c)) {
    848 				bool overflowed = false;
    849 
    850 				tmp = ksh_numdig(c);
    851 				while (c = *fmt++, ksh_isdigit(c)) {
    852 					if (notok2mul(2147483647, tmp, 10))
    853 						overflowed = true;
    854 					tmp = tmp * 10 + ksh_numdig(c);
    855 				}
    856 				--fmt;
    857 				if (overflowed)
    858 					tmp = 0;
    859 				if (flags & FL_DOT)
    860 					precision = tmp;
    861 				else
    862 					field = tmp;
    863 				continue;
    864 			}
    865 			break;
    866 		}
    867 
    868 		if (precision < 0)
    869 			precision = 0;
    870 
    871 		if (!c)
    872 			/* nasty format */
    873 			break;
    874 
    875 		if (ksh_isupper(c)) {
    876 			flags |= FL_UPPER;
    877 			c = ksh_tolower(c);
    878 		}
    879 
    880 		switch (c) {
    881 		case 'd':
    882 		case 'i':
    883 			if (flags & FL_SIZET)
    884 				lnum = (long)VA(ssize_t);
    885 			else if (flags & FL_LONG)
    886 				lnum = VA(long);
    887 			else if (flags & FL_SHORT)
    888 				lnum = (long)(short)VA(int);
    889 			else
    890 				lnum = (long)VA(int);
    891 			goto integral;
    892 
    893 		case 'o':
    894 		case 'u':
    895 		case 'x':
    896 			if (flags & FL_SIZET)
    897 				lnum = VA(size_t);
    898 			else if (flags & FL_LONG)
    899 				lnum = VA(unsigned long);
    900 			else if (flags & FL_SHORT)
    901 				lnum = (unsigned long)(unsigned short)VA(int);
    902 			else
    903 				lnum = (unsigned long)VA(unsigned int);
    904 
    905  integral:
    906 			flags |= FL_NUMBER;
    907 			cp = numbuf + sizeof(numbuf);
    908 
    909 			switch (c) {
    910 			case 'd':
    911 			case 'i':
    912 				if (0 > (long)lnum) {
    913 					lnum = -(long)lnum;
    914 					tmp = 1;
    915 				} else
    916 					tmp = 0;
    917 				/* FALLTHROUGH */
    918 			case 'u':
    919 				do {
    920 					*--cp = digits_lc[lnum % 10];
    921 					lnum /= 10;
    922 				} while (lnum);
    923 
    924 				if (c != 'u') {
    925 					if (tmp)
    926 						*--cp = '-';
    927 					else if (flags & FL_PLUS)
    928 						*--cp = '+';
    929 					else if (flags & FL_BLANK)
    930 						*--cp = ' ';
    931 				}
    932 				break;
    933 
    934 			case 'o':
    935 				do {
    936 					*--cp = digits_lc[lnum & 0x7];
    937 					lnum >>= 3;
    938 				} while (lnum);
    939 
    940 				if ((flags & FL_HASH) && *cp != '0')
    941 					*--cp = '0';
    942 				break;
    943 
    944 			case 'x': {
    945 				const char *digits = (flags & FL_UPPER) ?
    946 				    digits_uc : digits_lc;
    947 				do {
    948 					*--cp = digits[lnum & 0xF];
    949 					lnum >>= 4;
    950 				} while (lnum);
    951 
    952 				if (flags & FL_HASH) {
    953 					*--cp = (flags & FL_UPPER) ? 'X' : 'x';
    954 					*--cp = '0';
    955 				}
    956 			}
    957 			}
    958 			len = numbuf + sizeof(numbuf) - (s = cp);
    959 			if (flags & FL_DOT) {
    960 				if (precision > len) {
    961 					field = precision;
    962 					flags |= FL_ZERO;
    963 				} else
    964 					/* no loss */
    965 					precision = len;
    966 			}
    967 			break;
    968 
    969 		case 's':
    970 			if ((s = VA(const char *)) == NULL)
    971 				s = "(null)";
    972 			else if (flags & FL_HASH) {
    973 				print_value_quoted(shf, s);
    974 				continue;
    975 			}
    976 			len = utf_mbswidth(s);
    977 			break;
    978 
    979 		case 'c':
    980 			flags &= ~FL_DOT;
    981 			c = (char)(VA(int));
    982 			/* FALLTHROUGH */
    983 
    984 		case '%':
    985 		default:
    986 			numbuf[0] = c;
    987 			numbuf[1] = 0;
    988 			s = numbuf;
    989 			len = 1;
    990 			break;
    991 		}
    992 
    993 		/*
    994 		 * At this point s should point to a string that is to be
    995 		 * formatted, and len should be the length of the string.
    996 		 */
    997 		if (!(flags & FL_DOT) || len < precision)
    998 			precision = len;
    999 		if (field > precision) {
   1000 			field -= precision;
   1001 			if (!(flags & FL_RIGHT)) {
   1002 				field = -field;
   1003 				/* skip past sign or 0x when padding with 0 */
   1004 				if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
   1005 					if (*s == '+' || *s == '-' ||
   1006 					    *s == ' ') {
   1007 						shf_putc(*s, shf);
   1008 						s++;
   1009 						precision--;
   1010 						nwritten++;
   1011 					} else if (*s == '0') {
   1012 						shf_putc(*s, shf);
   1013 						s++;
   1014 						nwritten++;
   1015 						if (--precision > 0 &&
   1016 						    ksh_eq(*s, 'X', 'x')) {
   1017 							shf_putc(*s, shf);
   1018 							s++;
   1019 							precision--;
   1020 							nwritten++;
   1021 						}
   1022 					}
   1023 					c = '0';
   1024 				} else
   1025 					c = flags & FL_ZERO ? '0' : ' ';
   1026 				if (field < 0) {
   1027 					nwritten += -field;
   1028 					while (field < 0) {
   1029 						shf_putc(c, shf);
   1030 						++field;
   1031 					}
   1032 				}
   1033 			} else
   1034 				c = ' ';
   1035 		} else
   1036 			field = 0;
   1037 
   1038 		if (precision > 0) {
   1039 			const char *q;
   1040 
   1041 			nwritten += precision;
   1042 			q = utf_skipcols(s, precision);
   1043 			do {
   1044 				shf_putc(*s, shf);
   1045 			} while (++s < q);
   1046 		}
   1047 		if (field > 0) {
   1048 			nwritten += field;
   1049 			for ( ; field > 0 ; --field)
   1050 				shf_putc(c, shf);
   1051 		}
   1052 	}
   1053 
   1054 	return (shf_error(shf) ? -1 : nwritten);
   1055 }
   1056 
   1057 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
   1058 int
   1059 shf_getc(struct shf *shf)
   1060 {
   1061 	return (shf_getc_i(shf));
   1062 }
   1063 
   1064 int
   1065 shf_putc(int c, struct shf *shf)
   1066 {
   1067 	return (shf_putc_i(c, shf));
   1068 }
   1069 #endif
   1070 
   1071 #ifdef DEBUG
   1072 const char *
   1073 cstrerror(int errnum)
   1074 {
   1075 #undef strerror
   1076 	return (strerror(errnum));
   1077 #define strerror dontuse_strerror /* poisoned */
   1078 }
   1079 #elif !HAVE_STRERROR
   1080 
   1081 #if HAVE_SYS_ERRLIST
   1082 #if !HAVE_SYS_ERRLIST_DECL
   1083 extern const int sys_nerr;
   1084 extern const char * const sys_errlist[];
   1085 #endif
   1086 #endif
   1087 
   1088 const char *
   1089 cstrerror(int errnum)
   1090 {
   1091 	/* "Unknown error: " + sign + rough estimate + NUL */
   1092 	static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
   1093 
   1094 #if HAVE_SYS_ERRLIST
   1095 	if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
   1096 		return (sys_errlist[errnum]);
   1097 #endif
   1098 
   1099 	switch (errnum) {
   1100 	case 0:
   1101 		return ("Undefined error: 0");
   1102 	case EPERM:
   1103 		return ("Operation not permitted");
   1104 	case ENOENT:
   1105 		return ("No such file or directory");
   1106 #ifdef ESRCH
   1107 	case ESRCH:
   1108 		return ("No such process");
   1109 #endif
   1110 #ifdef E2BIG
   1111 	case E2BIG:
   1112 		return ("Argument list too long");
   1113 #endif
   1114 	case ENOEXEC:
   1115 		return ("Exec format error");
   1116 	case EBADF:
   1117 		return ("Bad file descriptor");
   1118 #ifdef ENOMEM
   1119 	case ENOMEM:
   1120 		return ("Cannot allocate memory");
   1121 #endif
   1122 	case EACCES:
   1123 		return ("Permission denied");
   1124 	case EEXIST:
   1125 		return ("File exists");
   1126 	case ENOTDIR:
   1127 		return ("Not a directory");
   1128 #ifdef EINVAL
   1129 	case EINVAL:
   1130 		return ("Invalid argument");
   1131 #endif
   1132 #ifdef ELOOP
   1133 	case ELOOP:
   1134 		return ("Too many levels of symbolic links");
   1135 #endif
   1136 	default:
   1137 		shf_snprintf(errbuf, sizeof(errbuf),
   1138 		    "Unknown error: %d", errnum);
   1139 		return (errbuf);
   1140 	}
   1141 }
   1142 #endif
   1143