-/* $OpenBSD: shf.c,v 1.15 2006/04/02 00:48:33 deraadt Exp $ */
+/* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
/*-
- * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009
+ * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
+ * 2012, 2013
* Thorsten Glaser <tg@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
+ *-
+ * Use %zX instead of %p and floating point isn't supported at all.
*/
#include "sh.h"
-__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.36 2010/07/19 22:41:04 tg Exp $");
+__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.62 2013/10/09 11:59:30 tg Exp $");
/* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */
static int shf_fillbuf(struct shf *);
static int shf_emptybuf(struct shf *, int);
-/* Open a file. First three args are for open(), last arg is flags for
+/*
+ * Open a file. First three args are for open(), last arg is flags for
* this package. Returns NULL if file could not be opened, or if a dup
* fails.
*/
shf_open(const char *name, int oflags, int mode, int sflags)
{
struct shf *shf;
- int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
- int fd;
+ ssize_t bsize =
+ /* at most 512 */
+ sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
+ int fd, eno;
/* Done before open so if alloca fails, fd won't be lost. */
shf = alloc(sizeof(struct shf) + bsize, ATEMP);
shf->flags = SHF_ALLOCS;
/* Rest filled in by reopen. */
- fd = open(name, oflags, mode);
+ fd = open(name, oflags | O_BINARY, mode);
if (fd < 0) {
+ eno = errno;
afree(shf, shf->areap);
+ errno = eno;
return (NULL);
}
if ((sflags & SHF_MAPHI) && fd < FDBASE) {
int nfd;
nfd = fcntl(fd, F_DUPFD, FDBASE);
+ eno = errno;
close(fd);
if (nfd < 0) {
afree(shf, shf->areap);
+ errno = eno;
return (NULL);
}
fd = nfd;
return (shf_reopen(fd, sflags, shf));
}
-/* Set up the shf structure for a file descriptor. Doesn't fail. */
-struct shf *
-shf_fdopen(int fd, int sflags, struct shf *shf)
+/* helper function for shf_fdopen and shf_reopen */
+static void
+shf_open_hlp(int fd, int *sflagsp, const char *where)
{
- int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
+ int sflags = *sflagsp;
/* use fcntl() to figure out correct read/write flags */
if (sflags & SHF_GETFL) {
break;
}
}
+ *sflagsp = sflags;
}
if (!(sflags & (SHF_RD | SHF_WR)))
- internal_errorf("shf_fdopen: missing read/write");
+ internal_errorf("%s: %s", where, "missing read/write");
+}
+
+/* Set up the shf structure for a file descriptor. Doesn't fail. */
+struct shf *
+shf_fdopen(int fd, int sflags, struct shf *shf)
+{
+ ssize_t bsize =
+ /* at most 512 */
+ sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
+ shf_open_hlp(fd, &sflags, "shf_fdopen");
if (shf) {
if (bsize) {
shf->buf = alloc(bsize, ATEMP);
shf->wnleft = 0; /* force call to shf_emptybuf() */
shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
shf->flags = sflags;
- shf->errno_ = 0;
+ shf->errnosv = 0;
shf->bsize = bsize;
if (sflags & SHF_CLEXEC)
fcntl(fd, F_SETFD, FD_CLOEXEC);
struct shf *
shf_reopen(int fd, int sflags, struct shf *shf)
{
- int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
-
- /* use fcntl() to figure out correct read/write flags */
- if (sflags & SHF_GETFL) {
- int flags = fcntl(fd, F_GETFL, 0);
-
- if (flags < 0)
- /* will get an error on first read/write */
- sflags |= SHF_RDWR;
- else {
- switch (flags & O_ACCMODE) {
- case O_RDONLY:
- sflags |= SHF_RD;
- break;
- case O_WRONLY:
- sflags |= SHF_WR;
- break;
- case O_RDWR:
- sflags |= SHF_RDWR;
- break;
- }
- }
- }
+ ssize_t bsize =
+ /* at most 512 */
+ sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
- if (!(sflags & (SHF_RD | SHF_WR)))
- internal_errorf("shf_reopen: missing read/write");
+ shf_open_hlp(fd, &sflags, "shf_reopen");
if (!shf || !shf->buf || shf->bsize < bsize)
- internal_errorf("shf_reopen: bad shf/buf/bsize");
+ internal_errorf("%s: %s", "shf_reopen", "bad shf/buf/bsize");
/* assumes shf->buf and shf->bsize already set up */
shf->fd = fd;
shf->wnleft = 0; /* force call to shf_emptybuf() */
shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
- shf->errno_ = 0;
+ shf->errnosv = 0;
if (sflags & SHF_CLEXEC)
fcntl(fd, F_SETFD, FD_CLOEXEC);
return (shf);
}
-/* Open a string for reading or writing. If reading, bsize is the number
+/*
+ * Open a string for reading or writing. If reading, bsize is the number
* of bytes that can be read. If writing, bsize is the maximum number of
- * bytes that can be written. If shf is not null, it is filled in and
- * returned, if it is null, shf is allocated. If writing and buf is null
+ * bytes that can be written. If shf is not NULL, it is filled in and
+ * returned, if it is NULL, shf is allocated. If writing and buf is NULL
* and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
* used for the initial size). Doesn't fail.
- * When writing, a byte is reserved for a trailing null - see shf_sclose().
+ * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
*/
struct shf *
-shf_sopen(char *buf, int bsize, int sflags, struct shf *shf)
+shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
{
/* can't have a read+write string */
if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
- internal_errorf("shf_sopen: flags 0x%x", sflags);
+ internal_errorf("%s: flags 0x%X", "shf_sopen", sflags);
if (!shf) {
shf = alloc(sizeof(struct shf), ATEMP);
shf->wnleft = bsize - 1; /* space for a '\0' */
shf->wbsize = bsize;
shf->flags = sflags | SHF_STRING;
- shf->errno_ = 0;
+ shf->errnosv = 0;
shf->bsize = bsize;
return (shf);
return (ret);
}
-/* Close a string - if it was opened for writing, it is null terminated;
+/*
+ * Close a string - if it was opened for writing, it is NUL terminated;
* returns a pointer to the string and frees shf if it was allocated
* (does not free string if it was allocated).
*/
{
unsigned char *s = shf->buf;
- /* null terminate */
+ /* NUL terminate */
if (shf->flags & SHF_WR) {
shf->wnleft++;
shf_putc('\0', shf);
return ((char *)s);
}
-/* Un-read what has been read but not examined, or write what has been
+/*
+ * Un-read what has been read but not examined, or write what has been
* buffered. Returns 0 for success, EOF for (write) error.
*/
int
return ((shf->flags & SHF_WR) ? EOF : 0);
if (shf->fd < 0)
- internal_errorf("shf_flush: no fd");
+ internal_errorf("%s: %s", "shf_flush", "no fd");
if (shf->flags & SHF_ERROR) {
- errno = shf->errno_;
+ errno = shf->errnosv;
return (EOF);
}
return (0);
}
-/* Write out any buffered data. If currently reading, flushes the read
+/*
+ * Write out any buffered data. If currently reading, flushes the read
* buffer. Returns 0 for success, EOF for (write) error.
*/
static int
int ret = 0;
if (!(shf->flags & SHF_STRING) && shf->fd < 0)
- internal_errorf("shf_emptybuf: no fd");
+ internal_errorf("%s: %s", "shf_emptybuf", "no fd");
if (shf->flags & SHF_ERROR) {
- errno = shf->errno_;
+ errno = shf->errnosv;
return (EOF);
}
if (shf->flags & SHF_READING) {
- if (flags & EB_READSW) /* doesn't happen */
+ if (flags & EB_READSW)
+ /* doesn't happen */
return (0);
ret = shf_flush(shf);
shf->flags &= ~SHF_READING;
if (shf->flags & SHF_STRING) {
unsigned char *nbuf;
- /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
- * is set... (changing the shf pointer could cause problems)
+ /*
+ * Note that we assume SHF_ALLOCS is not set if
+ * SHF_ALLOCB is set... (changing the shf pointer could
+ * cause problems)
*/
if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
!(shf->flags & SHF_ALLOCB))
return (EOF);
/* allocate more space for buffer */
- nbuf = aresize(shf->buf, 2 * shf->wbsize, shf->areap);
+ nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
shf->rp = nbuf + (shf->rp - shf->buf);
shf->wp = nbuf + (shf->wp - shf->buf);
shf->rbsize += shf->wbsize;
shf->wnleft += shf->wbsize;
- shf->wbsize *= 2;
+ shf->wbsize <<= 1;
shf->buf = nbuf;
} else {
if (shf->flags & SHF_WRITING) {
- int ntowrite = shf->wp - shf->buf;
+ ssize_t n, ntowrite = shf->wp - shf->buf;
unsigned char *buf = shf->buf;
- int n;
while (ntowrite > 0) {
n = write(shf->fd, buf, ntowrite);
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
- shf->errno_ = errno;
+ shf->errnosv = errno;
shf->wnleft = 0;
if (buf != shf->buf) {
- /* allow a second flush
- * to work */
+ /*
+ * allow a second flush
+ * to work
+ */
memmove(shf->buf, buf,
ntowrite);
shf->wp = shf->buf + ntowrite;
static int
shf_fillbuf(struct shf *shf)
{
+ ssize_t n;
+
if (shf->flags & SHF_STRING)
return (0);
if (shf->fd < 0)
- internal_errorf("shf_fillbuf: no fd");
+ internal_errorf("%s: %s", "shf_fillbuf", "no fd");
if (shf->flags & (SHF_EOF | SHF_ERROR)) {
if (shf->flags & SHF_ERROR)
- errno = shf->errno_;
+ errno = shf->errnosv;
return (EOF);
}
shf->flags |= SHF_READING;
shf->rp = shf->buf;
- while (1) {
- shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
- shf->rbsize);
- if (shf->rnleft < 0 && errno == EINTR &&
- !(shf->flags & SHF_INTERRUPT))
+ while (/* CONSTCOND */ 1) {
+ n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
+ if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
continue;
break;
}
- if (shf->rnleft <= 0) {
- if (shf->rnleft < 0) {
- shf->flags |= SHF_ERROR;
- shf->errno_ = errno;
- shf->rnleft = 0;
- shf->rp = shf->buf;
- return (EOF);
- }
- shf->flags |= SHF_EOF;
+ if (n < 0) {
+ shf->flags |= SHF_ERROR;
+ shf->errnosv = errno;
+ shf->rnleft = 0;
+ shf->rp = shf->buf;
+ return (EOF);
}
+ if ((shf->rnleft = n) == 0)
+ shf->flags |= SHF_EOF;
return (0);
}
-/* Read a buffer from shf. Returns the number of bytes read into buf,
- * if no bytes were read, returns 0 if end of file was seen, EOF if
- * a read error occurred.
+/*
+ * Read a buffer from shf. Returns the number of bytes read into buf, if
+ * no bytes were read, returns 0 if end of file was seen, EOF if a read
+ * error occurred.
*/
-int
-shf_read(char *buf, int bsize, struct shf *shf)
+ssize_t
+shf_read(char *buf, ssize_t bsize, struct shf *shf)
{
- int orig_bsize = bsize;
- int ncopy;
+ ssize_t ncopy, orig_bsize = bsize;
if (!(shf->flags & SHF_RD))
- internal_errorf("shf_read: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
if (bsize <= 0)
- internal_errorf("shf_read: bsize %d", bsize);
+ internal_errorf("%s: %s %zd", "shf_write", "bsize", bsize);
while (bsize > 0) {
if (shf->rnleft == 0 &&
orig_bsize - bsize);
}
-/* Read up to a newline or EOF. The newline is put in buf; buf is always
- * null terminated. Returns NULL on read error or if nothing was read before
- * end of file, returns a pointer to the null byte in buf otherwise.
+/*
+ * Read up to a newline or EOF. The newline is put in buf; buf is always
+ * NUL terminated. Returns NULL on read error or if nothing was read
+ * before end of file, returns a pointer to the NUL byte in buf
+ * otherwise.
*/
char *
-shf_getse(char *buf, int bsize, struct shf *shf)
+shf_getse(char *buf, ssize_t bsize, struct shf *shf)
{
unsigned char *end;
- int ncopy;
+ ssize_t ncopy;
char *orig_buf = buf;
if (!(shf->flags & SHF_RD))
- internal_errorf("shf_getse: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
if (bsize <= 0)
return (NULL);
- --bsize; /* save room for null */
+ /* save room for NUL */
+ --bsize;
do {
if (shf->rnleft == 0) {
if (shf_fillbuf(shf) == EOF)
return (buf == orig_buf ? NULL : buf);
}
}
- end = (unsigned char *)memchr((char *) shf->rp, '\n',
+ end = (unsigned char *)memchr((char *)shf->rp, '\n',
shf->rnleft);
ncopy = end ? end - shf->rp + 1 : shf->rnleft;
if (ncopy > bsize)
shf_getchar(struct shf *shf)
{
if (!(shf->flags & SHF_RD))
- internal_errorf("shf_getchar: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
return (EOF);
return (*shf->rp++);
}
-/* Put a character back in the input stream. Returns the character if
+/*
+ * Put a character back in the input stream. Returns the character if
* successful, EOF if there is no room.
*/
int
shf_ungetc(int c, struct shf *shf)
{
if (!(shf->flags & SHF_RD))
- internal_errorf("shf_ungetc: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
if ((shf->flags & SHF_ERROR) || c == EOF ||
(shf->rp == shf->buf && shf->rnleft))
if (shf->rp == shf->buf)
shf->rp = shf->buf + shf->rbsize;
if (shf->flags & SHF_STRING) {
- /* Can unget what was read, but not something different - we
- * don't want to modify a string.
+ /*
+ * Can unget what was read, but not something different;
+ * we don't want to modify a string.
*/
- if (shf->rp[-1] != c)
+ if ((int)(shf->rp[-1]) != c)
return (EOF);
shf->flags &= ~SHF_EOF;
shf->rp--;
return (c);
}
-/* Write a character. Returns the character if successful, EOF if
- * the char could not be written.
+/*
+ * Write a character. Returns the character if successful, EOF if the
+ * char could not be written.
*/
int
shf_putchar(int c, struct shf *shf)
{
if (!(shf->flags & SHF_WR))
- internal_errorf("shf_putchar: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
if (c == EOF)
return (EOF);
if (shf->flags & SHF_UNBUF) {
unsigned char cc = (unsigned char)c;
- int n;
+ ssize_t n;
if (shf->fd < 0)
- internal_errorf("shf_putchar: no fd");
+ internal_errorf("%s: %s", "shf_putchar", "no fd");
if (shf->flags & SHF_ERROR) {
- errno = shf->errno_;
+ errno = shf->errnosv;
return (EOF);
}
while ((n = write(shf->fd, &cc, 1)) != 1)
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
- shf->errno_ = errno;
+ shf->errnosv = errno;
return (EOF);
}
} else {
return (c);
}
-/* Write a string. Returns the length of the string if successful, EOF if
- * the string could not be written.
+/*
+ * Write a string. Returns the length of the string if successful, EOF
+ * if the string could not be written.
*/
-int
+ssize_t
shf_puts(const char *s, struct shf *shf)
{
if (!s)
}
/* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
-int
-shf_write(const char *buf, int nbytes, struct shf *shf)
+ssize_t
+shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
{
- int n, ncopy, orig_nbytes = nbytes;
+ ssize_t n, ncopy, orig_nbytes = nbytes;
if (!(shf->flags & SHF_WR))
- internal_errorf("shf_write: flags %x", shf->flags);
+ internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
if (nbytes < 0)
- internal_errorf("shf_write: nbytes %d", nbytes);
+ internal_errorf("%s: %s %zd", "shf_write", "nbytes", nbytes);
/* Don't buffer if buffer is empty and we're writting a large amount. */
if ((ncopy = shf->wnleft) &&
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
- shf->errno_ = errno;
+ shf->errnosv = errno;
shf->wnleft = 0;
/*
* Note: fwrite(3) returns 0
return (orig_nbytes);
}
-int
+ssize_t
shf_fprintf(struct shf *shf, const char *fmt, ...)
{
va_list args;
- int n;
+ ssize_t n;
va_start(args, fmt);
n = shf_vfprintf(shf, fmt, args);
return (n);
}
-int
-shf_snprintf(char *buf, int bsize, const char *fmt, ...)
+ssize_t
+shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
{
struct shf shf;
va_list args;
- int n;
+ ssize_t n;
if (!buf || bsize <= 0)
- internal_errorf("shf_snprintf: buf %p, bsize %d", buf, bsize);
+ internal_errorf("shf_snprintf: buf %zX, bsize %zd",
+ (size_t)buf, bsize);
shf_sopen(buf, bsize, SHF_WR, &shf);
va_start(args, fmt);
n = shf_vfprintf(&shf, fmt, args);
va_end(args);
- shf_sclose(&shf); /* null terminates */
+ /* NUL terminates */
+ shf_sclose(&shf);
return (n);
}
va_start(args, fmt);
shf_vfprintf(&shf, fmt, args);
va_end(args);
- return (shf_sclose(&shf)); /* null terminates */
+ /* NUL terminates */
+ return (shf_sclose(&shf));
}
-#undef FP /* if you want floating point stuff */
-
-#ifndef DMAXEXP
-# define DMAXEXP 128 /* should be big enough */
-#endif
-
-#define BUF_SIZE 128
-/* must be > MAX(DMAXEXP, log10(pow(2, DSIGNIF))) + ceil(log10(DMAXEXP)) + 8
- * (I think); since it's hard to express as a constant, just use a large buffer
- */
-#define FPBUF_SIZE (DMAXEXP+16)
-
#define FL_HASH 0x001 /* '#' seen */
#define FL_PLUS 0x002 /* '+' seen */
#define FL_RIGHT 0x004 /* '-' seen */
#define FL_DOT 0x080 /* '.' seen */
#define FL_UPPER 0x100 /* format character was uppercase */
#define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
+#define FL_SIZET 0x400 /* 'z' seen */
+#define FM_SIZES 0x430 /* h/l/z mask */
-
-int
+ssize_t
shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
{
const char *s;
char c, *cp;
- int tmp = 0, field, precision, len, flags;
+ int tmp = 0, flags;
+ ssize_t field, precision, len;
unsigned long lnum;
/* %#o produces the longest output */
- char numbuf[(8 * sizeof(long) + 2) / 3 + 1];
+ char numbuf[(8 * sizeof(long) + 2) / 3 + 1
+#ifdef DEBUG
+ /* a NUL for LLVM/Clang scan-build */
+ + 1
+#endif
+ ];
/* this stuff for dealing with the buffer */
- int nwritten = 0;
+ ssize_t nwritten = 0;
+
+#define VA(type) va_arg(args, type)
if (!fmt)
return (0);
continue;
}
/*
- * This will accept flags/fields in any order - not
- * just the order specified in printf(3), but this is
- * the way _doprnt() seems to work (on bsd and sysV).
- * The only restriction is that the format character must
- * come last :-).
+ * This will accept flags/fields in any order - not just
+ * the order specified in printf(3), but this is the way
+ * _doprnt() seems to work (on BSD and SYSV). The only
+ * restriction is that the format character must come
+ * last :-).
*/
- flags = field = precision = 0;
+ flags = 0;
+ field = precision = 0;
for ( ; (c = *fmt++) ; ) {
switch (c) {
case '#':
continue;
case '*':
- tmp = va_arg(args, int);
+ tmp = VA(int);
if (flags & FL_DOT)
precision = tmp;
else if ((field = tmp) < 0) {
continue;
case 'l':
+ flags &= ~FM_SIZES;
flags |= FL_LONG;
continue;
case 'h':
+ flags &= ~FM_SIZES;
flags |= FL_SHORT;
continue;
+
+ case 'z':
+ flags &= ~FM_SIZES;
+ flags |= FL_SIZET;
+ continue;
}
if (ksh_isdigit(c)) {
+ bool overflowed = false;
+
tmp = c - '0';
- while (c = *fmt++, ksh_isdigit(c))
+ while (c = *fmt++, ksh_isdigit(c)) {
+ if (notok2mul(2147483647, tmp, 10))
+ overflowed = true;
tmp = tmp * 10 + c - '0';
+ }
--fmt;
- if (tmp < 0) /* overflow? */
+ if (overflowed)
tmp = 0;
if (flags & FL_DOT)
precision = tmp;
if (precision < 0)
precision = 0;
- if (!c) /* nasty format */
+ if (!c)
+ /* nasty format */
break;
if (c >= 'A' && c <= 'Z') {
}
switch (c) {
- case 'p': /* pointer */
- flags &= ~(FL_LONG | FL_SHORT);
- flags |= (sizeof(char *) > sizeof(int)) ?
- /* hope it fits.. */ FL_LONG : 0;
- /* aaahhh... */
case 'd':
case 'i':
+ if (flags & FL_SIZET)
+ lnum = (long)VA(ssize_t);
+ else if (flags & FL_LONG)
+ lnum = VA(long);
+ else if (flags & FL_SHORT)
+ lnum = (long)(short)VA(int);
+ else
+ lnum = (long)VA(int);
+ goto integral;
+
case 'o':
case 'u':
case 'x':
+ if (flags & FL_SIZET)
+ lnum = VA(size_t);
+ else if (flags & FL_LONG)
+ lnum = VA(unsigned long);
+ else if (flags & FL_SHORT)
+ lnum = (unsigned long)(unsigned short)VA(int);
+ else
+ lnum = (unsigned long)VA(unsigned int);
+
+ integral:
flags |= FL_NUMBER;
cp = numbuf + sizeof(numbuf);
- /*-
- * XXX any better way to do this?
- * XXX hopefully the compiler optimises this out
- *
- * For shorts, we want sign extend for %d but not
- * for %[oxu] - on 16 bit machines it doesn't matter.
- * Assumes C compiler has converted shorts to ints
- * before pushing them. XXX optimise this -tg
+#ifdef DEBUG
+ /*
+ * this is necessary so Clang 3.2 realises
+ * utf_skipcols/shf_putc in the output loop
+ * terminate; these values are always ASCII
+ * so an out-of-bounds access cannot happen
+ * but Clang doesn't know that
*/
- if (flags & FL_LONG)
- lnum = va_arg(args, unsigned long);
- else if ((sizeof(int) < sizeof(long)) && (c == 'd'))
- lnum = (long)va_arg(args, int);
- else
- lnum = va_arg(args, unsigned int);
+ *--cp = '\0';
+#endif
+
switch (c) {
case 'd':
case 'i':
*--cp = '0';
break;
- case 'p':
case 'x': {
const char *digits = (flags & FL_UPPER) ?
digits_uc : digits_lc;
}
}
len = numbuf + sizeof(numbuf) - (s = cp);
+#ifdef DEBUG
+ /* see above comment for Clang 3.2 */
+ --len;
+#endif
if (flags & FL_DOT) {
if (precision > len) {
field = precision;
flags |= FL_ZERO;
} else
- precision = len; /* no loss */
+ /* no loss */
+ precision = len;
}
break;
case 's':
- if (!(s = va_arg(args, const char *)))
+ if ((s = VA(const char *)) == NULL)
s = "(null)";
+ else if (flags & FL_HASH) {
+ print_value_quoted(shf, s);
+ continue;
+ }
len = utf_mbswidth(s);
break;
case 'c':
flags &= ~FL_DOT;
- numbuf[0] = (char)(va_arg(args, int));
- s = numbuf;
- len = 1;
- break;
+ c = (char)(VA(int));
+ /* FALLTHROUGH */
case '%':
default:
numbuf[0] = c;
+ numbuf[1] = 0;
s = numbuf;
len = 1;
break;
return (shf_error(shf) ? EOF : nwritten);
}
-#ifdef MKSH_SMALL
+#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
int
shf_getc(struct shf *shf)
{
- return ((shf)->rnleft > 0 ? (shf)->rnleft--, *(shf)->rp++ :
- shf_getchar(shf));
+ return (shf_getc_i(shf));
}
int
shf_putc(int c, struct shf *shf)
{
- return ((shf)->wnleft == 0 ? shf_putchar((c), (shf)) :
- ((shf)->wnleft--, *(shf)->wp++ = (c)));
+ return (shf_putc_i(c, shf));
+}
+#endif
+
+#ifdef DEBUG
+const char *
+cstrerror(int errnum)
+{
+#undef strerror
+ return (strerror(errnum));
+#define strerror dontuse_strerror /* poisoned */
+}
+#elif !HAVE_STRERROR
+
+#if HAVE_SYS_ERRLIST
+#if !HAVE_SYS_ERRLIST_DECL
+extern const int sys_nerr;
+extern const char * const sys_errlist[];
+#endif
+#endif
+
+const char *
+cstrerror(int errnum)
+{
+ /* "Unknown error: " + sign + rough estimate + NUL */
+ static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
+
+#if HAVE_SYS_ERRLIST
+ if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
+ return (sys_errlist[errnum]);
+#endif
+
+ switch (errnum) {
+ case 0:
+ return ("Undefined error: 0");
+#ifdef EPERM
+ case EPERM:
+ return ("Operation not permitted");
+#endif
+#ifdef ENOENT
+ case ENOENT:
+ return ("No such file or directory");
+#endif
+#ifdef ESRCH
+ case ESRCH:
+ return ("No such process");
+#endif
+#ifdef E2BIG
+ case E2BIG:
+ return ("Argument list too long");
+#endif
+#ifdef ENOEXEC
+ case ENOEXEC:
+ return ("Exec format error");
+#endif
+#ifdef ENOMEM
+ case ENOMEM:
+ return ("Cannot allocate memory");
+#endif
+#ifdef EACCES
+ case EACCES:
+ return ("Permission denied");
+#endif
+#ifdef ENOTDIR
+ case ENOTDIR:
+ return ("Not a directory");
+#endif
+#ifdef EINVAL
+ case EINVAL:
+ return ("Invalid argument");
+#endif
+#ifdef ELOOP
+ case ELOOP:
+ return ("Too many levels of symbolic links");
+#endif
+ default:
+ shf_snprintf(errbuf, sizeof(errbuf),
+ "Unknown error: %d", errnum);
+ return (errbuf);
+ }
}
#endif