changelog for 0.9.1
[posh.git] / shf.c
blob8c639b6d2aaae1167d8075a5131a7f054d7478ca
1 /*
2 * Shell file I/O routines
3 */
5 #include "sh.h"
6 #include "ksh_stat.h"
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
17 * file descriptors.
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
25 * fails.
27 struct shf *
28 shf_open(name, oflags, mode, sflags)
29 const char *name;
30 int oflags;
31 int mode;
32 int sflags;
34 struct shf *shf;
35 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
36 int fd;
38 /* Done before open so if alloca fails, fd won't be lost. */
39 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
40 shf->areap = ATEMP;
41 shf->buf = (unsigned char *) &shf[1];
42 shf->bsize = bsize;
43 shf->flags = SHF_ALLOCS;
44 /* Rest filled in by reopen. */
46 fd = open(name, oflags, mode);
47 if (fd < 0) {
48 afree(shf, shf->areap);
49 return NULL;
51 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
52 int nfd;
54 nfd = ksh_dupbase(fd, FDBASE);
55 close(fd);
56 if (nfd < 0) {
57 afree(shf, shf->areap);
58 return NULL;
60 fd = nfd;
62 sflags &= ~SHF_ACCMODE;
63 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD
64 : ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR
65 : SHF_RDWR);
67 return shf_reopen(fd, sflags, shf);
70 /* Set up the shf structure for a file descriptor. Doesn't fail. */
71 struct shf *
72 shf_fdopen(fd, sflags, shf)
73 int fd;
74 int sflags;
75 struct shf *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);
83 if (flags < 0)
84 /* will get an error on first read/write */
85 sflags |= SHF_RDWR;
86 else
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");
97 if (shf) {
98 if (bsize) {
99 shf->buf = (unsigned char *) alloc(bsize, ATEMP);
100 sflags |= SHF_ALLOCB;
101 } else
102 shf->buf = (unsigned char *) 0;
103 } else {
104 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
105 shf->buf = (unsigned char *) &shf[1];
106 sflags |= SHF_ALLOCS;
108 shf->areap = ATEMP;
109 shf->fd = fd;
110 shf->rp = shf->wp = shf->buf;
111 shf->rnleft = 0;
112 shf->rbsize = bsize;
113 shf->wnleft = 0; /* force call to shf_emptybuf() */
114 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
115 shf->flags = sflags;
116 shf->errno_ = 0;
117 shf->bsize = bsize;
118 if (sflags & SHF_CLEXEC)
119 fd_clexec(fd);
120 return shf;
123 /* Set up an existing shf (and buffer) to use the given fd */
124 struct shf *
125 shf_reopen(fd, sflags, shf)
126 int fd;
127 int sflags;
128 struct shf *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);
136 if (flags < 0)
137 /* will get an error on first read/write */
138 sflags |= SHF_RDWR;
139 else
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 */
153 shf->fd = fd;
154 shf->rp = shf->wp = shf->buf;
155 shf->rnleft = 0;
156 shf->rbsize = bsize;
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;
160 shf->errno_ = 0;
161 if (sflags & SHF_CLEXEC)
162 fd_clexec(fd);
163 return shf;
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().
174 struct shf *
175 shf_sopen(buf, bsize, sflags, shf)
176 char *buf;
177 int bsize;
178 int sflags;
179 struct shf *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);
186 if (!shf) {
187 shf = (struct shf *) alloc(sizeof(struct shf), ATEMP);
188 sflags |= SHF_ALLOCS;
190 shf->areap = ATEMP;
191 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
192 if (bsize <= 0)
193 bsize = 64;
194 sflags |= SHF_ALLOCB;
195 buf = alloc(bsize, shf->areap);
197 shf->fd = -1;
198 shf->buf = shf->rp = shf->wp = (unsigned char *) buf;
199 shf->rnleft = bsize;
200 shf->rbsize = bsize;
201 shf->wnleft = bsize - 1; /* space for a '\0' */
202 shf->wbsize = bsize;
203 shf->flags = sflags | SHF_STRING;
204 shf->errno_ = 0;
205 shf->bsize = bsize;
207 return shf;
210 /* Flush and close file descriptor, free the shf structure */
212 shf_close(shf)
213 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(shf)
233 struct shf *shf;
235 int ret = 0;
237 if (shf->fd >= 0) {
238 ret = shf_flush(shf);
239 if (close(shf->fd) < 0)
240 ret = EOF;
241 shf->rnleft = 0;
242 shf->rp = shf->buf;
243 shf->wnleft = 0;
244 shf->fd = -1;
247 return ret;
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).
254 char *
255 shf_sclose(shf)
256 struct shf *shf;
258 unsigned char *s = shf->buf;
260 /* null terminate */
261 if (shf->flags & SHF_WR) {
262 shf->wnleft++;
263 shf_putc('\0', shf);
265 if (shf->flags & SHF_ALLOCS)
266 afree(shf, shf->areap);
267 return (char *) s;
270 /* Flush and free file structure, don't close file descriptor */
272 shf_finish(shf)
273 struct shf *shf;
275 int ret = 0;
277 if (shf->fd >= 0)
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);
284 return ret;
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.
291 shf_flush(shf)
292 struct shf *shf;
294 if (shf->flags & SHF_STRING)
295 return (shf->flags & SHF_WR) ? EOF : 0;
297 if (shf->fd < 0)
298 internal_errorf(1, "shf_flush: no fd");
300 if (shf->flags & SHF_ERROR) {
301 errno = shf->errno_;
302 return EOF;
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);
309 shf->rnleft = 0;
310 shf->rp = shf->buf;
312 return 0;
313 } else if (shf->flags & SHF_WRITING)
314 return shf_emptybuf(shf, 0);
316 return 0;
319 /* Write out any buffered data. If currently reading, flushes the read
320 * buffer. Returns 0 for success, EOF for (write) error.
322 static int
323 shf_emptybuf(shf, flags)
324 struct shf *shf;
325 int flags;
327 int ret = 0;
329 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
330 internal_errorf(1, "shf_emptybuf: no fd");
332 if (shf->flags & SHF_ERROR) {
333 errno = shf->errno_;
334 return EOF;
337 if (shf->flags & SHF_READING) {
338 if (flags & EB_READSW) /* doesn't happen */
339 return 0;
340 ret = shf_flush(shf);
341 shf->flags &= ~SHF_READING;
343 if (shf->flags & SHF_STRING) {
344 unsigned char *nbuf;
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))
351 return EOF;
352 /* allocate more space for buffer */
353 nbuf = (unsigned char *) aresize(shf->buf, shf->wbsize * 2,
354 shf->areap);
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;
359 shf->wbsize *= 2;
360 shf->buf = nbuf;
361 } else {
362 if (shf->flags & SHF_WRITING) {
363 int ntowrite = shf->wp - shf->buf;
364 unsigned char *buf = shf->buf;
365 int n;
367 while (ntowrite > 0) {
368 n = write(shf->fd, buf, ntowrite);
369 if (n < 0) {
370 if (errno == EINTR
371 && !(shf->flags & SHF_INTERRUPT))
372 continue;
373 shf->flags |= SHF_ERROR;
374 shf->errno_ = errno;
375 shf->wnleft = 0;
376 if (buf != shf->buf) {
377 /* allow a second flush
378 * to work */
379 memmove(shf->buf, buf,
380 ntowrite);
381 shf->wp = shf->buf + ntowrite;
383 return EOF;
385 buf += n;
386 ntowrite -= n;
388 if (flags & EB_READSW) {
389 shf->wp = shf->buf;
390 shf->wnleft = 0;
391 shf->flags &= ~SHF_WRITING;
392 return 0;
395 shf->wp = shf->buf;
396 shf->wnleft = shf->wbsize;
398 shf->flags |= SHF_WRITING;
400 return ret;
403 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
404 static int
405 shf_fillbuf(shf)
406 struct shf *shf;
408 if (shf->flags & SHF_STRING)
409 return 0;
411 if (shf->fd < 0)
412 internal_errorf(1, "shf_fillbuf: no fd");
414 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
415 if (shf->flags & SHF_ERROR)
416 errno = shf->errno_;
417 return EOF;
420 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
421 return EOF;
423 shf->flags |= SHF_READING;
425 shf->rp = shf->buf;
426 while (1) {
427 shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
428 shf->rbsize);
429 if (shf->rnleft < 0 && errno == EINTR
430 && !(shf->flags & SHF_INTERRUPT))
431 continue;
432 break;
434 if (shf->rnleft <= 0) {
435 if (shf->rnleft < 0) {
436 shf->flags |= SHF_ERROR;
437 shf->errno_ = errno;
438 shf->rnleft = 0;
439 shf->rp = shf->buf;
440 return EOF;
442 shf->flags |= SHF_EOF;
444 return 0;
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)
453 struct shf *shf;
454 off_t where;
455 int from;
457 if (shf->fd < 0) {
458 errno = EINVAL;
459 return EOF;
462 if (shf->flags & SHF_ERROR) {
463 errno = shf->errno_;
464 return EOF;
467 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
468 return EOF;
470 if (shf->flags & SHF_READING) {
471 if (from == SEEK_CUR &&
472 (where < 0 ?
473 -where >= shf->rbsize - shf->rnleft :
474 where < shf->rnleft)) {
475 shf->rnleft -= where;
476 shf->rp += where;
477 return 0;
479 shf->rnleft = 0;
480 shf->rp = shf->buf;
483 shf->flags &= ~(SHF_EOF | SHF_READING | SHF_WRITING);
485 if (lseek(shf->fd, where, from) < 0) {
486 shf->errno_ = errno;
487 shf->flags |= SHF_ERROR;
488 return EOF;
491 return 0;
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)
501 char *buf;
502 int bsize;
503 struct shf *shf;
505 int orig_bsize = bsize;
506 int ncopy;
508 if (!(shf->flags & SHF_RD))
509 internal_errorf(1, "shf_read: flags %x", shf->flags);
511 if (bsize <= 0)
512 internal_errorf(1, "shf_read: bsize %d", bsize);
514 while (bsize > 0) {
515 if (shf->rnleft == 0
516 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
517 break;
518 ncopy = shf->rnleft;
519 if (ncopy > bsize)
520 ncopy = bsize;
521 memcpy(buf, shf->rp, ncopy);
522 buf += ncopy;
523 bsize -= ncopy;
524 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.
536 char *
537 shf_getse(buf, bsize, shf)
538 char *buf;
539 int bsize;
540 struct shf *shf;
542 unsigned char *end;
543 int ncopy;
544 char *orig_buf = buf;
546 if (!(shf->flags & SHF_RD))
547 internal_errorf(1, "shf_getse: flags %x", shf->flags);
549 if (bsize <= 0)
550 return (char *) 0;
552 --bsize; /* save room for null */
553 do {
554 if (shf->rnleft == 0) {
555 if (shf_fillbuf(shf) == EOF)
556 return NULL;
557 if (shf->rnleft == 0) {
558 *buf = '\0';
559 return buf == orig_buf ? NULL : buf;
562 end = (unsigned char *) memchr((char *) shf->rp, '\n',
563 shf->rnleft);
564 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
565 if (ncopy > bsize)
566 ncopy = bsize;
567 memcpy(buf, (char *) shf->rp, ncopy);
568 shf->rp += ncopy;
569 shf->rnleft -= ncopy;
570 buf += ncopy;
571 bsize -= ncopy;
572 #ifdef OS2
573 if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
574 buf--;
575 bsize++;
576 buf[-1] = '\n';
578 #endif
580 } while (!end && bsize);
581 *buf = '\0';
582 return buf;
585 /* Returns the char read. Returns EOF for error and end of file. */
587 shf_getchar(shf)
588 struct shf *shf;
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))
594 return EOF;
595 --shf->rnleft;
596 return *shf->rp++;
599 /* Put a character back in the input stream. Returns the character if
600 * successful, EOF if there is no room.
603 shf_ungetc(c, shf)
604 int c;
605 struct shf *shf;
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))
612 return EOF;
614 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
615 return 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)
624 return EOF;
625 shf->flags &= ~SHF_EOF;
626 shf->rp--;
627 shf->rnleft++;
628 return c;
630 shf->flags &= ~SHF_EOF;
631 *--(shf->rp) = c;
632 shf->rnleft++;
633 return c;
636 /* Write a character. Returns the character if successful, EOF if
637 * the char could not be written.
640 shf_putchar(c, shf)
641 int c;
642 struct shf *shf;
644 if (!(shf->flags & SHF_WR))
645 internal_errorf(1, "shf_putchar: flags %x", shf->flags);
647 if (c == EOF)
648 return EOF;
650 if (shf->flags & SHF_UNBUF) {
651 char cc = c;
652 int n;
654 if (shf->fd < 0)
655 internal_errorf(1, "shf_putchar: no fd");
656 if (shf->flags & SHF_ERROR) {
657 errno = shf->errno_;
658 return EOF;
660 while ((n = write(shf->fd, &cc, 1)) != 1)
661 if (n < 0) {
662 if (errno == EINTR
663 && !(shf->flags & SHF_INTERRUPT))
664 continue;
665 shf->flags |= SHF_ERROR;
666 shf->errno_ = errno;
667 return EOF;
669 } else {
670 /* Flush deals with strings and sticky errors */
671 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
672 return EOF;
673 shf->wnleft--;
674 *shf->wp++ = c;
677 return c;
680 /* Write a string. Returns the length of the string if successful, EOF if
681 * the string could not be written.
684 shf_puts(s, shf)
685 const char *s;
686 struct shf *shf;
688 if (!s)
689 return EOF;
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)
697 const char *buf;
698 int nbytes;
699 struct shf *shf;
701 int orig_nbytes = nbytes;
702 int n;
703 int ncopy;
705 if (!(shf->flags & SHF_WR))
706 internal_errorf(1, "shf_write: flags %x", shf->flags);
708 if (nbytes < 0)
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))
715 if (ncopy > nbytes)
716 ncopy = nbytes;
717 memcpy(shf->wp, buf, ncopy);
718 nbytes -= ncopy;
719 buf += ncopy;
720 shf->wp += ncopy;
721 shf->wnleft -= ncopy;
723 if (nbytes > 0) {
724 /* Flush deals with strings and sticky errors */
725 if (shf_emptybuf(shf, EB_GROW) == EOF)
726 return EOF;
727 if (nbytes > shf->wbsize) {
728 ncopy = nbytes;
729 if (shf->wbsize)
730 ncopy -= nbytes % shf->wbsize;
731 nbytes -= ncopy;
732 while (ncopy > 0) {
733 n = write(shf->fd, buf, ncopy);
734 if (n < 0) {
735 if (errno == EINTR
736 && !(shf->flags & SHF_INTERRUPT))
737 continue;
738 shf->flags |= SHF_ERROR;
739 shf->errno_ = errno;
740 shf->wnleft = 0;
741 /* Note: fwrite(3S) returns 0 for
742 * errors - this doesn't */
743 return EOF;
745 buf += n;
746 ncopy -= n;
749 if (nbytes > 0) {
750 memcpy(shf->wp, buf, nbytes);
751 shf->wp += nbytes;
752 shf->wnleft -= nbytes;
756 return orig_nbytes;
760 shf_fprintf(struct shf *shf, const char *fmt, ...)
762 va_list args;
763 int n;
765 SH_VA_START(args, fmt);
766 n = shf_vfprintf(shf, fmt, args);
767 va_end(args);
769 return n;
772 #ifdef SILLY_FEATURES
773 char *
774 shf_smprintf(const char *fmt, ...)
776 struct shf shf;
777 va_list args;
779 shf_sopen((char *) 0, 0, SHF_WR|SHF_DYNAMIC, &shf);
780 SH_VA_START(args, fmt);
781 shf_vfprintf(&shf, fmt, args);
782 va_end(args);
783 return shf_sclose(&shf); /* null terminates */
785 #endif /* SILLY_FEATURES */
787 #undef FP /* if you want floating point stuff */
789 #define BUF_SIZE 128
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
799 * this out...
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) ? \
809 ((s) ? \
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] */
831 #ifdef FP
832 #include <math.h>
834 static double
835 my_ceil(d)
836 double d;
838 double i;
840 return d - modf(d, &i) + (d < 0 ? -1 : 1);
842 #endif /* FP */
844 #ifdef WHAT_THE_FUCK
846 shf_vfprintf(shf, fmt, args)
847 struct shf *shf;
848 const char *fmt;
849 va_list args;
851 char c, *s;
852 int UNINITIALIZED(tmp);
853 int field, precision;
854 int len;
855 int flags;
856 unsigned long lnum;
857 /* %#o produces the longest output */
858 char numbuf[(BITS(long) + 2) / 3 + 1];
859 /* this stuff for dealing with the buffer */
860 int nwritten = 0;
861 #ifdef FP
862 /* should be in <math.h>
863 * extern double frexp();
865 extern char *ecvt();
867 double fpnum;
868 int expo, decpt;
869 char style;
870 char fpbuf[FPBUF_SIZE];
871 #endif /* FP */
873 if (!fmt)
874 return 0;
876 while ((c = *fmt++)) {
877 if (c != '%') {
878 shf_putc(c, shf);
879 nwritten++;
880 continue;
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
887 * come last :-).
889 flags = field = precision = 0;
890 for ( ; (c = *fmt++) ; ) {
891 switch (c) {
892 case '#':
893 flags |= FL_HASH;
894 continue;
896 case '+':
897 flags |= FL_PLUS;
898 continue;
900 case '-':
901 flags |= FL_RIGHT;
902 continue;
904 case ' ':
905 flags |= FL_BLANK;
906 continue;
908 case '0':
909 if (!(flags & FL_DOT))
910 flags |= FL_ZERO;
911 continue;
913 case '.':
914 flags |= FL_DOT;
915 precision = 0;
916 continue;
918 case '*':
919 tmp = va_arg(args, int);
920 if (flags & FL_DOT)
921 precision = tmp;
922 else if ((field = tmp) < 0) {
923 field = -field;
924 flags |= FL_RIGHT;
926 continue;
928 case 'l':
929 flags |= FL_LONG;
930 continue;
932 case 'h':
933 flags |= FL_SHORT;
934 continue;
936 if (isdigit(c)) {
937 tmp = c - '0';
938 while (c = *fmt++, isdigit(c))
939 tmp = tmp * 10 + c - '0';
940 --fmt;
941 if (tmp < 0) /* overflow? */
942 tmp = 0;
943 if (flags & FL_DOT)
944 precision = tmp;
945 else
946 field = tmp;
947 continue;
949 break;
952 if (precision < 0)
953 precision = 0;
955 if (!c) /* nasty format */
956 break;
958 if (c >= 'A' && c <= 'Z') {
959 flags |= FL_UPPER;
960 c = c - 'A' + 'a';
963 switch (c) {
964 case 'p': /* pointer */
965 flags &= ~(FL_LONG | FL_SHORT);
966 if (sizeof(char *) > sizeof(int))
967 flags |= FL_LONG; /* hope it fits.. */
968 /* aaahhh... */
969 case 'd':
970 case 'i':
971 case 'o':
972 case 'u':
973 case 'x':
974 flags |= FL_NUMBER;
975 s = &numbuf[sizeof(numbuf)];
976 lnum = POP_INT(flags, c == 'd', args);
977 switch (c) {
978 case 'd':
979 case 'i':
980 if (0 > (long) lnum)
981 lnum = - (long) lnum, tmp = 1;
982 else
983 tmp = 0;
984 /* aaahhhh..... */
986 case 'u':
987 do {
988 *--s = lnum % 10 + '0';
989 lnum /= 10;
990 } while (lnum);
992 if (c != 'u') {
993 if (tmp)
994 *--s = '-';
995 else if (flags & FL_PLUS)
996 *--s = '+';
997 else if (flags & FL_BLANK)
998 *--s = ' ';
1000 break;
1002 case 'o':
1003 do {
1004 *--s = (lnum & 0x7) + '0';
1005 lnum >>= 3;
1006 } while (lnum);
1008 if ((flags & FL_HASH) && *s != '0')
1009 *--s = '0';
1010 break;
1012 case 'p':
1013 case 'x':
1015 const char *digits = (flags & FL_UPPER) ?
1016 "0123456789ABCDEF"
1017 : "0123456789abcdef";
1018 do {
1019 *--s = digits[lnum & 0xf];
1020 lnum >>= 4;
1021 } while (lnum);
1023 if (flags & FL_HASH) {
1024 *--s = (flags & FL_UPPER) ? 'X' : 'x';
1025 *--s = '0';
1029 len = &numbuf[sizeof(numbuf)] - s;
1030 if (flags & FL_DOT) {
1031 if (precision > len) {
1032 field = precision;
1033 flags |= FL_ZERO;
1034 } else
1035 precision = len; /* no loss */
1037 break;
1039 #ifdef FP
1040 case 'e':
1041 case 'g':
1042 case 'f':
1044 char *p;
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.
1052 flags |= FL_NUMBER;
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);
1061 s = fpbuf;
1062 style = c;
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);
1072 if (expo < 0)
1073 expo = 0;
1075 p = ecvt(fpnum, precision + 1 + expo,
1076 &decpt, &tmp);
1077 if (c == 'g') {
1078 if (decpt < -4 || decpt > precision)
1079 style = 'e';
1080 else
1081 style = 'f';
1082 if (decpt > 0 && (precision -= decpt) < 0)
1083 precision = 0;
1085 if (tmp)
1086 *s++ = '-';
1087 else if (flags & FL_PLUS)
1088 *s++ = '+';
1089 else if (flags & FL_BLANK)
1090 *s++ = ' ';
1092 if (style == 'e')
1093 *s++ = *p++;
1094 else {
1095 if (decpt > 0) {
1096 /* Overflow check - should
1097 * never have this problem.
1099 if (decpt >
1100 &fpbuf[sizeof(fpbuf)]
1101 - s - 8)
1102 decpt =
1103 &fpbuf[sizeof(fpbuf)]
1104 - s - 8;
1105 (void) memcpy(s, p, decpt);
1106 s += decpt;
1107 p += decpt;
1108 } else
1109 *s++ = '0';
1112 /* print the fraction? */
1113 if (precision > 0) {
1114 *s++ = '.';
1115 /* Overflow check - should
1116 * never have this problem.
1118 if (precision > &fpbuf[sizeof(fpbuf)]
1119 - s - 7)
1120 precision =
1121 &fpbuf[sizeof(fpbuf)]
1122 - s - 7;
1123 for (tmp = decpt; tmp++ < 0 &&
1124 precision > 0 ; precision--)
1125 *s++ = '0';
1126 tmp = strlen(p);
1127 if (precision > tmp)
1128 precision = tmp;
1129 /* Overflow check - should
1130 * never have this problem.
1132 if (precision > &fpbuf[sizeof(fpbuf)]
1133 - s - 7)
1134 precision =
1135 &fpbuf[sizeof(fpbuf)]
1136 - s - 7;
1137 (void) memcpy(s, p, precision);
1138 s += precision;
1140 * `g' format strips trailing
1141 * zeros after the decimal.
1143 if (c == 'g' && !(flags & FL_HASH)) {
1144 while (*--s == '0')
1146 if (*s != '.')
1147 s++;
1149 } else if (flags & FL_HASH)
1150 *s++ = '.';
1152 if (style == 'e') {
1153 *s++ = (flags & FL_UPPER) ? 'E' : 'e';
1154 if (--decpt >= 0)
1155 *s++ = '+';
1156 else {
1157 *s++ = '-';
1158 decpt = -decpt;
1160 p = &numbuf[sizeof(numbuf)];
1161 for (tmp = 0; tmp < 2 || decpt ; tmp++) {
1162 *--p = '0' + decpt % 10;
1163 decpt /= 10;
1165 tmp = &numbuf[sizeof(numbuf)] - p;
1166 (void) memcpy(s, p, tmp);
1167 s += tmp;
1170 len = s - fpbuf;
1171 s = fpbuf;
1172 precision = len;
1173 break;
1175 #endif /* FP */
1177 case 's':
1178 if (!(s = va_arg(args, char *)))
1179 s = "(null %s)";
1180 len = strlen(s);
1181 break;
1183 case 'c':
1184 flags &= ~FL_DOT;
1185 numbuf[0] = va_arg(args, int);
1186 s = numbuf;
1187 len = 1;
1188 break;
1190 case '%':
1191 default:
1192 numbuf[0] = c;
1193 s = numbuf;
1194 len = 1;
1195 break;
1199 * At this point s should point to a string that is
1200 * to be formatted, and len should be the length of the
1201 * string.
1203 if (!(flags & FL_DOT) || len < precision)
1204 precision = len;
1205 if (field > precision) {
1206 field -= precision;
1207 if (!(flags & FL_RIGHT)) {
1208 field = -field;
1209 /* skip past sign or 0x when padding with 0 */
1210 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1211 if (*s == '+' || *s == '-' || *s ==' ')
1213 shf_putc(*s, shf);
1214 s++;
1215 precision--;
1216 nwritten++;
1217 } else if (*s == '0') {
1218 shf_putc(*s, shf);
1219 s++;
1220 nwritten++;
1221 if (--precision > 0 &&
1222 (*s | 0x20) == 'x')
1224 shf_putc(*s, shf);
1225 s++;
1226 precision--;
1227 nwritten++;
1230 c = '0';
1231 } else
1232 c = flags & FL_ZERO ? '0' : ' ';
1233 if (field < 0) {
1234 nwritten += -field;
1235 for ( ; field < 0 ; field++)
1236 shf_putc(c, shf);
1238 } else
1239 c = ' ';
1240 } else
1241 field = 0;
1243 if (precision > 0) {
1244 nwritten += precision;
1245 for ( ; precision-- > 0 ; s++)
1246 shf_putc(*s, shf);
1248 if (field > 0) {
1249 nwritten += field;
1250 for ( ; field > 0 ; --field)
1251 shf_putc(c, shf);
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;
1262 char *buf = NULL;
1264 while(nwritten == -1) {
1265 buf = realloc(buf, bufsiz);
1266 nwritten = vsnprintf(buf, bufsiz, fmt, args);
1267 bufsiz *= 2;
1270 return nwritten ? shf_puts(buf, shf) : -1;
1272 #endif /* WHAT_THE_FUCK */