2 * Shell file I/O routines
7 #include "ksh_limval.h"
10 /* flags to shf_emptybuf() */
11 #define EB_READSW 0x01 /* about to switch to reading */
12 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
15 * Replacement stdio routines. Stdio is too flakey on too many machines
16 * to be useful when you have multiple processes using the same underlying
20 static int shf_fillbuf
ARGS((struct shf
*shf
));
21 static int shf_emptybuf
ARGS((struct shf
*shf
, int flags
));
23 /* Open a file. First three args are for open(), last arg is flags for
24 * this package. Returns NULL if file could not be opened, or if a dup
28 shf_open(name
, oflags
, mode
, sflags
)
35 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
38 /* Done before open so if alloca fails, fd won't be lost. */
39 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
41 shf
->buf
= (unsigned char *) &shf
[1];
43 shf
->flags
= SHF_ALLOCS
;
44 /* Rest filled in by reopen. */
46 fd
= open(name
, oflags
, mode
);
48 afree(shf
, shf
->areap
);
51 if ((sflags
& SHF_MAPHI
) && fd
< FDBASE
) {
54 nfd
= ksh_dupbase(fd
, FDBASE
);
57 afree(shf
, shf
->areap
);
62 sflags
&= ~SHF_ACCMODE
;
63 sflags
|= (oflags
& O_ACCMODE
) == O_RDONLY
? SHF_RD
64 : ((oflags
& O_ACCMODE
) == O_WRONLY
? SHF_WR
67 return shf_reopen(fd
, sflags
, shf
);
70 /* Set up the shf structure for a file descriptor. Doesn't fail. */
72 shf_fdopen(fd
, sflags
, shf
)
77 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
79 /* use fcntl() to figure out correct read/write flags */
80 if (sflags
& SHF_GETFL
) {
81 int flags
= fcntl(fd
, F_GETFL
, 0);
84 /* will get an error on first read/write */
87 switch (flags
& O_ACCMODE
) {
88 case O_RDONLY
: sflags
|= SHF_RD
; break;
89 case O_WRONLY
: sflags
|= SHF_WR
; break;
90 case O_RDWR
: sflags
|= SHF_RDWR
; break;
94 if (!(sflags
& (SHF_RD
| SHF_WR
)))
95 internal_errorf(1, "shf_fdopen: missing read/write");
99 shf
->buf
= (unsigned char *) alloc(bsize
, ATEMP
);
100 sflags
|= SHF_ALLOCB
;
102 shf
->buf
= (unsigned char *) 0;
104 shf
= (struct shf
*) alloc(sizeof(struct shf
) + bsize
, ATEMP
);
105 shf
->buf
= (unsigned char *) &shf
[1];
106 sflags
|= SHF_ALLOCS
;
110 shf
->rp
= shf
->wp
= shf
->buf
;
113 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
114 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
118 if (sflags
& SHF_CLEXEC
)
123 /* Set up an existing shf (and buffer) to use the given fd */
125 shf_reopen(fd
, sflags
, shf
)
130 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
132 /* use fcntl() to figure out correct read/write flags */
133 if (sflags
& SHF_GETFL
) {
134 int flags
= fcntl(fd
, F_GETFL
, 0);
137 /* will get an error on first read/write */
140 switch (flags
& O_ACCMODE
) {
141 case O_RDONLY
: sflags
|= SHF_RD
; break;
142 case O_WRONLY
: sflags
|= SHF_WR
; break;
143 case O_RDWR
: sflags
|= SHF_RDWR
; break;
147 if (!(sflags
& (SHF_RD
| SHF_WR
)))
148 internal_errorf(1, "shf_reopen: missing read/write");
149 if (!shf
|| !shf
->buf
|| shf
->bsize
< bsize
)
150 internal_errorf(1, "shf_reopen: bad shf/buf/bsize");
152 /* assumes shf->buf and shf->bsize already set up */
154 shf
->rp
= shf
->wp
= shf
->buf
;
157 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
158 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
159 shf
->flags
= (shf
->flags
& (SHF_ALLOCS
| SHF_ALLOCB
)) | sflags
;
161 if (sflags
& SHF_CLEXEC
)
166 /* Open a string for reading or writing. If reading, bsize is the number
167 * of bytes that can be read. If writing, bsize is the maximum number of
168 * bytes that can be written. If shf is not null, it is filled in and
169 * returned, if it is null, shf is allocated. If writing and buf is null
170 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
171 * used for the initial size). Doesn't fail.
172 * When writing, a byte is reserved for a trailing null - see shf_sclose().
175 shf_sopen(buf
, bsize
, sflags
, shf
)
181 /* can't have a read+write string */
182 if (!(sflags
& (SHF_RD
| SHF_WR
))
183 || (sflags
& (SHF_RD
| SHF_WR
)) == (SHF_RD
| SHF_WR
))
184 internal_errorf(1, "shf_sopen: flags 0x%x", sflags
);
187 shf
= (struct shf
*) alloc(sizeof(struct shf
), ATEMP
);
188 sflags
|= SHF_ALLOCS
;
191 if (!buf
&& (sflags
& SHF_WR
) && (sflags
& SHF_DYNAMIC
)) {
194 sflags
|= SHF_ALLOCB
;
195 buf
= alloc(bsize
, shf
->areap
);
198 shf
->buf
= shf
->rp
= shf
->wp
= (unsigned char *) buf
;
201 shf
->wnleft
= bsize
- 1; /* space for a '\0' */
203 shf
->flags
= sflags
| SHF_STRING
;
210 /* Flush and close file descriptor, free the shf structure */
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 */
238 ret
= shf_flush(shf
);
239 if (close(shf
->fd
) < 0)
250 /* Close a string - if it was opened for writing, it is null terminated;
251 * returns a pointer to the string and frees shf if it was allocated
252 * (does not free string if it was allocated).
258 unsigned char *s
= shf
->buf
;
261 if (shf
->flags
& SHF_WR
) {
265 if (shf
->flags
& SHF_ALLOCS
)
266 afree(shf
, shf
->areap
);
270 /* Flush and free file structure, don't close file descriptor */
278 ret
= shf_flush(shf
);
279 if (shf
->flags
& SHF_ALLOCS
)
280 afree(shf
, shf
->areap
);
281 else if (shf
->flags
& SHF_ALLOCB
)
282 afree(shf
->buf
, shf
->areap
);
287 /* Un-read what has been read but not examined, or write what has been
288 * buffered. Returns 0 for success, EOF for (write) error.
294 if (shf
->flags
& SHF_STRING
)
295 return (shf
->flags
& SHF_WR
) ? EOF
: 0;
298 internal_errorf(1, "shf_flush: no fd");
300 if (shf
->flags
& SHF_ERROR
) {
305 if (shf
->flags
& SHF_READING
) {
306 shf
->flags
&= ~(SHF_EOF
| SHF_READING
);
307 if (shf
->rnleft
> 0) {
308 lseek(shf
->fd
, (off_t
) -shf
->rnleft
, 1);
313 } else if (shf
->flags
& SHF_WRITING
)
314 return shf_emptybuf(shf
, 0);
319 /* Write out any buffered data. If currently reading, flushes the read
320 * buffer. Returns 0 for success, EOF for (write) error.
323 shf_emptybuf(shf
, flags
)
329 if (!(shf
->flags
& SHF_STRING
) && shf
->fd
< 0)
330 internal_errorf(1, "shf_emptybuf: no fd");
332 if (shf
->flags
& SHF_ERROR
) {
337 if (shf
->flags
& SHF_READING
) {
338 if (flags
& EB_READSW
) /* doesn't happen */
340 ret
= shf_flush(shf
);
341 shf
->flags
&= ~SHF_READING
;
343 if (shf
->flags
& SHF_STRING
) {
346 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
347 * is set... (changing the shf pointer could cause problems)
349 if (!(flags
& EB_GROW
) || !(shf
->flags
& SHF_DYNAMIC
)
350 || !(shf
->flags
& SHF_ALLOCB
))
352 /* allocate more space for buffer */
353 nbuf
= (unsigned char *) aresize(shf
->buf
, shf
->wbsize
* 2,
355 shf
->rp
= nbuf
+ (shf
->rp
- shf
->buf
);
356 shf
->wp
= nbuf
+ (shf
->wp
- shf
->buf
);
357 shf
->rbsize
+= shf
->wbsize
;
358 shf
->wnleft
+= shf
->wbsize
;
362 if (shf
->flags
& SHF_WRITING
) {
363 int ntowrite
= shf
->wp
- shf
->buf
;
364 unsigned char *buf
= shf
->buf
;
367 while (ntowrite
> 0) {
368 n
= write(shf
->fd
, buf
, ntowrite
);
371 && !(shf
->flags
& SHF_INTERRUPT
))
373 shf
->flags
|= SHF_ERROR
;
376 if (buf
!= shf
->buf
) {
377 /* allow a second flush
379 memmove(shf
->buf
, buf
,
381 shf
->wp
= shf
->buf
+ ntowrite
;
388 if (flags
& EB_READSW
) {
391 shf
->flags
&= ~SHF_WRITING
;
396 shf
->wnleft
= shf
->wbsize
;
398 shf
->flags
|= SHF_WRITING
;
403 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
408 if (shf
->flags
& SHF_STRING
)
412 internal_errorf(1, "shf_fillbuf: no fd");
414 if (shf
->flags
& (SHF_EOF
| SHF_ERROR
)) {
415 if (shf
->flags
& SHF_ERROR
)
420 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
423 shf
->flags
|= SHF_READING
;
427 shf
->rnleft
= blocking_read(shf
->fd
, (char *) shf
->buf
,
429 if (shf
->rnleft
< 0 && errno
== EINTR
430 && !(shf
->flags
& SHF_INTERRUPT
))
434 if (shf
->rnleft
<= 0) {
435 if (shf
->rnleft
< 0) {
436 shf
->flags
|= SHF_ERROR
;
442 shf
->flags
|= SHF_EOF
;
447 /* Seek to a new position in the file. If writing, flushes the buffer
448 * first. If reading, optimizes small relative seeks that stay inside the
449 * buffer. Returns 0 for success, EOF otherwise.
452 shf_seek(shf
, where
, from
)
462 if (shf
->flags
& SHF_ERROR
) {
467 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
470 if (shf
->flags
& SHF_READING
) {
471 if (from
== SEEK_CUR
&&
473 -where
>= shf
->rbsize
- shf
->rnleft
:
474 where
< shf
->rnleft
)) {
475 shf
->rnleft
-= where
;
483 shf
->flags
&= ~(SHF_EOF
| SHF_READING
| SHF_WRITING
);
485 if (lseek(shf
->fd
, where
, from
) < 0) {
487 shf
->flags
|= SHF_ERROR
;
495 /* Read a buffer from shf. Returns the number of bytes read into buf,
496 * if no bytes were read, returns 0 if end of file was seen, EOF if
497 * a read error occurred.
500 shf_read(buf
, bsize
, shf
)
505 int orig_bsize
= bsize
;
508 if (!(shf
->flags
& SHF_RD
))
509 internal_errorf(1, "shf_read: flags %x", shf
->flags
);
512 internal_errorf(1, "shf_read: bsize %d", bsize
);
516 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
521 memcpy(buf
, shf
->rp
, ncopy
);
525 shf
->rnleft
-= ncopy
;
527 /* Note: fread(3S) returns 0 for errors - this doesn't */
528 return orig_bsize
== bsize
? (shf_error(shf
) ? EOF
: 0)
529 : orig_bsize
- bsize
;
532 /* Read up to a newline or EOF. The newline is put in buf; buf is always
533 * null terminated. Returns NULL on read error or if nothing was read before
534 * end of file, returns a pointer to the null byte in buf otherwise.
537 shf_getse(buf
, bsize
, shf
)
544 char *orig_buf
= buf
;
546 if (!(shf
->flags
& SHF_RD
))
547 internal_errorf(1, "shf_getse: flags %x", shf
->flags
);
552 --bsize
; /* save room for null */
554 if (shf
->rnleft
== 0) {
555 if (shf_fillbuf(shf
) == EOF
)
557 if (shf
->rnleft
== 0) {
559 return buf
== orig_buf
? NULL
: buf
;
562 end
= (unsigned char *) memchr((char *) shf
->rp
, '\n',
564 ncopy
= end
? end
- shf
->rp
+ 1 : shf
->rnleft
;
567 memcpy(buf
, (char *) shf
->rp
, ncopy
);
569 shf
->rnleft
-= ncopy
;
573 if (end
&& buf
> orig_buf
+ 1 && buf
[-2] == '\r') {
580 } while (!end
&& bsize
);
585 /* Returns the char read. Returns EOF for error and end of file. */
590 if (!(shf
->flags
& SHF_RD
))
591 internal_errorf(1, "shf_getchar: flags %x", shf
->flags
);
593 if (shf
->rnleft
== 0 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
599 /* Put a character back in the input stream. Returns the character if
600 * successful, EOF if there is no room.
607 if (!(shf
->flags
& SHF_RD
))
608 internal_errorf(1, "shf_ungetc: flags %x", shf
->flags
);
610 if ((shf
->flags
& SHF_ERROR
) || c
== EOF
611 || (shf
->rp
== shf
->buf
&& shf
->rnleft
))
614 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
617 if (shf
->rp
== shf
->buf
)
618 shf
->rp
= shf
->buf
+ shf
->rbsize
;
619 if (shf
->flags
& SHF_STRING
) {
620 /* Can unget what was read, but not something different - we
621 * don't want to modify a string.
623 if (shf
->rp
[-1] != c
)
625 shf
->flags
&= ~SHF_EOF
;
630 shf
->flags
&= ~SHF_EOF
;
636 /* Write a character. Returns the character if successful, EOF if
637 * the char could not be written.
644 if (!(shf
->flags
& SHF_WR
))
645 internal_errorf(1, "shf_putchar: flags %x", shf
->flags
);
650 if (shf
->flags
& SHF_UNBUF
) {
655 internal_errorf(1, "shf_putchar: no fd");
656 if (shf
->flags
& SHF_ERROR
) {
660 while ((n
= write(shf
->fd
, &cc
, 1)) != 1)
663 && !(shf
->flags
& SHF_INTERRUPT
))
665 shf
->flags
|= SHF_ERROR
;
670 /* Flush deals with strings and sticky errors */
671 if (shf
->wnleft
== 0 && shf_emptybuf(shf
, EB_GROW
) == EOF
)
680 /* Write a string. Returns the length of the string if successful, EOF if
681 * the string could not be written.
691 return shf_write(s
, strlen(s
), shf
);
694 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
696 shf_write(buf
, nbytes
, shf
)
701 int orig_nbytes
= nbytes
;
705 if (!(shf
->flags
& SHF_WR
))
706 internal_errorf(1, "shf_write: flags %x", shf
->flags
);
709 internal_errorf(1, "shf_write: nbytes %d", nbytes
);
711 /* Don't buffer if buffer is empty and we're writting a large amount. */
712 if ((ncopy
= shf
->wnleft
)
713 && (shf
->wp
!= shf
->buf
|| nbytes
< shf
->wnleft
))
717 memcpy(shf
->wp
, buf
, ncopy
);
721 shf
->wnleft
-= ncopy
;
724 /* Flush deals with strings and sticky errors */
725 if (shf_emptybuf(shf
, EB_GROW
) == EOF
)
727 if (nbytes
> shf
->wbsize
) {
730 ncopy
-= nbytes
% shf
->wbsize
;
733 n
= write(shf
->fd
, buf
, ncopy
);
736 && !(shf
->flags
& SHF_INTERRUPT
))
738 shf
->flags
|= SHF_ERROR
;
741 /* Note: fwrite(3S) returns 0 for
742 * errors - this doesn't */
750 memcpy(shf
->wp
, buf
, nbytes
);
752 shf
->wnleft
-= nbytes
;
760 shf_fprintf(struct shf
*shf
, const char *fmt
, ...)
765 SH_VA_START(args
, fmt
);
766 n
= shf_vfprintf(shf
, fmt
, args
);
772 #ifdef SILLY_FEATURES
774 shf_smprintf(const char *fmt
, ...)
779 shf_sopen((char *) 0, 0, SHF_WR
|SHF_DYNAMIC
, &shf
);
780 SH_VA_START(args
, fmt
);
781 shf_vfprintf(&shf
, fmt
, args
);
783 return shf_sclose(&shf
); /* null terminates */
785 #endif /* SILLY_FEATURES */
787 #undef FP /* if you want floating point stuff */
790 #define FPBUF_SIZE (DMAXEXP+16)/* this must be >
791 * MAX(DMAXEXP, log10(pow(2, DSIGNIF)))
792 * + ceil(log10(DMAXEXP)) + 8 (I think).
793 * Since this is hard to express as a
794 * constant, just use a large buffer.
798 * What kinda of machine we on? Hopefully the C compiler will optimize
801 * For shorts, we want sign extend for %d but not for %[oxu] - on 16 bit
802 * machines it don't matter. Assmumes C compiler has converted shorts to
803 * ints before pushing them.
805 #define POP_INT(f, s, a) (((f) & FL_LONG) ? \
806 va_arg((a), unsigned long) \
808 (sizeof(int) < sizeof(long) ? \
810 (long) va_arg((a), int) \
812 va_arg((a), unsigned)) \
814 va_arg((a), unsigned)))
816 #define ABIGNUM 32000 /* big numer that will fit in a short */
817 #define LOG2_10 3.321928094887362347870319429 /* log base 2 of 10 */
819 #define FL_HASH 0x001 /* `#' seen */
820 #define FL_PLUS 0x002 /* `+' seen */
821 #define FL_RIGHT 0x004 /* `-' seen */
822 #define FL_BLANK 0x008 /* ` ' seen */
823 #define FL_SHORT 0x010 /* `h' seen */
824 #define FL_LONG 0x020 /* `l' seen */
825 #define FL_ZERO 0x040 /* `0' seen */
826 #define FL_DOT 0x080 /* '.' seen */
827 #define FL_UPPER 0x100 /* format character was uppercase */
828 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
840 return d
- modf(d
, &i
) + (d
< 0 ? -1 : 1);
846 shf_vfprintf(shf
, fmt
, args
)
852 int UNINITIALIZED(tmp
);
853 int field
, precision
;
857 /* %#o produces the longest output */
858 char numbuf
[(BITS(long) + 2) / 3 + 1];
859 /* this stuff for dealing with the buffer */
862 /* should be in <math.h>
863 * extern double frexp();
870 char fpbuf
[FPBUF_SIZE
];
876 while ((c
= *fmt
++)) {
883 * This will accept flags/fields in any order - not
884 * just the order specified in printf(3), but this is
885 * the way _doprnt() seems to work (on bsd and sysV).
886 * The only resriction is that the format character must
889 flags
= field
= precision
= 0;
890 for ( ; (c
= *fmt
++) ; ) {
909 if (!(flags
& FL_DOT
))
919 tmp
= va_arg(args
, int);
922 else if ((field
= tmp
) < 0) {
938 while (c
= *fmt
++, isdigit(c
))
939 tmp
= tmp
* 10 + c
- '0';
941 if (tmp
< 0) /* overflow? */
955 if (!c
) /* nasty format */
958 if (c
>= 'A' && c
<= 'Z') {
964 case 'p': /* pointer */
965 flags
&= ~(FL_LONG
| FL_SHORT
);
966 if (sizeof(char *) > sizeof(int))
967 flags
|= FL_LONG
; /* hope it fits.. */
975 s
= &numbuf
[sizeof(numbuf
)];
976 lnum
= POP_INT(flags
, c
== 'd', args
);
981 lnum
= - (long) lnum
, tmp
= 1;
988 *--s
= lnum
% 10 + '0';
995 else if (flags
& FL_PLUS
)
997 else if (flags
& FL_BLANK
)
1004 *--s
= (lnum
& 0x7) + '0';
1008 if ((flags
& FL_HASH
) && *s
!= '0')
1015 const char *digits
= (flags
& FL_UPPER
) ?
1017 : "0123456789abcdef";
1019 *--s
= digits
[lnum
& 0xf];
1023 if (flags
& FL_HASH
) {
1024 *--s
= (flags
& FL_UPPER
) ? 'X' : 'x';
1029 len
= &numbuf
[sizeof(numbuf
)] - s
;
1030 if (flags
& FL_DOT
) {
1031 if (precision
> len
) {
1035 precision
= len
; /* no loss */
1047 * This could proabably be done better,
1048 * but it seems to work. Note that gcvt()
1049 * is not used, as you cannot tell it to
1050 * not strip the zeros.
1053 if (!(flags
& FL_DOT
))
1054 precision
= 6; /* default */
1056 * Assumes doubles are pushed on
1057 * the stack. If this is not so, then
1058 * FL_LONG/FL_SHORT should be checked.
1060 fpnum
= va_arg(args
, double);
1064 * This is the same as
1065 * expo = ceil(log10(fpnum))
1066 * but doesn't need -lm. This is an
1067 * aproximation as expo is rounded up.
1069 (void) frexp(fpnum
, &expo
);
1070 expo
= my_ceil(expo
/ LOG2_10
);
1075 p
= ecvt(fpnum
, precision
+ 1 + expo
,
1078 if (decpt
< -4 || decpt
> precision
)
1082 if (decpt
> 0 && (precision
-= decpt
) < 0)
1087 else if (flags
& FL_PLUS
)
1089 else if (flags
& FL_BLANK
)
1096 /* Overflow check - should
1097 * never have this problem.
1100 &fpbuf
[sizeof(fpbuf
)]
1103 &fpbuf
[sizeof(fpbuf
)]
1105 (void) memcpy(s
, p
, decpt
);
1112 /* print the fraction? */
1113 if (precision
> 0) {
1115 /* Overflow check - should
1116 * never have this problem.
1118 if (precision
> &fpbuf
[sizeof(fpbuf
)]
1121 &fpbuf
[sizeof(fpbuf
)]
1123 for (tmp
= decpt
; tmp
++ < 0 &&
1124 precision
> 0 ; precision
--)
1127 if (precision
> tmp
)
1129 /* Overflow check - should
1130 * never have this problem.
1132 if (precision
> &fpbuf
[sizeof(fpbuf
)]
1135 &fpbuf
[sizeof(fpbuf
)]
1137 (void) memcpy(s
, p
, precision
);
1140 * `g' format strips trailing
1141 * zeros after the decimal.
1143 if (c
== 'g' && !(flags
& FL_HASH
)) {
1149 } else if (flags
& FL_HASH
)
1153 *s
++ = (flags
& FL_UPPER
) ? 'E' : 'e';
1160 p
= &numbuf
[sizeof(numbuf
)];
1161 for (tmp
= 0; tmp
< 2 || decpt
; tmp
++) {
1162 *--p
= '0' + decpt
% 10;
1165 tmp
= &numbuf
[sizeof(numbuf
)] - p
;
1166 (void) memcpy(s
, p
, tmp
);
1178 if (!(s
= va_arg(args
, char *)))
1185 numbuf
[0] = va_arg(args
, int);
1199 * At this point s should point to a string that is
1200 * to be formatted, and len should be the length of the
1203 if (!(flags
& FL_DOT
) || len
< precision
)
1205 if (field
> precision
) {
1207 if (!(flags
& FL_RIGHT
)) {
1209 /* skip past sign or 0x when padding with 0 */
1210 if ((flags
& FL_ZERO
) && (flags
& FL_NUMBER
)) {
1211 if (*s
== '+' || *s
== '-' || *s
==' ')
1217 } else if (*s
== '0') {
1221 if (--precision
> 0 &&
1232 c
= flags
& FL_ZERO
? '0' : ' ';
1235 for ( ; field
< 0 ; field
++)
1243 if (precision
> 0) {
1244 nwritten
+= precision
;
1245 for ( ; precision
-- > 0 ; s
++)
1250 for ( ; field
> 0 ; --field
)
1255 return shf_error(shf
) ? EOF
: nwritten
;
1257 #else /* WHAT_THE_FUCK */
1259 shf_vfprintf(struct shf
*shf
, const char *fmt
, va_list args
)
1261 int nwritten
= -1, bufsiz
= 2048;
1264 while(nwritten
== -1) {
1265 buf
= realloc(buf
, bufsiz
);
1266 nwritten
= vsnprintf(buf
, bufsiz
, fmt
, args
);
1270 return nwritten
? shf_puts(buf
, shf
) : -1;
1272 #endif /* WHAT_THE_FUCK */