1 /* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
5 * 2012, 2013, 2015, 2016
6 * mirabilos <m@mirbsd.org>
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
23 * Use %zX instead of %p and floating point isn't supported at all.
28 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.76 2016/07/25 00:04:47 tg Exp $");
30 /* flags to shf_emptybuf() */
31 #define EB_READSW 0x01 /* about to switch to reading */
32 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
35 * Replacement stdio routines. Stdio is too flakey on too many machines
36 * to be useful when you have multiple processes using the same underlying
40 static int shf_fillbuf(struct shf *);
41 static int shf_emptybuf(struct shf *, int);
44 * Open a file. First three args are for open(), last arg is flags for
45 * this package. Returns NULL if file could not be opened, or if a dup
49 shf_open(const char *name, int oflags, int mode, int sflags)
54 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
57 /* Done before open so if alloca fails, fd won't be lost. */
58 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
60 shf->buf = (unsigned char *)&shf[1];
62 shf->flags = SHF_ALLOCS;
63 /* Rest filled in by reopen. */
65 fd = binopen3(name, oflags, mode);
68 afree(shf, shf->areap);
72 if ((sflags & SHF_MAPHI) && fd < FDBASE) {
75 nfd = fcntl(fd, F_DUPFD, FDBASE);
79 afree(shf, shf->areap);
85 sflags &= ~SHF_ACCMODE;
86 sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
87 ((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
89 return (shf_reopen(fd, sflags, shf));
92 /* helper function for shf_fdopen and shf_reopen */
94 shf_open_hlp(int fd, int *sflagsp, const char *where)
96 int sflags = *sflagsp;
98 /* use fcntl() to figure out correct read/write flags */
99 if (sflags & SHF_GETFL) {
100 int flags = fcntl(fd, F_GETFL, 0);
103 /* will get an error on first read/write */
106 switch (flags & O_ACCMODE) {
121 if (!(sflags & (SHF_RD | SHF_WR)))
122 internal_errorf(Tf_sD_s, where, "missing read/write");
125 /* Set up the shf structure for a file descriptor. Doesn't fail. */
127 shf_fdopen(int fd, int sflags, struct shf *shf)
131 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
133 shf_open_hlp(fd, &sflags, "shf_fdopen");
136 shf->buf = alloc(bsize, ATEMP);
137 sflags |= SHF_ALLOCB;
141 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
142 shf->buf = (unsigned char *)&shf[1];
143 sflags |= SHF_ALLOCS;
147 shf->rp = shf->wp = shf->buf;
150 shf->wnleft = 0; /* force call to shf_emptybuf() */
151 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
155 if (sflags & SHF_CLEXEC)
156 fcntl(fd, F_SETFD, FD_CLOEXEC);
160 /* Set up an existing shf (and buffer) to use the given fd */
162 shf_reopen(int fd, int sflags, struct shf *shf)
166 sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
168 shf_open_hlp(fd, &sflags, "shf_reopen");
169 if (!shf || !shf->buf || shf->bsize < bsize)
170 internal_errorf(Tf_sD_s, "shf_reopen", Tbad_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);
187 * Open a string for reading or writing. If reading, bsize is the number
188 * of bytes that can be read. If writing, bsize is the maximum number of
189 * bytes that can be written. If shf is not NULL, it is filled in and
190 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
191 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
192 * used for the initial size). Doesn't fail.
193 * When writing, a byte is reserved for a trailing NUL - see shf_sclose().
196 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
198 /* can't have a read+write string */
199 if (!(!(sflags & SHF_RD) ^ !(sflags & SHF_WR)))
200 internal_errorf(Tf_flags, "shf_sopen",
201 (unsigned int)sflags);
204 shf = alloc(sizeof(struct shf), ATEMP);
205 sflags |= SHF_ALLOCS;
208 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
211 sflags |= SHF_ALLOCB;
212 buf = alloc(bsize, shf->areap);
215 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
218 shf->wnleft = bsize - 1; /* space for a '\0' */
220 shf->flags = sflags | SHF_STRING;
227 /* Flush and close file descriptor, free the shf structure */
229 shf_close(struct shf *shf)
234 ret = shf_flush(shf);
235 if (close(shf->fd) < 0)
238 if (shf->flags & SHF_ALLOCS)
239 afree(shf, shf->areap);
240 else if (shf->flags & SHF_ALLOCB)
241 afree(shf->buf, shf->areap);
246 /* Flush and close file descriptor, don't free file structure */
248 shf_fdclose(struct shf *shf)
253 ret = shf_flush(shf);
254 if (close(shf->fd) < 0)
266 * Close a string - if it was opened for writing, it is NUL terminated;
267 * returns a pointer to the string and frees shf if it was allocated
268 * (does not free string if it was allocated).
271 shf_sclose(struct shf *shf)
273 unsigned char *s = shf->buf;
276 if (shf->flags & SHF_WR) {
280 if (shf->flags & SHF_ALLOCS)
281 afree(shf, shf->areap);
286 * Un-read what has been read but not examined, or write what has been
287 * buffered. Returns 0 for success, -1 for (write) error.
290 shf_flush(struct shf *shf)
292 if (shf->flags & SHF_STRING)
293 return ((shf->flags & SHF_WR) ? -1 : 0);
296 internal_errorf(Tf_sD_s, "shf_flush", "no fd");
298 if (shf->flags & SHF_ERROR) {
299 errno = shf->errnosv;
303 if (shf->flags & SHF_READING) {
304 shf->flags &= ~(SHF_EOF | SHF_READING);
305 if (shf->rnleft > 0) {
306 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
311 } else if (shf->flags & SHF_WRITING)
312 return (shf_emptybuf(shf, 0));
318 * Write out any buffered data. If currently reading, flushes the read
319 * buffer. Returns 0 for success, -1 for (write) error.
322 shf_emptybuf(struct shf *shf, int flags)
326 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
327 internal_errorf(Tf_sD_s, "shf_emptybuf", "no fd");
329 if (shf->flags & SHF_ERROR) {
330 errno = shf->errnosv;
334 if (shf->flags & SHF_READING) {
335 if (flags & EB_READSW)
338 ret = shf_flush(shf);
339 shf->flags &= ~SHF_READING;
341 if (shf->flags & SHF_STRING) {
345 * Note that we assume SHF_ALLOCS is not set if
346 * SHF_ALLOCB is set... (changing the shf pointer could
349 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
350 !(shf->flags & SHF_ALLOCB))
352 /* allocate more space for buffer */
353 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
354 shf->rp = nbuf + (shf->rp - shf->buf);
355 shf->wp = nbuf + (shf->wp - shf->buf);
356 shf->rbsize += shf->wbsize;
357 shf->wnleft += shf->wbsize;
361 if (shf->flags & SHF_WRITING) {
362 ssize_t n, ntowrite = shf->wp - shf->buf;
363 unsigned char *buf = shf->buf;
365 while (ntowrite > 0) {
366 n = write(shf->fd, buf, ntowrite);
368 if (errno == EINTR &&
369 !(shf->flags & SHF_INTERRUPT))
371 shf->flags |= SHF_ERROR;
372 shf->errnosv = errno;
374 if (buf != shf->buf) {
376 * allow a second flush
379 memmove(shf->buf, buf,
381 shf->wp = shf->buf + ntowrite;
388 if (flags & EB_READSW) {
391 shf->flags &= ~SHF_WRITING;
396 shf->wnleft = shf->wbsize;
398 shf->flags |= SHF_WRITING;
403 /* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
405 shf_fillbuf(struct shf *shf)
409 if (shf->flags & SHF_STRING)
413 internal_errorf(Tf_sD_s, "shf_fillbuf", "no fd");
415 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
416 if (shf->flags & SHF_ERROR)
417 errno = shf->errnosv;
421 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
424 shf->flags |= SHF_READING;
427 while (/* CONSTCOND */ 1) {
428 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
429 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
434 shf->flags |= SHF_ERROR;
435 shf->errnosv = errno;
440 if ((shf->rnleft = n) == 0)
441 shf->flags |= SHF_EOF;
446 * Read a buffer from shf. Returns the number of bytes read into buf, if
447 * no bytes were read, returns 0 if end of file was seen, -1 if a read
451 shf_read(char *buf, ssize_t bsize, struct shf *shf)
453 ssize_t ncopy, orig_bsize = bsize;
455 if (!(shf->flags & SHF_RD))
456 internal_errorf(Tf_flags, Tshf_read,
457 (unsigned int)shf->flags);
460 internal_errorf(Tf_szs, Tshf_read, bsize, Tbsize);
463 if (shf->rnleft == 0 &&
464 (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
469 memcpy(buf, shf->rp, ncopy);
473 shf->rnleft -= ncopy;
475 /* Note: fread(3S) returns 0 for errors - this doesn't */
476 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
481 * Read up to a newline or -1. The newline is put in buf; buf is always
482 * NUL terminated. Returns NULL on read error or if nothing was read
483 * before end of file, returns a pointer to the NUL byte in buf
487 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
491 char *orig_buf = buf;
493 if (!(shf->flags & SHF_RD))
494 internal_errorf(Tf_flags, "shf_getse",
495 (unsigned int)shf->flags);
500 /* save room for NUL */
503 if (shf->rnleft == 0) {
504 if (shf_fillbuf(shf) == -1)
506 if (shf->rnleft == 0) {
508 return (buf == orig_buf ? NULL : buf);
511 end = (unsigned char *)memchr((char *)shf->rp, '\n',
513 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
516 memcpy(buf, (char *) shf->rp, ncopy);
518 shf->rnleft -= ncopy;
521 } while (!end && bsize);
526 /* Returns the char read. Returns -1 for error and end of file. */
528 shf_getchar(struct shf *shf)
530 if (!(shf->flags & SHF_RD))
531 internal_errorf(Tf_flags, "shf_getchar",
532 (unsigned int)shf->flags);
534 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
541 * Put a character back in the input stream. Returns the character if
542 * successful, -1 if there is no room.
545 shf_ungetc(int c, struct shf *shf)
547 if (!(shf->flags & SHF_RD))
548 internal_errorf(Tf_flags, "shf_ungetc",
549 (unsigned int)shf->flags);
551 if ((shf->flags & SHF_ERROR) || c == -1 ||
552 (shf->rp == shf->buf && shf->rnleft))
555 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
558 if (shf->rp == shf->buf)
559 shf->rp = shf->buf + shf->rbsize;
560 if (shf->flags & SHF_STRING) {
562 * Can unget what was read, but not something different;
563 * we don't want to modify a string.
565 if ((int)(shf->rp[-1]) != c)
567 shf->flags &= ~SHF_EOF;
572 shf->flags &= ~SHF_EOF;
579 * Write a character. Returns the character if successful, -1 if the
580 * char could not be written.
583 shf_putchar(int c, struct shf *shf)
585 if (!(shf->flags & SHF_WR))
586 internal_errorf(Tf_flags, "shf_putchar",
587 (unsigned int)shf->flags);
592 if (shf->flags & SHF_UNBUF) {
593 unsigned char cc = (unsigned char)c;
597 internal_errorf(Tf_sD_s, "shf_putchar", "no fd");
598 if (shf->flags & SHF_ERROR) {
599 errno = shf->errnosv;
602 while ((n = write(shf->fd, &cc, 1)) != 1)
604 if (errno == EINTR &&
605 !(shf->flags & SHF_INTERRUPT))
607 shf->flags |= SHF_ERROR;
608 shf->errnosv = errno;
612 /* Flush deals with strings and sticky errors */
613 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
623 * Write a string. Returns the length of the string if successful, -1
624 * if the string could not be written.
627 shf_puts(const char *s, struct shf *shf)
632 return (shf_write(s, strlen(s), shf));
635 /* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
637 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
639 ssize_t n, ncopy, orig_nbytes = nbytes;
641 if (!(shf->flags & SHF_WR))
642 internal_errorf(Tf_flags, Tshf_write,
643 (unsigned int)shf->flags);
646 internal_errorf(Tf_szs, Tshf_write, nbytes, Tbytes);
648 /* Don't buffer if buffer is empty and we're writting a large amount. */
649 if ((ncopy = shf->wnleft) &&
650 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
653 memcpy(shf->wp, buf, ncopy);
657 shf->wnleft -= ncopy;
660 if (shf->flags & SHF_STRING) {
661 /* resize buffer until there's enough space left */
662 while (nbytes > shf->wnleft)
663 if (shf_emptybuf(shf, EB_GROW) == -1)
665 /* then write everything into the buffer */
667 /* flush deals with sticky errors */
668 if (shf_emptybuf(shf, EB_GROW) == -1)
670 /* write chunks larger than window size directly */
671 if (nbytes > shf->wbsize) {
674 ncopy -= nbytes % shf->wbsize;
677 n = write(shf->fd, buf, ncopy);
679 if (errno == EINTR &&
680 !(shf->flags & SHF_INTERRUPT))
682 shf->flags |= SHF_ERROR;
683 shf->errnosv = errno;
686 * Note: fwrite(3) returns 0
687 * for errors - this doesn't
695 /* ... and buffer the rest */
698 /* write remaining bytes to buffer */
699 memcpy(shf->wp, buf, nbytes);
701 shf->wnleft -= nbytes;
705 return (orig_nbytes);
709 shf_fprintf(struct shf *shf, const char *fmt, ...)
715 n = shf_vfprintf(shf, fmt, args);
722 shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
728 if (!buf || bsize <= 0)
729 internal_errorf("shf_snprintf: buf %zX, bsize %zd",
732 shf_sopen(buf, bsize, SHF_WR, &shf);
734 n = shf_vfprintf(&shf, fmt, args);
742 shf_smprintf(const char *fmt, ...)
747 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
749 shf_vfprintf(&shf, fmt, args);
752 return (shf_sclose(&shf));
755 #define FL_HASH 0x001 /* '#' seen */
756 #define FL_PLUS 0x002 /* '+' seen */
757 #define FL_RIGHT 0x004 /* '-' seen */
758 #define FL_BLANK 0x008 /* ' ' seen */
759 #define FL_SHORT 0x010 /* 'h' seen */
760 #define FL_LONG 0x020 /* 'l' seen */
761 #define FL_ZERO 0x040 /* '0' seen */
762 #define FL_DOT 0x080 /* '.' seen */
763 #define FL_UPPER 0x100 /* format character was uppercase */
764 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
765 #define FL_SIZET 0x400 /* 'z' seen */
766 #define FM_SIZES 0x430 /* h/l/z mask */
769 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
774 size_t field, precision, len;
776 /* %#o produces the longest output */
777 char numbuf[(8 * sizeof(long) + 2) / 3 + 1 + /* NUL */ 1];
778 /* this stuff for dealing with the buffer */
779 ssize_t nwritten = 0;
781 #define VA(type) va_arg(args, type)
786 while ((c = *fmt++)) {
793 * This will accept flags/fields in any order - not just
794 * the order specified in printf(3), but this is the way
795 * _doprnt() seems to work (on BSD and SYSV). The only
796 * restriction is that the format character must come
800 field = precision = 0;
801 while ((c = *fmt++)) {
820 if (!(flags & FL_DOT))
835 field = (unsigned int)-tmp;
838 } else if (flags & FL_DOT)
839 precision = (unsigned int)tmp;
841 field = (unsigned int)tmp;
859 if (ksh_isdigit(c)) {
860 bool overflowed = false;
863 while (c = *fmt++, ksh_isdigit(c))
864 if (notok2mul(2147483647, tmp, 10))
867 tmp = tmp * 10 + ksh_numdig(c);
872 precision = (unsigned int)tmp;
874 field = (unsigned int)tmp;
884 if (ksh_isupper(c)) {
892 if (flags & FL_SIZET)
893 lnum = (long)VA(ssize_t);
894 else if (flags & FL_LONG)
896 else if (flags & FL_SHORT)
897 lnum = (long)(short)VA(int);
899 lnum = (long)VA(int);
905 if (flags & FL_SIZET)
907 else if (flags & FL_LONG)
908 lnum = VA(unsigned long);
909 else if (flags & FL_SHORT)
910 lnum = (unsigned long)(unsigned short)VA(int);
912 lnum = (unsigned long)VA(unsigned int);
916 cp = numbuf + sizeof(numbuf);
922 if (0 > (long)lnum) {
930 *--cp = digits_lc[lnum % 10];
937 else if (flags & FL_PLUS)
939 else if (flags & FL_BLANK)
946 *--cp = digits_lc[lnum & 0x7];
950 if ((flags & FL_HASH) && *cp != '0')
955 const char *digits = (flags & FL_UPPER) ?
956 digits_uc : digits_lc;
958 *--cp = digits[lnum & 0xF];
962 if (flags & FL_HASH) {
963 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
968 len = numbuf + sizeof(numbuf) - 1 - (s = cp);
969 if (flags & FL_DOT) {
970 if (precision > len) {
980 if ((s = VA(const char *)) == NULL)
982 else if (flags & FL_HASH) {
983 print_value_quoted(shf, s);
986 len = utf_mbswidth(s);
1004 * At this point s should point to a string that is to be
1005 * formatted, and len should be the length of the string.
1007 if (!(flags & FL_DOT) || len < precision)
1009 if (field > precision) {
1011 if (!(flags & FL_RIGHT)) {
1012 /* skip past sign or 0x when padding with 0 */
1013 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1014 if (*s == '+' || *s == '-' ||
1020 } else if (*s == '0') {
1025 ksh_eq(*s, 'X', 'x')) {
1034 c = flags & FL_ZERO ? '0' : ' ';
1044 nwritten += precision;
1045 precision = utf_skipcols(s, precision, &tmp) - s;
1047 shf_putc(*s++, shf);
1054 return (shf_error(shf) ? -1 : nwritten);
1057 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
1059 shf_getc(struct shf *shf)
1061 return (shf_getc_i(shf));
1065 shf_putc(int c, struct shf *shf)
1067 return (shf_putc_i(c, shf));
1073 cstrerror(int errnum)
1076 return (strerror(errnum));
1077 #define strerror dontuse_strerror /* poisoned */
1079 #elif !HAVE_STRERROR
1081 #if HAVE_SYS_ERRLIST
1082 #if !HAVE_SYS_ERRLIST_DECL
1083 extern const int sys_nerr;
1084 extern const char * const sys_errlist[];
1089 cstrerror(int errnum)
1091 /* "Unknown error: " + sign + rough estimate + NUL */
1092 static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
1094 #if HAVE_SYS_ERRLIST
1095 if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
1096 return (sys_errlist[errnum]);
1101 return ("Undefined error: 0");
1103 return ("Operation not permitted");
1105 return ("No such file or directory");
1108 return ("No such process");
1112 return ("Argument list too long");
1115 return ("Exec format error");
1117 return ("Bad file descriptor");
1120 return ("Cannot allocate memory");
1123 return ("Permission denied");
1125 return ("File exists");
1127 return ("Not a directory");
1130 return ("Invalid argument");
1134 return ("Too many levels of symbolic links");
1137 shf_snprintf(errbuf, sizeof(errbuf),
1138 "Unknown error: %d", errnum);