1 /* $OpenBSD: shf.c,v 1.15 2006/04/02 00:48:33 deraadt Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 * Thorsten Glaser <tg@mirbsd.org>
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
13 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 * the utmost extent permitted by applicable law, neither express nor
15 * implied; without malicious intent or gross negligence. In no event
16 * may a licensor, author or contributor be held liable for indirect,
17 * direct, other damage, loss, or other issues arising in any way out
18 * of dealing in the work, even if advised of the possibility of such
19 * damage or existence of a defect, except proven that it results out
20 * of said person's immediate fault when using the work as intended.
25 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.36 2010/07/19 22:41:04 tg Exp $");
27 /* flags to shf_emptybuf() */
28 #define EB_READSW 0x01 /* about to switch to reading */
29 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
32 * Replacement stdio routines. Stdio is too flakey on too many machines
33 * to be useful when you have multiple processes using the same underlying
37 static int shf_fillbuf(struct shf *);
38 static int shf_emptybuf(struct shf *, int);
40 /* Open a file. First three args are for open(), last arg is flags for
41 * this package. Returns NULL if file could not be opened, or if a dup
45 shf_open(const char *name, int oflags, int mode, int sflags)
48 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
51 /* Done before open so if alloca fails, fd won't be lost. */
52 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
54 shf->buf = (unsigned char *)&shf[1];
56 shf->flags = SHF_ALLOCS;
57 /* Rest filled in by reopen. */
59 fd = open(name, oflags, mode);
61 afree(shf, shf->areap);
64 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
67 nfd = fcntl(fd, F_DUPFD, FDBASE);
70 afree(shf, shf->areap);
75 sflags &= ~SHF_ACCMODE;
76 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
77 ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
79 return (shf_reopen(fd, sflags, shf));
82 /* Set up the shf structure for a file descriptor. Doesn't fail. */
84 shf_fdopen(int fd, int sflags, struct shf *shf)
86 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
88 /* use fcntl() to figure out correct read/write flags */
89 if (sflags & SHF_GETFL) {
90 int flags = fcntl(fd, F_GETFL, 0);
93 /* will get an error on first read/write */
96 switch (flags & O_ACCMODE) {
110 if (!(sflags & (SHF_RD | SHF_WR)))
111 internal_errorf("shf_fdopen: missing read/write");
115 shf->buf = alloc(bsize, ATEMP);
116 sflags |= SHF_ALLOCB;
120 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
121 shf->buf = (unsigned char *)&shf[1];
122 sflags |= SHF_ALLOCS;
126 shf->rp = shf->wp = shf->buf;
129 shf->wnleft = 0; /* force call to shf_emptybuf() */
130 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
134 if (sflags & SHF_CLEXEC)
135 fcntl(fd, F_SETFD, FD_CLOEXEC);
139 /* Set up an existing shf (and buffer) to use the given fd */
141 shf_reopen(int fd, int sflags, struct shf *shf)
143 int bsize = sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
145 /* use fcntl() to figure out correct read/write flags */
146 if (sflags & SHF_GETFL) {
147 int flags = fcntl(fd, F_GETFL, 0);
150 /* will get an error on first read/write */
153 switch (flags & O_ACCMODE) {
167 if (!(sflags & (SHF_RD | SHF_WR)))
168 internal_errorf("shf_reopen: missing read/write");
169 if (!shf || !shf->buf || shf->bsize < bsize)
170 internal_errorf("shf_reopen: bad shf/buf/bsize");
172 /* assumes shf->buf and shf->bsize already set up */
174 shf->rp = shf->wp = shf->buf;
177 shf->wnleft = 0; /* force call to shf_emptybuf() */
178 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
179 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
181 if (sflags & SHF_CLEXEC)
182 fcntl(fd, F_SETFD, FD_CLOEXEC);
186 /* Open a string for reading or writing. If reading, bsize is the number
187 * of bytes that can be read. If writing, bsize is the maximum number of
188 * bytes that can be written. If shf is not null, it is filled in and
189 * returned, if it is null, shf is allocated. If writing and buf is null
190 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
191 * used for the initial size). Doesn't fail.
192 * When writing, a byte is reserved for a trailing null - see shf_sclose().
195 shf_sopen(char *buf, int bsize, int sflags, struct shf *shf)
197 /* can't have a read+write string */
198 if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
199 internal_errorf("shf_sopen: flags 0x%x", sflags);
202 shf = alloc(sizeof(struct shf), ATEMP);
203 sflags |= SHF_ALLOCS;
206 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
209 sflags |= SHF_ALLOCB;
210 buf = alloc(bsize, shf->areap);
213 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
216 shf->wnleft = bsize - 1; /* space for a '\0' */
218 shf->flags = sflags | SHF_STRING;
225 /* Flush and close file descriptor, free the shf structure */
227 shf_close(struct shf *shf)
232 ret = shf_flush(shf);
233 if (close(shf->fd) < 0)
236 if (shf->flags & SHF_ALLOCS)
237 afree(shf, shf->areap);
238 else if (shf->flags & SHF_ALLOCB)
239 afree(shf->buf, shf->areap);
244 /* Flush and close file descriptor, don't free file structure */
246 shf_fdclose(struct shf *shf)
251 ret = shf_flush(shf);
252 if (close(shf->fd) < 0)
263 /* Close a string - if it was opened for writing, it is null terminated;
264 * returns a pointer to the string and frees shf if it was allocated
265 * (does not free string if it was allocated).
268 shf_sclose(struct shf *shf)
270 unsigned char *s = shf->buf;
273 if (shf->flags & SHF_WR) {
277 if (shf->flags & SHF_ALLOCS)
278 afree(shf, shf->areap);
282 /* Un-read what has been read but not examined, or write what has been
283 * buffered. Returns 0 for success, EOF for (write) error.
286 shf_flush(struct shf *shf)
288 if (shf->flags & SHF_STRING)
289 return ((shf->flags & SHF_WR) ? EOF : 0);
292 internal_errorf("shf_flush: no fd");
294 if (shf->flags & SHF_ERROR) {
299 if (shf->flags & SHF_READING) {
300 shf->flags &= ~(SHF_EOF | SHF_READING);
301 if (shf->rnleft > 0) {
302 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
307 } else if (shf->flags & SHF_WRITING)
308 return (shf_emptybuf(shf, 0));
313 /* Write out any buffered data. If currently reading, flushes the read
314 * buffer. Returns 0 for success, EOF for (write) error.
317 shf_emptybuf(struct shf *shf, int flags)
321 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
322 internal_errorf("shf_emptybuf: no fd");
324 if (shf->flags & SHF_ERROR) {
329 if (shf->flags & SHF_READING) {
330 if (flags & EB_READSW) /* doesn't happen */
332 ret = shf_flush(shf);
333 shf->flags &= ~SHF_READING;
335 if (shf->flags & SHF_STRING) {
338 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
339 * is set... (changing the shf pointer could cause problems)
341 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
342 !(shf->flags & SHF_ALLOCB))
344 /* allocate more space for buffer */
345 nbuf = aresize(shf->buf, 2 * shf->wbsize, shf->areap);
346 shf->rp = nbuf + (shf->rp - shf->buf);
347 shf->wp = nbuf + (shf->wp - shf->buf);
348 shf->rbsize += shf->wbsize;
349 shf->wnleft += shf->wbsize;
353 if (shf->flags & SHF_WRITING) {
354 int ntowrite = shf->wp - shf->buf;
355 unsigned char *buf = shf->buf;
358 while (ntowrite > 0) {
359 n = write(shf->fd, buf, ntowrite);
361 if (errno == EINTR &&
362 !(shf->flags & SHF_INTERRUPT))
364 shf->flags |= SHF_ERROR;
367 if (buf != shf->buf) {
368 /* allow a second flush
370 memmove(shf->buf, buf,
372 shf->wp = shf->buf + ntowrite;
379 if (flags & EB_READSW) {
382 shf->flags &= ~SHF_WRITING;
387 shf->wnleft = shf->wbsize;
389 shf->flags |= SHF_WRITING;
394 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
396 shf_fillbuf(struct shf *shf)
398 if (shf->flags & SHF_STRING)
402 internal_errorf("shf_fillbuf: no fd");
404 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
405 if (shf->flags & SHF_ERROR)
410 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
413 shf->flags |= SHF_READING;
417 shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
419 if (shf->rnleft < 0 && errno == EINTR &&
420 !(shf->flags & SHF_INTERRUPT))
424 if (shf->rnleft <= 0) {
425 if (shf->rnleft < 0) {
426 shf->flags |= SHF_ERROR;
432 shf->flags |= SHF_EOF;
437 /* Read a buffer from shf. Returns the number of bytes read into buf,
438 * if no bytes were read, returns 0 if end of file was seen, EOF if
439 * a read error occurred.
442 shf_read(char *buf, int bsize, struct shf *shf)
444 int orig_bsize = bsize;
447 if (!(shf->flags & SHF_RD))
448 internal_errorf("shf_read: flags %x", shf->flags);
451 internal_errorf("shf_read: bsize %d", bsize);
454 if (shf->rnleft == 0 &&
455 (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
460 memcpy(buf, shf->rp, ncopy);
464 shf->rnleft -= ncopy;
466 /* Note: fread(3S) returns 0 for errors - this doesn't */
467 return (orig_bsize == bsize ? (shf_error(shf) ? EOF : 0) :
471 /* Read up to a newline or EOF. The newline is put in buf; buf is always
472 * null terminated. Returns NULL on read error or if nothing was read before
473 * end of file, returns a pointer to the null byte in buf otherwise.
476 shf_getse(char *buf, int bsize, struct shf *shf)
480 char *orig_buf = buf;
482 if (!(shf->flags & SHF_RD))
483 internal_errorf("shf_getse: flags %x", shf->flags);
488 --bsize; /* save room for null */
490 if (shf->rnleft == 0) {
491 if (shf_fillbuf(shf) == EOF)
493 if (shf->rnleft == 0) {
495 return (buf == orig_buf ? NULL : buf);
498 end = (unsigned char *)memchr((char *) shf->rp, '\n',
500 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
503 memcpy(buf, (char *) shf->rp, ncopy);
505 shf->rnleft -= ncopy;
508 } while (!end && bsize);
513 /* Returns the char read. Returns EOF for error and end of file. */
515 shf_getchar(struct shf *shf)
517 if (!(shf->flags & SHF_RD))
518 internal_errorf("shf_getchar: flags %x", shf->flags);
520 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
526 /* Put a character back in the input stream. Returns the character if
527 * successful, EOF if there is no room.
530 shf_ungetc(int c, struct shf *shf)
532 if (!(shf->flags & SHF_RD))
533 internal_errorf("shf_ungetc: flags %x", shf->flags);
535 if ((shf->flags & SHF_ERROR) || c == EOF ||
536 (shf->rp == shf->buf && shf->rnleft))
539 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
542 if (shf->rp == shf->buf)
543 shf->rp = shf->buf + shf->rbsize;
544 if (shf->flags & SHF_STRING) {
545 /* Can unget what was read, but not something different - we
546 * don't want to modify a string.
548 if (shf->rp[-1] != c)
550 shf->flags &= ~SHF_EOF;
555 shf->flags &= ~SHF_EOF;
561 /* Write a character. Returns the character if successful, EOF if
562 * the char could not be written.
565 shf_putchar(int c, struct shf *shf)
567 if (!(shf->flags & SHF_WR))
568 internal_errorf("shf_putchar: flags %x", shf->flags);
573 if (shf->flags & SHF_UNBUF) {
574 unsigned char cc = (unsigned char)c;
578 internal_errorf("shf_putchar: no fd");
579 if (shf->flags & SHF_ERROR) {
583 while ((n = write(shf->fd, &cc, 1)) != 1)
585 if (errno == EINTR &&
586 !(shf->flags & SHF_INTERRUPT))
588 shf->flags |= SHF_ERROR;
593 /* Flush deals with strings and sticky errors */
594 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
603 /* Write a string. Returns the length of the string if successful, EOF if
604 * the string could not be written.
607 shf_puts(const char *s, struct shf *shf)
612 return (shf_write(s, strlen(s), shf));
615 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
617 shf_write(const char *buf, int nbytes, struct shf *shf)
619 int n, ncopy, orig_nbytes = nbytes;
621 if (!(shf->flags & SHF_WR))
622 internal_errorf("shf_write: flags %x", shf->flags);
625 internal_errorf("shf_write: nbytes %d", nbytes);
627 /* Don't buffer if buffer is empty and we're writting a large amount. */
628 if ((ncopy = shf->wnleft) &&
629 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
632 memcpy(shf->wp, buf, ncopy);
636 shf->wnleft -= ncopy;
639 if (shf->flags & SHF_STRING) {
640 /* resize buffer until there's enough space left */
641 while (nbytes > shf->wnleft)
642 if (shf_emptybuf(shf, EB_GROW) == EOF)
644 /* then write everything into the buffer */
646 /* flush deals with sticky errors */
647 if (shf_emptybuf(shf, EB_GROW) == EOF)
649 /* write chunks larger than window size directly */
650 if (nbytes > shf->wbsize) {
653 ncopy -= nbytes % shf->wbsize;
656 n = write(shf->fd, buf, ncopy);
658 if (errno == EINTR &&
659 !(shf->flags & SHF_INTERRUPT))
661 shf->flags |= SHF_ERROR;
665 * Note: fwrite(3) returns 0
666 * for errors - this doesn't
674 /* ... and buffer the rest */
677 /* write remaining bytes to buffer */
678 memcpy(shf->wp, buf, nbytes);
680 shf->wnleft -= nbytes;
684 return (orig_nbytes);
688 shf_fprintf(struct shf *shf, const char *fmt, ...)
694 n = shf_vfprintf(shf, fmt, args);
701 shf_snprintf(char *buf, int bsize, const char *fmt, ...)
707 if (!buf || bsize <= 0)
708 internal_errorf("shf_snprintf: buf %p, bsize %d", buf, bsize);
710 shf_sopen(buf, bsize, SHF_WR, &shf);
712 n = shf_vfprintf(&shf, fmt, args);
714 shf_sclose(&shf); /* null terminates */
719 shf_smprintf(const char *fmt, ...)
724 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
726 shf_vfprintf(&shf, fmt, args);
728 return (shf_sclose(&shf)); /* null terminates */
731 #undef FP /* if you want floating point stuff */
734 # define DMAXEXP 128 /* should be big enough */
738 /* must be > MAX(DMAXEXP, log10(pow(2, DSIGNIF))) + ceil(log10(DMAXEXP)) + 8
739 * (I think); since it's hard to express as a constant, just use a large buffer
741 #define FPBUF_SIZE (DMAXEXP+16)
743 #define FL_HASH 0x001 /* '#' seen */
744 #define FL_PLUS 0x002 /* '+' seen */
745 #define FL_RIGHT 0x004 /* '-' seen */
746 #define FL_BLANK 0x008 /* ' ' seen */
747 #define FL_SHORT 0x010 /* 'h' seen */
748 #define FL_LONG 0x020 /* 'l' seen */
749 #define FL_ZERO 0x040 /* '0' seen */
750 #define FL_DOT 0x080 /* '.' seen */
751 #define FL_UPPER 0x100 /* format character was uppercase */
752 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
756 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
760 int tmp = 0, field, precision, len, flags;
762 /* %#o produces the longest output */
763 char numbuf[(8 * sizeof(long) + 2) / 3 + 1];
764 /* this stuff for dealing with the buffer */
770 while ((c = *fmt++)) {
777 * This will accept flags/fields in any order - not
778 * just the order specified in printf(3), but this is
779 * the way _doprnt() seems to work (on bsd and sysV).
780 * The only restriction is that the format character must
783 flags = field = precision = 0;
784 for ( ; (c = *fmt++) ; ) {
803 if (!(flags & FL_DOT))
813 tmp = va_arg(args, int);
816 else if ((field = tmp) < 0) {
830 if (ksh_isdigit(c)) {
832 while (c = *fmt++, ksh_isdigit(c))
833 tmp = tmp * 10 + c - '0';
835 if (tmp < 0) /* overflow? */
849 if (!c) /* nasty format */
852 if (c >= 'A' && c <= 'Z') {
858 case 'p': /* pointer */
859 flags &= ~(FL_LONG | FL_SHORT);
860 flags |= (sizeof(char *) > sizeof(int)) ?
861 /* hope it fits.. */ FL_LONG : 0;
869 cp = numbuf + sizeof(numbuf);
871 * XXX any better way to do this?
872 * XXX hopefully the compiler optimises this out
874 * For shorts, we want sign extend for %d but not
875 * for %[oxu] - on 16 bit machines it doesn't matter.
876 * Assumes C compiler has converted shorts to ints
877 * before pushing them. XXX optimise this -tg
880 lnum = va_arg(args, unsigned long);
881 else if ((sizeof(int) < sizeof(long)) && (c == 'd'))
882 lnum = (long)va_arg(args, int);
884 lnum = va_arg(args, unsigned int);
888 if (0 > (long)lnum) {
896 *--cp = lnum % 10 + '0';
903 else if (flags & FL_PLUS)
905 else if (flags & FL_BLANK)
912 *--cp = (lnum & 0x7) + '0';
916 if ((flags & FL_HASH) && *cp != '0')
922 const char *digits = (flags & FL_UPPER) ?
923 digits_uc : digits_lc;
925 *--cp = digits[lnum & 0xf];
929 if (flags & FL_HASH) {
930 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
935 len = numbuf + sizeof(numbuf) - (s = cp);
936 if (flags & FL_DOT) {
937 if (precision > len) {
941 precision = len; /* no loss */
946 if (!(s = va_arg(args, const char *)))
948 len = utf_mbswidth(s);
953 numbuf[0] = (char)(va_arg(args, int));
967 * At this point s should point to a string that is to be
968 * formatted, and len should be the length of the string.
970 if (!(flags & FL_DOT) || len < precision)
972 if (field > precision) {
974 if (!(flags & FL_RIGHT)) {
976 /* skip past sign or 0x when padding with 0 */
977 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
978 if (*s == '+' || *s == '-' ||
984 } else if (*s == '0') {
988 if (--precision > 0 &&
989 (*s | 0x20) == 'x') {
998 c = flags & FL_ZERO ? '0' : ' ';
1001 for ( ; field < 0 ; field++)
1009 if (precision > 0) {
1012 nwritten += precision;
1013 q = utf_skipcols(s, precision);
1020 for ( ; field > 0 ; --field)
1025 return (shf_error(shf) ? EOF : nwritten);
1030 shf_getc(struct shf *shf)
1032 return ((shf)->rnleft > 0 ? (shf)->rnleft--, *(shf)->rp++ :
1037 shf_putc(int c, struct shf *shf)
1039 return ((shf)->wnleft == 0 ? shf_putchar((c), (shf)) :
1040 ((shf)->wnleft--, *(shf)->wp++ = (c)));