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, 2011, 2012
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.
22 * Use %zX instead of %p and floating point isn't supported at all.
27 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.56 2013/01/01 03:32:44 tg Exp $");
29 /* flags to shf_emptybuf() */
30 #define EB_READSW 0x01 /* about to switch to reading */
31 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
34 * Replacement stdio routines. Stdio is too flakey on too many machines
35 * to be useful when you have multiple processes using the same underlying
39 static int shf_fillbuf(struct shf *);
40 static int shf_emptybuf(struct shf *, int);
43 * Open a file. First three args are for open(), last arg is flags for
44 * this package. Returns NULL if file could not be opened, or if a dup
48 shf_open(const char *name, int oflags, int mode, int sflags)
53 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
56 /* Done before open so if alloca fails, fd won't be lost. */
57 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
59 shf->buf = (unsigned char *)&shf[1];
61 shf->flags = SHF_ALLOCS;
62 /* Rest filled in by reopen. */
64 fd = open(name, oflags, mode);
66 afree(shf, shf->areap);
69 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
72 nfd = fcntl(fd, F_DUPFD, FDBASE);
75 afree(shf, shf->areap);
80 sflags &= ~SHF_ACCMODE;
81 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
82 ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
84 return (shf_reopen(fd, sflags, shf));
87 /* helper function for shf_fdopen and shf_reopen */
89 shf_open_hlp(int fd, int *sflagsp, const char *where)
91 int sflags = *sflagsp;
93 /* use fcntl() to figure out correct read/write flags */
94 if (sflags & SHF_GETFL) {
95 int flags = fcntl(fd, F_GETFL, 0);
98 /* will get an error on first read/write */
101 switch (flags & O_ACCMODE) {
116 if (!(sflags & (SHF_RD | SHF_WR)))
117 internal_errorf("%s: %s", where, "missing read/write");
120 /* Set up the shf structure for a file descriptor. Doesn't fail. */
122 shf_fdopen(int fd, int sflags, struct shf *shf)
126 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
128 shf_open_hlp(fd, &sflags, "shf_fdopen");
131 shf->buf = alloc(bsize, ATEMP);
132 sflags |= SHF_ALLOCB;
136 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
137 shf->buf = (unsigned char *)&shf[1];
138 sflags |= SHF_ALLOCS;
142 shf->rp = shf->wp = shf->buf;
145 shf->wnleft = 0; /* force call to shf_emptybuf() */
146 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
150 if (sflags & SHF_CLEXEC)
151 fcntl(fd, F_SETFD, FD_CLOEXEC);
155 /* Set up an existing shf (and buffer) to use the given fd */
157 shf_reopen(int fd, int sflags, struct shf *shf)
161 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
163 shf_open_hlp(fd, &sflags, "shf_reopen");
164 if (!shf || !shf->buf || shf->bsize < bsize)
165 internal_errorf("%s: %s", "shf_reopen", "bad shf/buf/bsize");
167 /* assumes shf->buf and shf->bsize already set up */
169 shf->rp = shf->wp = shf->buf;
172 shf->wnleft = 0; /* force call to shf_emptybuf() */
173 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
174 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
176 if (sflags & SHF_CLEXEC)
177 fcntl(fd, F_SETFD, FD_CLOEXEC);
182 * Open a string for reading or writing. If reading, bsize is the number
183 * of bytes that can be read. If writing, bsize is the maximum number of
184 * bytes that can be written. If shf is not NULL, it is filled in and
185 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
186 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
187 * used for the initial size). Doesn't fail.
188 * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
191 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
193 /* can't have a read+write string */
194 if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
195 internal_errorf("%s: flags 0x%X", "shf_sopen", sflags);
198 shf = alloc(sizeof(struct shf), ATEMP);
199 sflags |= SHF_ALLOCS;
202 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
205 sflags |= SHF_ALLOCB;
206 buf = alloc(bsize, shf->areap);
209 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
212 shf->wnleft = bsize - 1; /* space for a '\0' */
214 shf->flags = sflags | SHF_STRING;
221 /* Flush and close file descriptor, free the shf structure */
223 shf_close(struct shf *shf)
228 ret = shf_flush(shf);
229 if (close(shf->fd) < 0)
232 if (shf->flags & SHF_ALLOCS)
233 afree(shf, shf->areap);
234 else if (shf->flags & SHF_ALLOCB)
235 afree(shf->buf, shf->areap);
240 /* Flush and close file descriptor, don't free file structure */
242 shf_fdclose(struct shf *shf)
247 ret = shf_flush(shf);
248 if (close(shf->fd) < 0)
260 * Close a string - if it was opened for writing, it is NUL terminated;
261 * returns a pointer to the string and frees shf if it was allocated
262 * (does not free string if it was allocated).
265 shf_sclose(struct shf *shf)
267 unsigned char *s = shf->buf;
270 if (shf->flags & SHF_WR) {
274 if (shf->flags & SHF_ALLOCS)
275 afree(shf, shf->areap);
280 * Un-read what has been read but not examined, or write what has been
281 * buffered. Returns 0 for success, EOF for (write) error.
284 shf_flush(struct shf *shf)
286 if (shf->flags & SHF_STRING)
287 return ((shf->flags & SHF_WR) ? EOF : 0);
290 internal_errorf("%s: %s", "shf_flush", "no fd");
292 if (shf->flags & SHF_ERROR) {
293 errno = shf->errnosv;
297 if (shf->flags & SHF_READING) {
298 shf->flags &= ~(SHF_EOF | SHF_READING);
299 if (shf->rnleft > 0) {
300 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
305 } else if (shf->flags & SHF_WRITING)
306 return (shf_emptybuf(shf, 0));
312 * Write out any buffered data. If currently reading, flushes the read
313 * buffer. Returns 0 for success, EOF for (write) error.
316 shf_emptybuf(struct shf *shf, int flags)
320 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
321 internal_errorf("%s: %s", "shf_emptybuf", "no fd");
323 if (shf->flags & SHF_ERROR) {
324 errno = shf->errnosv;
328 if (shf->flags & SHF_READING) {
329 if (flags & EB_READSW)
332 ret = shf_flush(shf);
333 shf->flags &= ~SHF_READING;
335 if (shf->flags & SHF_STRING) {
339 * Note that we assume SHF_ALLOCS is not set if
340 * SHF_ALLOCB is set... (changing the shf pointer could
343 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
344 !(shf->flags & SHF_ALLOCB))
346 /* allocate more space for buffer */
347 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
348 shf->rp = nbuf + (shf->rp - shf->buf);
349 shf->wp = nbuf + (shf->wp - shf->buf);
350 shf->rbsize += shf->wbsize;
351 shf->wnleft += shf->wbsize;
355 if (shf->flags & SHF_WRITING) {
356 ssize_t n, ntowrite = shf->wp - shf->buf;
357 unsigned char *buf = shf->buf;
359 while (ntowrite > 0) {
360 n = write(shf->fd, buf, ntowrite);
362 if (errno == EINTR &&
363 !(shf->flags & SHF_INTERRUPT))
365 shf->flags |= SHF_ERROR;
366 shf->errnosv = errno;
368 if (buf != shf->buf) {
370 * allow a second flush
373 memmove(shf->buf, buf,
375 shf->wp = shf->buf + ntowrite;
382 if (flags & EB_READSW) {
385 shf->flags &= ~SHF_WRITING;
390 shf->wnleft = shf->wbsize;
392 shf->flags |= SHF_WRITING;
397 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
399 shf_fillbuf(struct shf *shf)
403 if (shf->flags & SHF_STRING)
407 internal_errorf("%s: %s", "shf_fillbuf", "no fd");
409 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
410 if (shf->flags & SHF_ERROR)
411 errno = shf->errnosv;
415 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
418 shf->flags |= SHF_READING;
421 while (/* CONSTCOND */ 1) {
422 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
423 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
428 shf->flags |= SHF_ERROR;
429 shf->errnosv = errno;
434 if ((shf->rnleft = n) == 0)
435 shf->flags |= SHF_EOF;
440 * Read a buffer from shf. Returns the number of bytes read into buf, if
441 * no bytes were read, returns 0 if end of file was seen, EOF if a read
445 shf_read(char *buf, ssize_t bsize, struct shf *shf)
447 ssize_t ncopy, orig_bsize = bsize;
449 if (!(shf->flags & SHF_RD))
450 internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
453 internal_errorf("%s: %s %zd", "shf_write", "bsize", bsize);
456 if (shf->rnleft == 0 &&
457 (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
462 memcpy(buf, shf->rp, ncopy);
466 shf->rnleft -= ncopy;
468 /* Note: fread(3S) returns 0 for errors - this doesn't */
469 return (orig_bsize == bsize ? (shf_error(shf) ? EOF : 0) :
474 * Read up to a newline or EOF. The newline is put in buf; buf is always
475 * NUL terminated. Returns NULL on read error or if nothing was read
476 * before end of file, returns a pointer to the NUL byte in buf
480 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
484 char *orig_buf = buf;
486 if (!(shf->flags & SHF_RD))
487 internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
492 /* save room for NUL */
495 if (shf->rnleft == 0) {
496 if (shf_fillbuf(shf) == EOF)
498 if (shf->rnleft == 0) {
500 return (buf == orig_buf ? NULL : buf);
503 end = (unsigned char *)memchr((char *)shf->rp, '\n',
505 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
508 memcpy(buf, (char *) shf->rp, ncopy);
510 shf->rnleft -= ncopy;
513 } while (!end && bsize);
518 /* Returns the char read. Returns EOF for error and end of file. */
520 shf_getchar(struct shf *shf)
522 if (!(shf->flags & SHF_RD))
523 internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
525 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
532 * Put a character back in the input stream. Returns the character if
533 * successful, EOF if there is no room.
536 shf_ungetc(int c, struct shf *shf)
538 if (!(shf->flags & SHF_RD))
539 internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
541 if ((shf->flags & SHF_ERROR) || c == EOF ||
542 (shf->rp == shf->buf && shf->rnleft))
545 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
548 if (shf->rp == shf->buf)
549 shf->rp = shf->buf + shf->rbsize;
550 if (shf->flags & SHF_STRING) {
552 * Can unget what was read, but not something different;
553 * we don't want to modify a string.
555 if ((int)(shf->rp[-1]) != c)
557 shf->flags &= ~SHF_EOF;
562 shf->flags &= ~SHF_EOF;
569 * Write a character. Returns the character if successful, EOF if the
570 * char could not be written.
573 shf_putchar(int c, struct shf *shf)
575 if (!(shf->flags & SHF_WR))
576 internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
581 if (shf->flags & SHF_UNBUF) {
582 unsigned char cc = (unsigned char)c;
586 internal_errorf("%s: %s", "shf_putchar", "no fd");
587 if (shf->flags & SHF_ERROR) {
588 errno = shf->errnosv;
591 while ((n = write(shf->fd, &cc, 1)) != 1)
593 if (errno == EINTR &&
594 !(shf->flags & SHF_INTERRUPT))
596 shf->flags |= SHF_ERROR;
597 shf->errnosv = errno;
601 /* Flush deals with strings and sticky errors */
602 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
612 * Write a string. Returns the length of the string if successful, EOF
613 * if the string could not be written.
616 shf_puts(const char *s, struct shf *shf)
621 return (shf_write(s, strlen(s), shf));
624 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
626 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
628 ssize_t n, ncopy, orig_nbytes = nbytes;
630 if (!(shf->flags & SHF_WR))
631 internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
634 internal_errorf("%s: %s %zd", "shf_write", "nbytes", nbytes);
636 /* Don't buffer if buffer is empty and we're writting a large amount. */
637 if ((ncopy = shf->wnleft) &&
638 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
641 memcpy(shf->wp, buf, ncopy);
645 shf->wnleft -= ncopy;
648 if (shf->flags & SHF_STRING) {
649 /* resize buffer until there's enough space left */
650 while (nbytes > shf->wnleft)
651 if (shf_emptybuf(shf, EB_GROW) == EOF)
653 /* then write everything into the buffer */
655 /* flush deals with sticky errors */
656 if (shf_emptybuf(shf, EB_GROW) == EOF)
658 /* write chunks larger than window size directly */
659 if (nbytes > shf->wbsize) {
662 ncopy -= nbytes % shf->wbsize;
665 n = write(shf->fd, buf, ncopy);
667 if (errno == EINTR &&
668 !(shf->flags & SHF_INTERRUPT))
670 shf->flags |= SHF_ERROR;
671 shf->errnosv = errno;
674 * Note: fwrite(3) returns 0
675 * for errors - this doesn't
683 /* ... and buffer the rest */
686 /* write remaining bytes to buffer */
687 memcpy(shf->wp, buf, nbytes);
689 shf->wnleft -= nbytes;
693 return (orig_nbytes);
697 shf_fprintf(struct shf *shf, const char *fmt, ...)
703 n = shf_vfprintf(shf, fmt, args);
710 shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
716 if (!buf || bsize <= 0)
717 internal_errorf("shf_snprintf: buf %zX, bsize %zd",
720 shf_sopen(buf, bsize, SHF_WR, &shf);
722 n = shf_vfprintf(&shf, fmt, args);
730 shf_smprintf(const char *fmt, ...)
735 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
737 shf_vfprintf(&shf, fmt, args);
740 return (shf_sclose(&shf));
745 #define FL_HASH 0x001 /* '#' seen */
746 #define FL_PLUS 0x002 /* '+' seen */
747 #define FL_RIGHT 0x004 /* '-' seen */
748 #define FL_BLANK 0x008 /* ' ' seen */
749 #define FL_SHORT 0x010 /* 'h' seen */
750 #define FL_LONG 0x020 /* 'l' seen */
751 #define FL_ZERO 0x040 /* '0' seen */
752 #define FL_DOT 0x080 /* '.' seen */
753 #define FL_UPPER 0x100 /* format character was uppercase */
754 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
755 #define FL_SIZET 0x400 /* 'z' seen */
756 #define FM_SIZES 0x430 /* h/l/z mask */
759 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
764 ssize_t field, precision, len;
766 /* %#o produces the longest output */
767 char numbuf[(8 * sizeof(long) + 2) / 3 + 1
769 /* a NUL for LLVM/Clang scan-build */
773 /* this stuff for dealing with the buffer */
774 ssize_t nwritten = 0;
776 #define VA(type) va_arg(args, type)
781 while ((c = *fmt++)) {
788 * This will accept flags/fields in any order - not just
789 * the order specified in printf(3), but this is the way
790 * _doprnt() seems to work (on BSD and SYSV). The only
791 * restriction is that the format character must come
795 field = precision = 0;
796 for ( ; (c = *fmt++) ; ) {
815 if (!(flags & FL_DOT))
828 else if ((field = tmp) < 0) {
849 if (ksh_isdigit(c)) {
850 bool overflowed = false;
853 while (c = *fmt++, ksh_isdigit(c)) {
854 if (notok2mul(2147483647, tmp, 10))
856 tmp = tmp * 10 + c - '0';
877 if (c >= 'A' && c <= 'Z') {
885 if (flags & FL_SIZET)
886 lnum = (long)VA(ssize_t);
887 else if (flags & FL_LONG)
889 else if (flags & FL_SHORT)
890 lnum = (long)(short)VA(int);
892 lnum = (long)VA(int);
898 if (flags & FL_SIZET)
900 else if (flags & FL_LONG)
901 lnum = VA(unsigned long);
902 else if (flags & FL_SHORT)
903 lnum = (unsigned long)(unsigned short)VA(int);
905 lnum = (unsigned long)VA(unsigned int);
909 cp = numbuf + sizeof(numbuf);
912 * this is necessary so Clang 3.2 realises
913 * utf_skipcols/shf_putc in the output loop
914 * terminate; these values are always ASCII
915 * so an out-of-bounds access cannot happen
916 * but Clang doesn't know that
924 if (0 > (long)lnum) {
932 *--cp = lnum % 10 + '0';
939 else if (flags & FL_PLUS)
941 else if (flags & FL_BLANK)
948 *--cp = (lnum & 0x7) + '0';
952 if ((flags & FL_HASH) && *cp != '0')
957 const char *digits = (flags & FL_UPPER) ?
958 digits_uc : digits_lc;
960 *--cp = digits[lnum & 0xf];
964 if (flags & FL_HASH) {
965 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
970 len = numbuf + sizeof(numbuf) - (s = cp);
972 /* see above comment for Clang 3.2 */
975 if (flags & FL_DOT) {
976 if (precision > len) {
986 if ((s = VA(const char *)) == NULL)
988 len = utf_mbswidth(s);
1006 * At this point s should point to a string that is to be
1007 * formatted, and len should be the length of the string.
1009 if (!(flags & FL_DOT) || len < precision)
1011 if (field > precision) {
1013 if (!(flags & FL_RIGHT)) {
1015 /* skip past sign or 0x when padding with 0 */
1016 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1017 if (*s == '+' || *s == '-' ||
1023 } else if (*s == '0') {
1027 if (--precision > 0 &&
1028 (*s | 0x20) == 'x') {
1037 c = flags & FL_ZERO ? '0' : ' ';
1040 for ( ; field < 0 ; field++)
1048 if (precision > 0) {
1051 nwritten += precision;
1052 q = utf_skipcols(s, precision);
1059 for ( ; field > 0 ; --field)
1064 return (shf_error(shf) ? EOF : nwritten);
1067 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
1069 shf_getc(struct shf *shf)
1071 return (shf_getc_i(shf));
1075 shf_putc(int c, struct shf *shf)
1077 return (shf_putc_i(c, shf));
1083 cstrerror(int errnum)
1086 return (strerror(errnum));
1087 #define strerror dontuse_strerror /* poisoned */
1089 #elif !HAVE_STRERROR
1091 #if HAVE_SYS_ERRLIST
1092 #if !HAVE_SYS_ERRLIST_DECL
1093 extern const int sys_nerr;
1094 extern const char * const sys_errlist[];
1099 cstrerror(int errnum)
1101 /* "Unknown error: " + sign + rough estimate + NUL */
1102 static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
1104 #if HAVE_SYS_ERRLIST
1105 if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
1106 return (sys_errlist[errnum]);
1111 return ("Undefined error: 0");
1114 return ("Operation not permitted");
1118 return ("No such file or directory");
1122 return ("No such process");
1126 return ("Argument list too long");
1130 return ("Exec format error");
1134 return ("Cannot allocate memory");
1138 return ("Permission denied");
1142 return ("Not a directory");
1146 return ("Invalid argument");
1150 return ("Too many levels of symbolic links");
1153 shf_snprintf(errbuf, sizeof(errbuf),
1154 "Unknown error: %d", errnum);