1 /* $NetBSD: shf.c,v 1.7 2005/06/26 19:09:00 christos Exp $ */
4 * Shell file I/O routines
9 __RCSID("$NetBSD: shf.c,v 1.7 2005/06/26 19:09:00 christos Exp $");
15 #include "ksh_limval.h"
18 /* flags to shf_emptybuf() */
19 #define EB_READSW 0x01 /* about to switch to reading */
20 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
23 * Replacement stdio routines. Stdio is too flakey on too many machines
24 * to be useful when you have multiple processes using the same underlying
28 static int shf_fillbuf
ARGS((struct shf
*shf
));
29 static int shf_emptybuf
ARGS((struct shf
*shf
, int flags
));
31 /* Open a file. First three args are for open(), last arg is flags for
32 * this package. Returns NULL if file could not be opened, or if a dup
36 shf_open(name
, oflags
, mode
, sflags
)
43 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
46 /* Done before open so if alloca fails, fd won't be lost. */
47 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
49 shf
->buf
= (unsigned char *) &shf
[1];
51 shf
->flags
= SHF_ALLOCS
;
52 /* Rest filled in by reopen. */
54 fd
= open(name
, oflags
, mode
);
56 afree(shf
, shf
->areap
);
59 if ((sflags
& SHF_MAPHI
) && fd
< FDBASE
) {
62 nfd
= ksh_dupbase(fd
, FDBASE
);
65 afree(shf
, shf
->areap
);
70 sflags
&= ~SHF_ACCMODE
;
71 sflags
|= (oflags
& O_ACCMODE
) == O_RDONLY
? SHF_RD
72 : ((oflags
& O_ACCMODE
) == O_WRONLY
? SHF_WR
75 return shf_reopen(fd
, sflags
, shf
);
78 /* Set up the shf structure for a file descriptor. Doesn't fail. */
80 shf_fdopen(fd
, sflags
, shf
)
85 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
87 /* use fcntl() to figure out correct read/write flags */
88 if (sflags
& SHF_GETFL
) {
89 int flags
= fcntl(fd
, F_GETFL
, 0);
92 /* will get an error on first read/write */
95 switch (flags
& O_ACCMODE
) {
96 case O_RDONLY
: sflags
|= SHF_RD
; break;
97 case O_WRONLY
: sflags
|= SHF_WR
; break;
98 case O_RDWR
: sflags
|= SHF_RDWR
; break;
102 if (!(sflags
& (SHF_RD
| SHF_WR
)))
103 internal_errorf(1, "shf_fdopen: missing read/write");
107 shf
->buf
= (unsigned char *) alloc(bsize
, ATEMP
);
108 sflags
|= SHF_ALLOCB
;
110 shf
->buf
= (unsigned char *) 0;
112 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
113 shf
->buf
= (unsigned char *) &shf
[1];
114 sflags
|= SHF_ALLOCS
;
118 shf
->rp
= shf
->wp
= shf
->buf
;
121 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
122 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
126 if (sflags
& SHF_CLEXEC
)
131 /* Set up an existing shf (and buffer) to use the given fd */
133 shf_reopen(fd
, sflags
, shf
)
138 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
140 /* use fcntl() to figure out correct read/write flags */
141 if (sflags
& SHF_GETFL
) {
142 int flags
= fcntl(fd
, F_GETFL
, 0);
145 /* will get an error on first read/write */
148 switch (flags
& O_ACCMODE
) {
149 case O_RDONLY
: sflags
|= SHF_RD
; break;
150 case O_WRONLY
: sflags
|= SHF_WR
; break;
151 case O_RDWR
: sflags
|= SHF_RDWR
; break;
155 if (!(sflags
& (SHF_RD
| SHF_WR
)))
156 internal_errorf(1, "shf_reopen: missing read/write");
157 if (!shf
|| !shf
->buf
|| shf
->bsize
< bsize
)
158 internal_errorf(1, "shf_reopen: bad shf/buf/bsize");
160 /* assumes shf->buf and shf->bsize already set up */
162 shf
->rp
= shf
->wp
= shf
->buf
;
165 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
166 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
167 shf
->flags
= (shf
->flags
& (SHF_ALLOCS
| SHF_ALLOCB
)) | sflags
;
169 if (sflags
& SHF_CLEXEC
)
174 /* Open a string for reading or writing. If reading, bsize is the number
175 * of bytes that can be read. If writing, bsize is the maximum number of
176 * bytes that can be written. If shf is not null, it is filled in and
177 * returned, if it is null, shf is allocated. If writing and buf is null
178 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
179 * used for the initial size). Doesn't fail.
180 * When writing, a byte is reserved for a trailing null - see shf_sclose().
183 shf_sopen(buf
, bsize
, sflags
, shf
)
189 /* can't have a read+write string */
190 if (!(sflags
& (SHF_RD
| SHF_WR
))
191 || (sflags
& (SHF_RD
| SHF_WR
)) == (SHF_RD
| SHF_WR
))
192 internal_errorf(1, "shf_sopen: flags 0x%x", sflags
);
195 shf
= (struct shf
*) alloc(sizeof(struct shf
), ATEMP
);
196 sflags
|= SHF_ALLOCS
;
199 if (!buf
&& (sflags
& SHF_WR
) && (sflags
& SHF_DYNAMIC
)) {
202 sflags
|= SHF_ALLOCB
;
203 buf
= alloc(bsize
, shf
->areap
);
206 shf
->buf
= shf
->rp
= shf
->wp
= (unsigned char *) buf
;
209 shf
->wnleft
= bsize
- 1; /* space for a '\0' */
211 shf
->flags
= sflags
| SHF_STRING
;
218 /* Flush and close file descriptor, free the shf structure */
226 ret
= shf_flush(shf
);
227 if (close(shf
->fd
) < 0)
230 if (shf
->flags
& SHF_ALLOCS
)
231 afree(shf
, shf
->areap
);
232 else if (shf
->flags
& SHF_ALLOCB
)
233 afree(shf
->buf
, shf
->areap
);
238 /* Flush and close file descriptor, don't free file structure */
246 ret
= shf_flush(shf
);
247 if (close(shf
->fd
) < 0)
258 /* Close a string - if it was opened for writing, it is null terminated;
259 * returns a pointer to the string and frees shf if it was allocated
260 * (does not free string if it was allocated).
266 unsigned char *s
= shf
->buf
;
269 if (shf
->flags
& SHF_WR
) {
273 if (shf
->flags
& SHF_ALLOCS
)
274 afree(shf
, shf
->areap
);
278 /* Flush and free file structure, don't close file descriptor */
286 ret
= shf_flush(shf
);
287 if (shf
->flags
& SHF_ALLOCS
)
288 afree(shf
, shf
->areap
);
289 else if (shf
->flags
& SHF_ALLOCB
)
290 afree(shf
->buf
, shf
->areap
);
295 /* Un-read what has been read but not examined, or write what has been
296 * buffered. Returns 0 for success, EOF for (write) error.
302 if (shf
->flags
& SHF_STRING
)
303 return (shf
->flags
& SHF_WR
) ? EOF
: 0;
306 internal_errorf(1, "shf_flush: no fd");
308 if (shf
->flags
& SHF_ERROR
) {
313 if (shf
->flags
& SHF_READING
) {
314 shf
->flags
&= ~(SHF_EOF
| SHF_READING
);
315 if (shf
->rnleft
> 0) {
316 lseek(shf
->fd
, (off_t
) -shf
->rnleft
, 1);
321 } else if (shf
->flags
& SHF_WRITING
)
322 return shf_emptybuf(shf
, 0);
327 /* Write out any buffered data. If currently reading, flushes the read
328 * buffer. Returns 0 for success, EOF for (write) error.
331 shf_emptybuf(shf
, flags
)
337 if (!(shf
->flags
& SHF_STRING
) && shf
->fd
< 0)
338 internal_errorf(1, "shf_emptybuf: no fd");
340 if (shf
->flags
& SHF_ERROR
) {
345 if (shf
->flags
& SHF_READING
) {
346 if (flags
& EB_READSW
) /* doesn't happen */
348 ret
= shf_flush(shf
);
349 shf
->flags
&= ~SHF_READING
;
351 if (shf
->flags
& SHF_STRING
) {
354 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
355 * is set... (changing the shf pointer could cause problems)
357 if (!(flags
& EB_GROW
) || !(shf
->flags
& SHF_DYNAMIC
)
358 || !(shf
->flags
& SHF_ALLOCB
))
360 /* allocate more space for buffer */
361 nbuf
= (unsigned char *) aresize(shf
->buf
, shf
->wbsize
* 2,
363 shf
->rp
= nbuf
+ (shf
->rp
- shf
->buf
);
364 shf
->wp
= nbuf
+ (shf
->wp
- shf
->buf
);
365 shf
->rbsize
+= shf
->wbsize
;
366 shf
->wnleft
+= shf
->wbsize
;
370 if (shf
->flags
& SHF_WRITING
) {
371 int ntowrite
= shf
->wp
- shf
->buf
;
372 unsigned char *buf
= shf
->buf
;
375 while (ntowrite
> 0) {
376 n
= write(shf
->fd
, buf
, ntowrite
);
379 && !(shf
->flags
& SHF_INTERRUPT
))
381 shf
->flags
|= SHF_ERROR
;
384 if (buf
!= shf
->buf
) {
385 /* allow a second flush
387 memmove(shf
->buf
, buf
,
389 shf
->wp
= shf
->buf
+ ntowrite
;
396 if (flags
& EB_READSW
) {
399 shf
->flags
&= ~SHF_WRITING
;
404 shf
->wnleft
= shf
->wbsize
;
406 shf
->flags
|= SHF_WRITING
;
411 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
416 if (shf
->flags
& SHF_STRING
)
420 internal_errorf(1, "shf_fillbuf: no fd");
422 if (shf
->flags
& (SHF_EOF
| SHF_ERROR
)) {
423 if (shf
->flags
& SHF_ERROR
)
428 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
431 shf
->flags
|= SHF_READING
;
435 shf
->rnleft
= blocking_read(shf
->fd
, (char *) shf
->buf
,
437 if (shf
->rnleft
< 0 && errno
== EINTR
438 && !(shf
->flags
& SHF_INTERRUPT
))
442 if (shf
->rnleft
<= 0) {
443 if (shf
->rnleft
< 0) {
444 shf
->flags
|= SHF_ERROR
;
450 shf
->flags
|= SHF_EOF
;
455 /* Seek to a new position in the file. If writing, flushes the buffer
456 * first. If reading, optimizes small relative seeks that stay inside the
457 * buffer. Returns 0 for success, EOF otherwise.
460 shf_seek(shf
, where
, from
)
470 if (shf
->flags
& SHF_ERROR
) {
475 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
478 if (shf
->flags
& SHF_READING
) {
479 if (from
== SEEK_CUR
&&
481 -where
>= shf
->rbsize
- shf
->rnleft
:
482 where
< shf
->rnleft
)) {
483 shf
->rnleft
-= where
;
491 shf
->flags
&= ~(SHF_EOF
| SHF_READING
| SHF_WRITING
);
493 if (lseek(shf
->fd
, where
, from
) < 0) {
495 shf
->flags
|= SHF_ERROR
;
503 /* Read a buffer from shf. Returns the number of bytes read into buf,
504 * if no bytes were read, returns 0 if end of file was seen, EOF if
505 * a read error occurred.
508 shf_read(buf
, bsize
, shf
)
513 int orig_bsize
= bsize
;
516 if (!(shf
->flags
& SHF_RD
))
517 internal_errorf(1, "shf_read: flags %x", shf
->flags
);
520 internal_errorf(1, "shf_read: bsize %d", bsize
);
524 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
529 memcpy(buf
, shf
->rp
, ncopy
);
533 shf
->rnleft
-= ncopy
;
535 /* Note: fread(3S) returns 0 for errors - this doesn't */
536 return orig_bsize
== bsize
? (shf_error(shf
) ? EOF
: 0)
537 : orig_bsize
- bsize
;
540 /* Read up to a newline or EOF. The newline is put in buf; buf is always
541 * null terminated. Returns NULL on read error or if nothing was read before
542 * end of file, returns a pointer to the null byte in buf otherwise.
545 shf_getse(buf
, bsize
, shf
)
552 char *orig_buf
= buf
;
554 if (!(shf
->flags
& SHF_RD
))
555 internal_errorf(1, "shf_getse: flags %x", shf
->flags
);
560 --bsize
; /* save room for null */
562 if (shf
->rnleft
== 0) {
563 if (shf_fillbuf(shf
) == EOF
)
565 if (shf
->rnleft
== 0) {
567 return buf
== orig_buf
? NULL
: buf
;
570 end
= (unsigned char *) memchr((char *) shf
->rp
, '\n',
572 ncopy
= end
? end
- shf
->rp
+ 1 : shf
->rnleft
;
575 memcpy(buf
, (char *) shf
->rp
, ncopy
);
577 shf
->rnleft
-= ncopy
;
581 if (end
&& buf
> orig_buf
+ 1 && buf
[-2] == '\r') {
588 } while (!end
&& bsize
);
593 /* Returns the char read. Returns EOF for error and end of file. */
598 if (!(shf
->flags
& SHF_RD
))
599 internal_errorf(1, "shf_getchar: flags %x", shf
->flags
);
601 if (shf
->rnleft
== 0 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
607 /* Put a character back in the input stream. Returns the character if
608 * successful, EOF if there is no room.
615 if (!(shf
->flags
& SHF_RD
))
616 internal_errorf(1, "shf_ungetc: flags %x", shf
->flags
);
618 if ((shf
->flags
& SHF_ERROR
) || c
== EOF
619 || (shf
->rp
== shf
->buf
&& shf
->rnleft
))
622 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
625 if (shf
->rp
== shf
->buf
)
626 shf
->rp
= shf
->buf
+ shf
->rbsize
;
627 if (shf
->flags
& SHF_STRING
) {
628 /* Can unget what was read, but not something different - we
629 * don't want to modify a string.
631 if (shf
->rp
[-1] != c
)
633 shf
->flags
&= ~SHF_EOF
;
638 shf
->flags
&= ~SHF_EOF
;
644 /* Write a character. Returns the character if successful, EOF if
645 * the char could not be written.
652 if (!(shf
->flags
& SHF_WR
))
653 internal_errorf(1, "shf_putchar: flags %x", shf
->flags
);
658 if (shf
->flags
& SHF_UNBUF
) {
663 internal_errorf(1, "shf_putchar: no fd");
664 if (shf
->flags
& SHF_ERROR
) {
668 while ((n
= write(shf
->fd
, &cc
, 1)) != 1)
671 && !(shf
->flags
& SHF_INTERRUPT
))
673 shf
->flags
|= SHF_ERROR
;
678 /* Flush deals with strings and sticky errors */
679 if (shf
->wnleft
== 0 && shf_emptybuf(shf
, EB_GROW
) == EOF
)
688 /* Write a string. Returns the length of the string if successful, EOF if
689 * the string could not be written.
699 return shf_write(s
, strlen(s
), shf
);
702 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
704 shf_write(buf
, nbytes
, shf
)
709 int orig_nbytes
= nbytes
;
713 if (!(shf
->flags
& SHF_WR
))
714 internal_errorf(1, "shf_write: flags %x", shf
->flags
);
717 internal_errorf(1, "shf_write: nbytes %d", nbytes
);
719 /* Don't buffer if buffer is empty and we're writting a large amount. */
720 if ((ncopy
= shf
->wnleft
)
721 && (shf
->wp
!= shf
->buf
|| nbytes
< shf
->wnleft
))
725 memcpy(shf
->wp
, buf
, ncopy
);
729 shf
->wnleft
-= ncopy
;
732 /* Flush deals with strings and sticky errors */
733 if (shf_emptybuf(shf
, EB_GROW
) == EOF
)
735 if (nbytes
> shf
->wbsize
) {
738 ncopy
-= nbytes
% shf
->wbsize
;
741 n
= write(shf
->fd
, buf
, ncopy
);
744 && !(shf
->flags
& SHF_INTERRUPT
))
746 shf
->flags
|= SHF_ERROR
;
749 /* Note: fwrite(3S) returns 0 for
750 * errors - this doesn't */
758 memcpy(shf
->wp
, buf
, nbytes
);
760 shf
->wnleft
-= nbytes
;
768 #ifdef HAVE_PROTOTYPES
769 shf_fprintf(struct shf
*shf
, const char *fmt
, ...)
771 shf_fprintf(shf
, fmt
, va_alist
)
780 SH_VA_START(args
, fmt
);
781 n
= shf_vfprintf(shf
, fmt
, args
);
788 #ifdef HAVE_PROTOTYPES
789 shf_snprintf(char *buf
, int bsize
, const char *fmt
, ...)
791 shf_snprintf(buf
, bsize
, fmt
, va_alist
)
802 if (!buf
|| bsize
<= 0)
803 internal_errorf(1, "shf_snprintf: buf %lx, bsize %d",
806 shf_sopen(buf
, bsize
, SHF_WR
, &shf
);
807 SH_VA_START(args
, fmt
);
808 n
= shf_vfprintf(&shf
, fmt
, args
);
810 shf_sclose(&shf
); /* null terminates */
815 #ifdef HAVE_PROTOTYPES
816 shf_smprintf(const char *fmt
, ...)
818 shf_smprintf(fmt
, va_alist
)
826 shf_sopen((char *) 0, 0, SHF_WR
|SHF_DYNAMIC
, &shf
);
827 SH_VA_START(args
, fmt
);
828 shf_vfprintf(&shf
, fmt
, args
);
830 return shf_sclose(&shf
); /* null terminates */
833 #undef FP /* if you want floating point stuff */
836 #define FPBUF_SIZE (DMAXEXP+16)/* this must be >
837 * MAX(DMAXEXP, log10(pow(2, DSIGNIF)))
838 * + ceil(log10(DMAXEXP)) + 8 (I think).
839 * Since this is hard to express as a
840 * constant, just use a large buffer.
844 * What kinda of machine we on? Hopefully the C compiler will optimize
847 * For shorts, we want sign extend for %d but not for %[oxu] - on 16 bit
848 * machines it don't matter. Assumes C compiler has converted shorts to
849 * ints before pushing them.
851 #define POP_INT(f, s, a) (((f) & FL_LONG) ? \
852 va_arg((a), unsigned long) \
854 (sizeof(int) < sizeof(long) ? \
856 (long) va_arg((a), int) \
858 va_arg((a), unsigned)) \
860 va_arg((a), unsigned)))
862 #define ABIGNUM 32000 /* big numer that will fit in a short */
863 #define LOG2_10 3.321928094887362347870319429 /* log base 2 of 10 */
865 #define FL_HASH 0x001 /* `#' seen */
866 #define FL_PLUS 0x002 /* `+' seen */
867 #define FL_RIGHT 0x004 /* `-' seen */
868 #define FL_BLANK 0x008 /* ` ' seen */
869 #define FL_SHORT 0x010 /* `h' seen */
870 #define FL_LONG 0x020 /* `l' seen */
871 #define FL_ZERO 0x040 /* `0' seen */
872 #define FL_DOT 0x080 /* '.' seen */
873 #define FL_UPPER 0x100 /* format character was uppercase */
874 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
886 return d
- modf(d
, &i
) + (d
< 0 ? -1 : 1);
891 shf_vfprintf(shf
, fmt
, args
)
897 int UNINITIALIZED(tmp
);
898 int field
, precision
;
902 /* %#o produces the longest output */
903 char numbuf
[(BITS(long) + 2) / 3 + 1];
904 /* this stuff for dealing with the buffer */
906 static char nulls
[] = "(null %s)";
908 /* should be in <math.h>
909 * extern double frexp();
916 char fpbuf
[FPBUF_SIZE
];
922 while ((c
= *fmt
++)) {
929 * This will accept flags/fields in any order - not
930 * just the order specified in printf(3), but this is
931 * the way _doprnt() seems to work (on bsd and sysV).
932 * The only restriction is that the format character must
935 flags
= field
= precision
= 0;
936 for ( ; (c
= *fmt
++) ; ) {
955 if (!(flags
& FL_DOT
))
965 tmp
= va_arg(args
, int);
968 else if ((field
= tmp
) < 0) {
984 while (c
= *fmt
++, digit(c
))
985 tmp
= tmp
* 10 + c
- '0';
987 if (tmp
< 0) /* overflow? */
1001 if (!c
) /* nasty format */
1004 if (c
>= 'A' && c
<= 'Z') {
1010 case 'p': /* pointer */
1011 flags
&= ~(FL_LONG
| FL_SHORT
);
1012 if (sizeof(char *) > sizeof(int))
1013 flags
|= FL_LONG
; /* hope it fits.. */
1021 s
= &numbuf
[sizeof(numbuf
)];
1022 lnum
= POP_INT(flags
, c
== 'd', args
);
1026 if (0 > (long) lnum
)
1027 lnum
= - (long) lnum
, tmp
= 1;
1034 *--s
= lnum
% 10 + '0';
1041 else if (flags
& FL_PLUS
)
1043 else if (flags
& FL_BLANK
)
1050 *--s
= (lnum
& 0x7) + '0';
1054 if ((flags
& FL_HASH
) && *s
!= '0')
1061 const char *digits
= (flags
& FL_UPPER
) ?
1063 : "0123456789abcdef";
1065 *--s
= digits
[lnum
& 0xf];
1069 if (flags
& FL_HASH
) {
1070 *--s
= (flags
& FL_UPPER
) ? 'X' : 'x';
1075 len
= &numbuf
[sizeof(numbuf
)] - s
;
1076 if (flags
& FL_DOT
) {
1077 if (precision
> len
) {
1081 precision
= len
; /* no loss */
1093 * This could probably be done better,
1094 * but it seems to work. Note that gcvt()
1095 * is not used, as you cannot tell it to
1096 * not strip the zeros.
1099 if (!(flags
& FL_DOT
))
1100 precision
= 6; /* default */
1102 * Assumes doubles are pushed on
1103 * the stack. If this is not so, then
1104 * FL_LONG/FL_SHORT should be checked.
1106 fpnum
= va_arg(args
, double);
1110 * This is the same as
1111 * expo = ceil(log10(fpnum))
1112 * but doesn't need -lm. This is an
1113 * approximation as expo is rounded up.
1115 (void) frexp(fpnum
, &expo
);
1116 expo
= my_ceil(expo
/ LOG2_10
);
1121 p
= ecvt(fpnum
, precision
+ 1 + expo
,
1124 if (decpt
< -4 || decpt
> precision
)
1128 if (decpt
> 0 && (precision
-= decpt
) < 0)
1133 else if (flags
& FL_PLUS
)
1135 else if (flags
& FL_BLANK
)
1142 /* Overflow check - should
1143 * never have this problem.
1146 &fpbuf
[sizeof(fpbuf
)]
1149 &fpbuf
[sizeof(fpbuf
)]
1151 (void) memcpy(s
, p
, decpt
);
1158 /* print the fraction? */
1159 if (precision
> 0) {
1161 /* Overflow check - should
1162 * never have this problem.
1164 if (precision
> &fpbuf
[sizeof(fpbuf
)]
1167 &fpbuf
[sizeof(fpbuf
)]
1169 for (tmp
= decpt
; tmp
++ < 0 &&
1170 precision
> 0 ; precision
--)
1173 if (precision
> tmp
)
1175 /* Overflow check - should
1176 * never have this problem.
1178 if (precision
> &fpbuf
[sizeof(fpbuf
)]
1181 &fpbuf
[sizeof(fpbuf
)]
1183 (void) memcpy(s
, p
, precision
);
1186 * `g' format strips trailing
1187 * zeros after the decimal.
1189 if (c
== 'g' && !(flags
& FL_HASH
)) {
1195 } else if (flags
& FL_HASH
)
1199 *s
++ = (flags
& FL_UPPER
) ? 'E' : 'e';
1206 p
= &numbuf
[sizeof(numbuf
)];
1207 for (tmp
= 0; tmp
< 2 || decpt
; tmp
++) {
1208 *--p
= '0' + decpt
% 10;
1211 tmp
= &numbuf
[sizeof(numbuf
)] - p
;
1212 (void) memcpy(s
, p
, tmp
);
1224 if (!(s
= va_arg(args
, char *)))
1231 numbuf
[0] = va_arg(args
, int);
1245 * At this point s should point to a string that is
1246 * to be formatted, and len should be the length of the
1249 if (!(flags
& FL_DOT
) || len
< precision
)
1251 if (field
> precision
) {
1253 if (!(flags
& FL_RIGHT
)) {
1255 /* skip past sign or 0x when padding with 0 */
1256 if ((flags
& FL_ZERO
) && (flags
& FL_NUMBER
)) {
1257 if (*s
== '+' || *s
== '-' || *s
==' ')
1263 } else if (*s
== '0') {
1267 if (--precision
> 0 &&
1278 c
= flags
& FL_ZERO
? '0' : ' ';
1281 for ( ; field
< 0 ; field
++)
1289 if (precision
> 0) {
1290 nwritten
+= precision
;
1291 for ( ; precision
-- > 0 ; s
++)
1296 for ( ; field
> 0 ; --field
)
1301 return shf_error(shf
) ? EOF
: nwritten
;