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