reenabled swaptest. quake should now load data and start on big endian architectures...
[AROS-Contrib.git] / gnu / abc-shell / shf.c
blobdf8ac980bfe572149f6aeed6c140d6c755cc016a
1 /*
2 * Shell file I/O routines
3 */
5 #include <sys/stat.h>
6 #include "sh.h"
7 #include "ksh_limval.h"
9 #include <proto/dos.h>
11 /* flags to shf_emptybuf() */
12 #define EB_READSW 0x01 /* about to switch to reading */
13 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
16 * Replacement stdio routines. Stdio is too flakey on too many machines
17 * to be useful when you have multiple processes using the same underlying
18 * file descriptors.
21 static int shf_fillbuf(struct shf *);
22 static int shf_emptybuf(struct shf *, int);
24 /* Open a file. First three args are for open(), last arg is flags for
25 * this package. Returns NULL if file could not be opened, or if a dup
26 * fails.
28 struct shf *
29 shf_open(const char *name, int oflags, int mode, int sflags)
31 struct shf *shf;
32 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
33 int fd;
35 /* Done before open so if alloca fails, fd won't be lost. */
36 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
37 shf->areap = ATEMP;
38 shf->buf = (unsigned char *) &shf[1];
39 shf->bsize = bsize;
40 shf->flags = SHF_ALLOCS;
41 /* Rest filled in by reopen. */
43 fd = open(name, oflags, mode);
44 if (fd < 0) {
45 afree(shf, shf->areap);
46 return NULL;
48 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
49 int nfd;
51 nfd = fcntl(fd, F_DUPFD, FDBASE);
52 close(fd);
53 if (nfd < 0) {
54 afree(shf, shf->areap);
55 return NULL;
57 fd = nfd;
59 sflags &= ~SHF_ACCMODE;
60 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
61 ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR :
62 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;
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 shf->ispipe = false;
120 #if !defined __amigaos4__ && !defined __AROS__
121 if (sflags & SHF_CLEXEC)
122 fcntl(fd, F_SETFD, FD_CLOEXEC);
123 #endif
124 return shf;
127 /* Set up an existing shf (and buffer) to use the given fd */
128 struct shf *
129 shf_reopen(int fd, int sflags, struct shf *shf)
131 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
133 /* use fcntl() to figure out correct read/write flags */
134 if (sflags & SHF_GETFL) {
135 int flags = fcntl(fd, F_GETFL, 0);
137 if (flags < 0)
138 /* will get an error on first read/write */
139 sflags |= SHF_RDWR;
140 else
141 switch (flags & O_ACCMODE) {
142 case O_RDONLY: sflags |= SHF_RD; break;
143 case O_WRONLY: sflags |= SHF_WR; break;
144 case O_RDWR: sflags |= SHF_RDWR; break;
148 if (!(sflags & (SHF_RD | SHF_WR)))
149 internal_errorf(1, "shf_reopen: missing read/write");
150 if (!shf || !shf->buf || shf->bsize < bsize)
151 internal_errorf(1, "shf_reopen: bad shf/buf/bsize");
153 /* assumes shf->buf and shf->bsize already set up */
154 shf->fd = fd;
155 shf->rp = shf->wp = shf->buf;
156 shf->rnleft = 0;
157 shf->rbsize = bsize;
158 shf->wnleft = 0; /* force call to shf_emptybuf() */
159 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
160 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
161 shf->errno_ = 0;
162 shf->ispipe = false;
163 #if !defined __amigaos4__ && !defined __AROS__
164 if (sflags & SHF_CLEXEC)
165 fcntl(fd, F_SETFD, FD_CLOEXEC);
166 #endif
167 return shf;
170 /* Open a string for reading or writing. If reading, bsize is the number
171 * of bytes that can be read. If writing, bsize is the maximum number of
172 * bytes that can be written. If shf is not null, it is filled in and
173 * returned, if it is null, shf is allocated. If writing and buf is null
174 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
175 * used for the initial size). Doesn't fail.
176 * When writing, a byte is reserved for a trailing null - see shf_sclose().
178 struct shf *
179 shf_sopen(char *buf, int bsize, int sflags, 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;
206 shf->ispipe = false;
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);
220 if(shf->ispipe == true)
222 #ifdef USE_TEMPFILES
223 BPTR f=0;
224 UBYTE pname[256];
227 __get_default_file(shf->fd,(long *)&f);
228 NameFromFH(f,pname,255);
230 if(close(shf->fd) < 0) ret = EOF;
231 if(f)
233 DeleteFile(pname);
236 #else
237 char buffer[256];
238 int n,t;
239 t = 0;
240 while((n = read(shf->fd,buffer,255)) > 0) t +=n;
241 if (close(shf->fd) < 0) ret = EOF;
243 #endif
245 else
247 if (close(shf->fd) < 0) ret = EOF;
250 if (shf->flags & SHF_ALLOCS)
251 afree(shf, shf->areap);
252 else if (shf->flags & SHF_ALLOCB)
253 afree(shf->buf, shf->areap);
255 return ret;
258 /* Flush and close file descriptor, don't free file structure */
260 shf_fdclose(struct shf *shf)
262 int ret = 0;
264 if (shf->fd >= 0) {
265 ret = shf_flush(shf);
267 if(shf->ispipe == true)
269 #ifdef USE_TEMPFILES
270 BPTR f=0;
271 UBYTE pname[256];
274 __get_default_file(shf->fd,(long *)&f);
275 NameFromFH(f,pname,255);
277 if(close(shf->fd) < 0) ret = EOF;
278 if(f)
280 DeleteFile(pname);
283 #else
284 char buffer[256];
285 int n,t;
286 t = 0;
287 while((n = read(shf->fd,buffer,255)) > 0) t +=n;
288 if (close(shf->fd) < 0) ret = EOF;
290 #endif
292 else
294 if (close(shf->fd) < 0) ret = EOF;
297 shf->rnleft = 0;
298 shf->rp = shf->buf;
299 shf->wnleft = 0;
300 shf->fd = -1;
303 return ret;
306 /* Close a string - if it was opened for writing, it is null terminated;
307 * returns a pointer to the string and frees shf if it was allocated
308 * (does not free string if it was allocated).
310 char *
311 shf_sclose(struct shf *shf)
313 unsigned char *s = shf->buf;
315 /* null terminate */
316 if (shf->flags & SHF_WR) {
317 shf->wnleft++;
318 shf_putc('\0', shf);
320 if (shf->flags & SHF_ALLOCS)
321 afree(shf, shf->areap);
322 return (char *) s;
325 /* Un-read what has been read but not examined, or write what has been
326 * buffered. Returns 0 for success, EOF for (write) error.
329 shf_flush(struct shf *shf)
331 if (shf->flags & SHF_STRING)
332 return (shf->flags & SHF_WR) ? EOF : 0;
334 if (shf->fd < 0)
335 internal_errorf(1, "shf_flush: no fd");
337 if (shf->flags & SHF_ERROR) {
338 errno = shf->errno_;
339 return EOF;
342 if (shf->flags & SHF_READING) {
343 shf->flags &= ~(SHF_EOF | SHF_READING);
344 if (shf->rnleft > 0) {
345 lseek(shf->fd, (off_t) -shf->rnleft, SEEK_CUR);
346 shf->rnleft = 0;
347 shf->rp = shf->buf;
349 return 0;
350 } else if (shf->flags & SHF_WRITING)
351 return shf_emptybuf(shf, 0);
353 return 0;
356 /* Write out any buffered data. If currently reading, flushes the read
357 * buffer. Returns 0 for success, EOF for (write) error.
359 static int
360 shf_emptybuf(struct shf *shf, int flags)
362 int ret = 0;
364 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
365 internal_errorf(1, "shf_emptybuf: no fd");
367 if (shf->flags & SHF_ERROR) {
368 errno = shf->errno_;
369 return EOF;
372 if (shf->flags & SHF_READING) {
373 if (flags & EB_READSW) /* doesn't happen */
374 return 0;
375 ret = shf_flush(shf);
376 shf->flags &= ~SHF_READING;
378 if (shf->flags & SHF_STRING) {
379 unsigned char *nbuf;
381 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
382 * is set... (changing the shf pointer could cause problems)
384 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
385 !(shf->flags & SHF_ALLOCB))
386 return EOF;
387 /* allocate more space for buffer */
388 nbuf = (unsigned char *) aresize(shf->buf, shf->wbsize * 2,
389 shf->areap);
390 shf->rp = nbuf + (shf->rp - shf->buf);
391 shf->wp = nbuf + (shf->wp - shf->buf);
392 shf->rbsize += shf->wbsize;
393 shf->wnleft += shf->wbsize;
394 shf->wbsize *= 2;
395 shf->buf = nbuf;
396 } else {
397 if (shf->flags & SHF_WRITING) {
398 int ntowrite = shf->wp - shf->buf;
399 unsigned char *buf = shf->buf;
400 int n;
402 while (ntowrite > 0) {
403 n = write(shf->fd, buf, ntowrite);
404 if (n < 0) {
405 if (errno == EINTR
406 && !(shf->flags & SHF_INTERRUPT))
407 continue;
408 shf->flags |= SHF_ERROR;
409 shf->errno_ = errno;
410 shf->wnleft = 0;
411 if (buf != shf->buf) {
412 /* allow a second flush
413 * to work */
414 memmove(shf->buf, buf,
415 ntowrite);
416 shf->wp = shf->buf + ntowrite;
418 return EOF;
420 buf += n;
421 ntowrite -= n;
423 if (flags & EB_READSW) {
424 shf->wp = shf->buf;
425 shf->wnleft = 0;
426 shf->flags &= ~SHF_WRITING;
427 return 0;
430 shf->wp = shf->buf;
431 shf->wnleft = shf->wbsize;
433 shf->flags |= SHF_WRITING;
435 return ret;
438 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
439 static int
440 shf_fillbuf(struct shf *shf)
442 if (shf->flags & SHF_STRING)
443 return 0;
445 if (shf->fd < 0)
446 internal_errorf(1, "shf_fillbuf: no fd");
448 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
449 if (shf->flags & SHF_ERROR)
450 errno = shf->errno_;
451 return EOF;
454 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
455 return EOF;
457 shf->flags |= SHF_READING;
459 shf->rp = shf->buf;
460 while (1) {
461 shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
462 shf->rbsize);
463 if (shf->rnleft < 0 && errno == EINTR
464 && !(shf->flags & SHF_INTERRUPT))
465 continue;
466 break;
468 if (shf->rnleft <= 0) {
469 if (shf->rnleft < 0) {
470 shf->flags |= SHF_ERROR;
471 shf->errno_ = errno;
472 shf->rnleft = 0;
473 shf->rp = shf->buf;
474 return EOF;
476 shf->flags |= SHF_EOF;
478 return 0;
481 /* Read a buffer from shf. Returns the number of bytes read into buf,
482 * if no bytes were read, returns 0 if end of file was seen, EOF if
483 * a read error occurred.
486 shf_read(char *buf, int bsize, struct shf *shf)
488 int orig_bsize = bsize;
489 int ncopy;
491 if (!(shf->flags & SHF_RD))
492 internal_errorf(1, "shf_read: flags %x", shf->flags);
494 if (bsize <= 0)
495 internal_errorf(1, "shf_read: bsize %d", bsize);
497 while (bsize > 0) {
498 if (shf->rnleft == 0
499 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
500 break;
501 ncopy = shf->rnleft;
502 if (ncopy > bsize)
503 ncopy = bsize;
504 memcpy(buf, shf->rp, ncopy);
505 buf += ncopy;
506 bsize -= ncopy;
507 shf->rp += ncopy;
508 shf->rnleft -= ncopy;
510 /* Note: fread(3S) returns 0 for errors - this doesn't */
511 return orig_bsize == bsize ? (shf_error(shf) ? EOF : 0)
512 : orig_bsize - bsize;
515 /* Read up to a newline or EOF. The newline is put in buf; buf is always
516 * null terminated. Returns NULL on read error or if nothing was read before
517 * end of file, returns a pointer to the null byte in buf otherwise.
519 char *
520 shf_getse(char *buf, int bsize, struct shf *shf)
522 unsigned char *end;
523 int ncopy;
524 char *orig_buf = buf;
526 if (!(shf->flags & SHF_RD))
527 internal_errorf(1, "shf_getse: flags %x", shf->flags);
529 if (bsize <= 0)
530 return (char *) 0;
532 --bsize; /* save room for null */
533 do {
534 if (shf->rnleft == 0) {
535 if (shf_fillbuf(shf) == EOF)
536 return NULL;
537 if (shf->rnleft == 0) {
538 *buf = '\0';
539 return buf == orig_buf ? NULL : buf;
542 end = (unsigned char *) memchr((char *) shf->rp, '\n',
543 shf->rnleft);
544 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
545 if (ncopy > bsize)
546 ncopy = bsize;
547 memcpy(buf, (char *) shf->rp, ncopy);
548 shf->rp += ncopy;
549 shf->rnleft -= ncopy;
550 buf += ncopy;
551 bsize -= ncopy;
553 } while (!end && bsize);
554 *buf = '\0';
555 return buf;
558 /* Returns the char read. Returns EOF for error and end of file. */
560 shf_getchar(struct shf *shf)
562 if (!(shf->flags & SHF_RD))
563 internal_errorf(1, "shf_getchar: flags %x", shf->flags);
565 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
566 return EOF;
567 --shf->rnleft;
568 return *shf->rp++;
571 /* Put a character back in the input stream. Returns the character if
572 * successful, EOF if there is no room.
575 shf_ungetc(int c, struct shf *shf)
577 if (!(shf->flags & SHF_RD))
578 internal_errorf(1, "shf_ungetc: flags %x", shf->flags);
580 if ((shf->flags & SHF_ERROR) || c == EOF
581 || (shf->rp == shf->buf && shf->rnleft))
582 return EOF;
584 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
585 return EOF;
587 if (shf->rp == shf->buf)
588 shf->rp = shf->buf + shf->rbsize;
589 if (shf->flags & SHF_STRING) {
590 /* Can unget what was read, but not something different - we
591 * don't want to modify a string.
593 if (shf->rp[-1] != c)
594 return EOF;
595 shf->flags &= ~SHF_EOF;
596 shf->rp--;
597 shf->rnleft++;
598 return c;
600 shf->flags &= ~SHF_EOF;
601 *--(shf->rp) = c;
602 shf->rnleft++;
603 return c;
606 /* Write a character. Returns the character if successful, EOF if
607 * the char could not be written.
610 shf_putchar(int c, struct shf *shf)
612 if (!(shf->flags & SHF_WR))
613 internal_errorf(1, "shf_putchar: flags %x", shf->flags);
615 if (c == EOF)
616 return EOF;
618 if (shf->flags & SHF_UNBUF) {
619 char cc = c;
620 int n;
622 if (shf->fd < 0)
623 internal_errorf(1, "shf_putchar: no fd");
624 if (shf->flags & SHF_ERROR) {
625 errno = shf->errno_;
626 return EOF;
628 while ((n = write(shf->fd, &cc, 1)) != 1)
629 if (n < 0) {
630 if (errno == EINTR
631 && !(shf->flags & SHF_INTERRUPT))
632 continue;
633 shf->flags |= SHF_ERROR;
634 shf->errno_ = errno;
635 return EOF;
637 } else {
638 /* Flush deals with strings and sticky errors */
639 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
640 return EOF;
641 shf->wnleft--;
642 *shf->wp++ = c;
645 return c;
648 /* Write a string. Returns the length of the string if successful, EOF if
649 * the string could not be written.
652 shf_puts(const char *s, struct shf *shf)
654 if (!s)
655 return EOF;
657 return shf_write(s, strlen(s), shf);
660 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
662 shf_write(const char *buf, int nbytes, struct shf *shf)
664 int orig_nbytes = nbytes;
665 int n;
666 int ncopy;
668 if (!(shf->flags & SHF_WR))
669 internal_errorf(1, "shf_write: flags %x", shf->flags);
671 if (nbytes < 0)
672 internal_errorf(1, "shf_write: nbytes %d", nbytes);
674 /* Don't buffer if buffer is empty and we're writting a large amount. */
675 if ((ncopy = shf->wnleft)
676 && (shf->wp != shf->buf || nbytes < shf->wnleft))
678 if (ncopy > nbytes)
679 ncopy = nbytes;
680 memcpy(shf->wp, buf, ncopy);
681 nbytes -= ncopy;
682 buf += ncopy;
683 shf->wp += ncopy;
684 shf->wnleft -= ncopy;
686 if (nbytes > 0) {
687 /* Flush deals with strings and sticky errors */
688 if (shf_emptybuf(shf, EB_GROW) == EOF)
689 return EOF;
690 if (nbytes > shf->wbsize) {
691 ncopy = nbytes;
692 if (shf->wbsize)
693 ncopy -= nbytes % shf->wbsize;
694 nbytes -= ncopy;
695 while (ncopy > 0) {
696 n = write(shf->fd, buf, ncopy);
697 if (n < 0) {
698 if (errno == EINTR
699 && !(shf->flags & SHF_INTERRUPT))
700 continue;
701 shf->flags |= SHF_ERROR;
702 shf->errno_ = errno;
703 shf->wnleft = 0;
704 /* Note: fwrite(3S) returns 0 for
705 * errors - this doesn't */
706 return EOF;
708 buf += n;
709 ncopy -= n;
712 if (nbytes > 0) {
713 memcpy(shf->wp, buf, nbytes);
714 shf->wp += nbytes;
715 shf->wnleft -= nbytes;
719 return orig_nbytes;
723 shf_fprintf(struct shf *shf, const char *fmt, ...)
725 va_list args;
726 int n;
728 va_start(args, fmt);
729 n = shf_vfprintf(shf, fmt, args);
730 va_end(args);
732 return n;
736 shf_snprintf(char *buf, int bsize, const char *fmt, ...)
738 struct shf shf;
739 va_list args;
740 int n;
742 if (!buf || bsize <= 0)
743 internal_errorf(1, "shf_snprintf: buf %lx, bsize %d",
744 (long) buf, bsize);
746 shf_sopen(buf, bsize, SHF_WR, &shf);
747 va_start(args, fmt);
748 n = shf_vfprintf(&shf, fmt, args);
749 va_end(args);
750 shf_sclose(&shf); /* null terminates */
751 return n;
754 char *
755 shf_smprintf(const char *fmt, ...)
757 struct shf shf;
758 va_list args;
760 shf_sopen((char *) 0, 0, SHF_WR|SHF_DYNAMIC, &shf);
761 va_start(args, fmt);
762 shf_vfprintf(&shf, fmt, args);
763 va_end(args);
764 return shf_sclose(&shf); /* null terminates */
767 #undef FP /* if you want floating point stuff */
769 #define BUF_SIZE 128
770 #define FPBUF_SIZE (DMAXEXP+16)/* this must be >
771 * MAX(DMAXEXP, log10(pow(2, DSIGNIF)))
772 * + ceil(log10(DMAXEXP)) + 8 (I think).
773 * Since this is hard to express as a
774 * constant, just use a large buffer.
778 * What kinda of machine we on? Hopefully the C compiler will optimize
779 * this out...
781 * For shorts, we want sign extend for %d but not for %[oxu] - on 16 bit
782 * machines it don't matter. Assumes C compiler has converted shorts to
783 * ints before pushing them.
785 #define POP_INT(f, s, a) \
786 (((f) & FL_LONG) ? va_arg((a), unsigned long) : \
787 (sizeof(int) < sizeof(long) ? ((s) ? \
788 (long) va_arg((a), int) : va_arg((a), unsigned)) : \
789 va_arg((a), unsigned)))
791 #define ABIGNUM 32000 /* big numer that will fit in a short */
792 #define LOG2_10 3.321928094887362347870319429 /* log base 2 of 10 */
794 #define FL_HASH 0x001 /* `#' seen */
795 #define FL_PLUS 0x002 /* `+' seen */
796 #define FL_RIGHT 0x004 /* `-' seen */
797 #define FL_BLANK 0x008 /* ` ' seen */
798 #define FL_SHORT 0x010 /* `h' seen */
799 #define FL_LONG 0x020 /* `l' seen */
800 #define FL_ZERO 0x040 /* `0' seen */
801 #define FL_DOT 0x080 /* '.' seen */
802 #define FL_UPPER 0x100 /* format character was uppercase */
803 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
806 #ifdef FP
807 #include <math.h>
809 static double
810 my_ceil(double d)
812 double i;
814 return d - modf(d, &i) + (d < 0 ? -1 : 1);
816 #endif /* FP */
819 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
821 char c, *s;
822 int tmp = 0;
823 int field, precision;
824 int len;
825 int flags;
826 unsigned long lnum;
827 /* %#o produces the longest output */
828 char numbuf[(BITS(long) + 2) / 3 + 1];
829 /* this stuff for dealing with the buffer */
830 int nwritten = 0;
831 #ifdef FP
832 /* should be in <math.h>
833 * extern double frexp();
835 extern char *ecvt();
837 double fpnum;
838 int expo, decpt;
839 char style;
840 char fpbuf[FPBUF_SIZE];
841 #endif /* FP */
843 if (!fmt)
844 return 0;
846 while ((c = *fmt++)) {
847 if (c != '%') {
848 shf_putc(c, shf);
849 nwritten++;
850 continue;
853 * This will accept flags/fields in any order - not
854 * just the order specified in printf(3), but this is
855 * the way _doprnt() seems to work (on bsd and sysV).
856 * The only restriction is that the format character must
857 * come last :-).
859 flags = field = precision = 0;
860 for ( ; (c = *fmt++) ; ) {
861 switch (c) {
862 case '#':
863 flags |= FL_HASH;
864 continue;
866 case '+':
867 flags |= FL_PLUS;
868 continue;
870 case '-':
871 flags |= FL_RIGHT;
872 continue;
874 case ' ':
875 flags |= FL_BLANK;
876 continue;
878 case '0':
879 if (!(flags & FL_DOT))
880 flags |= FL_ZERO;
881 continue;
883 case '.':
884 flags |= FL_DOT;
885 precision = 0;
886 continue;
888 case '*':
889 tmp = va_arg(args, int);
890 if (flags & FL_DOT)
891 precision = tmp;
892 else if ((field = tmp) < 0) {
893 field = -field;
894 flags |= FL_RIGHT;
896 continue;
898 case 'l':
899 flags |= FL_LONG;
900 continue;
902 case 'h':
903 flags |= FL_SHORT;
904 continue;
906 if (digit(c)) {
907 tmp = c - '0';
908 while (c = *fmt++, digit(c))
909 tmp = tmp * 10 + c - '0';
910 --fmt;
911 if (tmp < 0) /* overflow? */
912 tmp = 0;
913 if (flags & FL_DOT)
914 precision = tmp;
915 else
916 field = tmp;
917 continue;
919 break;
922 if (precision < 0)
923 precision = 0;
925 if (!c) /* nasty format */
926 break;
928 if (c >= 'A' && c <= 'Z') {
929 flags |= FL_UPPER;
930 c = c - 'A' + 'a';
933 switch (c) {
934 case 'p': /* pointer */
935 flags &= ~(FL_LONG | FL_SHORT);
936 if (sizeof(char *) > sizeof(int))
937 flags |= FL_LONG; /* hope it fits.. */
938 /* aaahhh... */
939 case 'd':
940 case 'i':
941 case 'o':
942 case 'u':
943 case 'x':
944 flags |= FL_NUMBER;
945 s = &numbuf[sizeof(numbuf)];
946 lnum = POP_INT(flags, c == 'd', args);
947 switch (c) {
948 case 'd':
949 case 'i':
950 if (0 > (long) lnum)
951 lnum = - (long) lnum, tmp = 1;
952 else
953 tmp = 0;
954 /* aaahhhh..... */
956 case 'u':
957 do {
958 *--s = lnum % 10 + '0';
959 lnum /= 10;
960 } while (lnum);
962 if (c != 'u') {
963 if (tmp)
964 *--s = '-';
965 else if (flags & FL_PLUS)
966 *--s = '+';
967 else if (flags & FL_BLANK)
968 *--s = ' ';
970 break;
972 case 'o':
973 do {
974 *--s = (lnum & 0x7) + '0';
975 lnum >>= 3;
976 } while (lnum);
978 if ((flags & FL_HASH) && *s != '0')
979 *--s = '0';
980 break;
982 case 'p':
983 case 'x':
985 const char *digits = (flags & FL_UPPER) ?
986 "0123456789ABCDEF"
987 : "0123456789abcdef";
988 do {
989 *--s = digits[lnum & 0xf];
990 lnum >>= 4;
991 } while (lnum);
993 if (flags & FL_HASH) {
994 *--s = (flags & FL_UPPER) ? 'X' : 'x';
995 *--s = '0';
999 len = &numbuf[sizeof(numbuf)] - s;
1000 if (flags & FL_DOT) {
1001 if (precision > len) {
1002 field = precision;
1003 flags |= FL_ZERO;
1004 } else
1005 precision = len; /* no loss */
1007 break;
1009 #ifdef FP
1010 case 'e':
1011 case 'g':
1012 case 'f':
1014 char *p;
1017 * This could probably be done better,
1018 * but it seems to work. Note that gcvt()
1019 * is not used, as you cannot tell it to
1020 * not strip the zeros.
1022 flags |= FL_NUMBER;
1023 if (!(flags & FL_DOT))
1024 precision = 6; /* default */
1026 * Assumes doubles are pushed on
1027 * the stack. If this is not so, then
1028 * FL_LONG/FL_SHORT should be checked.
1030 fpnum = va_arg(args, double);
1031 s = fpbuf;
1032 style = c;
1034 * This is the same as
1035 * expo = ceil(log10(fpnum))
1036 * but doesn't need -lm. This is an
1037 * approximation as expo is rounded up.
1039 (void) frexp(fpnum, &expo);
1040 expo = my_ceil(expo / LOG2_10);
1042 if (expo < 0)
1043 expo = 0;
1045 p = ecvt(fpnum, precision + 1 + expo,
1046 &decpt, &tmp);
1047 if (c == 'g') {
1048 if (decpt < -4 || decpt > precision)
1049 style = 'e';
1050 else
1051 style = 'f';
1052 if (decpt > 0 && (precision -= decpt) < 0)
1053 precision = 0;
1055 if (tmp)
1056 *s++ = '-';
1057 else if (flags & FL_PLUS)
1058 *s++ = '+';
1059 else if (flags & FL_BLANK)
1060 *s++ = ' ';
1062 if (style == 'e')
1063 *s++ = *p++;
1064 else {
1065 if (decpt > 0) {
1066 /* Overflow check - should
1067 * never have this problem.
1069 if (decpt > &fpbuf[sizeof(fpbuf)] - s - 8)
1070 decpt = &fpbuf[sizeof(fpbuf)] - s - 8;
1071 (void) memcpy(s, p, decpt);
1072 s += decpt;
1073 p += decpt;
1074 } else
1075 *s++ = '0';
1078 /* print the fraction? */
1079 if (precision > 0) {
1080 *s++ = '.';
1081 /* Overflow check - should
1082 * never have this problem.
1084 if (precision > &fpbuf[sizeof(fpbuf)] - s - 7)
1085 precision = &fpbuf[sizeof(fpbuf)] - s - 7;
1086 for (tmp = decpt; tmp++ < 0 &&
1087 precision > 0 ; precision--)
1088 *s++ = '0';
1089 tmp = strlen(p);
1090 if (precision > tmp)
1091 precision = tmp;
1092 /* Overflow check - should
1093 * never have this problem.
1095 if (precision > &fpbuf[sizeof(fpbuf)] - s - 7)
1096 precision = &fpbuf[sizeof(fpbuf)] - s - 7;
1097 (void) memcpy(s, p, precision);
1098 s += precision;
1100 * `g' format strips trailing
1101 * zeros after the decimal.
1103 if (c == 'g' && !(flags & FL_HASH)) {
1104 while (*--s == '0')
1106 if (*s != '.')
1107 s++;
1109 } else if (flags & FL_HASH)
1110 *s++ = '.';
1112 if (style == 'e') {
1113 *s++ = (flags & FL_UPPER) ? 'E' : 'e';
1114 if (--decpt >= 0)
1115 *s++ = '+';
1116 else {
1117 *s++ = '-';
1118 decpt = -decpt;
1120 p = &numbuf[sizeof(numbuf)];
1121 for (tmp = 0; tmp < 2 || decpt ; tmp++) {
1122 *--p = '0' + decpt % 10;
1123 decpt /= 10;
1125 tmp = &numbuf[sizeof(numbuf)] - p;
1126 (void) memcpy(s, p, tmp);
1127 s += tmp;
1130 len = s - fpbuf;
1131 s = fpbuf;
1132 precision = len;
1133 break;
1135 #endif /* FP */
1137 case 's':
1138 if (!(s = va_arg(args, char *)))
1139 s = strdup("(null %s)");
1140 len = strlen(s);
1141 break;
1143 case 'c':
1144 flags &= ~FL_DOT;
1145 numbuf[0] = va_arg(args, int);
1146 s = numbuf;
1147 len = 1;
1148 break;
1150 case '%':
1151 default:
1152 numbuf[0] = c;
1153 s = numbuf;
1154 len = 1;
1155 break;
1159 * At this point s should point to a string that is
1160 * to be formatted, and len should be the length of the
1161 * string.
1163 if (!(flags & FL_DOT) || len < precision)
1164 precision = len;
1165 if (field > precision) {
1166 field -= precision;
1167 if (!(flags & FL_RIGHT)) {
1168 field = -field;
1169 /* skip past sign or 0x when padding with 0 */
1170 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1171 if (*s == '+' || *s == '-' || *s ==' ')
1173 shf_putc(*s, shf);
1174 s++;
1175 precision--;
1176 nwritten++;
1177 } else if (*s == '0') {
1178 shf_putc(*s, shf);
1179 s++;
1180 nwritten++;
1181 if (--precision > 0 &&
1182 (*s | 0x20) == 'x')
1184 shf_putc(*s, shf);
1185 s++;
1186 precision--;
1187 nwritten++;
1190 c = '0';
1191 } else
1192 c = flags & FL_ZERO ? '0' : ' ';
1193 if (field < 0) {
1194 nwritten += -field;
1195 for ( ; field < 0 ; field++)
1196 shf_putc(c, shf);
1198 } else
1199 c = ' ';
1200 } else
1201 field = 0;
1203 if (precision > 0) {
1204 nwritten += precision;
1205 for ( ; precision-- > 0 ; s++)
1206 shf_putc(*s, shf);
1208 if (field > 0) {
1209 nwritten += field;
1210 for ( ; field > 0 ; --field)
1211 shf_putc(c, shf);
1215 return shf_error(shf) ? EOF : nwritten;