From: Simon Josefsson Date: Tue, 2 Aug 2005 09:33:30 +0000 (+0000) Subject: 2005-08-02 Simon Josefsson X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5e93ef0613280d12d81460251dc3084d12beb28;p=pspp 2005-08-02 Simon Josefsson * getline.h, getline.c: Rewrite. * getdelim.h, getdelim.c: New files, ported from glibc. 2005-08-02 Simon Josefsson * getline.m4: Separate out getdelim stuff into separate module. * getdelim.m4: New file. 2005-08-02 Simon Josefsson * modules/getdelim: New file. * modules/getline: Rewrite, don't use getndelim2. --- diff --git a/ChangeLog b/ChangeLog index b3a16d27c2..5bac178082 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2005-08-02 Simon Josefsson + + * modules/getdelim: New file. + + * modules/getline: Rewrite, don't use getndelim2. + +2005-07-16 Simon Josefsson + + * modules/readline: New file. + 2005-07-24 Bruno Haible * modules/visibility: New file. diff --git a/lib/ChangeLog b/lib/ChangeLog index 909a201f85..a44fa6f6fc 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2005-08-02 Simon Josefsson + + * getline.h, getline.c: Rewrite. + + * getdelim.h, getdelim.c: New files, ported from glibc. + 2005-07-31 Bruno Haible * lock.h (gl_lock_initializer): New macro. diff --git a/lib/getdelim.c b/lib/getdelim.c new file mode 100644 index 0000000000..02bb9a4976 --- /dev/null +++ b/lib/getdelim.c @@ -0,0 +1,116 @@ +/* getdelim.c --- Implementation of replacement getdelim function. + Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 Free + Software Foundation, Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + This program 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ + +/* Ported from glibc by Simon Josefsson. */ + +#if HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include "getdelim.h" + +#if !HAVE_FLOCKFILE +# undef flockfile +# define flockfile(x) ((void) 0) +#endif +#if !HAVE_FUNLOCKFILE +# undef funlockfile +# define funlockfile(x) ((void) 0) +#endif + +/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and + NUL-terminate it). *LINEPTR is a pointer returned from malloc (or + NULL), pointing to *N characters of space. It is realloc'ed as + necessary. Returns the number of characters read (not including + the null terminator), or -1 on error or EOF. */ + +ssize_t +getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) +{ + int result; + ssize_t cur_len = 0; + ssize_t len; + + if (lineptr == NULL || n == NULL || fp == NULL) + { + errno = EINVAL; + return -1; + } + + flockfile (fp); + + if (*lineptr == NULL || *n == 0) + { + *n = 120; + *lineptr = (char *) malloc (*n); + if (*lineptr == NULL) + { + result = -1; + goto unlock_return; + } + } + + for (;;) + { + char *t; + int i; + + i = getc (fp); + if (i == EOF) + break; + + /* Make enough space for len+1 (for final NUL) bytes. */ + if (cur_len + 1 >= *n) + { + size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */ + char *new_lineptr; + + if (needed < cur_len) + { + result = -1; + goto unlock_return; + } + + new_lineptr = (char *) realloc (*lineptr, needed); + if (new_lineptr == NULL) + { + result = -1; + goto unlock_return; + } + + *lineptr = new_lineptr; + *n = needed; + } + + (*lineptr)[cur_len] = i; + cur_len++; + + if (i == delimiter) + break; + } + (*lineptr)[cur_len] = '\0'; + result = cur_len; + + unlock_return: + funlockfile (fp); + return result; +} diff --git a/lib/getdelim.h b/lib/getdelim.h new file mode 100644 index 0000000000..8bc61307e5 --- /dev/null +++ b/lib/getdelim.h @@ -0,0 +1,28 @@ +/* getdelim.h --- Prototype for replacement getdelim function. + Copyright (C) 2005 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + This program 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ + +/* Written by Simon Josefsson. */ + +/* Get size_t, FILE, ssize_t. And getdelim, if available. */ +# include +# include +# include + +#if !HAVE_DECL_GETDELIM +ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream); +#endif /* !HAVE_GETDELIM */ diff --git a/lib/getline.c b/lib/getline.c index 5a7980ec9d..00f02f4aea 100644 --- a/lib/getline.c +++ b/lib/getline.c @@ -1,44 +1,32 @@ -/* getline.c -- Replacement for GNU C library function getline +/* getline.c --- Implementation of replacement getline function. + Copyright (C) 2005 Free Software Foundation, Inc. - Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004 Free - Software Foundation, Inc. + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - This program 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 General Public License for more details. + This program 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 + General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ -/* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */ +/* Written by Simon Josefsson. */ #if HAVE_CONFIG_H # include #endif +#include "getdelim.h" #include "getline.h" -#if ! (defined __GNU_LIBRARY__ && HAVE_GETDELIM) - -# include "getndelim2.h" - -ssize_t -getdelim (char **lineptr, size_t *linesize, int delimiter, FILE *stream) -{ - return getndelim2 (lineptr, linesize, 0, GETNLINE_NO_LIMIT, delimiter, EOF, - stream); -} -#endif - ssize_t -getline (char **lineptr, size_t *linesize, FILE *stream) +getline (char **lineptr, size_t *n, FILE *stream) { - return getdelim (lineptr, linesize, '\n', stream); + return getdelim (lineptr, n, '\n', stream); } diff --git a/lib/getline.h b/lib/getline.h index b58e93f4f3..f0f61c4b2e 100644 --- a/lib/getline.h +++ b/lib/getline.h @@ -1,39 +1,28 @@ -/* Replacement for GNU C library function getline +/* getline.h --- Prototype for replacement getline function. + Copyright (C) 2005 Free Software Foundation, Inc. - Copyright (C) 1995, 1997, 1999, 2000, 2001, 2002, 2003 Free - Software Foundation, Inc. + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. + This program 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 + General Public License for more details. -This program 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 General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#ifndef GETLINE_H_ -# define GETLINE_H_ 1 +/* Written by Simon Josefsson. */ +/* Get size_t, FILE, ssize_t. And getline, if available. */ # include # include - -/* Get ssize_t. */ # include -/* glibc2 has these functions declared in . Avoid redeclarations. */ -# if __GLIBC__ < 2 - -extern ssize_t getline (char **_lineptr, size_t *_linesize, FILE *_stream); - -extern ssize_t getdelim (char **_lineptr, size_t *_linesize, int _delimiter, - FILE *_stream); - -# endif - -#endif /* not GETLINE_H_ */ +#if !HAVE_DECL_GETLINE +ssize_t getline (char **lineptr, size_t *n, FILE *stream); +#endif /* !HAVE_GETLINE */ diff --git a/m4/ChangeLog b/m4/ChangeLog index c30cb077ab..34f5351d38 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,9 @@ +2005-08-02 Simon Josefsson + + * getline.m4: Separate out getdelim stuff into separate module. + + * getdelim.m4: New file. + 2005-07-26 Paul Eggert Add support to getopt for Emacs, which doesn't use LIBOBJS in the diff --git a/m4/getdelim.m4 b/m4/getdelim.m4 new file mode 100644 index 0000000000..340bb7126d --- /dev/null +++ b/m4/getdelim.m4 @@ -0,0 +1,30 @@ +# getdelim.m4 serial 1 + +dnl Copyright (C) 2005 Free Software dnl Foundation, Inc. +dnl +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_PREREQ(2.52) + +AC_DEFUN([gl_FUNC_GETDELIM], +[ + AC_LIBSOURCES([getdelim.c, getdelim.h]) + + dnl Persuade glibc to declare getdelim(). + AC_REQUIRE([AC_GNU_SOURCE]) + + AC_REPLACE_FUNCS(getdelim) + AC_CHECK_DECLS_ONCE(getdelim) + + if test $ac_cv_func_getdelim = no; then + gl_PREREQ_GETDELIM + fi +]) + +# Prerequisites of lib/getdelim.c. +AC_DEFUN([gl_PREREQ_GETDELIM], +[ + AC_CHECK_FUNCS([flockfile funlockfile]) +]) diff --git a/m4/getline.m4 b/m4/getline.m4 index 983f261463..ff255a3664 100644 --- a/m4/getline.m4 +++ b/m4/getline.m4 @@ -1,4 +1,4 @@ -# getline.m4 serial 12 +# getline.m4 serial 13 dnl Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005 Free Software dnl Foundation, Inc. @@ -13,19 +13,21 @@ dnl See if there's a working, system-supplied version of the getline function. dnl We can't just do AC_REPLACE_FUNCS(getline) because some systems dnl have a function by that name in -linet that doesn't have anything dnl to do with the function we need. -AC_DEFUN([AM_FUNC_GETLINE], +AC_DEFUN([gl_FUNC_GETLINE], [ AC_LIBSOURCES([getline.c, getline.h]) - dnl Persuade glibc to declare getline() and getdelim(). + dnl Persuade glibc to declare getline(). AC_REQUIRE([AC_GNU_SOURCE]) - am_getline_needs_run_time_check=no + AC_CHECK_DECLS([getline]) + + gl_getline_needs_run_time_check=no AC_CHECK_FUNC(getline, dnl Found it in some library. Verify that it works. - am_getline_needs_run_time_check=yes, + gl_getline_needs_run_time_check=yes, am_cv_func_working_getline=no) - if test $am_getline_needs_run_time_check = yes; then + if test $gl_getline_needs_run_time_check = yes; then AC_CACHE_CHECK([for working getline function], am_cv_func_working_getline, [echo fooN |tr -d '\012'|tr N '\012' > conftest.data AC_TRY_RUN([ @@ -57,20 +59,12 @@ AC_DEFUN([AM_FUNC_GETLINE], [Define to a replacement function name for getline().]) AC_LIBOBJ(getline) - # Avoid multiple inclusions of getndelim2.o into LIBOBJS. - # This hack won't be needed after gnulib requires Autoconf 2.58 or later. - case " $LIB@&t@OBJS " in - *" getndelim2.$ac_objext "* ) ;; - *) AC_LIBOBJ(getndelim2);; - esac - gl_PREREQ_GETLINE - gl_PREREQ_GETNDELIM2 fi ]) # Prerequisites of lib/getline.c. AC_DEFUN([gl_PREREQ_GETLINE], [ - AC_CHECK_FUNCS(getdelim) + gl_FUNC_GETDELIM ]) diff --git a/modules/getdelim b/modules/getdelim new file mode 100644 index 0000000000..a635ccb6d3 --- /dev/null +++ b/modules/getdelim @@ -0,0 +1,23 @@ +Description: +Read character delimited data from a stream. + +Files: +lib/getdelim.h +lib/getdelim.c +m4/getdelim.m4 + +Depends-on: + +configure.ac: +gl_FUNC_GETDELIM + +Makefile.am: + +Include: +"getdelim.h" + +License: +LGPL + +Maintainer: +Simon Josefsson diff --git a/modules/getline b/modules/getline index dd456564c6..0f2f43f5fe 100644 --- a/modules/getline +++ b/modules/getline @@ -4,25 +4,21 @@ Read a line from a stream. Files: lib/getline.h lib/getline.c -lib/getndelim2.h -lib/getndelim2.c m4/getline.m4 -m4/getndelim2.m4 -m4/ssize_t.m4 Depends-on: +getdelim configure.ac: -AM_FUNC_GETLINE +gl_FUNC_GETLINE Makefile.am: -EXTRA_DIST += getndelim2.h getndelim2.c Include: "getline.h" License: -GPL +LGPL Maintainer: -all +Simon Josefsson