without poking directly into the FILE structure.
Repeated testing, "nope, need a few more" feedback, and ok tb@
-# $OpenBSD: Makefile,v 1.231 2023/08/20 15:02:50 schwarze Exp $
+# $OpenBSD: Makefile,v 1.232 2024/08/12 20:56:55 guenther Exp $
# $NetBSD: Makefile,v 1.59 1996/05/15 21:36:43 jtc Exp $
# @(#)Makefile 5.45.1.1 (Berkeley) 5/6/91
ranlib.h readpassphrase.h regex.h resolv.h rmd160.h \
sched.h search.h setjmp.h semaphore.h sha1.h sha2.h \
signal.h siphash.h sndio.h spawn.h stdbool.h stddef.h \
- stdio.h stdlib.h string.h strings.h sysexits.h \
+ stdio.h stdio_ext.h stdlib.h string.h strings.h sysexits.h \
tar.h tgmath.h tib.h time.h ttyent.h \
uchar.h unistd.h utime.h utmp.h uuid.h \
vis.h \
--- /dev/null
+/* $OpenBSD: stdio_ext.h,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _STDIO_EXT_H_
+#define _STDIO_EXT_H_
+
+#include <stdio.h>
+
+size_t __fpending(FILE *);
+size_t __freadahead(FILE *);
+int __freading(FILE *);
+const char *__freadptr(FILE *, size_t *_sizep);
+void __freadptrinc(FILE *, size_t _inc);
+void __fseterr(FILE *);
+int __fwriting(FILE *);
+
+#endif /* _STDIO_EXT_H_ */
+
xprt_unregister
/* stdio */
+__fpending
+__freadahead
+__freading
+__freadptr
+__freadptrinc
+__fseterr
+__fwriting
__sF
__srget
__swbuf
--- /dev/null
+/* $OpenBSD: stdio_ext.h,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _LIBC_STDIO_EXT_H_
+#define _LIBC_STDIO_EXT_H_
+
+#include_next <stdio_ext.h>
+
+PROTO_DEPRECATED(__fpending);
+PROTO_DEPRECATED(__freadahead);
+PROTO_DEPRECATED(__freading);
+PROTO_DEPRECATED(__freadptr);
+PROTO_DEPRECATED(__freadptrinc);
+PROTO_DEPRECATED(__fseterr);
+PROTO_DEPRECATED(__fwriting);
+
+#endif /* _LIBC_STDIO_EXT_H_ */
major=100
-minor=2
+minor=3
# note: If changes were made to include/thread_private.h or if system calls
# were added/changed then librthread/shlib_version must also be updated.
-# $OpenBSD: Makefile.inc,v 1.30 2024/01/19 16:30:28 millert Exp $
+# $OpenBSD: Makefile.inc,v 1.31 2024/08/12 20:56:55 guenther Exp $
# stdio sources
.PATH: ${LIBCSRCDIR}/stdio
getwchar.c putwc.c putwchar.c ungetwc.c \
fwprintf.c swprintf.c vfwprintf.c vswprintf.c vwprintf.c wprintf.c \
fwscanf.c swscanf.c vfwscanf.c vswscanf.c vwscanf.c wscanf.c \
- getdelim.c getline.c dprintf.c vdprintf.c
+ getdelim.c getline.c dprintf.c vdprintf.c \
+ fpending.c freadahead.c freading.c freadptr.c fseterr.c fwriting.c
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fmemopen.3 \
- fopen.3 fputs.3 fread.3 fseek.3 funopen.3 getc.3 \
+ fopen.3 __fpending.3 fputs.3 fread.3 fseek.3 funopen.3 getc.3 \
open_memstream.3 perror.3 printf.3 putc.3 remove.3 scanf.3 setbuf.3 \
setvbuf.3 stdio.3 tmpnam.3 ungetc.3 fgetws.3 fputws.3 fwide.3 getwc.3 \
putwc.3 ungetwc.3 wprintf.3 wscanf.3 getdelim.3
--- /dev/null
+.\" $OpenBSD: __fpending.3,v 1.1 2024/08/12 20:56:55 guenther Exp $
+.\"
+.\" Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: August 12 2024 $
+.Dt __FPENDING 3
+.Os
+.Sh NAME
+.Nm __fpending ,
+.Nm __freadahead ,
+.Nm __freading ,
+.Nm __freadptr ,
+.Nm __freadptrinc ,
+.Nm __fseterr ,
+.Nm __fwriting
+.Nd stream extension functions
+.Sh SYNOPSIS
+.In stdio_ext.h
+.Ft size_t
+.Fn __fpending "FILE *stream"
+.Ft size_t
+.Fn __freadahead "FILE *stream"
+.Ft int
+.Fn __freading "FILE *stream"
+.Ft "const char *"
+.Fn __freadptr "FILE *stream" "size_t *sizep"
+.Ft void
+.Fn __freadptrinc "FILE *stream" "size_t increment"
+.Ft void
+.Fn __fseterr "FILE *stream"
+.Ft int
+.Fn __fwriting "FILE *stream"
+.Sh DESCRIPTION
+The
+.Fn __fpending
+function returns the number of bytes of output data currently
+buffered on
+.Fa stream .
+.Pp
+The
+.Fn __freadahead
+function returns the number of bytes of input data currently
+buffered on
+.Fa stream .
+.Pp
+The
+.Fn __freading
+function returns non-zero if
+.Fa stream
+either was opened read-only or if the last operation on the stream
+was a read or push-back operation.
+.Pp
+The
+.Fn __freadptr
+function returns either a pointer to the next byte of buffered input
+data on
+.Fa stream
+and stores the number of consecutive bytes of buffered data available
+to the location pointed to by
+.Fa sizep ,
+or return
+.Dv NULL
+if there's no buffered input data.
+The value returned via
+.Fa sizep
+may differ from the value that would be returned by
+.Fn __freadahead .
+.Pp
+The
+.Fn __freadptrinc
+function consumes
+.Fa increment
+bytes of buffered input data on
+.Fa stream .
+This is only valid immediately after a non-NULL return from
+.Fn __freadptr
+and
+.Fa increment
+must not be greater than the size value from that call.
+.Pp
+The
+.Fn __fseterr
+function sets the error indicator for
+.Fa stream .
+.Pp
+The
+.Fn __fwriting
+function returns non-zero if
+.Fa stream
+either was opened write-only or append-only or if the last operation
+on the stream was a write operation.
+.Sh ERRORS
+These functions should not fail and do not set the external
+variable
+.Va errno .
+.Sh SEE ALSO
+.Xr fflush 3
+.Sh HISTORY
+The
+.Fn __fpending ,
+.Fn __freadahead ,
+.Fn __freading ,
+.Fn __freadptr ,
+.Fn __freadptrinc ,
+.Fn __fseterr ,
+.Fn __fwriting
+and
+functions appeared in
+.Ox 7.6 .
+.Sh BUGS
+These functions are under-specified and non-portable.
+They exist to permit a particular
+.Dq portability
+library to function without direct manipulation of stdio structures;
+everyone else should either implement their own stdio layer,
+do the work of defining and standardizing the required functionality,
+or reconsider their life decisions.
--- /dev/null
+/* $OpenBSD: fpending.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio_ext.h>
+#include "local.h"
+
+size_t
+__fpending(FILE *fp)
+{
+ size_t r = 0;
+
+ if ((fp->_flags & __SWR) && fp->_bf._base != NULL)
+ r = fp->_p - fp->_bf._base;
+ return r;
+}
--- /dev/null
+/* $OpenBSD: freadahead.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio_ext.h>
+#include "local.h"
+
+size_t
+__freadahead(FILE *fp)
+{
+ size_t r = 0;
+
+ if (fp->_flags & __SRD) {
+ r = fp->_r;
+ if (HASUB(fp))
+ r += fp->_ur;
+ }
+ return r;
+}
--- /dev/null
+/* $OpenBSD: freading.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio_ext.h>
+#include "local.h"
+
+int
+__freading(FILE *fp)
+{
+ return (fp->_flags & __SRD) != 0;
+}
--- /dev/null
+/* $OpenBSD: freadptr.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdio_ext.h>
+#include "local.h"
+
+const char *
+__freadptr(FILE *fp, size_t *sizep)
+{
+ if ((fp->_flags & __SRD) && fp->_r > 0) {
+ *sizep = fp->_r;
+ return fp->_p;
+ }
+ return NULL;
+}
+
+void
+__freadptrinc(FILE *fp, size_t inc)
+{
+ if (fp->_flags & __SRD) {
+ fp->_r -= inc;
+ fp->_p += inc;
+ if (fp->_r == 0 && HASUB(fp)) {
+ /* consumed the pushback buffer; switch back */
+ FREEUB(fp);
+ if ((fp->_r = fp->_ur) != 0)
+ fp->_p = fp->_up;
+ }
+ }
+}
--- /dev/null
+/* $OpenBSD: fseterr.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio_ext.h>
+#include "local.h"
+
+void
+__fseterr(FILE *fp)
+{
+ FLOCKFILE(fp);
+ fp->_flags |= __SERR;
+ FUNLOCKFILE(fp);
+}
--- /dev/null
+/* $OpenBSD: fwriting.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */
+/*
+ * Copyright (c) 2024 Philip Guenther <guenther@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio_ext.h>
+#include "local.h"
+
+int
+__fwriting(FILE *fp)
+{
+ return (fp->_flags & __SWR) != 0;
+}