From a5854d713eb8d41341b1ce2465382adf44fa87c0 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 18 Mar 2025 22:02:05 -0400 Subject: [PATCH] libroot/glibc: Add missing stdio_ext methods needed by gnulib. Otherwise it needs to use libio internals to implement these, which we don't want. musl also exports all these methods for the same reason. Implementations taken from glibc 2.15, current gnulib (5077f67), and in a few cases tweaked for better Haiku support. Should fix #19479. --- headers/posix/stdio_ext.h | 24 ++--- .../libroot/posix/glibc/include/stdio_ext.h | 11 --- src/system/libroot/posix/glibc/libio/Jamfile | 10 +++ .../libroot/posix/glibc/libio/__fbufsize.c | 33 +++++++ src/system/libroot/posix/glibc/libio/__flbf.c | 25 ++++++ .../libroot/posix/glibc/libio/__fpending.c | 28 ++++++ .../libroot/posix/glibc/libio/__freadable.c | 25 ++++++ .../libroot/posix/glibc/libio/__freadahead.c | 28 ++++++ .../libroot/posix/glibc/libio/__freadptr.c | 31 +++++++ .../libroot/posix/glibc/libio/__freadptrinc.c | 24 +++++ .../libroot/posix/glibc/libio/__fseterr.c | 24 +++++ .../libroot/posix/glibc/libio/__fwritable.c | 25 ++++++ .../libroot/posix/glibc/libio/__fwriting.c | 25 ++++++ .../posix/glibc/stdio-common/stdio_ext.h | 87 ------------------- 14 files changed, 291 insertions(+), 109 deletions(-) delete mode 100644 src/system/libroot/posix/glibc/include/stdio_ext.h create mode 100644 src/system/libroot/posix/glibc/libio/__fbufsize.c create mode 100644 src/system/libroot/posix/glibc/libio/__flbf.c create mode 100644 src/system/libroot/posix/glibc/libio/__fpending.c create mode 100644 src/system/libroot/posix/glibc/libio/__freadable.c create mode 100644 src/system/libroot/posix/glibc/libio/__freadahead.c create mode 100644 src/system/libroot/posix/glibc/libio/__freadptr.c create mode 100644 src/system/libroot/posix/glibc/libio/__freadptrinc.c create mode 100644 src/system/libroot/posix/glibc/libio/__fseterr.c create mode 100644 src/system/libroot/posix/glibc/libio/__fwritable.c create mode 100644 src/system/libroot/posix/glibc/libio/__fwriting.c delete mode 100644 src/system/libroot/posix/glibc/stdio-common/stdio_ext.h diff --git a/headers/posix/stdio_ext.h b/headers/posix/stdio_ext.h index fb0c4c28a3..12a2fae2b9 100644 --- a/headers/posix/stdio_ext.h +++ b/headers/posix/stdio_ext.h @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 Haiku, Inc. All Rights Reserved. + * Copyright 2008-2025, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. */ #ifndef _STDIO_EXT_H_ @@ -16,19 +16,21 @@ extern "C" { #define FSETLOCKING_INTERNAL 1 #define FSETLOCKING_BYCALLER 2 -/* The following stdio extensions are not implemented yet */ -/* extern size_t __fufsize(FILE* stream); */ -extern int __freading(FILE* stream); -/* extern int __fwriting(FILE* stream); */ -/* extern int __freadable(FILE* stream); */ -/* extern int __fwritable(FILE* stream); */ -/* extern int __flbf(FILE* stream); */ -extern void __fpurge(FILE* stream); -/* extern size_t __fpending(FILE* stream); */ - extern void _flushlbf(void); extern int __fsetlocking(FILE* stream, int type); +extern int __freading(FILE* stream); +extern int __fwriting(FILE* stream); +extern int __freadable(FILE* stream); +extern int __fwritable(FILE* stream); +extern size_t __freadahead(FILE* stream); +extern const char* __freadptr(FILE* stream, size_t* size); +extern void __freadptrinc(FILE* stream, size_t inc); +extern int __flbf(FILE* stream); +extern size_t __fbufsize(FILE* stream); +extern size_t __fpending(FILE* stream); +extern void __fpurge(FILE* stream); + #ifdef __cplusplus } #endif diff --git a/src/system/libroot/posix/glibc/include/stdio_ext.h b/src/system/libroot/posix/glibc/include/stdio_ext.h deleted file mode 100644 index 31c7a4e7a1..0000000000 --- a/src/system/libroot/posix/glibc/include/stdio_ext.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _STDIO_EXT_H - -# include - -extern int __fsetlocking_internal (FILE *__fp, int __type) attribute_hidden; - -#ifndef NOT_IN_libc -# define __fsetlocking(fp, type) INTUSE(__fsetlocking) (fp, type) -#endif - -#endif diff --git a/src/system/libroot/posix/glibc/libio/Jamfile b/src/system/libroot/posix/glibc/libio/Jamfile index 0c1601d7e7..974c934fd4 100644 --- a/src/system/libroot/posix/glibc/libio/Jamfile +++ b/src/system/libroot/posix/glibc/libio/Jamfile @@ -25,9 +25,19 @@ for architectureObject in [ MultiArchSubDirSetup ] { UsePrivateHeaders libroot ; MergeObject <$(architecture)>posix_gnu_libio.o : + __fbufsize.c + __flbf.c + __fpending.c __fpurge.c + __freadable.c + __freadahead.c __freading.c + __freadptr.c + __freadptrinc.c + __fseterr.c __fsetlocking.c + __fwritable.c + __fwriting.c clearerr.c clearerr_u.c fcloseall.c diff --git a/src/system/libroot/posix/glibc/libio/__fbufsize.c b/src/system/libroot/posix/glibc/libio/__fbufsize.c new file mode 100644 index 0000000000..f39c0136be --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__fbufsize.c @@ -0,0 +1,33 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +size_t +__fbufsize (FILE *fp) +{ +#ifdef __HAIKU__ + if (fp->_flags & _IO_UNBUFFERED) + return 0; +#endif + + if (fp->_mode > 0) + return fp->_wide_data->_IO_buf_end - fp->_wide_data->_IO_buf_base; + else + return fp->_IO_buf_end - fp->_IO_buf_base; +} diff --git a/src/system/libroot/posix/glibc/libio/__flbf.c b/src/system/libroot/posix/glibc/libio/__flbf.c new file mode 100644 index 0000000000..70c6806aeb --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__flbf.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +int +__flbf (FILE *fp) +{ + return fp->_flags & _IO_LINE_BUF; +} diff --git a/src/system/libroot/posix/glibc/libio/__fpending.c b/src/system/libroot/posix/glibc/libio/__fpending.c new file mode 100644 index 0000000000..c9be0f8ab8 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__fpending.c @@ -0,0 +1,28 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +size_t +__fpending (FILE *fp) +{ + if (fp->_mode > 0) + return fp->_wide_data->_IO_write_ptr - fp->_wide_data->_IO_write_base; + else + return fp->_IO_write_ptr - fp->_IO_write_base; +} diff --git a/src/system/libroot/posix/glibc/libio/__freadable.c b/src/system/libroot/posix/glibc/libio/__freadable.c new file mode 100644 index 0000000000..ff14250829 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__freadable.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +int +__freadable (FILE *fp) +{ + return (fp->_flags & _IO_NO_READS) == 0; +} diff --git a/src/system/libroot/posix/glibc/libio/__freadahead.c b/src/system/libroot/posix/glibc/libio/__freadahead.c new file mode 100644 index 0000000000..0149a94797 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__freadahead.c @@ -0,0 +1,28 @@ +/* Retrieve information about a FILE stream. + Copyright (C) 2007-2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#include +#include "libioP.h" + +size_t +__freadahead (FILE *fp) +{ + if (fp->_IO_write_ptr > fp->_IO_write_base) + return 0; + return (fp->_IO_read_end - fp->_IO_read_ptr) + + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base : + 0); +} diff --git a/src/system/libroot/posix/glibc/libio/__freadptr.c b/src/system/libroot/posix/glibc/libio/__freadptr.c new file mode 100644 index 0000000000..f4e0f65728 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__freadptr.c @@ -0,0 +1,31 @@ +/* Retrieve information about a FILE stream. + Copyright (C) 2007-2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#include +#include "libioP.h" + +const char * +__freadptr (FILE *fp, size_t *sizep) +{ + size_t size; + if (fp->_IO_write_ptr > fp->_IO_write_base) + return NULL; + size = fp->_IO_read_end - fp->_IO_read_ptr; + if (size == 0) + return NULL; + *sizep = size; + return (const char *) fp->_IO_read_ptr; +} diff --git a/src/system/libroot/posix/glibc/libio/__freadptrinc.c b/src/system/libroot/posix/glibc/libio/__freadptrinc.c new file mode 100644 index 0000000000..7e89bd0872 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__freadptrinc.c @@ -0,0 +1,24 @@ +/* Skipping input from a FILE stream. + Copyright (C) 2007-2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#include +#include "libioP.h" + +void +__freadptrinc (FILE *fp, size_t increment) +{ + fp->_IO_read_ptr += increment; +} diff --git a/src/system/libroot/posix/glibc/libio/__fseterr.c b/src/system/libroot/posix/glibc/libio/__fseterr.c new file mode 100644 index 0000000000..5a2d566e3a --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__fseterr.c @@ -0,0 +1,24 @@ +/* Set the error indicator of a stream. + Copyright (C) 2007-2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#include +#include "libioP.h" + +void +__fseterr (FILE *fp) +{ + fp->_flags |= _IO_ERR_SEEN; +} diff --git a/src/system/libroot/posix/glibc/libio/__fwritable.c b/src/system/libroot/posix/glibc/libio/__fwritable.c new file mode 100644 index 0000000000..60ceeb5cbd --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__fwritable.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +int +__fwritable (FILE *fp) +{ + return (fp->_flags & _IO_NO_WRITES) == 0; +} diff --git a/src/system/libroot/posix/glibc/libio/__fwriting.c b/src/system/libroot/posix/glibc/libio/__fwriting.c new file mode 100644 index 0000000000..c62884a5b1 --- /dev/null +++ b/src/system/libroot/posix/glibc/libio/__fwriting.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +int +__fwriting (FILE *fp) +{ + return fp->_flags & (_IO_NO_READS | _IO_CURRENTLY_PUTTING); +} diff --git a/src/system/libroot/posix/glibc/stdio-common/stdio_ext.h b/src/system/libroot/posix/glibc/stdio-common/stdio_ext.h deleted file mode 100644 index 55586ea841..0000000000 --- a/src/system/libroot/posix/glibc/stdio-common/stdio_ext.h +++ /dev/null @@ -1,87 +0,0 @@ -/* Functions to access FILE structure internals. - Copyright (C) 2000, 2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* This header contains the same definitions as the header of the same name - on Sun's Solaris OS. */ - -#ifndef _STDIO_EXT_H -#define _STDIO_EXT_H 1 - -#include - -enum -{ - /* Query current state of the locking status. */ - FSETLOCKING_QUERY = 0, -#define FSETLOCKING_QUERY FSETLOCKING_QUERY - /* The library protects all uses of the stream functions, except for - uses of the *_unlocked functions, by calls equivalent to flockfile(). */ - FSETLOCKING_INTERNAL, -#define FSETLOCKING_INTERNAL FSETLOCKING_INTERNAL - /* The user will take care of locking. */ - FSETLOCKING_BYCALLER -#define FSETLOCKING_BYCALLER FSETLOCKING_BYCALLER -}; - - -__BEGIN_DECLS - -/* Return the size of the buffer of FP in bytes currently in use by - the given stream. */ -extern size_t __fbufsize (FILE *__fp); - - -/* Return non-zero value iff the stream FP is opened readonly, or if the - last operation on the stream was a read operation. */ -extern int __freading (FILE *__fp); - -/* Return non-zero value iff the stream FP is opened write-only or - append-only, or if the last operation on the stream was a write - operation. */ -extern int __fwriting (FILE *__fp); - - -/* Return non-zero value iff stream FP is not opened write-only or - append-only. */ -extern int __freadable (FILE *__fp); - -/* Return non-zero value iff stream FP is not opened read-only. */ -extern int __fwritable (FILE *__fp); - - -/* Return non-zero value iff the stream FP is line-buffered. */ -extern int __flbf (FILE *__fp); - - -/* Discard all pending buffered I/O on the stream FP. */ -extern void __fpurge (FILE *__fp); - -/* Return amount of output in bytes pending on a stream FP. */ -extern size_t __fpending (FILE *__fp); - -/* Flush all line-buffered files. */ -extern void _flushlbf (void); - - -/* Set locking status of stream FP to TYPE. */ -extern int __fsetlocking (FILE *__fp, int __type); - -__END_DECLS - -#endif /* stdio_ext.h */