Home | History | Annotate | Line # | Download | only in sh
output.c revision 1.35
      1  1.35  christos /*	$NetBSD: output.c,v 1.35 2016/03/12 14:59:26 christos Exp $	*/
      2  1.13       cgd 
      3   1.1       cgd /*-
      4   1.7       jtc  * Copyright (c) 1991, 1993
      5   1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.13       cgd #if 0
     38  1.14  christos static char sccsid[] = "@(#)output.c	8.2 (Berkeley) 5/4/95";
     39  1.13       cgd #else
     40  1.35  christos __RCSID("$NetBSD: output.c,v 1.35 2016/03/12 14:59:26 christos Exp $");
     41  1.13       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44   1.1       cgd /*
     45   1.1       cgd  * Shell output routines.  We use our own output routines because:
     46   1.1       cgd  *	When a builtin command is interrupted we have to discard
     47   1.1       cgd  *		any pending output.
     48   1.1       cgd  *	When a builtin command appears in back quotes, we want to
     49   1.1       cgd  *		save the output of the command in a region obtained
     50   1.1       cgd  *		via malloc, rather than doing a fork and reading the
     51   1.1       cgd  *		output of the command via a pipe.
     52   1.1       cgd  *	Our output routines may be smaller than the stdio routines.
     53   1.1       cgd  */
     54   1.1       cgd 
     55  1.20  christos #include <sys/types.h>		/* quad_t */
     56  1.20  christos #include <sys/param.h>		/* BSD4_4 */
     57  1.11       cgd #include <sys/ioctl.h>
     58  1.11       cgd 
     59   1.1       cgd #include <stdio.h>	/* defines BUFSIZ */
     60  1.12       cgd #include <string.h>
     61   1.1       cgd #include <errno.h>
     62   1.9       jtc #include <unistd.h>
     63  1.14  christos #include <stdlib.h>
     64  1.14  christos 
     65  1.14  christos #include "shell.h"
     66  1.14  christos #include "syntax.h"
     67  1.14  christos #include "output.h"
     68  1.14  christos #include "memalloc.h"
     69  1.14  christos #include "error.h"
     70   1.1       cgd 
     71   1.1       cgd 
     72   1.1       cgd #define OUTBUFSIZ BUFSIZ
     73   1.1       cgd #define BLOCK_OUT -2		/* output to a fixed block of memory */
     74   1.1       cgd #define MEM_OUT -3		/* output to dynamically allocated memory */
     75   1.1       cgd 
     76   1.1       cgd 
     77   1.1       cgd struct output output = {NULL, 0, NULL, OUTBUFSIZ, 1, 0};
     78  1.18  christos struct output errout = {NULL, 0, NULL, 100, 2, 0};
     79   1.1       cgd struct output memout = {NULL, 0, NULL, 0, MEM_OUT, 0};
     80   1.1       cgd struct output *out1 = &output;
     81   1.1       cgd struct output *out2 = &errout;
     82   1.1       cgd 
     83   1.1       cgd 
     84   1.1       cgd 
     85   1.1       cgd #ifdef mkinit
     86   1.1       cgd 
     87   1.1       cgd INCLUDE "output.h"
     88   1.1       cgd INCLUDE "memalloc.h"
     89   1.1       cgd 
     90   1.1       cgd RESET {
     91   1.1       cgd 	out1 = &output;
     92   1.1       cgd 	out2 = &errout;
     93   1.1       cgd 	if (memout.buf != NULL) {
     94   1.1       cgd 		ckfree(memout.buf);
     95   1.1       cgd 		memout.buf = NULL;
     96   1.1       cgd 	}
     97   1.1       cgd }
     98   1.1       cgd 
     99   1.1       cgd #endif
    100   1.1       cgd 
    101   1.1       cgd 
    102   1.1       cgd #ifdef notdef	/* no longer used */
    103   1.1       cgd /*
    104   1.1       cgd  * Set up an output file to write to memory rather than a file.
    105   1.1       cgd  */
    106   1.1       cgd 
    107   1.1       cgd void
    108  1.27  christos open_mem(char *block, int length, struct output *file)
    109  1.27  christos {
    110   1.1       cgd 	file->nextc = block;
    111   1.1       cgd 	file->nleft = --length;
    112   1.1       cgd 	file->fd = BLOCK_OUT;
    113   1.1       cgd 	file->flags = 0;
    114   1.1       cgd }
    115   1.1       cgd #endif
    116   1.1       cgd 
    117   1.1       cgd 
    118   1.1       cgd void
    119  1.27  christos out1str(const char *p)
    120  1.27  christos {
    121   1.1       cgd 	outstr(p, out1);
    122   1.1       cgd }
    123   1.1       cgd 
    124   1.1       cgd 
    125   1.1       cgd void
    126  1.27  christos out2str(const char *p)
    127  1.27  christos {
    128   1.1       cgd 	outstr(p, out2);
    129   1.1       cgd }
    130   1.1       cgd 
    131   1.1       cgd 
    132   1.1       cgd void
    133  1.27  christos outstr(const char *p, struct output *file)
    134  1.27  christos {
    135   1.1       cgd 	while (*p)
    136   1.1       cgd 		outc(*p++, file);
    137   1.7       jtc 	if (file == out2)
    138   1.7       jtc 		flushout(file);
    139   1.1       cgd }
    140   1.1       cgd 
    141   1.1       cgd 
    142  1.31  christos void
    143  1.31  christos out2shstr(const char *p)
    144  1.31  christos {
    145  1.31  christos 	outshstr(p, out2);
    146  1.31  christos }
    147  1.31  christos 
    148  1.31  christos 
    149  1.35  christos static const char norm_chars [] = \
    150  1.35  christos     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-=_.'";
    151  1.35  christos 
    152  1.35  christos static int
    153  1.35  christos inquote(const char *p)
    154  1.35  christos {
    155  1.35  christos 	size_t l = strspn(p, norm_chars);
    156  1.35  christos 	char *s = strchr(p, '\'');
    157  1.35  christos 
    158  1.35  christos 	return s == NULL ? p[l] != '\0' : s - p > (off_t)l;
    159  1.35  christos }
    160  1.35  christos 
    161  1.35  christos 
    162  1.31  christos void
    163  1.31  christos outshstr(const char *p, struct output *file)
    164  1.31  christos {
    165  1.35  christos 	/*
    166  1.35  christos 	 * ' is in this list, not because it does not require quoting
    167  1.35  christos 	 * (which applies to all the others) but because '' quoting cannot
    168  1.35  christos 	 * be used to quote it.
    169  1.35  christos 	 */
    170  1.31  christos 	int need_q = p[0] == 0 || p[strspn(p, norm_chars)] != 0;
    171  1.35  christos 	int inq;
    172  1.31  christos 	char c;
    173  1.31  christos 
    174  1.35  christos 	/*
    175  1.35  christos 	 * Don't emit ' unless something needs quoting before closing '
    176  1.35  christos 	 */
    177  1.35  christos 	if (need_q) {
    178  1.35  christos 		if ((inq = inquote(p)) != 0)
    179  1.35  christos 				outc('\'', file);
    180  1.35  christos 	} else
    181  1.35  christos 		inq = 0;
    182  1.31  christos 
    183  1.35  christos 	while ((c = *p++) != '\0') {
    184  1.35  christos 		if (c != '\'') {
    185  1.31  christos 			outc(c, file);
    186  1.35  christos 			continue;
    187  1.31  christos 		}
    188  1.35  christos 
    189  1.35  christos 		if (inq)
    190  1.35  christos 			outc('\'', file);	/* inq = 0, implicit */
    191  1.35  christos 		outc('\\', file);
    192  1.35  christos 		outc(c, file);
    193  1.35  christos 		if (need_q && *p != '\0') {
    194  1.35  christos 			if ((inq = inquote(p)) != 0)
    195  1.35  christos 				outc('\'', file);
    196  1.35  christos 		} else
    197  1.35  christos 			inq = 0;
    198  1.31  christos 	}
    199  1.31  christos 
    200  1.35  christos 	if (inq)
    201  1.31  christos 		outc('\'', file);
    202  1.31  christos 
    203  1.31  christos 	if (file == out2)
    204  1.31  christos 		flushout(file);
    205  1.31  christos }
    206  1.31  christos 
    207  1.31  christos 
    208   1.1       cgd char out_junk[16];
    209   1.1       cgd 
    210   1.1       cgd 
    211   1.1       cgd void
    212  1.27  christos emptyoutbuf(struct output *dest)
    213  1.27  christos {
    214   1.1       cgd 	int offset;
    215   1.1       cgd 
    216   1.1       cgd 	if (dest->fd == BLOCK_OUT) {
    217   1.1       cgd 		dest->nextc = out_junk;
    218   1.1       cgd 		dest->nleft = sizeof out_junk;
    219   1.1       cgd 		dest->flags |= OUTPUT_ERR;
    220   1.1       cgd 	} else if (dest->buf == NULL) {
    221   1.1       cgd 		INTOFF;
    222   1.1       cgd 		dest->buf = ckmalloc(dest->bufsize);
    223   1.1       cgd 		dest->nextc = dest->buf;
    224   1.1       cgd 		dest->nleft = dest->bufsize;
    225   1.1       cgd 		INTON;
    226   1.1       cgd 	} else if (dest->fd == MEM_OUT) {
    227   1.1       cgd 		offset = dest->bufsize;
    228   1.1       cgd 		INTOFF;
    229   1.1       cgd 		dest->bufsize <<= 1;
    230   1.1       cgd 		dest->buf = ckrealloc(dest->buf, dest->bufsize);
    231   1.1       cgd 		dest->nleft = dest->bufsize - offset;
    232   1.1       cgd 		dest->nextc = dest->buf + offset;
    233   1.1       cgd 		INTON;
    234   1.1       cgd 	} else {
    235   1.1       cgd 		flushout(dest);
    236   1.1       cgd 	}
    237   1.1       cgd 	dest->nleft--;
    238   1.1       cgd }
    239   1.1       cgd 
    240   1.1       cgd 
    241   1.1       cgd void
    242  1.27  christos flushall(void)
    243  1.27  christos {
    244   1.1       cgd 	flushout(&output);
    245   1.1       cgd 	flushout(&errout);
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.1       cgd 
    249   1.1       cgd void
    250  1.27  christos flushout(struct output *dest)
    251  1.27  christos {
    252   1.1       cgd 
    253   1.1       cgd 	if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
    254   1.1       cgd 		return;
    255   1.1       cgd 	if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
    256   1.1       cgd 		dest->flags |= OUTPUT_ERR;
    257   1.1       cgd 	dest->nextc = dest->buf;
    258   1.1       cgd 	dest->nleft = dest->bufsize;
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd 
    262   1.1       cgd void
    263  1.27  christos freestdout(void)
    264  1.27  christos {
    265   1.1       cgd 	INTOFF;
    266   1.1       cgd 	if (output.buf) {
    267   1.1       cgd 		ckfree(output.buf);
    268   1.1       cgd 		output.buf = NULL;
    269   1.1       cgd 		output.nleft = 0;
    270   1.1       cgd 	}
    271   1.1       cgd 	INTON;
    272   1.1       cgd }
    273   1.1       cgd 
    274   1.1       cgd 
    275  1.21  christos void
    276  1.21  christos outfmt(struct output *file, const char *fmt, ...)
    277  1.21  christos {
    278   1.1       cgd 	va_list ap;
    279   1.1       cgd 
    280  1.21  christos 	va_start(ap, fmt);
    281   1.1       cgd 	doformat(file, fmt, ap);
    282   1.1       cgd 	va_end(ap);
    283   1.1       cgd }
    284   1.1       cgd 
    285   1.1       cgd 
    286   1.1       cgd void
    287  1.21  christos out1fmt(const char *fmt, ...)
    288  1.21  christos {
    289   1.1       cgd 	va_list ap;
    290   1.1       cgd 
    291  1.21  christos 	va_start(ap, fmt);
    292   1.1       cgd 	doformat(out1, fmt, ap);
    293   1.1       cgd 	va_end(ap);
    294   1.1       cgd }
    295   1.1       cgd 
    296  1.33  christos #ifdef DEBUG
    297   1.7       jtc void
    298  1.33  christos debugprintf(const char *fmt, ...)
    299  1.21  christos {
    300   1.7       jtc 	va_list ap;
    301   1.7       jtc 
    302  1.21  christos 	va_start(ap, fmt);
    303   1.7       jtc 	doformat(out2, fmt, ap);
    304   1.7       jtc 	va_end(ap);
    305   1.7       jtc 	flushout(out2);
    306   1.7       jtc }
    307  1.33  christos #endif
    308   1.1       cgd 
    309   1.1       cgd void
    310  1.21  christos fmtstr(char *outbuf, size_t length, const char *fmt, ...)
    311  1.21  christos {
    312   1.1       cgd 	va_list ap;
    313   1.1       cgd 	struct output strout;
    314  1.26       wiz 
    315  1.21  christos 	va_start(ap, fmt);
    316   1.1       cgd 	strout.nextc = outbuf;
    317   1.1       cgd 	strout.nleft = length;
    318   1.1       cgd 	strout.fd = BLOCK_OUT;
    319   1.1       cgd 	strout.flags = 0;
    320   1.1       cgd 	doformat(&strout, fmt, ap);
    321   1.1       cgd 	outc('\0', &strout);
    322   1.1       cgd 	if (strout.flags & OUTPUT_ERR)
    323   1.1       cgd 		outbuf[length - 1] = '\0';
    324  1.24       wiz 	va_end(ap);
    325   1.1       cgd }
    326   1.1       cgd 
    327   1.1       cgd /*
    328   1.1       cgd  * Formatted output.  This routine handles a subset of the printf formats:
    329  1.21  christos  * - Formats supported: d, u, o, p, X, s, and c.
    330   1.1       cgd  * - The x format is also accepted but is treated like X.
    331  1.22     lukem  * - The l, ll and q modifiers are accepted.
    332   1.1       cgd  * - The - and # flags are accepted; # only works with the o format.
    333   1.1       cgd  * - Width and precision may be specified with any format except c.
    334   1.1       cgd  * - An * may be given for the width or precision.
    335   1.1       cgd  * - The obsolete practice of preceding the width with a zero to get
    336   1.1       cgd  *   zero padding is not supported; use the precision field.
    337   1.1       cgd  * - A % may be printed by writing %% in the format string.
    338   1.1       cgd  */
    339   1.1       cgd 
    340   1.1       cgd #define TEMPSIZE 24
    341   1.1       cgd 
    342  1.23     lukem #ifdef BSD4_4
    343  1.23     lukem #define HAVE_VASPRINTF 1
    344  1.23     lukem #endif
    345  1.23     lukem 
    346   1.1       cgd void
    347  1.27  christos doformat(struct output *dest, const char *f, va_list ap)
    348  1.23     lukem {
    349  1.23     lukem #if	HAVE_VASPRINTF
    350  1.23     lukem 	char *s;
    351  1.23     lukem 
    352  1.23     lukem 	vasprintf(&s, f, ap);
    353  1.29    rumble 	if (s == NULL)
    354  1.29    rumble 		error("Could not allocate formatted output buffer");
    355  1.23     lukem 	outstr(s, dest);
    356  1.23     lukem 	free(s);
    357  1.23     lukem #else	/* !HAVE_VASPRINTF */
    358  1.25   thorpej 	static const char digit[] = "0123456789ABCDEF";
    359  1.17       tls 	char c;
    360   1.1       cgd 	char temp[TEMPSIZE];
    361   1.1       cgd 	int flushleft;
    362   1.1       cgd 	int sharp;
    363   1.1       cgd 	int width;
    364   1.1       cgd 	int prec;
    365   1.1       cgd 	int islong;
    366  1.15       jtc 	int isquad;
    367   1.1       cgd 	char *p;
    368   1.1       cgd 	int sign;
    369  1.18  christos #ifdef BSD4_4
    370  1.15       jtc 	quad_t l;
    371  1.15       jtc 	u_quad_t num;
    372  1.18  christos #else
    373  1.18  christos 	long l;
    374  1.18  christos 	u_long num;
    375  1.18  christos #endif
    376   1.1       cgd 	unsigned base;
    377   1.1       cgd 	int len;
    378   1.1       cgd 	int size;
    379   1.1       cgd 	int pad;
    380   1.1       cgd 
    381   1.1       cgd 	while ((c = *f++) != '\0') {
    382   1.1       cgd 		if (c != '%') {
    383   1.1       cgd 			outc(c, dest);
    384   1.1       cgd 			continue;
    385   1.1       cgd 		}
    386   1.1       cgd 		flushleft = 0;
    387   1.1       cgd 		sharp = 0;
    388   1.1       cgd 		width = 0;
    389   1.1       cgd 		prec = -1;
    390   1.1       cgd 		islong = 0;
    391  1.15       jtc 		isquad = 0;
    392   1.1       cgd 		for (;;) {
    393   1.1       cgd 			if (*f == '-')
    394   1.1       cgd 				flushleft++;
    395   1.1       cgd 			else if (*f == '#')
    396   1.1       cgd 				sharp++;
    397   1.1       cgd 			else
    398   1.1       cgd 				break;
    399   1.1       cgd 			f++;
    400   1.1       cgd 		}
    401   1.1       cgd 		if (*f == '*') {
    402   1.1       cgd 			width = va_arg(ap, int);
    403   1.1       cgd 			f++;
    404   1.1       cgd 		} else {
    405   1.1       cgd 			while (is_digit(*f)) {
    406   1.1       cgd 				width = 10 * width + digit_val(*f++);
    407   1.1       cgd 			}
    408   1.1       cgd 		}
    409   1.1       cgd 		if (*f == '.') {
    410   1.1       cgd 			if (*++f == '*') {
    411   1.1       cgd 				prec = va_arg(ap, int);
    412   1.1       cgd 				f++;
    413   1.1       cgd 			} else {
    414   1.1       cgd 				prec = 0;
    415   1.1       cgd 				while (is_digit(*f)) {
    416   1.1       cgd 					prec = 10 * prec + digit_val(*f++);
    417   1.1       cgd 				}
    418   1.1       cgd 			}
    419   1.1       cgd 		}
    420   1.1       cgd 		if (*f == 'l') {
    421   1.1       cgd 			f++;
    422  1.22     lukem 			if (*f == 'l') {
    423  1.22     lukem 				isquad++;
    424  1.22     lukem 				f++;
    425  1.22     lukem 			} else
    426  1.22     lukem 				islong++;
    427  1.15       jtc 		} else if (*f == 'q') {
    428  1.15       jtc 			isquad++;
    429  1.15       jtc 			f++;
    430   1.1       cgd 		}
    431   1.1       cgd 		switch (*f) {
    432   1.1       cgd 		case 'd':
    433  1.18  christos #ifdef BSD4_4
    434  1.15       jtc 			if (isquad)
    435  1.15       jtc 				l = va_arg(ap, quad_t);
    436  1.18  christos 			else
    437  1.18  christos #endif
    438  1.18  christos 			if (islong)
    439   1.1       cgd 				l = va_arg(ap, long);
    440   1.1       cgd 			else
    441   1.1       cgd 				l = va_arg(ap, int);
    442   1.1       cgd 			sign = 0;
    443   1.1       cgd 			num = l;
    444   1.1       cgd 			if (l < 0) {
    445   1.1       cgd 				num = -l;
    446   1.1       cgd 				sign = 1;
    447   1.1       cgd 			}
    448   1.1       cgd 			base = 10;
    449   1.1       cgd 			goto number;
    450   1.1       cgd 		case 'u':
    451   1.1       cgd 			base = 10;
    452   1.1       cgd 			goto uns_number;
    453   1.1       cgd 		case 'o':
    454   1.1       cgd 			base = 8;
    455   1.1       cgd 			goto uns_number;
    456  1.21  christos 		case 'p':
    457  1.21  christos 			outc('0', dest);
    458  1.21  christos 			outc('x', dest);
    459  1.21  christos 			/*FALLTHROUGH*/
    460   1.1       cgd 		case 'x':
    461   1.1       cgd 			/* we don't implement 'x'; treat like 'X' */
    462   1.1       cgd 		case 'X':
    463   1.1       cgd 			base = 16;
    464   1.1       cgd uns_number:	  /* an unsigned number */
    465   1.1       cgd 			sign = 0;
    466  1.18  christos #ifdef BSD4_4
    467  1.15       jtc 			if (isquad)
    468  1.15       jtc 				num = va_arg(ap, u_quad_t);
    469  1.18  christos 			else
    470  1.18  christos #endif
    471  1.18  christos 			if (islong)
    472   1.1       cgd 				num = va_arg(ap, unsigned long);
    473   1.1       cgd 			else
    474   1.1       cgd 				num = va_arg(ap, unsigned int);
    475   1.1       cgd number:		  /* process a number */
    476   1.1       cgd 			p = temp + TEMPSIZE - 1;
    477   1.1       cgd 			*p = '\0';
    478   1.1       cgd 			while (num) {
    479   1.1       cgd 				*--p = digit[num % base];
    480   1.1       cgd 				num /= base;
    481   1.1       cgd 			}
    482   1.1       cgd 			len = (temp + TEMPSIZE - 1) - p;
    483   1.1       cgd 			if (prec < 0)
    484   1.1       cgd 				prec = 1;
    485   1.1       cgd 			if (sharp && *f == 'o' && prec <= len)
    486   1.1       cgd 				prec = len + 1;
    487   1.1       cgd 			pad = 0;
    488   1.1       cgd 			if (width) {
    489   1.1       cgd 				size = len;
    490   1.1       cgd 				if (size < prec)
    491   1.1       cgd 					size = prec;
    492   1.1       cgd 				size += sign;
    493   1.1       cgd 				pad = width - size;
    494   1.1       cgd 				if (flushleft == 0) {
    495   1.1       cgd 					while (--pad >= 0)
    496   1.1       cgd 						outc(' ', dest);
    497   1.1       cgd 				}
    498   1.1       cgd 			}
    499   1.1       cgd 			if (sign)
    500   1.1       cgd 				outc('-', dest);
    501   1.1       cgd 			prec -= len;
    502   1.1       cgd 			while (--prec >= 0)
    503   1.1       cgd 				outc('0', dest);
    504   1.1       cgd 			while (*p)
    505   1.1       cgd 				outc(*p++, dest);
    506   1.1       cgd 			while (--pad >= 0)
    507   1.1       cgd 				outc(' ', dest);
    508   1.1       cgd 			break;
    509   1.1       cgd 		case 's':
    510   1.1       cgd 			p = va_arg(ap, char *);
    511   1.1       cgd 			pad = 0;
    512   1.1       cgd 			if (width) {
    513   1.1       cgd 				len = strlen(p);
    514   1.1       cgd 				if (prec >= 0 && len > prec)
    515   1.1       cgd 					len = prec;
    516   1.1       cgd 				pad = width - len;
    517   1.1       cgd 				if (flushleft == 0) {
    518   1.1       cgd 					while (--pad >= 0)
    519   1.1       cgd 						outc(' ', dest);
    520   1.1       cgd 				}
    521   1.1       cgd 			}
    522   1.1       cgd 			prec++;
    523   1.1       cgd 			while (--prec != 0 && *p)
    524   1.1       cgd 				outc(*p++, dest);
    525   1.1       cgd 			while (--pad >= 0)
    526   1.1       cgd 				outc(' ', dest);
    527   1.1       cgd 			break;
    528   1.1       cgd 		case 'c':
    529   1.1       cgd 			c = va_arg(ap, int);
    530   1.1       cgd 			outc(c, dest);
    531   1.1       cgd 			break;
    532   1.1       cgd 		default:
    533   1.1       cgd 			outc(*f, dest);
    534   1.1       cgd 			break;
    535   1.1       cgd 		}
    536   1.1       cgd 		f++;
    537   1.1       cgd 	}
    538  1.23     lukem #endif	/* !HAVE_VASPRINTF */
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd 
    542   1.1       cgd 
    543   1.1       cgd /*
    544   1.1       cgd  * Version of write which resumes after a signal is caught.
    545   1.1       cgd  */
    546   1.1       cgd 
    547   1.1       cgd int
    548  1.27  christos xwrite(int fd, char *buf, int nbytes)
    549  1.27  christos {
    550   1.1       cgd 	int ntry;
    551   1.1       cgd 	int i;
    552   1.1       cgd 	int n;
    553   1.1       cgd 
    554   1.1       cgd 	n = nbytes;
    555   1.1       cgd 	ntry = 0;
    556  1.34  christos 	while (n > 0) {
    557   1.1       cgd 		i = write(fd, buf, n);
    558   1.1       cgd 		if (i > 0) {
    559   1.1       cgd 			if ((n -= i) <= 0)
    560   1.1       cgd 				return nbytes;
    561   1.1       cgd 			buf += i;
    562   1.1       cgd 			ntry = 0;
    563   1.1       cgd 		} else if (i == 0) {
    564   1.1       cgd 			if (++ntry > 10)
    565   1.1       cgd 				return nbytes - n;
    566   1.1       cgd 		} else if (errno != EINTR) {
    567   1.1       cgd 			return -1;
    568   1.1       cgd 		}
    569   1.1       cgd 	}
    570  1.34  christos 	return nbytes;
    571   1.1       cgd }
    572   1.1       cgd 
    573   1.1       cgd 
    574   1.1       cgd /*
    575   1.1       cgd  * Version of ioctl that retries after a signal is caught.
    576  1.11       cgd  * XXX unused function
    577   1.1       cgd  */
    578   1.1       cgd 
    579   1.1       cgd int
    580  1.27  christos xioctl(int fd, unsigned long request, char *arg)
    581  1.11       cgd {
    582   1.1       cgd 	int i;
    583   1.1       cgd 
    584   1.1       cgd 	while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
    585   1.1       cgd 	return i;
    586   1.1       cgd }
    587