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