diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2004-02-11 23:48:50 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2004-02-11 23:48:50 +0000 |
commit | 082e680bd54e999f2bb4eb77141958938b1e9ee9 (patch) | |
tree | 203c45b85ca608e1550d8ffc459456fc9cf0b30b /libc/stdio/getdelim.c | |
parent | 17c21765b4a97c6f0b74ba8466073e5a3f97cdee (diff) |
New stdio core. Should be more maintainable. Fixes a couple of bugs.
Codepaths streamlined. Improved performance for nonthreaded apps
when linked with a thread-enabled libc.
Minor iconv bug and some locale/thread related startup issues fixed.
These showed up in getting a gcj-compiled java helloworld app running.
Removed some old extension functions... _stdio_fdout and _stdio_fsfopen.
Diffstat (limited to 'libc/stdio/getdelim.c')
-rw-r--r-- | libc/stdio/getdelim.c | 124 |
1 files changed, 60 insertions, 64 deletions
diff --git a/libc/stdio/getdelim.c b/libc/stdio/getdelim.c index fd073a4d2..fe388ee11 100644 --- a/libc/stdio/getdelim.c +++ b/libc/stdio/getdelim.c @@ -1,81 +1,77 @@ -/* vi: set sw=4 ts=4: */ -/* getdelim for uClibc +/* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org> * - * Copyright (C) 2000 by Lineo, inc. and Erik Andersen - * Copyright (C) 2000,2001 by Erik Andersen <andersen@uclibc.org> - * Written by Erik Andersen <andersen@uclibc.org> + * GNU Library General Public License (LGPL) version 2 or later. * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU Library General Public License as published by - * the Free Software Foundation; either version 2 of the License, 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 Library General Public License - * for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details. + */ + +#include "_stdio.h" + +/* Note: There is a defect in this function. (size_t vs ssize_t). */ + +/* glibc function -- + * Return -1 if error or EOF prior to any chars read. + * Return number of chars read (including possible delimiter but not + * the terminating nul) otherwise. * + * NOTE: If we need to allocate a buffer, we do so prior to attempting + * a reading. So space may be allocated even if initially at EOF. */ -#include <stddef.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <limits.h> -#include <errno.h> +weak_alias(__getdelim,getdelim); +#define GETDELIM_GROWBY 64 -/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR - (and null-terminate it). *LINEPTR is a pointer returned from malloc (or - NULL), pointing to *N characters of space. It is realloc'd as - necessary. Returns the number of characters read (not including the - null delimiter), or -1 on error or EOF. */ -ssize_t getdelim(char **linebuf, size_t *linebufsz, int delimiter, FILE *file) +ssize_t __getdelim(char **__restrict lineptr, size_t *__restrict n, + int delimiter, register FILE *__restrict stream) { - static const int GROWBY = 80; /* how large we will grow strings by */ - - int ch; - int idx = 0; + register char *buf; + ssize_t pos = -1; + int c; + __STDIO_AUTO_THREADLOCK_VAR; - if (file == NULL || linebuf==NULL || linebufsz == NULL) { - __set_errno(EINVAL); - return -1; - } + if (!lineptr || !n || !stream) { /* Be compatable with glibc... even */ + __set_errno(EINVAL); /* though I think we should assert here */ + } else { + __STDIO_AUTO_THREADLOCK(stream); - if (*linebuf == NULL || *linebufsz < 2) { - *linebuf = malloc(GROWBY); - if (!*linebuf) { - __set_errno(ENOMEM); - return -1; + if (!(buf = *lineptr)) { /* If passed NULL for buffer, */ + *n = 0; /* ignore value passed and treat size as 0. */ } - *linebufsz += GROWBY; - } - while (1) { - ch = fgetc(file); - if (ch == EOF) - break; - /* grow the line buffer as necessary */ - while (idx > *linebufsz-2) { - *linebuf = realloc(*linebuf, *linebufsz += GROWBY); - if (!*linebuf) { - __set_errno(ENOMEM); - return -1; + /* Within the loop, pos is actually the current buffer index + 2, + * because we want to make sure we have enough space to store + * an additional char plus a nul terminator. + */ + pos = 1; + + do { + if (pos >= *n) { + if (!(buf = realloc(buf, *n + GETDELIM_GROWBY))) { + pos = -1; + break; + } + *n += GETDELIM_GROWBY; + *lineptr = buf; + } + + if ((c = __GETC_UNLOCKED(stream)) != EOF) { + buf[++pos - 2] = c; + if (c != delimiter) { + continue; + } + } + + /* We're done, so correct pos back to being the current index. */ + if ((pos -= 2) >= 0) { + buf[++pos] = 0; } - } - (*linebuf)[idx++] = (char)ch; - if ((char)ch == delimiter) break; + + } while (1); + + __STDIO_AUTO_THREADUNLOCK(stream); } - if (idx != 0) - (*linebuf)[idx] = 0; - else if ( ch == EOF ) - return -1; - return idx; + return pos; } - |