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,
6 * Thorsten Glaser <tg@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.66 2015/07/09 20:52:43 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("%s: %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("%s: %s", "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);
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("%s: flags 0x%X", "shf_sopen", sflags);
203 shf = alloc(sizeof(struct shf), ATEMP);
204 sflags |= SHF_ALLOCS;
207 if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
210 sflags |= SHF_ALLOCB;
211 buf = alloc(bsize, shf->areap);
214 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
217 shf->wnleft = bsize - 1; /* space for a '\0' */
219 shf->flags = sflags | SHF_STRING;
226 /* Flush and close file descriptor, free the shf structure */
228 shf_close(struct shf *shf)
233 ret = shf_flush(shf);
234 if (close(shf->fd) < 0)
237 if (shf->flags & SHF_ALLOCS)
238 afree(shf, shf->areap);
239 else if (shf->flags & SHF_ALLOCB)
240 afree(shf->buf, shf->areap);
245 /* Flush and close file descriptor, don't free file structure */
247 shf_fdclose(struct shf *shf)
252 ret = shf_flush(shf);
253 if (close(shf->fd) < 0)
265 * Close a string - if it was opened for writing, it is NUL terminated;
266 * returns a pointer to the string and frees shf if it was allocated
267 * (does not free string if it was allocated).
270 shf_sclose(struct shf *shf)
272 unsigned char *s = shf->buf;
275 if (shf->flags & SHF_WR) {
279 if (shf->flags & SHF_ALLOCS)
280 afree(shf, shf->areap);
285 * Un-read what has been read but not examined, or write what has been
286 * buffered. Returns 0 for success, -1 for (write) error.
289 shf_flush(struct shf *shf)
291 if (shf->flags & SHF_STRING)
292 return ((shf->flags & SHF_WR) ? -1 : 0);
295 internal_errorf("%s: %s", "shf_flush", "no fd");
297 if (shf->flags & SHF_ERROR) {
298 errno = shf->errnosv;
302 if (shf->flags & SHF_READING) {
303 shf->flags &= ~(SHF_EOF | SHF_READING);
304 if (shf->rnleft > 0) {
305 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
310 } else if (shf->flags & SHF_WRITING)
311 return (shf_emptybuf(shf, 0));
317 * Write out any buffered data. If currently reading, flushes the read
318 * buffer. Returns 0 for success, -1 for (write) error.
321 shf_emptybuf(struct shf *shf, int flags)
325 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
326 internal_errorf("%s: %s", "shf_emptybuf", "no fd");
328 if (shf->flags & SHF_ERROR) {
329 errno = shf->errnosv;
333 if (shf->flags & SHF_READING) {
334 if (flags & EB_READSW)
337 ret = shf_flush(shf);
338 shf->flags &= ~SHF_READING;
340 if (shf->flags & SHF_STRING) {
344 * Note that we assume SHF_ALLOCS is not set if
345 * SHF_ALLOCB is set... (changing the shf pointer could
348 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
349 !(shf->flags & SHF_ALLOCB))
351 /* allocate more space for buffer */
352 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
353 shf->rp = nbuf + (shf->rp - shf->buf);
354 shf->wp = nbuf + (shf->wp - shf->buf);
355 shf->rbsize += shf->wbsize;
356 shf->wnleft += shf->wbsize;
360 if (shf->flags & SHF_WRITING) {
361 ssize_t n, ntowrite = shf->wp - shf->buf;
362 unsigned char *buf = shf->buf;
364 while (ntowrite > 0) {
365 n = write(shf->fd, buf, ntowrite);
367 if (errno == EINTR &&
368 !(shf->flags & SHF_INTERRUPT))
370 shf->flags |= SHF_ERROR;
371 shf->errnosv = errno;
373 if (buf != shf->buf) {
375 * allow a second flush
378 memmove(shf->buf, buf,
380 shf->wp = shf->buf + ntowrite;
387 if (flags & EB_READSW) {
390 shf->flags &= ~SHF_WRITING;
395 shf->wnleft = shf->wbsize;
397 shf->flags |= SHF_WRITING;
402 /* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
404 shf_fillbuf(struct shf *shf)
408 if (shf->flags & SHF_STRING)
412 internal_errorf("%s: %s", "shf_fillbuf", "no fd");
414 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
415 if (shf->flags & SHF_ERROR)
416 errno = shf->errnosv;
420 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
423 shf->flags |= SHF_READING;
426 while (/* CONSTCOND */ 1) {
427 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
428 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
433 shf->flags |= SHF_ERROR;
434 shf->errnosv = errno;
439 if ((shf->rnleft = n) == 0)
440 shf->flags |= SHF_EOF;
445 * Read a buffer from shf. Returns the number of bytes read into buf, if
446 * no bytes were read, returns 0 if end of file was seen, -1 if a read
450 shf_read(char *buf, ssize_t bsize, struct shf *shf)
452 ssize_t ncopy, orig_bsize = bsize;
454 if (!(shf->flags & SHF_RD))
455 internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
458 internal_errorf("%s: %s %zd", "shf_write", "bsize", bsize);
461 if (shf->rnleft == 0 &&
462 (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
467 memcpy(buf, shf->rp, ncopy);
471 shf->rnleft -= ncopy;
473 /* Note: fread(3S) returns 0 for errors - this doesn't */
474 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
479 * Read up to a newline or -1. The newline is put in buf; buf is always
480 * NUL terminated. Returns NULL on read error or if nothing was read
481 * before end of file, returns a pointer to the NUL byte in buf
485 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
489 char *orig_buf = buf;
491 if (!(shf->flags & SHF_RD))
492 internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
497 /* save room for NUL */
500 if (shf->rnleft == 0) {
501 if (shf_fillbuf(shf) == -1)
503 if (shf->rnleft == 0) {
505 return (buf == orig_buf ? NULL : buf);
508 end = (unsigned char *)memchr((char *)shf->rp, '\n',
510 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
513 memcpy(buf, (char *) shf->rp, ncopy);
515 shf->rnleft -= ncopy;
518 } while (!end && bsize);
523 /* Returns the char read. Returns -1 for error and end of file. */
525 shf_getchar(struct shf *shf)
527 if (!(shf->flags & SHF_RD))
528 internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
530 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
537 * Put a character back in the input stream. Returns the character if
538 * successful, -1 if there is no room.
541 shf_ungetc(int c, struct shf *shf)
543 if (!(shf->flags & SHF_RD))
544 internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
546 if ((shf->flags & SHF_ERROR) || c == -1 ||
547 (shf->rp == shf->buf && shf->rnleft))
550 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
553 if (shf->rp == shf->buf)
554 shf->rp = shf->buf + shf->rbsize;
555 if (shf->flags & SHF_STRING) {
557 * Can unget what was read, but not something different;
558 * we don't want to modify a string.
560 if ((int)(shf->rp[-1]) != c)
562 shf->flags &= ~SHF_EOF;
567 shf->flags &= ~SHF_EOF;
574 * Write a character. Returns the character if successful, -1 if the
575 * char could not be written.
578 shf_putchar(int c, struct shf *shf)
580 if (!(shf->flags & SHF_WR))
581 internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
586 if (shf->flags & SHF_UNBUF) {
587 unsigned char cc = (unsigned char)c;
591 internal_errorf("%s: %s", "shf_putchar", "no fd");
592 if (shf->flags & SHF_ERROR) {
593 errno = shf->errnosv;
596 while ((n = write(shf->fd, &cc, 1)) != 1)
598 if (errno == EINTR &&
599 !(shf->flags & SHF_INTERRUPT))
601 shf->flags |= SHF_ERROR;
602 shf->errnosv = errno;
606 /* Flush deals with strings and sticky errors */
607 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
617 * Write a string. Returns the length of the string if successful, -1
618 * if the string could not be written.
621 shf_puts(const char *s, struct shf *shf)
626 return (shf_write(s, strlen(s), shf));
629 /* Write a buffer. Returns nbytes if successful, -1 if there is an error. */
631 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
633 ssize_t n, ncopy, orig_nbytes = nbytes;
635 if (!(shf->flags & SHF_WR))
636 internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
639 internal_errorf("%s: %s %zd", "shf_write", "nbytes", nbytes);
641 /* Don't buffer if buffer is empty and we're writting a large amount. */
642 if ((ncopy = shf->wnleft) &&
643 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
646 memcpy(shf->wp, buf, ncopy);
650 shf->wnleft -= ncopy;
653 if (shf->flags & SHF_STRING) {
654 /* resize buffer until there's enough space left */
655 while (nbytes > shf->wnleft)
656 if (shf_emptybuf(shf, EB_GROW) == -1)
658 /* then write everything into the buffer */
660 /* flush deals with sticky errors */
661 if (shf_emptybuf(shf, EB_GROW) == -1)
663 /* write chunks larger than window size directly */
664 if (nbytes > shf->wbsize) {
667 ncopy -= nbytes % shf->wbsize;
670 n = write(shf->fd, buf, ncopy);
672 if (errno == EINTR &&
673 !(shf->flags & SHF_INTERRUPT))
675 shf->flags |= SHF_ERROR;
676 shf->errnosv = errno;
679 * Note: fwrite(3) returns 0
680 * for errors - this doesn't
688 /* ... and buffer the rest */
691 /* write remaining bytes to buffer */
692 memcpy(shf->wp, buf, nbytes);
694 shf->wnleft -= nbytes;
698 return (orig_nbytes);
702 shf_fprintf(struct shf *shf, const char *fmt, ...)
708 n = shf_vfprintf(shf, fmt, args);
715 shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
721 if (!buf || bsize <= 0)
722 internal_errorf("shf_snprintf: buf %zX, bsize %zd",
725 shf_sopen(buf, bsize, SHF_WR, &shf);
727 n = shf_vfprintf(&shf, fmt, args);
735 shf_smprintf(const char *fmt, ...)
740 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
742 shf_vfprintf(&shf, fmt, args);
745 return (shf_sclose(&shf));
748 #define FL_HASH 0x001 /* '#' seen */
749 #define FL_PLUS 0x002 /* '+' seen */
750 #define FL_RIGHT 0x004 /* '-' seen */
751 #define FL_BLANK 0x008 /* ' ' seen */
752 #define FL_SHORT 0x010 /* 'h' seen */
753 #define FL_LONG 0x020 /* 'l' seen */
754 #define FL_ZERO 0x040 /* '0' seen */
755 #define FL_DOT 0x080 /* '.' seen */
756 #define FL_UPPER 0x100 /* format character was uppercase */
757 #define FL_NUMBER 0x200 /* a number was formated %[douxefg] */
758 #define FL_SIZET 0x400 /* 'z' seen */
759 #define FM_SIZES 0x430 /* h/l/z mask */
762 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
767 ssize_t field, precision, len;
769 /* %#o produces the longest output */
770 char numbuf[(8 * sizeof(long) + 2) / 3 + 1];
771 /* this stuff for dealing with the buffer */
772 ssize_t nwritten = 0;
774 #define VA(type) va_arg(args, type)
779 while ((c = *fmt++)) {
786 * This will accept flags/fields in any order - not just
787 * the order specified in printf(3), but this is the way
788 * _doprnt() seems to work (on BSD and SYSV). The only
789 * restriction is that the format character must come
793 field = precision = 0;
794 for ( ; (c = *fmt++) ; ) {
813 if (!(flags & FL_DOT))
826 else if ((field = tmp) < 0) {
847 if (ksh_isdigit(c)) {
848 bool overflowed = false;
851 while (c = *fmt++, ksh_isdigit(c)) {
852 if (notok2mul(2147483647, tmp, 10))
854 tmp = tmp * 10 + ksh_numdig(c);
875 if (ksh_isupper(c)) {
883 if (flags & FL_SIZET)
884 lnum = (long)VA(ssize_t);
885 else if (flags & FL_LONG)
887 else if (flags & FL_SHORT)
888 lnum = (long)(short)VA(int);
890 lnum = (long)VA(int);
896 if (flags & FL_SIZET)
898 else if (flags & FL_LONG)
899 lnum = VA(unsigned long);
900 else if (flags & FL_SHORT)
901 lnum = (unsigned long)(unsigned short)VA(int);
903 lnum = (unsigned long)VA(unsigned int);
907 cp = numbuf + sizeof(numbuf);
912 if (0 > (long)lnum) {
920 *--cp = digits_lc[lnum % 10];
927 else if (flags & FL_PLUS)
929 else if (flags & FL_BLANK)
936 *--cp = digits_lc[lnum & 0x7];
940 if ((flags & FL_HASH) && *cp != '0')
945 const char *digits = (flags & FL_UPPER) ?
946 digits_uc : digits_lc;
948 *--cp = digits[lnum & 0xF];
952 if (flags & FL_HASH) {
953 *--cp = (flags & FL_UPPER) ? 'X' : 'x';
958 len = numbuf + sizeof(numbuf) - (s = cp);
959 if (flags & FL_DOT) {
960 if (precision > len) {
970 if ((s = VA(const char *)) == NULL)
972 else if (flags & FL_HASH) {
973 print_value_quoted(shf, s);
976 len = utf_mbswidth(s);
994 * At this point s should point to a string that is to be
995 * formatted, and len should be the length of the string.
997 if (!(flags & FL_DOT) || len < precision)
999 if (field > precision) {
1001 if (!(flags & FL_RIGHT)) {
1003 /* skip past sign or 0x when padding with 0 */
1004 if ((flags & FL_ZERO) && (flags & FL_NUMBER)) {
1005 if (*s == '+' || *s == '-' ||
1011 } else if (*s == '0') {
1015 if (--precision > 0 &&
1016 ksh_eq(*s, 'X', 'x')) {
1025 c = flags & FL_ZERO ? '0' : ' ';
1038 if (precision > 0) {
1041 nwritten += precision;
1042 q = utf_skipcols(s, precision);
1049 for ( ; field > 0 ; --field)
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");
1104 return ("Operation not permitted");
1108 return ("No such file or directory");
1112 return ("No such process");
1116 return ("Argument list too long");
1120 return ("Exec format error");
1124 return ("Cannot allocate memory");
1128 return ("Permission denied");
1132 return ("Not a directory");
1136 return ("Invalid argument");
1140 return ("Too many levels of symbolic links");
1143 shf_snprintf(errbuf, sizeof(errbuf),
1144 "Unknown error: %d", errnum);