1 /* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
4 * Shell file I/O routines
9 #include "ksh_limval.h"
12 /* flags to shf_emptybuf() */
13 #define EB_READSW 0x01 /* about to switch to reading */
14 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
17 * Replacement stdio routines. Stdio is too flakey on too many machines
18 * to be useful when you have multiple processes using the same underlying
22 static int shf_fillbuf(struct shf
*);
23 static int shf_emptybuf(struct shf
*, int);
25 /* Open a file. First three args are for open(), last arg is flags for
26 * this package. Returns NULL if file could not be opened, or if a dup
30 shf_open(const char *name
, int oflags
, int mode
, int sflags
)
33 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
36 /* Done before open so if alloca fails, fd won't be lost. */
37 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
39 shf
->buf
= (unsigned char *) &shf
[1];
41 shf
->flags
= SHF_ALLOCS
;
42 /* Rest filled in by reopen. */
44 fd
= open(name
, oflags
, mode
);
46 afree(shf
, shf
->areap
);
49 if ((sflags
& SHF_MAPHI
) && fd
< FDBASE
) {
52 nfd
= fcntl(fd
, F_DUPFD
, FDBASE
);
55 afree(shf
, shf
->areap
);
60 sflags
&= ~SHF_ACCMODE
;
61 sflags
|= (oflags
& O_ACCMODE
) == O_RDONLY
? SHF_RD
:
62 ((oflags
& O_ACCMODE
) == O_WRONLY
? SHF_WR
: SHF_RDWR
);
64 return shf_reopen(fd
, sflags
, shf
);
67 /* Set up the shf structure for a file descriptor. Doesn't fail. */
69 shf_fdopen(int fd
, int sflags
, struct shf
*shf
)
71 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
73 /* use fcntl() to figure out correct read/write flags */
74 if (sflags
& SHF_GETFL
) {
75 int flags
= fcntl(fd
, F_GETFL
, 0);
78 /* will get an error on first read/write */
81 switch (flags
& O_ACCMODE
) {
95 if (!(sflags
& (SHF_RD
| SHF_WR
)))
96 internal_errorf(1, "shf_fdopen: missing read/write");
100 shf
->buf
= (unsigned char *) alloc(bsize
, ATEMP
);
101 sflags
|= SHF_ALLOCB
;
103 shf
->buf
= (unsigned char *) 0;
105 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
106 shf
->buf
= (unsigned char *) &shf
[1];
107 sflags
|= SHF_ALLOCS
;
111 shf
->rp
= shf
->wp
= shf
->buf
;
114 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
115 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
119 if (sflags
& SHF_CLEXEC
)
120 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
124 /* Set up an existing shf (and buffer) to use the given fd */
126 shf_reopen(int fd
, int sflags
, struct shf
*shf
)
128 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
130 /* use fcntl() to figure out correct read/write flags */
131 if (sflags
& SHF_GETFL
) {
132 int flags
= fcntl(fd
, F_GETFL
, 0);
135 /* will get an error on first read/write */
138 switch (flags
& O_ACCMODE
) {
152 if (!(sflags
& (SHF_RD
| SHF_WR
)))
153 internal_errorf(1, "shf_reopen: missing read/write");
154 if (!shf
|| !shf
->buf
|| shf
->bsize
< bsize
)
155 internal_errorf(1, "shf_reopen: bad shf/buf/bsize");
157 /* assumes shf->buf and shf->bsize already set up */
159 shf
->rp
= shf
->wp
= shf
->buf
;
162 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
163 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
164 shf
->flags
= (shf
->flags
& (SHF_ALLOCS
| SHF_ALLOCB
)) | sflags
;
166 if (sflags
& SHF_CLEXEC
)
167 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
171 /* Open a string for reading or writing. If reading, bsize is the number
172 * of bytes that can be read. If writing, bsize is the maximum number of
173 * bytes that can be written. If shf is not null, it is filled in and
174 * returned, if it is null, shf is allocated. If writing and buf is null
175 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
176 * used for the initial size). Doesn't fail.
177 * When writing, a byte is reserved for a trailing null - see shf_sclose().
180 shf_sopen(char *buf
, int bsize
, int sflags
, struct shf
*shf
)
182 /* can't have a read+write string */
183 if (!(sflags
& (SHF_RD
| SHF_WR
)) ||
184 (sflags
& (SHF_RD
| SHF_WR
)) == (SHF_RD
| SHF_WR
))
185 internal_errorf(1, "shf_sopen: flags 0x%x", sflags
);
188 shf
= (struct shf
*) alloc(sizeof(struct shf
), ATEMP
);
189 sflags
|= SHF_ALLOCS
;
192 if (!buf
&& (sflags
& SHF_WR
) && (sflags
& SHF_DYNAMIC
)) {
195 sflags
|= SHF_ALLOCB
;
196 buf
= alloc(bsize
, shf
->areap
);
199 shf
->buf
= shf
->rp
= shf
->wp
= (unsigned char *) buf
;
202 shf
->wnleft
= bsize
- 1; /* space for a '\0' */
204 shf
->flags
= sflags
| SHF_STRING
;
211 /* Flush and close file descriptor, free the shf structure */
213 shf_close(struct shf
*shf
)
218 ret
= shf_flush(shf
);
219 if (close(shf
->fd
) < 0)
222 if (shf
->flags
& SHF_ALLOCS
)
223 afree(shf
, shf
->areap
);
224 else if (shf
->flags
& SHF_ALLOCB
)
225 afree(shf
->buf
, shf
->areap
);
230 /* Flush and close file descriptor, don't free file structure */
232 shf_fdclose(struct shf
*shf
)
237 ret
= shf_flush(shf
);
238 if (close(shf
->fd
) < 0)
249 /* Close a string - if it was opened for writing, it is null terminated;
250 * returns a pointer to the string and frees shf if it was allocated
251 * (does not free string if it was allocated).
254 shf_sclose(struct shf
*shf
)
256 unsigned char *s
= shf
->buf
;
259 if (shf
->flags
& SHF_WR
) {
263 if (shf
->flags
& SHF_ALLOCS
)
264 afree(shf
, shf
->areap
);
268 /* Un-read what has been read but not examined, or write what has been
269 * buffered. Returns 0 for success, EOF for (write) error.
272 shf_flush(struct shf
*shf
)
274 if (shf
->flags
& SHF_STRING
)
275 return (shf
->flags
& SHF_WR
) ? EOF
: 0;
278 internal_errorf(1, "shf_flush: no fd");
280 if (shf
->flags
& SHF_ERROR
) {
285 if (shf
->flags
& SHF_READING
) {
286 shf
->flags
&= ~(SHF_EOF
| SHF_READING
);
287 if (shf
->rnleft
> 0) {
288 lseek(shf
->fd
, (off_t
) -shf
->rnleft
, SEEK_CUR
);
293 } else if (shf
->flags
& SHF_WRITING
)
294 return shf_emptybuf(shf
, 0);
299 /* Write out any buffered data. If currently reading, flushes the read
300 * buffer. Returns 0 for success, EOF for (write) error.
303 shf_emptybuf(struct shf
*shf
, int flags
)
307 if (!(shf
->flags
& SHF_STRING
) && shf
->fd
< 0)
308 internal_errorf(1, "shf_emptybuf: no fd");
310 if (shf
->flags
& SHF_ERROR
) {
315 if (shf
->flags
& SHF_READING
) {
316 if (flags
& EB_READSW
) /* doesn't happen */
318 ret
= shf_flush(shf
);
319 shf
->flags
&= ~SHF_READING
;
321 if (shf
->flags
& SHF_STRING
) {
324 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
325 * is set... (changing the shf pointer could cause problems)
327 if (!(flags
& EB_GROW
) || !(shf
->flags
& SHF_DYNAMIC
) ||
328 !(shf
->flags
& SHF_ALLOCB
))
330 /* allocate more space for buffer */
331 nbuf
= (unsigned char *) aresize(shf
->buf
, shf
->wbsize
* 2,
333 shf
->rp
= nbuf
+ (shf
->rp
- shf
->buf
);
334 shf
->wp
= nbuf
+ (shf
->wp
- shf
->buf
);
335 shf
->rbsize
+= shf
->wbsize
;
336 shf
->wnleft
+= shf
->wbsize
;
340 if (shf
->flags
& SHF_WRITING
) {
341 int ntowrite
= shf
->wp
- shf
->buf
;
342 unsigned char *buf
= shf
->buf
;
345 while (ntowrite
> 0) {
346 n
= write(shf
->fd
, buf
, ntowrite
);
348 if (errno
== EINTR
&&
349 !(shf
->flags
& SHF_INTERRUPT
))
351 shf
->flags
|= SHF_ERROR
;
354 if (buf
!= shf
->buf
) {
355 /* allow a second flush
357 memmove(shf
->buf
, buf
,
359 shf
->wp
= shf
->buf
+ ntowrite
;
366 if (flags
& EB_READSW
) {
369 shf
->flags
&= ~SHF_WRITING
;
374 shf
->wnleft
= shf
->wbsize
;
376 shf
->flags
|= SHF_WRITING
;
381 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
383 shf_fillbuf(struct shf
*shf
)
385 if (shf
->flags
& SHF_STRING
)
389 internal_errorf(1, "shf_fillbuf: no fd");
391 if (shf
->flags
& (SHF_EOF
| SHF_ERROR
)) {
392 if (shf
->flags
& SHF_ERROR
)
397 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
400 shf
->flags
|= SHF_READING
;
404 shf
->rnleft
= blocking_read(shf
->fd
, (char *) shf
->buf
,
406 if (shf
->rnleft
< 0 && errno
== EINTR
&&
407 !(shf
->flags
& SHF_INTERRUPT
))
411 if (shf
->rnleft
<= 0) {
412 if (shf
->rnleft
< 0) {
413 shf
->flags
|= SHF_ERROR
;
419 shf
->flags
|= SHF_EOF
;
424 /* Read a buffer from shf. Returns the number of bytes read into buf,
425 * if no bytes were read, returns 0 if end of file was seen, EOF if
426 * a read error occurred.
429 shf_read(char *buf
, int bsize
, struct shf
*shf
)
431 int orig_bsize
= bsize
;
434 if (!(shf
->flags
& SHF_RD
))
435 internal_errorf(1, "shf_read: flags %x", shf
->flags
);
438 internal_errorf(1, "shf_read: bsize %d", bsize
);
441 if (shf
->rnleft
== 0 &&
442 (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
447 memcpy(buf
, shf
->rp
, ncopy
);
451 shf
->rnleft
-= ncopy
;
453 /* Note: fread(3S) returns 0 for errors - this doesn't */
454 return orig_bsize
== bsize
? (shf_error(shf
) ? EOF
: 0) :
458 /* Read up to a newline or EOF. The newline is put in buf; buf is always
459 * null terminated. Returns NULL on read error or if nothing was read before
460 * end of file, returns a pointer to the null byte in buf otherwise.
463 shf_getse(char *buf
, int bsize
, struct shf
*shf
)
467 char *orig_buf
= buf
;
469 if (!(shf
->flags
& SHF_RD
))
470 internal_errorf(1, "shf_getse: flags %x", shf
->flags
);
475 --bsize
; /* save room for null */
477 if (shf
->rnleft
== 0) {
478 if (shf_fillbuf(shf
) == EOF
)
480 if (shf
->rnleft
== 0) {
482 return buf
== orig_buf
? NULL
: buf
;
485 end
= (unsigned char *) memchr((char *) shf
->rp
, '\n',
487 ncopy
= end
? end
- shf
->rp
+ 1 : shf
->rnleft
;
490 memcpy(buf
, (char *) shf
->rp
, ncopy
);
492 shf
->rnleft
-= ncopy
;
495 } while (!end
&& bsize
);
500 /* Returns the char read. Returns EOF for error and end of file. */
502 shf_getchar(struct shf
*shf
)
504 if (!(shf
->flags
& SHF_RD
))
505 internal_errorf(1, "shf_getchar: flags %x", shf
->flags
);
507 if (shf
->rnleft
== 0 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
513 /* Put a character back in the input stream. Returns the character if
514 * successful, EOF if there is no room.
517 shf_ungetc(int c
, struct shf
*shf
)
519 if (!(shf
->flags
& SHF_RD
))
520 internal_errorf(1, "shf_ungetc: flags %x", shf
->flags
);
522 if ((shf
->flags
& SHF_ERROR
) || c
== EOF
||
523 (shf
->rp
== shf
->buf
&& shf
->rnleft
))
526 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
529 if (shf
->rp
== shf
->buf
)
530 shf
->rp
= shf
->buf
+ shf
->rbsize
;
531 if (shf
->flags
& SHF_STRING
) {
532 /* Can unget what was read, but not something different - we
533 * don't want to modify a string.
535 if (shf
->rp
[-1] != c
)
537 shf
->flags
&= ~SHF_EOF
;
542 shf
->flags
&= ~SHF_EOF
;
548 /* Write a character. Returns the character if successful, EOF if
549 * the char could not be written.
552 shf_putchar(int c
, struct shf
*shf
)
554 if (!(shf
->flags
& SHF_WR
))
555 internal_errorf(1, "shf_putchar: flags %x", shf
->flags
);
560 if (shf
->flags
& SHF_UNBUF
) {
565 internal_errorf(1, "shf_putchar: no fd");
566 if (shf
->flags
& SHF_ERROR
) {
570 while ((n
= write(shf
->fd
, &cc
, 1)) != 1)
572 if (errno
== EINTR
&&
573 !(shf
->flags
& SHF_INTERRUPT
))
575 shf
->flags
|= SHF_ERROR
;
580 /* Flush deals with strings and sticky errors */
581 if (shf
->wnleft
== 0 && shf_emptybuf(shf
, EB_GROW
) == EOF
)
590 /* Write a string. Returns the length of the string if successful, EOF if
591 * the string could not be written.
594 shf_puts(const char *s
, struct shf
*shf
)
599 return shf_write(s
, strlen(s
), shf
);
602 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
604 shf_write(const char *buf
, int nbytes
, struct shf
*shf
)
606 int orig_nbytes
= nbytes
;
610 if (!(shf
->flags
& SHF_WR
))
611 internal_errorf(1, "shf_write: flags %x", shf
->flags
);
614 internal_errorf(1, "shf_write: nbytes %d", nbytes
);
616 /* Don't buffer if buffer is empty and we're writting a large amount. */
617 if ((ncopy
= shf
->wnleft
) &&
618 (shf
->wp
!= shf
->buf
|| nbytes
< shf
->wnleft
)) {
621 memcpy(shf
->wp
, buf
, ncopy
);
625 shf
->wnleft
-= ncopy
;
628 /* Flush deals with strings and sticky errors */
629 if (shf_emptybuf(shf
, EB_GROW
) == EOF
)
631 if (nbytes
> shf
->wbsize
) {
634 ncopy
-= nbytes
% shf
->wbsize
;
637 n
= write(shf
->fd
, buf
, ncopy
);
639 if (errno
== EINTR
&&
640 !(shf
->flags
& SHF_INTERRUPT
))
642 shf
->flags
|= SHF_ERROR
;
645 /* Note: fwrite(3S) returns 0 for
646 * errors - this doesn't */
654 memcpy(shf
->wp
, buf
, nbytes
);
656 shf
->wnleft
-= nbytes
;
664 shf_fprintf(struct shf
*shf
, const char *fmt
, ...)
670 n
= shf_vfprintf(shf
, fmt
, args
);
677 shf_snprintf(char *buf
, int bsize
, const char *fmt
, ...)
683 if (!buf
|| bsize
<= 0)
684 internal_errorf(1, "shf_snprintf: buf %lx, bsize %d",
687 shf_sopen(buf
, bsize
, SHF_WR
, &shf
);
689 n
= shf_vfprintf(&shf
, fmt
, args
);
691 shf_sclose(&shf
); /* null terminates */
696 shf_smprintf(const char *fmt
, ...)
701 shf_sopen((char *) 0, 0, SHF_WR
|SHF_DYNAMIC
, &shf
);
703 shf_vfprintf(&shf
, fmt
, args
);
705 return shf_sclose(&shf
); /* null terminates */
708 #undef FP /* if you want floating point stuff */
711 #define FPBUF_SIZE (DMAXEXP+16)/* this must be >
712 * MAX(DMAXEXP, log10(pow(2, DSIGNIF)))
713 * + ceil(log10(DMAXEXP)) + 8 (I think).
714 * Since this is hard to express as a
715 * constant, just use a large buffer.
719 * What kinda of machine we on? Hopefully the C compiler will optimize
722 * For shorts, we want sign extend for %d but not for %[oxu] - on 16 bit
723 * machines it don't matter. Assumes C compiler has converted shorts to
724 * ints before pushing them.
726 #define POP_INT(f, s, a) \
727 (((f) & FL_LLONG) ? va_arg((a), unsigned long long) : \
728 ((f) & FL_LONG) ? va_arg((a), unsigned long) : \
729 (sizeof(int) < sizeof(long) ? ((s) ? \
730 (long) va_arg((a), int) : va_arg((a), unsigned)) : \
731 va_arg((a), unsigned)))
733 #define ABIGNUM 32000 /* big numer that will fit in a short */
734 #define LOG2_10 3.321928094887362347870319429 /* log base 2 of 10 */
736 #define FL_HASH 0x001 /* `#' seen */
737 #define FL_PLUS 0x002 /* `+' seen */
738 #define FL_RIGHT 0x004 /* `-' seen */
739 #define FL_BLANK 0x008 /* ` ' seen */
740 #define FL_SHORT 0x010 /* `h' seen */
741 #define FL_LONG 0x020 /* `l' seen */
742 #define FL_LLONG 0x040 /* `ll' seen */
743 #define FL_ZERO 0x080 /* `0' seen */
744 #define FL_DOT 0x100 /* '.' seen */
745 #define FL_UPPER 0x200 /* format character was uppercase */
746 #define FL_NUMBER 0x400 /* a number was formated %[douxefg] */
757 return d
- modf(d
, &i
) + (d
< 0 ? -1 : 1);
762 shf_vfprintf(struct shf
*shf
, const char *fmt
, va_list args
)
766 int field
, precision
;
769 unsigned long long llnum
;
770 /* %#o produces the longest output */
771 char numbuf
[(BITS(long long) + 2) / 3 + 1];
772 /* this stuff for dealing with the buffer */
775 /* should be in <math.h>
776 * extern double frexp();
783 char fpbuf
[FPBUF_SIZE
];
789 while ((c
= *fmt
++)) {
796 * This will accept flags/fields in any order - not
797 * just the order specified in printf(3), but this is
798 * the way _doprnt() seems to work (on bsd and sysV).
799 * The only restriction is that the format character must
802 flags
= field
= precision
= 0;
803 for ( ; (c
= *fmt
++) ; ) {
822 if (!(flags
& FL_DOT
))
832 tmp
= va_arg(args
, int);
835 else if ((field
= tmp
) < 0) {
855 while (c
= *fmt
++, digit(c
))
856 tmp
= tmp
* 10 + c
- '0';
858 if (tmp
< 0) /* overflow? */
872 if (!c
) /* nasty format */
875 if (c
>= 'A' && c
<= 'Z') {
881 case 'p': /* pointer */
882 flags
&= ~(FL_LLONG
| FL_LONG
| FL_SHORT
);
883 if (sizeof(char *) > sizeof(int))
884 flags
|= FL_LONG
; /* hope it fits.. */
892 s
= &numbuf
[sizeof(numbuf
)];
893 llnum
= POP_INT(flags
, c
== 'd', args
);
897 if (0 > (long long) llnum
)
898 llnum
= - (long long) llnum
, tmp
= 1;
905 *--s
= llnum
% 10 + '0';
912 else if (flags
& FL_PLUS
)
914 else if (flags
& FL_BLANK
)
921 *--s
= (llnum
& 0x7) + '0';
925 if ((flags
& FL_HASH
) && *s
!= '0')
932 const char *digits
= (flags
& FL_UPPER
) ?
936 *--s
= digits
[llnum
& 0xf];
940 if (flags
& FL_HASH
) {
941 *--s
= (flags
& FL_UPPER
) ? 'X' : 'x';
946 len
= &numbuf
[sizeof(numbuf
)] - s
;
947 if (flags
& FL_DOT
) {
948 if (precision
> len
) {
952 precision
= len
; /* no loss */
964 * This could probably be done better,
965 * but it seems to work. Note that gcvt()
966 * is not used, as you cannot tell it to
967 * not strip the zeros.
970 if (!(flags
& FL_DOT
))
971 precision
= 6; /* default */
973 * Assumes doubles are pushed on
974 * the stack. If this is not so, then
975 * FL_LLONG/FL_LONG/FL_SHORT should be checked.
977 fpnum
= va_arg(args
, double);
981 * This is the same as
982 * expo = ceil(log10(fpnum))
983 * but doesn't need -lm. This is an
984 * approximation as expo is rounded up.
986 (void) frexp(fpnum
, &expo
);
987 expo
= my_ceil(expo
/ LOG2_10
);
992 p
= ecvt(fpnum
, precision
+ 1 + expo
,
995 if (decpt
< -4 || decpt
> precision
)
999 if (decpt
> 0 && (precision
-= decpt
) < 0)
1004 else if (flags
& FL_PLUS
)
1006 else if (flags
& FL_BLANK
)
1013 /* Overflow check - should
1014 * never have this problem.
1016 if (decpt
> &fpbuf
[sizeof(fpbuf
)] - s
- 8)
1017 decpt
= &fpbuf
[sizeof(fpbuf
)] - s
- 8;
1018 (void) memcpy(s
, p
, decpt
);
1025 /* print the fraction? */
1026 if (precision
> 0) {
1028 /* Overflow check - should
1029 * never have this problem.
1031 if (precision
> &fpbuf
[sizeof(fpbuf
)] - s
- 7)
1032 precision
= &fpbuf
[sizeof(fpbuf
)] - s
- 7;
1033 for (tmp
= decpt
; tmp
++ < 0 &&
1034 precision
> 0 ; precision
--)
1037 if (precision
> tmp
)
1039 /* Overflow check - should
1040 * never have this problem.
1042 if (precision
> &fpbuf
[sizeof(fpbuf
)] - s
- 7)
1043 precision
= &fpbuf
[sizeof(fpbuf
)] - s
- 7;
1044 (void) memcpy(s
, p
, precision
);
1047 * `g' format strips trailing
1048 * zeros after the decimal.
1050 if (c
== 'g' && !(flags
& FL_HASH
)) {
1056 } else if (flags
& FL_HASH
)
1060 *s
++ = (flags
& FL_UPPER
) ? 'E' : 'e';
1067 p
= &numbuf
[sizeof(numbuf
)];
1068 for (tmp
= 0; tmp
< 2 || decpt
; tmp
++) {
1069 *--p
= '0' + decpt
% 10;
1072 tmp
= &numbuf
[sizeof(numbuf
)] - p
;
1073 (void) memcpy(s
, p
, tmp
);
1085 if (!(s
= va_arg(args
, char *)))
1092 numbuf
[0] = va_arg(args
, int);
1106 * At this point s should point to a string that is
1107 * to be formatted, and len should be the length of the
1110 if (!(flags
& FL_DOT
) || len
< precision
)
1112 if (field
> precision
) {
1114 if (!(flags
& FL_RIGHT
)) {
1116 /* skip past sign or 0x when padding with 0 */
1117 if ((flags
& FL_ZERO
) && (flags
& FL_NUMBER
)) {
1118 if (*s
== '+' || *s
== '-' || *s
==' ') {
1123 } else if (*s
== '0') {
1127 if (--precision
> 0 &&
1128 (*s
| 0x20) == 'x') {
1137 c
= flags
& FL_ZERO
? '0' : ' ';
1140 for ( ; field
< 0 ; field
++)
1148 if (precision
> 0) {
1149 nwritten
+= precision
;
1150 for ( ; precision
-- > 0 ; s
++)
1155 for ( ; field
> 0 ; --field
)
1160 return shf_error(shf
) ? EOF
: nwritten
;