Rearrange "struct output" to be slightly more friendly (I think)
to I32 P64 systems - keep nextc first, as that's used in macros,
and nleft next, as that's used (and both are updated) in the same macro,
which is used frequently, this increases the chance they're in the
same cache line (unchanged from before). Beyond that it matters less,
so just shuffle a bit to avoid internal padding when pointers are 64 bits.
Note that there are just 3 of these structs (currently), even if there was
to be a memory saving (there probably won't be, trailing padding will eat it)
it would be of the order of 12 or 24 bytes total, so all this really
just panders to my sense of rightness....
Note to anyone who might be tempted, please don't update the struct
initializers to use newer C forms - eventually sh is planned to become
a host tool, and a separable package, so it wants to remain able to be
compiled using older (though at least ansi) compilers that implement only
older C variants.
2017-11-19 06:22:55 +03:00
|
|
|
/* $NetBSD: output.c,v 1.38 2017/11/19 03:22:55 kre Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-05-12 01:28:33 +04:00
|
|
|
static char sccsid[] = "@(#)output.c 8.2 (Berkeley) 5/4/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
Rearrange "struct output" to be slightly more friendly (I think)
to I32 P64 systems - keep nextc first, as that's used in macros,
and nleft next, as that's used (and both are updated) in the same macro,
which is used frequently, this increases the chance they're in the
same cache line (unchanged from before). Beyond that it matters less,
so just shuffle a bit to avoid internal padding when pointers are 64 bits.
Note that there are just 3 of these structs (currently), even if there was
to be a memory saving (there probably won't be, trailing padding will eat it)
it would be of the order of 12 or 24 bytes total, so all this really
just panders to my sense of rightness....
Note to anyone who might be tempted, please don't update the struct
initializers to use newer C forms - eventually sh is planned to become
a host tool, and a separable package, so it wants to remain able to be
compiled using older (though at least ansi) compilers that implement only
older C variants.
2017-11-19 06:22:55 +03:00
|
|
|
__RCSID("$NetBSD: output.c,v 1.38 2017/11/19 03:22:55 kre Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shell output routines. We use our own output routines because:
|
|
|
|
* When a builtin command is interrupted we have to discard
|
|
|
|
* any pending output.
|
|
|
|
* When a builtin command appears in back quotes, we want to
|
|
|
|
* save the output of the command in a region obtained
|
|
|
|
* via malloc, rather than doing a fork and reading the
|
|
|
|
* output of the command via a pipe.
|
|
|
|
* Our output routines may be smaller than the stdio routines.
|
|
|
|
*/
|
|
|
|
|
1998-01-21 13:47:37 +03:00
|
|
|
#include <sys/types.h> /* quad_t */
|
|
|
|
#include <sys/param.h> /* BSD4_4 */
|
1994-12-05 22:07:32 +03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h> /* defines BUFSIZ */
|
1994-12-23 16:24:10 +03:00
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <errno.h>
|
1994-05-12 21:03:32 +04:00
|
|
|
#include <unistd.h>
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define OUTBUFSIZ BUFSIZ
|
|
|
|
#define BLOCK_OUT -2 /* output to a fixed block of memory */
|
|
|
|
#define MEM_OUT -3 /* output to dynamically allocated memory */
|
|
|
|
|
|
|
|
|
Rearrange "struct output" to be slightly more friendly (I think)
to I32 P64 systems - keep nextc first, as that's used in macros,
and nleft next, as that's used (and both are updated) in the same macro,
which is used frequently, this increases the chance they're in the
same cache line (unchanged from before). Beyond that it matters less,
so just shuffle a bit to avoid internal padding when pointers are 64 bits.
Note that there are just 3 of these structs (currently), even if there was
to be a memory saving (there probably won't be, trailing padding will eat it)
it would be of the order of 12 or 24 bytes total, so all this really
just panders to my sense of rightness....
Note to anyone who might be tempted, please don't update the struct
initializers to use newer C forms - eventually sh is planned to become
a host tool, and a separable package, so it wants to remain able to be
compiled using older (though at least ansi) compilers that implement only
older C variants.
2017-11-19 06:22:55 +03:00
|
|
|
struct output output = {NULL, 0, OUTBUFSIZ, NULL, 1, 0};
|
|
|
|
struct output errout = {NULL, 0, 100, NULL, 2, 0};
|
|
|
|
struct output memout = {NULL, 0, 0, NULL, MEM_OUT, 0};
|
1993-03-21 12:45:37 +03:00
|
|
|
struct output *out1 = &output;
|
|
|
|
struct output *out2 = &errout;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
|
|
|
|
INCLUDE "output.h"
|
|
|
|
INCLUDE "memalloc.h"
|
|
|
|
|
|
|
|
RESET {
|
|
|
|
out1 = &output;
|
|
|
|
out2 = &errout;
|
|
|
|
if (memout.buf != NULL) {
|
|
|
|
ckfree(memout.buf);
|
|
|
|
memout.buf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef notdef /* no longer used */
|
|
|
|
/*
|
|
|
|
* Set up an output file to write to memory rather than a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
open_mem(char *block, int length, struct output *file)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
file->nextc = block;
|
|
|
|
file->nleft = --length;
|
|
|
|
file->fd = BLOCK_OUT;
|
|
|
|
file->flags = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
out1str(const char *p)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
outstr(p, out1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
out2str(const char *p)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
outstr(p, out2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
outstr(const char *p, struct output *file)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
while (*p)
|
|
|
|
outc(*p++, file);
|
1994-05-11 21:09:42 +04:00
|
|
|
if (file == out2)
|
|
|
|
flushout(file);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-31 17:38:42 +03:00
|
|
|
void
|
|
|
|
out2shstr(const char *p)
|
|
|
|
{
|
|
|
|
outshstr(p, out2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-16 22:41:02 +03:00
|
|
|
/*
|
|
|
|
* ' is in this list, not because it does not require quoting
|
|
|
|
* (which applies to all the others) but because '' quoting cannot
|
|
|
|
* be used to quote it.
|
|
|
|
*/
|
2016-03-12 17:59:26 +03:00
|
|
|
static const char norm_chars [] = \
|
2017-05-18 16:31:10 +03:00
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+-=_,.'";
|
2016-03-12 17:59:26 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
inquote(const char *p)
|
|
|
|
{
|
|
|
|
size_t l = strspn(p, norm_chars);
|
|
|
|
char *s = strchr(p, '\'');
|
|
|
|
|
|
|
|
return s == NULL ? p[l] != '\0' : s - p > (off_t)l;
|
|
|
|
}
|
|
|
|
|
2008-10-31 17:38:42 +03:00
|
|
|
void
|
|
|
|
outshstr(const char *p, struct output *file)
|
|
|
|
{
|
2017-11-16 22:41:02 +03:00
|
|
|
int need_q;
|
2016-03-12 17:59:26 +03:00
|
|
|
int inq;
|
2008-10-31 17:38:42 +03:00
|
|
|
char c;
|
|
|
|
|
2017-11-16 22:41:02 +03:00
|
|
|
if (strchr(p, '\'') != NULL && p[1] != '\0') {
|
|
|
|
/*
|
|
|
|
* string contains ' in it, and is not only the '
|
|
|
|
* see if " quoting will work
|
|
|
|
*/
|
|
|
|
size_t i = strcspn(p, "\\\"$`");
|
|
|
|
|
|
|
|
if (p[i] == '\0') {
|
|
|
|
/*
|
|
|
|
* string contains no $ ` \ or " chars, perfect...
|
|
|
|
*
|
|
|
|
* We know it contains ' so needs quoting, so
|
|
|
|
* this is easy...
|
|
|
|
*/
|
|
|
|
outc('"', file);
|
|
|
|
outstr(p, file);
|
|
|
|
outc('"', file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
|
|
|
|
|
2016-03-12 17:59:26 +03:00
|
|
|
/*
|
|
|
|
* Don't emit ' unless something needs quoting before closing '
|
|
|
|
*/
|
2017-11-16 22:41:02 +03:00
|
|
|
if (need_q && (p[0] == 0 || inquote(p) != 0)) {
|
|
|
|
outc('\'', file);
|
|
|
|
inq = 1;
|
2016-03-12 17:59:26 +03:00
|
|
|
} else
|
|
|
|
inq = 0;
|
|
|
|
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
if (c != '\'') {
|
2008-10-31 17:38:42 +03:00
|
|
|
outc(c, file);
|
2016-03-12 17:59:26 +03:00
|
|
|
continue;
|
2008-10-31 17:38:42 +03:00
|
|
|
}
|
2016-03-12 17:59:26 +03:00
|
|
|
|
|
|
|
if (inq)
|
|
|
|
outc('\'', file); /* inq = 0, implicit */
|
|
|
|
outc('\\', file);
|
|
|
|
outc(c, file);
|
|
|
|
if (need_q && *p != '\0') {
|
|
|
|
if ((inq = inquote(p)) != 0)
|
|
|
|
outc('\'', file);
|
|
|
|
} else
|
|
|
|
inq = 0;
|
2008-10-31 17:38:42 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 17:59:26 +03:00
|
|
|
if (inq)
|
2008-10-31 17:38:42 +03:00
|
|
|
outc('\'', file);
|
|
|
|
|
|
|
|
if (file == out2)
|
|
|
|
flushout(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
char out_junk[16];
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
emptyoutbuf(struct output *dest)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int offset;
|
|
|
|
|
|
|
|
if (dest->fd == BLOCK_OUT) {
|
|
|
|
dest->nextc = out_junk;
|
|
|
|
dest->nleft = sizeof out_junk;
|
|
|
|
dest->flags |= OUTPUT_ERR;
|
|
|
|
} else if (dest->buf == NULL) {
|
|
|
|
INTOFF;
|
|
|
|
dest->buf = ckmalloc(dest->bufsize);
|
|
|
|
dest->nextc = dest->buf;
|
|
|
|
dest->nleft = dest->bufsize;
|
|
|
|
INTON;
|
|
|
|
} else if (dest->fd == MEM_OUT) {
|
|
|
|
offset = dest->bufsize;
|
|
|
|
INTOFF;
|
|
|
|
dest->bufsize <<= 1;
|
|
|
|
dest->buf = ckrealloc(dest->buf, dest->bufsize);
|
|
|
|
dest->nleft = dest->bufsize - offset;
|
|
|
|
dest->nextc = dest->buf + offset;
|
|
|
|
INTON;
|
|
|
|
} else {
|
|
|
|
flushout(dest);
|
|
|
|
}
|
|
|
|
dest->nleft--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
flushall(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
flushout(&output);
|
|
|
|
flushout(&errout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
flushout(struct output *dest)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
|
|
|
|
return;
|
|
|
|
if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
|
|
|
|
dest->flags |= OUTPUT_ERR;
|
|
|
|
dest->nextc = dest->buf;
|
|
|
|
dest->nleft = dest->bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
freestdout(void)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
INTOFF;
|
|
|
|
if (output.buf) {
|
|
|
|
ckfree(output.buf);
|
|
|
|
output.buf = NULL;
|
|
|
|
output.nleft = 0;
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
void
|
1998-01-31 15:37:55 +03:00
|
|
|
outfmt(struct output *file, const char *fmt, ...)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
va_list ap;
|
|
|
|
|
1998-01-31 15:37:55 +03:00
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
doformat(file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-01-31 15:37:55 +03:00
|
|
|
out1fmt(const char *fmt, ...)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
va_list ap;
|
|
|
|
|
1998-01-31 15:37:55 +03:00
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
doformat(out1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-08-30 10:27:14 +04:00
|
|
|
#ifdef DEBUG
|
1994-05-11 21:09:42 +04:00
|
|
|
void
|
2010-08-30 10:27:14 +04:00
|
|
|
debugprintf(const char *fmt, ...)
|
1998-01-31 15:37:55 +03:00
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
va_list ap;
|
|
|
|
|
1998-01-31 15:37:55 +03:00
|
|
|
va_start(ap, fmt);
|
1994-05-11 21:09:42 +04:00
|
|
|
doformat(out2, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
flushout(out2);
|
|
|
|
}
|
2010-08-30 10:27:14 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
void
|
1998-01-31 15:37:55 +03:00
|
|
|
fmtstr(char *outbuf, size_t length, const char *fmt, ...)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
va_list ap;
|
|
|
|
struct output strout;
|
2002-05-26 03:09:06 +04:00
|
|
|
|
1998-01-31 15:37:55 +03:00
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
strout.nextc = outbuf;
|
|
|
|
strout.nleft = length;
|
|
|
|
strout.fd = BLOCK_OUT;
|
|
|
|
strout.flags = 0;
|
|
|
|
doformat(&strout, fmt, ap);
|
|
|
|
outc('\0', &strout);
|
|
|
|
if (strout.flags & OUTPUT_ERR)
|
|
|
|
outbuf[length - 1] = '\0';
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Formatted output. This routine handles a subset of the printf formats:
|
1998-01-31 15:37:55 +03:00
|
|
|
* - Formats supported: d, u, o, p, X, s, and c.
|
1993-03-21 12:45:37 +03:00
|
|
|
* - The x format is also accepted but is treated like X.
|
2001-01-08 01:19:53 +03:00
|
|
|
* - The l, ll and q modifiers are accepted.
|
1993-03-21 12:45:37 +03:00
|
|
|
* - The - and # flags are accepted; # only works with the o format.
|
|
|
|
* - Width and precision may be specified with any format except c.
|
|
|
|
* - An * may be given for the width or precision.
|
|
|
|
* - The obsolete practice of preceding the width with a zero to get
|
|
|
|
* zero padding is not supported; use the precision field.
|
|
|
|
* - A % may be printed by writing %% in the format string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TEMPSIZE 24
|
|
|
|
|
2001-01-08 02:39:07 +03:00
|
|
|
#ifdef BSD4_4
|
|
|
|
#define HAVE_VASPRINTF 1
|
|
|
|
#endif
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
doformat(struct output *dest, const char *f, va_list ap)
|
2001-01-08 02:39:07 +03:00
|
|
|
{
|
|
|
|
#if HAVE_VASPRINTF
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
vasprintf(&s, f, ap);
|
2006-03-17 17:43:11 +03:00
|
|
|
if (s == NULL)
|
|
|
|
error("Could not allocate formatted output buffer");
|
2001-01-08 02:39:07 +03:00
|
|
|
outstr(s, dest);
|
|
|
|
free(s);
|
|
|
|
#else /* !HAVE_VASPRINTF */
|
2002-04-09 04:52:05 +04:00
|
|
|
static const char digit[] = "0123456789ABCDEF";
|
1997-01-11 05:04:27 +03:00
|
|
|
char c;
|
1993-03-21 12:45:37 +03:00
|
|
|
char temp[TEMPSIZE];
|
|
|
|
int flushleft;
|
|
|
|
int sharp;
|
|
|
|
int width;
|
|
|
|
int prec;
|
|
|
|
int islong;
|
1995-09-14 20:19:06 +04:00
|
|
|
int isquad;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *p;
|
|
|
|
int sign;
|
1997-04-12 03:05:43 +04:00
|
|
|
#ifdef BSD4_4
|
1995-09-14 20:19:06 +04:00
|
|
|
quad_t l;
|
|
|
|
u_quad_t num;
|
1997-04-12 03:05:43 +04:00
|
|
|
#else
|
|
|
|
long l;
|
|
|
|
u_long num;
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
unsigned base;
|
|
|
|
int len;
|
|
|
|
int size;
|
|
|
|
int pad;
|
|
|
|
|
|
|
|
while ((c = *f++) != '\0') {
|
|
|
|
if (c != '%') {
|
|
|
|
outc(c, dest);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
flushleft = 0;
|
|
|
|
sharp = 0;
|
|
|
|
width = 0;
|
|
|
|
prec = -1;
|
|
|
|
islong = 0;
|
1995-09-14 20:19:06 +04:00
|
|
|
isquad = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
|
|
|
if (*f == '-')
|
|
|
|
flushleft++;
|
|
|
|
else if (*f == '#')
|
|
|
|
sharp++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
if (*f == '*') {
|
|
|
|
width = va_arg(ap, int);
|
|
|
|
f++;
|
|
|
|
} else {
|
|
|
|
while (is_digit(*f)) {
|
|
|
|
width = 10 * width + digit_val(*f++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*f == '.') {
|
|
|
|
if (*++f == '*') {
|
|
|
|
prec = va_arg(ap, int);
|
|
|
|
f++;
|
|
|
|
} else {
|
|
|
|
prec = 0;
|
|
|
|
while (is_digit(*f)) {
|
|
|
|
prec = 10 * prec + digit_val(*f++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*f == 'l') {
|
|
|
|
f++;
|
2001-01-08 01:19:53 +03:00
|
|
|
if (*f == 'l') {
|
|
|
|
isquad++;
|
|
|
|
f++;
|
|
|
|
} else
|
|
|
|
islong++;
|
1995-09-14 20:19:06 +04:00
|
|
|
} else if (*f == 'q') {
|
|
|
|
isquad++;
|
|
|
|
f++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
switch (*f) {
|
|
|
|
case 'd':
|
1997-04-12 03:05:43 +04:00
|
|
|
#ifdef BSD4_4
|
1995-09-14 20:19:06 +04:00
|
|
|
if (isquad)
|
|
|
|
l = va_arg(ap, quad_t);
|
1997-04-12 03:05:43 +04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (islong)
|
1993-03-21 12:45:37 +03:00
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
sign = 0;
|
|
|
|
num = l;
|
|
|
|
if (l < 0) {
|
|
|
|
num = -l;
|
|
|
|
sign = 1;
|
|
|
|
}
|
|
|
|
base = 10;
|
|
|
|
goto number;
|
|
|
|
case 'u':
|
|
|
|
base = 10;
|
|
|
|
goto uns_number;
|
|
|
|
case 'o':
|
|
|
|
base = 8;
|
|
|
|
goto uns_number;
|
1998-01-31 15:37:55 +03:00
|
|
|
case 'p':
|
|
|
|
outc('0', dest);
|
|
|
|
outc('x', dest);
|
|
|
|
/*FALLTHROUGH*/
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'x':
|
|
|
|
/* we don't implement 'x'; treat like 'X' */
|
|
|
|
case 'X':
|
|
|
|
base = 16;
|
|
|
|
uns_number: /* an unsigned number */
|
|
|
|
sign = 0;
|
1997-04-12 03:05:43 +04:00
|
|
|
#ifdef BSD4_4
|
1995-09-14 20:19:06 +04:00
|
|
|
if (isquad)
|
|
|
|
num = va_arg(ap, u_quad_t);
|
1997-04-12 03:05:43 +04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (islong)
|
1993-03-21 12:45:37 +03:00
|
|
|
num = va_arg(ap, unsigned long);
|
|
|
|
else
|
|
|
|
num = va_arg(ap, unsigned int);
|
|
|
|
number: /* process a number */
|
|
|
|
p = temp + TEMPSIZE - 1;
|
|
|
|
*p = '\0';
|
|
|
|
while (num) {
|
|
|
|
*--p = digit[num % base];
|
|
|
|
num /= base;
|
|
|
|
}
|
|
|
|
len = (temp + TEMPSIZE - 1) - p;
|
|
|
|
if (prec < 0)
|
|
|
|
prec = 1;
|
|
|
|
if (sharp && *f == 'o' && prec <= len)
|
|
|
|
prec = len + 1;
|
|
|
|
pad = 0;
|
|
|
|
if (width) {
|
|
|
|
size = len;
|
|
|
|
if (size < prec)
|
|
|
|
size = prec;
|
|
|
|
size += sign;
|
|
|
|
pad = width - size;
|
|
|
|
if (flushleft == 0) {
|
|
|
|
while (--pad >= 0)
|
|
|
|
outc(' ', dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sign)
|
|
|
|
outc('-', dest);
|
|
|
|
prec -= len;
|
|
|
|
while (--prec >= 0)
|
|
|
|
outc('0', dest);
|
|
|
|
while (*p)
|
|
|
|
outc(*p++, dest);
|
|
|
|
while (--pad >= 0)
|
|
|
|
outc(' ', dest);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
p = va_arg(ap, char *);
|
|
|
|
pad = 0;
|
|
|
|
if (width) {
|
|
|
|
len = strlen(p);
|
|
|
|
if (prec >= 0 && len > prec)
|
|
|
|
len = prec;
|
|
|
|
pad = width - len;
|
|
|
|
if (flushleft == 0) {
|
|
|
|
while (--pad >= 0)
|
|
|
|
outc(' ', dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prec++;
|
|
|
|
while (--prec != 0 && *p)
|
|
|
|
outc(*p++, dest);
|
|
|
|
while (--pad >= 0)
|
|
|
|
outc(' ', dest);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
outc(c, dest);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
outc(*f, dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
f++;
|
|
|
|
}
|
2001-01-08 02:39:07 +03:00
|
|
|
#endif /* !HAVE_VASPRINTF */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version of write which resumes after a signal is caught.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
xwrite(int fd, char *buf, int nbytes)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int ntry;
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = nbytes;
|
|
|
|
ntry = 0;
|
2016-02-29 02:12:23 +03:00
|
|
|
while (n > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
i = write(fd, buf, n);
|
|
|
|
if (i > 0) {
|
|
|
|
if ((n -= i) <= 0)
|
|
|
|
return nbytes;
|
|
|
|
buf += i;
|
|
|
|
ntry = 0;
|
|
|
|
} else if (i == 0) {
|
|
|
|
if (++ntry > 10)
|
|
|
|
return nbytes - n;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 02:12:23 +03:00
|
|
|
return nbytes;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version of ioctl that retries after a signal is caught.
|
1994-12-05 22:07:32 +03:00
|
|
|
* XXX unused function
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
xioctl(int fd, unsigned long request, char *arg)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
|
|
|
|
return i;
|
|
|
|
}
|