From 24f9d669f84293935af75b1b5351b987dc03ad0b Mon Sep 17 00:00:00 2001 From: guenther Date: Mon, 12 Aug 2024 20:56:55 +0000 Subject: [PATCH] Add with the seven APIs needed to have gnulib operate without poking directly into the FILE structure. Repeated testing, "nope, need a few more" feedback, and ok tb@ --- include/Makefile | 4 +- include/stdio_ext.h | 32 +++++++++ lib/libc/Symbols.list | 7 ++ lib/libc/hidden/stdio_ext.h | 31 +++++++++ lib/libc/shlib_version | 2 +- lib/libc/stdio/Makefile.inc | 7 +- lib/libc/stdio/__fpending.3 | 129 ++++++++++++++++++++++++++++++++++++ lib/libc/stdio/fpending.c | 29 ++++++++ lib/libc/stdio/freadahead.c | 32 +++++++++ lib/libc/stdio/freading.c | 25 +++++++ lib/libc/stdio/freadptr.c | 45 +++++++++++++ lib/libc/stdio/fseterr.c | 27 ++++++++ lib/libc/stdio/fwriting.c | 25 +++++++ 13 files changed, 389 insertions(+), 6 deletions(-) create mode 100644 include/stdio_ext.h create mode 100644 lib/libc/hidden/stdio_ext.h create mode 100644 lib/libc/stdio/__fpending.3 create mode 100644 lib/libc/stdio/fpending.c create mode 100644 lib/libc/stdio/freadahead.c create mode 100644 lib/libc/stdio/freading.c create mode 100644 lib/libc/stdio/freadptr.c create mode 100644 lib/libc/stdio/fseterr.c create mode 100644 lib/libc/stdio/fwriting.c diff --git a/include/Makefile b/include/Makefile index a2d539f1c3b..79360433de5 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,4 +1,4 @@ -# $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 @@ -25,7 +25,7 @@ FILES= a.out.h ar.h asr.h assert.h \ 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 \ diff --git a/include/stdio_ext.h b/include/stdio_ext.h new file mode 100644 index 00000000000..6a18d7c0bc9 --- /dev/null +++ b/include/stdio_ext.h @@ -0,0 +1,32 @@ +/* $OpenBSD: stdio_ext.h,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 + +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_ */ + diff --git a/lib/libc/Symbols.list b/lib/libc/Symbols.list index 6d11dfff172..6eb2cca93e5 100644 --- a/lib/libc/Symbols.list +++ b/lib/libc/Symbols.list @@ -1361,6 +1361,13 @@ xprt_register xprt_unregister /* stdio */ +__fpending +__freadahead +__freading +__freadptr +__freadptrinc +__fseterr +__fwriting __sF __srget __swbuf diff --git a/lib/libc/hidden/stdio_ext.h b/lib/libc/hidden/stdio_ext.h new file mode 100644 index 00000000000..7f8c6074f11 --- /dev/null +++ b/lib/libc/hidden/stdio_ext.h @@ -0,0 +1,31 @@ +/* $OpenBSD: stdio_ext.h,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 + +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_ */ diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index 066d6db0146..7a749b0c915 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,4 +1,4 @@ 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. diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index 356e3fc956f..4da652e324d 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,4 +1,4 @@ -# $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 @@ -19,10 +19,11 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ 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 diff --git a/lib/libc/stdio/__fpending.3 b/lib/libc/stdio/__fpending.3 new file mode 100644 index 00000000000..c0a58ddd6e8 --- /dev/null +++ b/lib/libc/stdio/__fpending.3 @@ -0,0 +1,129 @@ +.\" $OpenBSD: __fpending.3,v 1.1 2024/08/12 20:56:55 guenther Exp $ +.\" +.\" Copyright (c) 2024 Philip Guenther +.\" +.\" 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. diff --git a/lib/libc/stdio/fpending.c b/lib/libc/stdio/fpending.c new file mode 100644 index 00000000000..81029a0ac96 --- /dev/null +++ b/lib/libc/stdio/fpending.c @@ -0,0 +1,29 @@ +/* $OpenBSD: fpending.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#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; +} diff --git a/lib/libc/stdio/freadahead.c b/lib/libc/stdio/freadahead.c new file mode 100644 index 00000000000..5a4a2b5d1e1 --- /dev/null +++ b/lib/libc/stdio/freadahead.c @@ -0,0 +1,32 @@ +/* $OpenBSD: freadahead.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#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; +} diff --git a/lib/libc/stdio/freading.c b/lib/libc/stdio/freading.c new file mode 100644 index 00000000000..ab510cbde80 --- /dev/null +++ b/lib/libc/stdio/freading.c @@ -0,0 +1,25 @@ +/* $OpenBSD: freading.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#include "local.h" + +int +__freading(FILE *fp) +{ + return (fp->_flags & __SRD) != 0; +} diff --git a/lib/libc/stdio/freadptr.c b/lib/libc/stdio/freadptr.c new file mode 100644 index 00000000000..a020e218ad1 --- /dev/null +++ b/lib/libc/stdio/freadptr.c @@ -0,0 +1,45 @@ +/* $OpenBSD: freadptr.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#include +#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; + } + } +} diff --git a/lib/libc/stdio/fseterr.c b/lib/libc/stdio/fseterr.c new file mode 100644 index 00000000000..2374b1a4773 --- /dev/null +++ b/lib/libc/stdio/fseterr.c @@ -0,0 +1,27 @@ +/* $OpenBSD: fseterr.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#include "local.h" + +void +__fseterr(FILE *fp) +{ + FLOCKFILE(fp); + fp->_flags |= __SERR; + FUNLOCKFILE(fp); +} diff --git a/lib/libc/stdio/fwriting.c b/lib/libc/stdio/fwriting.c new file mode 100644 index 00000000000..344ae4fdbb1 --- /dev/null +++ b/lib/libc/stdio/fwriting.c @@ -0,0 +1,25 @@ +/* $OpenBSD: fwriting.c,v 1.1 2024/08/12 20:56:55 guenther Exp $ */ +/* + * Copyright (c) 2024 Philip Guenther + * + * 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 +#include "local.h" + +int +__fwriting(FILE *fp) +{ + return (fp->_flags & __SWR) != 0; +} -- 2.20.1