sync
[bitrig.git] / bin / ksh / shf.c
blob0af129c4ae7c703b248ed0b5aeb89c53ee7313c9
1 /* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
3 /*
4 * Shell file I/O routines
5 */
7 #include "sh.h"
8 #include <sys/stat.h>
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
19 * file descriptors.
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
27 * fails.
29 struct shf *
30 shf_open(const char *name, int oflags, int mode, int sflags)
32 struct shf *shf;
33 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
34 int fd;
36 /* Done before open so if alloca fails, fd won't be lost. */
37 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
38 shf->areap = ATEMP;
39 shf->buf = (unsigned char *) &shf[1];
40 shf->bsize = bsize;
41 shf->flags = SHF_ALLOCS;
42 /* Rest filled in by reopen. */
44 fd = open(name, oflags, mode);
45 if (fd < 0) {
46 afree(shf, shf->areap);
47 return NULL;
49 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
50 int nfd;
52 nfd = fcntl(fd, F_DUPFD, FDBASE);
53 close(fd);
54 if (nfd < 0) {
55 afree(shf, shf->areap);
56 return NULL;
58 fd = nfd;
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. */
68 struct shf *
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);
77 if (flags < 0)
78 /* will get an error on first read/write */
79 sflags |= SHF_RDWR;
80 else {
81 switch (flags & O_ACCMODE) {
82 case O_RDONLY:
83 sflags |= SHF_RD;
84 break;
85 case O_WRONLY:
86 sflags |= SHF_WR;
87 break;
88 case O_RDWR:
89 sflags |= SHF_RDWR;
90 break;
95 if (!(sflags & (SHF_RD | SHF_WR)))
96 internal_errorf(1, "shf_fdopen: missing read/write");
98 if (shf) {
99 if (bsize) {
100 shf->buf = (unsigned char *) alloc(bsize, ATEMP);
101 sflags |= SHF_ALLOCB;
102 } else
103 shf->buf = (unsigned char *) 0;
104 } else {
105 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
106 shf->buf = (unsigned char *) &shf[1];
107 sflags |= SHF_ALLOCS;
109 shf->areap = ATEMP;
110 shf->fd = fd;
111 shf->rp = shf->wp = shf->buf;
112 shf->rnleft = 0;
113 shf->rbsize = bsize;
114 shf->wnleft = 0; /* force call to shf_emptybuf() */
115 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
116 shf->flags = sflags;
117 shf->errno_ = 0;
118 shf->bsize = bsize;
119 if (sflags & SHF_CLEXEC)
120 fcntl(fd, F_SETFD, FD_CLOEXEC);
121 return shf;
124 /* Set up an existing shf (and buffer) to use the given fd */
125 struct shf *
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);
134 if (flags < 0)
135 /* will get an error on first read/write */
136 sflags |= SHF_RDWR;
137 else {
138 switch (flags & O_ACCMODE) {
139 case O_RDONLY:
140 sflags |= SHF_RD;
141 break;
142 case O_WRONLY:
143 sflags |= SHF_WR;
144 break;
145 case O_RDWR:
146 sflags |= SHF_RDWR;
147 break;
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 */
158 shf->fd = fd;
159 shf->rp = shf->wp = shf->buf;
160 shf->rnleft = 0;
161 shf->rbsize = bsize;
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;
165 shf->errno_ = 0;
166 if (sflags & SHF_CLEXEC)
167 fcntl(fd, F_SETFD, FD_CLOEXEC);
168 return shf;
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().
179 struct shf *
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);
187 if (!shf) {
188 shf = (struct shf *) alloc(sizeof(struct shf), ATEMP);
189 sflags |= SHF_ALLOCS;
191 shf->areap = ATEMP;
192 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
193 if (bsize <= 0)
194 bsize = 64;
195 sflags |= SHF_ALLOCB;
196 buf = alloc(bsize, shf->areap);
198 shf->fd = -1;
199 shf->buf = shf->rp = shf->wp = (unsigned char *) buf;
200 shf->rnleft = bsize;
201 shf->rbsize = bsize;
202 shf->wnleft = bsize - 1; /* space for a '\0' */
203 shf->wbsize = bsize;
204 shf->flags = sflags | SHF_STRING;
205 shf->errno_ = 0;
206 shf->bsize = bsize;
208 return shf;
211 /* Flush and close file descriptor, free the shf structure */
213 shf_close(struct shf *shf)
215 int ret = 0;
217 if (shf->fd >= 0) {
218 ret = shf_flush(shf);
219 if (close(shf->fd) < 0)
220 ret = EOF;
222 if (shf->flags & SHF_ALLOCS)
223 afree(shf, shf->areap);
224 else if (shf->flags & SHF_ALLOCB)
225 afree(shf->buf, shf->areap);
227 return ret;
230 /* Flush and close file descriptor, don't free file structure */
232 shf_fdclose(struct shf *shf)
234 int ret = 0;
236 if (shf->fd >= 0) {
237 ret = shf_flush(shf);
238 if (close(shf->fd) < 0)
239 ret = EOF;
240 shf->rnleft = 0;
241 shf->rp = shf->buf;
242 shf->wnleft = 0;
243 shf->fd = -1;
246 return ret;
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).
253 char *
254 shf_sclose(struct shf *shf)
256 unsigned char *s = shf->buf;
258 /* null terminate */
259 if (shf->flags & SHF_WR) {
260 shf->wnleft++;
261 shf_putc('\0', shf);
263 if (shf->flags & SHF_ALLOCS)
264 afree(shf, shf->areap);
265 return (char *) s;
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;
277 if (shf->fd < 0)
278 internal_errorf(1, "shf_flush: no fd");
280 if (shf->flags & SHF_ERROR) {
281 errno = shf->errno_;
282 return EOF;
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);
289 shf->rnleft = 0;
290 shf->rp = shf->buf;
292 return 0;
293 } else if (shf->flags & SHF_WRITING)
294 return shf_emptybuf(shf, 0);
296 return 0;
299 /* Write out any buffered data. If currently reading, flushes the read
300 * buffer. Returns 0 for success, EOF for (write) error.
302 static int
303 shf_emptybuf(struct shf *shf, int flags)
305 int ret = 0;
307 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
308 internal_errorf(1, "shf_emptybuf: no fd");
310 if (shf->flags & SHF_ERROR) {
311 errno = shf->errno_;
312 return EOF;
315 if (shf->flags & SHF_READING) {
316 if (flags & EB_READSW) /* doesn't happen */
317 return 0;
318 ret = shf_flush(shf);
319 shf->flags &= ~SHF_READING;
321 if (shf->flags & SHF_STRING) {
322 unsigned char *nbuf;
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))
329 return EOF;
330 /* allocate more space for buffer */
331 nbuf = (unsigned char *) aresize(shf->buf, shf->wbsize * 2,
332 shf->areap);
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;
337 shf->wbsize *= 2;
338 shf->buf = nbuf;
339 } else {
340 if (shf->flags & SHF_WRITING) {
341 int ntowrite = shf->wp - shf->buf;
342 unsigned char *buf = shf->buf;
343 int n;
345 while (ntowrite > 0) {
346 n = write(shf->fd, buf, ntowrite);
347 if (n < 0) {
348 if (errno == EINTR &&
349 !(shf->flags & SHF_INTERRUPT))
350 continue;
351 shf->flags |= SHF_ERROR;
352 shf->errno_ = errno;
353 shf->wnleft = 0;
354 if (buf != shf->buf) {
355 /* allow a second flush
356 * to work */
357 memmove(shf->buf, buf,
358 ntowrite);
359 shf->wp = shf->buf + ntowrite;
361 return EOF;
363 buf += n;
364 ntowrite -= n;
366 if (flags & EB_READSW) {
367 shf->wp = shf->buf;
368 shf->wnleft = 0;
369 shf->flags &= ~SHF_WRITING;
370 return 0;
373 shf->wp = shf->buf;
374 shf->wnleft = shf->wbsize;
376 shf->flags |= SHF_WRITING;
378 return ret;
381 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
382 static int
383 shf_fillbuf(struct shf *shf)
385 if (shf->flags & SHF_STRING)
386 return 0;
388 if (shf->fd < 0)
389 internal_errorf(1, "shf_fillbuf: no fd");
391 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
392 if (shf->flags & SHF_ERROR)
393 errno = shf->errno_;
394 return EOF;
397 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
398 return EOF;
400 shf->flags |= SHF_READING;
402 shf->rp = shf->buf;
403 while (1) {
404 shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
405 shf->rbsize);
406 if (shf->rnleft < 0 && errno == EINTR &&
407 !(shf->flags & SHF_INTERRUPT))
408 continue;
409 break;
411 if (shf->rnleft <= 0) {
412 if (shf->rnleft < 0) {
413 shf->flags |= SHF_ERROR;
414 shf->errno_ = errno;
415 shf->rnleft = 0;
416 shf->rp = shf->buf;
417 return EOF;
419 shf->flags |= SHF_EOF;
421 return 0;
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;
432 int ncopy;
434 if (!(shf->flags & SHF_RD))
435 internal_errorf(1, "shf_read: flags %x", shf->flags);
437 if (bsize <= 0)
438 internal_errorf(1, "shf_read: bsize %d", bsize);
440 while (bsize > 0) {
441 if (shf->rnleft == 0 &&
442 (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
443 break;
444 ncopy = shf->rnleft;
445 if (ncopy > bsize)
446 ncopy = bsize;
447 memcpy(buf, shf->rp, ncopy);
448 buf += ncopy;
449 bsize -= ncopy;
450 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) :
455 orig_bsize - bsize;
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.
462 char *
463 shf_getse(char *buf, int bsize, struct shf *shf)
465 unsigned char *end;
466 int ncopy;
467 char *orig_buf = buf;
469 if (!(shf->flags & SHF_RD))
470 internal_errorf(1, "shf_getse: flags %x", shf->flags);
472 if (bsize <= 0)
473 return (char *) 0;
475 --bsize; /* save room for null */
476 do {
477 if (shf->rnleft == 0) {
478 if (shf_fillbuf(shf) == EOF)
479 return NULL;
480 if (shf->rnleft == 0) {
481 *buf = '\0';
482 return buf == orig_buf ? NULL : buf;
485 end = (unsigned char *) memchr((char *) shf->rp, '\n',
486 shf->rnleft);
487 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
488 if (ncopy > bsize)
489 ncopy = bsize;
490 memcpy(buf, (char *) shf->rp, ncopy);
491 shf->rp += ncopy;
492 shf->rnleft -= ncopy;
493 buf += ncopy;
494 bsize -= ncopy;
495 } while (!end && bsize);
496 *buf = '\0';
497 return buf;
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))
508 return EOF;
509 --shf->rnleft;
510 return *shf->rp++;
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))
524 return EOF;
526 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
527 return 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)
536 return EOF;
537 shf->flags &= ~SHF_EOF;
538 shf->rp--;
539 shf->rnleft++;
540 return c;
542 shf->flags &= ~SHF_EOF;
543 *--(shf->rp) = c;
544 shf->rnleft++;
545 return c;
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);
557 if (c == EOF)
558 return EOF;
560 if (shf->flags & SHF_UNBUF) {
561 char cc = c;
562 int n;
564 if (shf->fd < 0)
565 internal_errorf(1, "shf_putchar: no fd");
566 if (shf->flags & SHF_ERROR) {
567 errno = shf->errno_;
568 return EOF;
570 while ((n = write(shf->fd, &cc, 1)) != 1)
571 if (n < 0) {
572 if (errno == EINTR &&
573 !(shf->flags & SHF_INTERRUPT))
574 continue;
575 shf->flags |= SHF_ERROR;
576 shf->errno_ = errno;
577 return EOF;
579 } else {
580 /* Flush deals with strings and sticky errors */
581 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
582 return EOF;
583 shf->wnleft--;
584 *shf->wp++ = c;
587 return c;
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)
596 if (!s)
597 return EOF;
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;
607 int n;
608 int ncopy;
610 if (!(shf->flags & SHF_WR))
611 internal_errorf(1, "shf_write: flags %x", shf->flags);
613 if (nbytes < 0)
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)) {
619 if (ncopy > nbytes)
620 ncopy = nbytes;
621 memcpy(shf->wp, buf, ncopy);
622 nbytes -= ncopy;
623 buf += ncopy;
624 shf->wp += ncopy;
625 shf->wnleft -= ncopy;
627 if (nbytes > 0) {
628 /* Flush deals with strings and sticky errors */
629 if (shf_emptybuf(shf, EB_GROW) == EOF)
630 return EOF;
631 if (nbytes > shf->wbsize) {
632 ncopy = nbytes;
633 if (shf->wbsize)
634 ncopy -= nbytes % shf->wbsize;
635 nbytes -= ncopy;
636 while (ncopy > 0) {
637 n = write(shf->fd, buf, ncopy);
638 if (n < 0) {
639 if (errno == EINTR &&
640 !(shf->flags & SHF_INTERRUPT))
641 continue;
642 shf->flags |= SHF_ERROR;
643 shf->errno_ = errno;
644 shf->wnleft = 0;
645 /* Note: fwrite(3S) returns 0 for
646 * errors - this doesn't */
647 return EOF;
649 buf += n;
650 ncopy -= n;
653 if (nbytes > 0) {
654 memcpy(shf->wp, buf, nbytes);
655 shf->wp += nbytes;
656 shf->wnleft -= nbytes;
660 return orig_nbytes;
664 shf_fprintf(struct shf *shf, const char *fmt, ...)
666 va_list args;
667 int n;
669 va_start(args, fmt);
670 n = shf_vfprintf(shf, fmt, args);
671 va_end(args);
673 return n;
677 shf_snprintf(char *buf, int bsize, const char *fmt, ...)
679 struct shf shf;
680 va_list args;
681 int n;
683 if (!buf || bsize <= 0)
684 internal_errorf(1, "shf_snprintf: buf %lx, bsize %d",
685 (long) buf, bsize);
687 shf_sopen(buf, bsize, SHF_WR, &shf);
688 va_start(args, fmt);
689 n = shf_vfprintf(&shf, fmt, args);
690 va_end(args);
691 shf_sclose(&shf); /* null terminates */
692 return n;
695 char *
696 shf_smprintf(const char *fmt, ...)
698 struct shf shf;
699 va_list args;
701 shf_sopen((char *) 0, 0, SHF_WR|SHF_DYNAMIC, &shf);
702 va_start(args, fmt);
703 shf_vfprintf(&shf, fmt, args);
704 va_end(args);
705 return shf_sclose(&shf); /* null terminates */
708 #undef FP /* if you want floating point stuff */
710 #define BUF_SIZE 128
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
720 * this out...
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] */
749 #ifdef FP
750 #include <math.h>
752 static double
753 my_ceil(double d)
755 double i;
757 return d - modf(d, &i) + (d < 0 ? -1 : 1);
759 #endif /* FP */
762 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
764 char c, *s;
765 int tmp = 0;
766 int field, precision;
767 int len;
768 int flags;
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 */
773 int nwritten = 0;
774 #ifdef FP
775 /* should be in <math.h>
776 * extern double frexp();
778 extern char *ecvt();
780 double fpnum;
781 int expo, decpt;
782 char style;
783 char fpbuf[FPBUF_SIZE];
784 #endif /* FP */
786 if (!fmt)
787 return 0;
789 while ((c = *fmt++)) {
790 if (c != '%') {
791 shf_putc(c, shf);
792 nwritten++;
793 continue;
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
800 * come last :-).
802 flags = field = precision = 0;
803 for ( ; (c = *fmt++) ; ) {
804 switch (c) {
805 case '#':
806 flags |= FL_HASH;
807 continue;
809 case '+':
810 flags |= FL_PLUS;
811 continue;
813 case '-':
814 flags |= FL_RIGHT;
815 continue;
817 case ' ':
818 flags |= FL_BLANK;
819 continue;
821 case '0':
822 if (!(flags & FL_DOT))
823 flags |= FL_ZERO;
824 continue;
826 case '.':
827 flags |= FL_DOT;
828 precision = 0;
829 continue;
831 case '*':
832 tmp = va_arg(args, int);
833 if (flags & FL_DOT)
834 precision = tmp;
835 else if ((field = tmp) < 0) {
836 field = -field;
837 flags |= FL_RIGHT;
839 continue;
841 case 'l':
842 if (*fmt == 'l') {
843 fmt++;
844 flags |= FL_LLONG;
845 } else
846 flags |= FL_LONG;
847 continue;
849 case 'h':
850 flags |= FL_SHORT;
851 continue;
853 if (digit(c)) {
854 tmp = c - '0';
855 while (c = *fmt++, digit(c))
856 tmp = tmp * 10 + c - '0';
857 --fmt;
858 if (tmp < 0) /* overflow? */
859 tmp = 0;
860 if (flags & FL_DOT)
861 precision = tmp;
862 else
863 field = tmp;
864 continue;
866 break;
869 if (precision < 0)
870 precision = 0;
872 if (!c) /* nasty format */
873 break;
875 if (c >= 'A' && c <= 'Z') {
876 flags |= FL_UPPER;
877 c = c - 'A' + 'a';
880 switch (c) {
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.. */
885 /* aaahhh... */
886 case 'd':
887 case 'i':
888 case 'o':
889 case 'u':
890 case 'x':
891 flags |= FL_NUMBER;
892 s = &numbuf[sizeof(numbuf)];
893 llnum = POP_INT(flags, c == 'd', args);
894 switch (c) {
895 case 'd':
896 case 'i':
897 if (0 > (long long) llnum)
898 llnum = - (long long) llnum, tmp = 1;
899 else
900 tmp = 0;
901 /* aaahhhh..... */
903 case 'u':
904 do {
905 *--s = llnum % 10 + '0';
906 llnum /= 10;
907 } while (llnum);
909 if (c != 'u') {
910 if (tmp)
911 *--s = '-';
912 else if (flags & FL_PLUS)
913 *--s = '+';
914 else if (flags & FL_BLANK)
915 *--s = ' ';
917 break;
919 case 'o':
920 do {
921 *--s = (llnum & 0x7) + '0';
922 llnum >>= 3;
923 } while (llnum);
925 if ((flags & FL_HASH) && *s != '0')
926 *--s = '0';
927 break;
929 case 'p':
930 case 'x':
932 const char *digits = (flags & FL_UPPER) ?
933 "0123456789ABCDEF" :
934 "0123456789abcdef";
935 do {
936 *--s = digits[llnum & 0xf];
937 llnum >>= 4;
938 } while (llnum);
940 if (flags & FL_HASH) {
941 *--s = (flags & FL_UPPER) ? 'X' : 'x';
942 *--s = '0';
946 len = &numbuf[sizeof(numbuf)] - s;
947 if (flags & FL_DOT) {
948 if (precision > len) {
949 field = precision;
950 flags |= FL_ZERO;
951 } else
952 precision = len; /* no loss */
954 break;
956 #ifdef FP
957 case 'e':
958 case 'g':
959 case 'f':
961 char *p;
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.
969 flags |= FL_NUMBER;
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);
978 s = fpbuf;
979 style = c;
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);
989 if (expo < 0)
990 expo = 0;
992 p = ecvt(fpnum, precision + 1 + expo,
993 &decpt, &tmp);
994 if (c == 'g') {
995 if (decpt < -4 || decpt > precision)
996 style = 'e';
997 else
998 style = 'f';
999 if (decpt > 0 && (precision -= decpt) < 0)
1000 precision = 0;
1002 if (tmp)
1003 *s++ = '-';
1004 else if (flags & FL_PLUS)
1005 *s++ = '+';
1006 else if (flags & FL_BLANK)
1007 *s++ = ' ';
1009 if (style == 'e')
1010 *s++ = *p++;
1011 else {
1012 if (decpt > 0) {
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);
1019 s += decpt;
1020 p += decpt;
1021 } else
1022 *s++ = '0';
1025 /* print the fraction? */
1026 if (precision > 0) {
1027 *s++ = '.';
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--)
1035 *s++ = '0';
1036 tmp = strlen(p);
1037 if (precision > tmp)
1038 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);
1045 s += precision;
1047 * `g' format strips trailing
1048 * zeros after the decimal.
1050 if (c == 'g' && !(flags & FL_HASH)) {
1051 while (*--s == '0')
1053 if (*s != '.')
1054 s++;
1056 } else if (flags & FL_HASH)
1057 *s++ = '.';
1059 if (style == 'e') {
1060 *s++ = (flags & FL_UPPER) ? 'E' : 'e';
1061 if (--decpt >= 0)
1062 *s++ = '+';
1063 else {
1064 *s++ = '-';
1065 decpt = -decpt;
1067 p = &numbuf[sizeof(numbuf)];
1068 for (tmp = 0; tmp < 2 || decpt ; tmp++) {
1069 *--p = '0' + decpt % 10;
1070 decpt /= 10;
1072 tmp = &numbuf[sizeof(numbuf)] - p;
1073 (void) memcpy(s, p, tmp);
1074 s += tmp;
1077 len = s - fpbuf;
1078 s = fpbuf;
1079 precision = len;
1080 break;
1082 #endif /* FP */
1084 case 's':
1085 if (!(s = va_arg(args, char *)))
1086 s = "(null %s)";
1087 len = strlen(s);
1088 break;
1090 case 'c':
1091 flags &= ~FL_DOT;
1092 numbuf[0] = va_arg(args, int);
1093 s = numbuf;
1094 len = 1;
1095 break;
1097 case '%':
1098 default:
1099 numbuf[0] = c;
1100 s = numbuf;
1101 len = 1;
1102 break;
1106 * At this point s should point to a string that is
1107 * to be formatted, and len should be the length of the
1108 * string.
1110 if (!(flags & FL_DOT) || len < precision)
1111 precision = len;
1112 if (field > precision) {
1113 field -= precision;
1114 if (!(flags & FL_RIGHT)) {
1115 field = -field;
1116 /* skip past sign or 0x when padding with 0 */
1117 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1118 if (*s == '+' || *s == '-' || *s ==' ') {
1119 shf_putc(*s, shf);
1120 s++;
1121 precision--;
1122 nwritten++;
1123 } else if (*s == '0') {
1124 shf_putc(*s, shf);
1125 s++;
1126 nwritten++;
1127 if (--precision > 0 &&
1128 (*s | 0x20) == 'x') {
1129 shf_putc(*s, shf);
1130 s++;
1131 precision--;
1132 nwritten++;
1135 c = '0';
1136 } else
1137 c = flags & FL_ZERO ? '0' : ' ';
1138 if (field < 0) {
1139 nwritten += -field;
1140 for ( ; field < 0 ; field++)
1141 shf_putc(c, shf);
1143 } else
1144 c = ' ';
1145 } else
1146 field = 0;
1148 if (precision > 0) {
1149 nwritten += precision;
1150 for ( ; precision-- > 0 ; s++)
1151 shf_putc(*s, shf);
1153 if (field > 0) {
1154 nwritten += field;
1155 for ( ; field > 0 ; --field)
1156 shf_putc(c, shf);
1160 return shf_error(shf) ? EOF : nwritten;