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, 2017
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.79 2017/04/12 17:08:49 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)
294 if (shf->flags & SHF_STRING)
295 rv = (shf->flags & SHF_WR) ? -1 : 0;
296 else if (shf->fd < 0)
297 internal_errorf(Tf_sD_s, "shf_flush", "no fd");
298 else if (shf->flags & SHF_ERROR) {
299 errno = shf->errnosv;
301 } else if (shf->flags & SHF_READING) {
302 shf->flags &= ~(SHF_EOF | SHF_READING);
303 if (shf->rnleft > 0) {
304 if (lseek(shf->fd, (off_t)-shf->rnleft,
306 shf->flags |= SHF_ERROR;
307 shf->errnosv = errno;
313 } else if (shf->flags & SHF_WRITING)
314 rv = shf_emptybuf(shf, 0);
320 * Write out any buffered data. If currently reading, flushes the read
321 * buffer. Returns 0 for success, -1 for (write) error.
324 shf_emptybuf(struct shf *shf, int flags)
328 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
329 internal_errorf(Tf_sD_s, "shf_emptybuf", "no fd");
331 if (shf->flags & SHF_ERROR) {
332 errno = shf->errnosv;
336 if (shf->flags & SHF_READING) {
337 if (flags & EB_READSW)
340 ret = shf_flush(shf);
341 shf->flags &= ~SHF_READING;
343 if (shf->flags & SHF_STRING) {
347 * Note that we assume SHF_ALLOCS is not set if
348 * SHF_ALLOCB is set... (changing the shf pointer could
351 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
352 !(shf->flags & SHF_ALLOCB))
354 /* allocate more space for buffer */
355 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
356 shf->rp = nbuf + (shf->rp - shf->buf);
357 shf->wp = nbuf + (shf->wp - shf->buf);
358 shf->rbsize += shf->wbsize;
359 shf->wnleft += shf->wbsize;
363 if (shf->flags & SHF_WRITING) {
364 ssize_t n, ntowrite = shf->wp - shf->buf;
365 unsigned char *buf = shf->buf;
367 while (ntowrite > 0) {
368 n = write(shf->fd, buf, ntowrite);
370 if (errno == EINTR &&
371 !(shf->flags & SHF_INTERRUPT))
373 shf->flags |= SHF_ERROR;
374 shf->errnosv = errno;
376 if (buf != shf->buf) {
378 * allow a second flush
381 memmove(shf->buf, buf,
383 shf->wp = shf->buf + ntowrite;
390 if (flags & EB_READSW) {
393 shf->flags &= ~SHF_WRITING;
398 shf->wnleft = shf->wbsize;
400 shf->flags |= SHF_WRITING;
405 /* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
407 shf_fillbuf(struct shf *shf)
411 if (shf->flags & SHF_STRING)
415 internal_errorf(Tf_sD_s, "shf_fillbuf", "no fd");
417 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
418 if (shf->flags & SHF_ERROR)
419 errno = shf->errnosv;
423 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
426 shf->flags |= SHF_READING;
429 while (/* CONSTCOND */ 1) {
430 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
431 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
436 shf->flags |= SHF_ERROR;
437 shf->errnosv = errno;
442 if ((shf->rnleft = n) == 0)
443 shf->flags |= SHF_EOF;
448 * Read a buffer from shf. Returns the number of bytes read into buf, if
449 * no bytes were read, returns 0 if end of file was seen, -1 if a read
453 shf_read(char *buf, ssize_t bsize, struct shf *shf)
455 ssize_t ncopy, orig_bsize = bsize;
457 if (!(shf->flags & SHF_RD))
458 internal_errorf(Tf_flags, Tshf_read,
459 (unsigned int)shf->flags);
462 internal_errorf(Tf_szs, Tshf_read, bsize, Tbsize);
465 if (shf->rnleft == 0 &&
466 (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
471 memcpy(buf, shf->rp, ncopy);
475 shf->rnleft -= ncopy;
477 /* Note: fread(3S) returns 0 for errors - this doesn't */
478 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
483 * Read up to a newline or -1. The newline is put in buf; buf is always
484 * NUL terminated. Returns NULL on read error or if nothing was read
485 * before end of file, returns a pointer to the NUL byte in buf
489 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
493 char *orig_buf = buf;
495 if (!(shf->flags & SHF_RD))
496 internal_errorf(Tf_flags, "shf_getse",
497 (unsigned int)shf->flags);
502 /* save room for NUL */
505 if (shf->rnleft == 0) {
506 if (shf_fillbuf(shf) == -1)
508 if (shf->rnleft == 0) {
510 return (buf == orig_buf ? NULL : buf);
513 end = (unsigned char *)memchr((char *)shf->rp, '\n',
515 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
518 memcpy(buf, (char *) shf->rp, ncopy);
520 shf->rnleft -= ncopy;
523 #ifdef MKSH_WITH_TEXTMODE
524 if (end && buf > orig_buf + 1 && buf[-2] == '\r') {
530 } while (!end && bsize);
531 #ifdef MKSH_WITH_TEXTMODE
532 if (!bsize && buf[-1] == '\r') {
533 int c = shf_getc(shf);
544 /* Returns the char read. Returns -1 for error and end of file. */
546 shf_getchar(struct shf *shf)
548 if (!(shf->flags & SHF_RD))
549 internal_errorf(Tf_flags, "shf_getchar",
550 (unsigned int)shf->flags);
552 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
559 * Put a character back in the input stream. Returns the character if
560 * successful, -1 if there is no room.
563 shf_ungetc(int c, struct shf *shf)
565 if (!(shf->flags & SHF_RD))
566 internal_errorf(Tf_flags, "shf_ungetc",
567 (unsigned int)shf->flags);
569 if ((shf->flags & SHF_ERROR) || c == -1 ||
570 (shf->rp == shf->buf && shf->rnleft))
573 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
576 if (shf->rp == shf->buf)
577 shf->rp = shf->buf + shf->rbsize;
578 if (shf->flags & SHF_STRING) {
580 * Can unget what was read, but not something different;
581 * we don't want to modify a string.
583 if ((int)(shf->rp[-1]) != c)
585 shf->flags &= ~SHF_EOF;
590 shf->flags &= ~SHF_EOF;
597 * Write a character. Returns the character if successful, -1 if the
598 * char could not be written.
601 shf_putchar(int c, struct shf *shf)
603 if (!(shf->flags & SHF_WR))
604 internal_errorf(Tf_flags, "shf_putchar",
605 (unsigned int)shf->flags);
610 if (shf->flags & SHF_UNBUF) {
611 unsigned char cc = (unsigned char)c;
615 internal_errorf(Tf_sD_s, "shf_putchar", "no fd");
616 if (shf->flags & SHF_ERROR) {
617 errno = shf->errnosv;
620 while ((n = write(shf->fd, &cc, 1)) != 1)
622 if (errno == EINTR &&
623 !(shf->flags & SHF_INTERRUPT))
625 shf->flags |= SHF_ERROR;
626 shf->errnosv = errno;
630 /* Flush deals with strings and sticky errors */
631 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
641 * Write a string. Returns the length of the string if successful, -1
642 * if the string could not be written.
645 shf_puts(const char *s, struct shf *shf)
650 return (shf_write(s, strlen(s), shf));
653 /* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
655 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
657 ssize_t n, ncopy, orig_nbytes = nbytes;
659 if (!(shf->flags & SHF_WR))
660 internal_errorf(Tf_flags, Tshf_write,
661 (unsigned int)shf->flags);
664 internal_errorf(Tf_szs, Tshf_write, nbytes, Tbytes);
666 /* Don't buffer if buffer is empty and we're writting a large amount. */
667 if ((ncopy = shf->wnleft) &&
668 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
671 memcpy(shf->wp, buf, ncopy);
675 shf->wnleft -= ncopy;
678 if (shf->flags & SHF_STRING) {
679 /* resize buffer until there's enough space left */
680 while (nbytes > shf->wnleft)
681 if (shf_emptybuf(shf, EB_GROW) == -1)
683 /* then write everything into the buffer */
685 /* flush deals with sticky errors */
686 if (shf_emptybuf(shf, EB_GROW) == -1)
688 /* write chunks larger than window size directly */
689 if (nbytes > shf->wbsize) {
692 ncopy -= nbytes % shf->wbsize;
695 n = write(shf->fd, buf, ncopy);
697 if (errno == EINTR &&
698 !(shf->flags & SHF_INTERRUPT))
700 shf->flags |= SHF_ERROR;
701 shf->errnosv = errno;
704 * Note: fwrite(3) returns 0
705 * for errors - this doesn't
713 /* ... and buffer the rest */
716 /* write remaining bytes to buffer */
717 memcpy(shf->wp, buf, nbytes);
719 shf->wnleft -= nbytes;
723 return (orig_nbytes);
727 shf_fprintf(struct shf *shf, const char *fmt, ...)
733 n = shf_vfprintf(shf, fmt, args);
740 shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
746 if (!buf || bsize <= 0)
747 internal_errorf("shf_snprintf: buf %zX, bsize %zd",
750 shf_sopen(buf, bsize, SHF_WR, &shf);
752 n = shf_vfprintf(&shf, fmt, args);
760 shf_smprintf(const char *fmt, ...)
765 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
767 shf_vfprintf(&shf, fmt, args);
770 return (shf_sclose(&shf));
773 #define FL_HASH 0x001 /* '#' seen */
774 #define FL_PLUS 0x002 /* '+' seen */
775 #define FL_RIGHT 0x004 /* '-' seen */
776 #define FL_BLANK 0x008 /* ' ' seen */
777 #define FL_SHORT 0x010 /* 'h' seen */
778 #define FL_LONG 0x020 /* 'l' seen */
779 #define FL_ZERO 0x040 /* '0' seen */
780 #define FL_DOT 0x080 /* '.' seen */
781 #define FL_UPPER 0x100 /* format character was uppercase */
782 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
783 #define FL_SIZET 0x400 /* 'z' seen */
784 #define FM_SIZES 0x430 /* h/l/z mask */
787 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
792 size_t field, precision, len;
794 /* %#o produces the longest output */
795 char numbuf[(8 * sizeof(long) + 2) / 3 + 1 + /* NUL */ 1];
796 /* this stuff for dealing with the buffer */
797 ssize_t nwritten = 0;
799 #define VA(type) va_arg(args, type)
804 while ((c = *fmt++)) {
811 * This will accept flags/fields in any order - not just
812 * the order specified in printf(3), but this is the way
813 * _doprnt() seems to work (on BSD and SYSV). The only
814 * restriction is that the format character must come
818 field = precision = 0;
819 while ((c = *fmt++)) {
838 if (!(flags & FL_DOT))
853 field = (unsigned int)-tmp;
856 } else if (flags & FL_DOT)
857 precision = (unsigned int)tmp;
859 field = (unsigned int)tmp;
877 if (ksh_isdigit(c)) {
878 bool overflowed = false;
881 while (c = *fmt++, ksh_isdigit(c))
882 if (notok2mul(2147483647, tmp, 10))
885 tmp = tmp * 10 + ksh_numdig(c);
890 precision = (unsigned int)tmp;
892 field = (unsigned int)tmp;
902 if (ksh_isupper(c)) {
910 if (flags & FL_SIZET)
911 lnum = (long)VA(ssize_t);
912 else if (flags & FL_LONG)
914 else if (flags & FL_SHORT)
915 lnum = (long)(short)VA(int);
917 lnum = (long)VA(int);
923 if (flags & FL_SIZET)
925 else if (flags & FL_LONG)
926 lnum = VA(unsigned long);
927 else if (flags & FL_SHORT)
928 lnum = (unsigned long)(unsigned short)VA(int);
930 lnum = (unsigned long)VA(unsigned int);
934 cp = numbuf + sizeof(numbuf);
940 if (0 > (long)lnum) {
948 *--cp = digits_lc[lnum % 10];
955 else if (flags & FL_PLUS)
957 else if (flags & FL_BLANK)
964 *--cp = digits_lc[lnum & 0x7];
968 if ((flags & FL_HASH) && *cp != '0')
973 const char *digits = (flags & FL_UPPER) ?
974 digits_uc : digits_lc;
976 *--cp = digits[lnum & 0xF];
980 if (flags & FL_HASH) {
981 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
986 len = numbuf + sizeof(numbuf) - 1 - (s = cp);
987 if (flags & FL_DOT) {
988 if (precision > len) {
998 if ((s = VA(const char *)) == NULL)
1000 else if (flags & FL_HASH) {
1001 print_value_quoted(shf, s);
1004 len = utf_mbswidth(s);
1009 c = (char)(VA(int));
1022 * At this point s should point to a string that is to be
1023 * formatted, and len should be the length of the string.
1025 if (!(flags & FL_DOT) || len < precision)
1027 if (field > precision) {
1029 if (!(flags & FL_RIGHT)) {
1030 /* skip past sign or 0x when padding with 0 */
1031 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1032 if (*s == '+' || *s == '-' ||
1038 } else if (*s == '0') {
1043 ksh_eq(*s, 'X', 'x')) {
1052 c = flags & FL_ZERO ? '0' : ' ';
1062 nwritten += precision;
1063 precision = utf_skipcols(s, precision, &tmp) - s;
1065 shf_putc(*s++, shf);
1072 return (shf_error(shf) ? -1 : nwritten);
1075 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
1077 shf_getc(struct shf *shf)
1079 return (shf_getc_i(shf));
1083 shf_putc(int c, struct shf *shf)
1085 return (shf_putc_i(c, shf));
1091 cstrerror(int errnum)
1094 return (strerror(errnum));
1095 #define strerror dontuse_strerror /* poisoned */
1097 #elif !HAVE_STRERROR
1099 #if HAVE_SYS_ERRLIST
1100 #if !HAVE_SYS_ERRLIST_DECL
1101 extern const int sys_nerr;
1102 extern const char * const sys_errlist[];
1107 cstrerror(int errnum)
1109 /* "Unknown error: " + sign + rough estimate + NUL */
1110 static char errbuf[15 + 1 + (8 * sizeof(int) + 2) / 3 + 1];
1112 #if HAVE_SYS_ERRLIST
1113 if (errnum > 0 && errnum < sys_nerr && sys_errlist[errnum])
1114 return (sys_errlist[errnum]);
1119 return ("Undefined error: 0");
1121 return ("Operation not permitted");
1123 return ("No such file or directory");
1126 return ("No such process");
1130 return ("Argument list too long");
1133 return ("Exec format error");
1135 return ("Bad file descriptor");
1138 return ("Cannot allocate memory");
1141 return ("Permission denied");
1143 return ("File exists");
1145 return ("Not a directory");
1148 return ("Invalid argument");
1152 return ("Too many levels of symbolic links");
1155 shf_snprintf(errbuf, sizeof(errbuf),
1156 "Unknown error: %d", errnum);