From c796292bc8455284eeda17244e0e7e2f982333b6 Mon Sep 17 00:00:00 2001 From: Jay Freeman Date: Sat, 6 Dec 2008 22:48:06 +0000 Subject: Moved to a more complete getline() fix. git-svn-id: http://svn.telesphoreo.org/trunk@520 514c082c-b64e-11dc-b46d-3d985efe055d --- data/raop-play/getline.c | 174 ++++++++++++++++++++++++++++++++++++++++++++ data/raop-play/getline.diff | 55 ++++++++------ data/raop-play/getline.h | 23 ++++++ data/raop-play/make.sh | 2 + 4 files changed, 231 insertions(+), 23 deletions(-) create mode 100644 data/raop-play/getline.c create mode 100644 data/raop-play/getline.h (limited to 'data') diff --git a/data/raop-play/getline.c b/data/raop-play/getline.c new file mode 100644 index 000000000..9830b4d25 --- /dev/null +++ b/data/raop-play/getline.c @@ -0,0 +1,174 @@ +/* getline.c -- Replacement for GNU C library function getline + +Copyright (C) 1993 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 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 +General Public License for more details. */ + +/* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include "getline.h" + +#if STDC_HEADERS +#include +#else +char *malloc (), *realloc (); +#endif + +/* Always add at least this many bytes when extending the buffer. */ +#define MIN_CHUNK 64 + +/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR + + OFFSET (and null-terminate it). If LIMIT is non-negative, then + read no more than LIMIT chars. + + *LINEPTR is a pointer returned from malloc (or NULL), pointing to + *N characters of space. It is realloc'd as necessary. + + Return the number of characters read (not including the null + terminator), or -1 on error or EOF. On a -1 return, the caller + should check feof(), if not then errno has been set to indicate the + error. */ + +int +getstr (lineptr, n, stream, terminator, offset, limit) + char **lineptr; + size_t *n; + FILE *stream; + int terminator; + int offset; + int limit; +{ + int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ + char *read_pos; /* Where we're reading into *LINEPTR. */ + int ret; + + if (!lineptr || !n || !stream) + { + errno = EINVAL; + return -1; + } + + if (!*lineptr) + { + *n = MIN_CHUNK; + *lineptr = malloc (*n); + if (!*lineptr) + { + errno = ENOMEM; + return -1; + } + *lineptr[0] = '\0'; + } + + nchars_avail = *n - offset; + read_pos = *lineptr + offset; + + for (;;) + { + int save_errno; + register int c; + + if (limit == 0) + break; + else + { + c = getc (stream); + + /* If limit is negative, then we shouldn't pay attention to + it, so decrement only if positive. */ + if (limit > 0) + limit--; + } + + save_errno = errno; + + /* We always want at least one char left in the buffer, since we + always (unless we get an error while reading the first char) + NUL-terminate the line buffer. */ + + assert((*lineptr + *n) == (read_pos + nchars_avail)); + if (nchars_avail < 2) + { + if (*n > MIN_CHUNK) + *n *= 2; + else + *n += MIN_CHUNK; + + nchars_avail = *n + *lineptr - read_pos; + *lineptr = realloc (*lineptr, *n); + if (!*lineptr) + { + errno = ENOMEM; + return -1; + } + read_pos = *n - nchars_avail + *lineptr; + assert((*lineptr + *n) == (read_pos + nchars_avail)); + } + + if (ferror (stream)) + { + /* Might like to return partial line, but there is no + place for us to store errno. And we don't want to just + lose errno. */ + errno = save_errno; + return -1; + } + + if (c == EOF) + { + /* Return partial line, if any. */ + if (read_pos == *lineptr) + return -1; + else + break; + } + + *read_pos++ = c; + nchars_avail--; + + if (c == terminator) + /* Return the line. */ + break; + } + + /* Done - NUL terminate and return the number of chars read. */ + *read_pos = '\0'; + + ret = read_pos - (*lineptr + offset); + return ret; +} + +int +getline (lineptr, n, stream) + char **lineptr; + size_t *n; + FILE *stream; +{ + return getstr (lineptr, n, stream, '\n', 0, GETLINE_NO_LIMIT); +} + +int +getline_safe (lineptr, n, stream, limit) + char **lineptr; + size_t *n; + FILE *stream; + int limit; +{ + return getstr (lineptr, n, stream, '\n', 0, limit); +} diff --git a/data/raop-play/getline.diff b/data/raop-play/getline.diff index 3205fd2ae..3130dceb2 100644 --- a/data/raop-play/getline.diff +++ b/data/raop-play/getline.diff @@ -1,37 +1,46 @@ +diff -ru raop_play-0.5.1/raop_play/Makefile.in raop_play-0.5.1+iPhone/raop_play/Makefile.in +--- raop_play-0.5.1/raop_play/Makefile.in 2005-12-16 14:17:00.000000000 +0000 ++++ raop_play-0.5.1+iPhone/raop_play/Makefile.in 2008-12-06 22:45:54.000000000 +0000 +@@ -15,7 +15,7 @@ + CFLAGS=-Wall + OBJS := raop_play.o raop_client.o rtsp_client.o aexcl_lib.o base64.o aes.o m4a_stream.o \ + audio_stream.o wav_stream.o mp3_stream.o flac_stream.o ogg_stream.o aac_stream.o pls_stream.o \ +-pcm_stream.o flac_stream.o ++pcm_stream.o flac_stream.o getline.o + + all: $(TARGET) + +diff -ru raop_play-0.5.1/rendezvous/Makefile.in raop_play-0.5.1+iPhone/rendezvous/Makefile.in +--- raop_play-0.5.1/rendezvous/Makefile.in 2005-12-16 14:17:02.000000000 +0000 ++++ raop_play-0.5.1+iPhone/rendezvous/Makefile.in 2008-12-06 22:45:48.000000000 +0000 +@@ -17,7 +17,7 @@ + all: $(TARGET) + + +-mDNSClient: mDNSPosix.o mDNSUNP.o ExampleClientApp.o mDNS.o Client.o ++mDNSClient: mDNSPosix.o mDNSUNP.o ExampleClientApp.o mDNS.o Client.o getline.o + $(CC) $(LFLAGS) $^ -o $@ + + install: diff -ru raop_play-0.5.1/raop_play/pls_stream.c raop_play-0.5.1+iPhone/raop_play/pls_stream.c --- raop_play-0.5.1/raop_play/pls_stream.c 2005-12-16 14:17:01.000000000 +0000 -+++ raop_play-0.5.1+iPhone/raop_play/pls_stream.c 2008-12-06 04:06:57.000000000 +0000 -@@ -29,6 +29,9 @@ ++++ raop_play-0.5.1+iPhone/raop_play/pls_stream.c 2008-12-06 22:46:54.000000000 +0000 +@@ -27,6 +27,7 @@ + #include "audio_stream.h" + #include "pls_stream.h" #include "aexcl_lib.h" ++#include "getline.h" -+/* XXX: this is slightly wrong, but I don't think this code cares */ -+#define getline(a, b, c) (fgets(*a, *b, c) == NULL ? -1 : strlen(*a)) -+ - static int str_termlf(char *line) - { diff -ru raop_play-0.5.1/rendezvous/Client.c raop_play-0.5.1+iPhone/rendezvous/Client.c --- raop_play-0.5.1/rendezvous/Client.c 2005-12-16 14:17:02.000000000 +0000 -+++ raop_play-0.5.1+iPhone/rendezvous/Client.c 2008-12-06 04:07:02.000000000 +0000 -@@ -83,8 +83,11 @@ - #include "mDNSClientAPI.h"// Defines the interface to the mDNS core code ++++ raop_play-0.5.1+iPhone/rendezvous/Client.c 2008-12-06 22:46:50.000000000 +0000 +@@ -89,6 +89,7 @@ #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform #include "ExampleClientApp.h" #include "mDNS.h" ++#include "getline.h" -+/* XXX: this is slightly wrong, but I don't think this code cares */ -+#define getline(a, b, c) (fgets(*a, *b, c) == NULL ? -1 : strlen(*a)) -+ // Globals - static mDNS mDNSStorage; // mDNS core uses this to store its globals -@@ -106,7 +108,7 @@ - char fname[128],wfname[128]; - FILE *inf,*outf; - char *sline; -- size_t linelen=128; -+ size_t linelen=128*128; - int i,ln,ws,cp1=0,cp2=0,cp3=0; - - if(addr->type != mDNSAddrType_IPv4) return -1; diff --git a/data/raop-play/getline.h b/data/raop-play/getline.h new file mode 100644 index 000000000..3bbad5698 --- /dev/null +++ b/data/raop-play/getline.h @@ -0,0 +1,23 @@ +#ifndef _getline_h_ +#define _getline_h_ 1 + +#include + +#if defined (__GNUC__) || (defined (__STDC__) && __STDC__) +#define __PROTO(args) args +#else +#define __PROTO(args) () +#endif /* GCC. */ + +#define GETLINE_NO_LIMIT -1 + +int + getline __PROTO ((char **_lineptr, size_t *_n, FILE *_stream)); +int + getline_safe __PROTO ((char **_lineptr, size_t *_n, FILE *_stream, + int limit)); +int + getstr __PROTO ((char **_lineptr, size_t *_n, FILE *_stream, + int _terminator, int _offset, int limit)); + +#endif /* _getline_h_ */ diff --git a/data/raop-play/make.sh b/data/raop-play/make.sh index 572b6a87a..ff553c0be 100644 --- a/data/raop-play/make.sh +++ b/data/raop-play/make.sh @@ -1,5 +1,7 @@ pkg:setup pkg:configure cp -a "${PKG_DATA}"/linuxint.h raop_play +cp -a "${PKG_DATA}"/getline.[hc] rendezvous +cp -a "${PKG_DATA}"/getline.[hc] raop_play make pkg:install -- cgit v1.2.3