]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - libc/upstream-openbsd/lib/libc/stdio/fgetln.c
am f27cc051: am 806f3bd7: Upgrade to tzdata2013i.
[android-sdk/platform-bionic.git] / libc / upstream-openbsd / lib / libc / stdio / fgetln.c
1 /*      $OpenBSD: fgetln.c,v 1.11 2009/11/21 09:53:44 guenther Exp $ */
2 /*-
3  * Copyright (c) 1990, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Chris Torek.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "local.h"
39 /*
40  * Expand the line buffer.  Return -1 on error.
41 #ifdef notdef
42  * The `new size' does not account for a terminating '\0',
43  * so we add 1 here.
44 #endif
45  */
46 int
47 __slbexpand(FILE *fp, size_t newsize)
48 {
49         void *p;
51 #ifdef notdef
52         ++newsize;
53 #endif
54         if (fp->_lb._size >= newsize)
55                 return (0);
56         if ((p = realloc(fp->_lb._base, newsize)) == NULL)
57                 return (-1);
58         fp->_lb._base = p;
59         fp->_lb._size = newsize;
60         return (0);
61 }
63 /*
64  * Get an input line.  The returned pointer often (but not always)
65  * points into a stdio buffer.  Fgetline does not alter the text of
66  * the returned line (which is thus not a C string because it will
67  * not necessarily end with '\0'), but does allow callers to modify
68  * it if they wish.  Thus, we set __SMOD in case the caller does.
69  */
70 char *
71 fgetln(FILE *fp, size_t *lenp)
72 {
73         unsigned char *p;
74         char *ret;
75         size_t len;
76         size_t off;
78         FLOCKFILE(fp);
79         _SET_ORIENTATION(fp, -1);
81         /* make sure there is input */
82         if (fp->_r <= 0 && __srefill(fp))
83                 goto error;
85         /* look for a newline in the input */
86         if ((p = memchr((void *)fp->_p, '\n', fp->_r)) != NULL) {
87                 /*
88                  * Found one.  Flag buffer as modified to keep fseek from
89                  * `optimising' a backward seek, in case the user stomps on
90                  * the text.
91                  */
92                 p++;            /* advance over it */
93                 ret = (char *)fp->_p;
94                 *lenp = len = p - fp->_p;
95                 fp->_flags |= __SMOD;
96                 fp->_r -= len;
97                 fp->_p = p;
98                 FUNLOCKFILE(fp);
99                 return (ret);
100         }
102         /*
103          * We have to copy the current buffered data to the line buffer.
104          * As a bonus, though, we can leave off the __SMOD.
105          *
106          * OPTIMISTIC is length that we (optimistically) expect will
107          * accommodate the `rest' of the string, on each trip through the
108          * loop below.
109          */
110 #define OPTIMISTIC 80
112         for (len = fp->_r, off = 0;; len += fp->_r) {
113                 size_t diff;
115                 /*
116                  * Make sure there is room for more bytes.  Copy data from
117                  * file buffer to line buffer, refill file and look for
118                  * newline.  The loop stops only when we find a newline.
119                  */
120                 if (__slbexpand(fp, len + OPTIMISTIC))
121                         goto error;
122                 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
123                     len - off);
124                 off = len;
125                 if (__srefill(fp))
126                         break;  /* EOF or error: return partial line */
127                 if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL)
128                         continue;
130                 /* got it: finish up the line (like code above) */
131                 p++;
132                 diff = p - fp->_p;
133                 len += diff;
134                 if (__slbexpand(fp, len))
135                         goto error;
136                 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
137                     diff);
138                 fp->_r -= diff;
139                 fp->_p = p;
140                 break;
141         }
142         *lenp = len;
143         ret = (char *)fp->_lb._base;
144 #ifdef notdef
145         ret[len] = '\0';
146 #endif
147         FUNLOCKFILE(fp);
148         return (ret);
150 error:
151         *lenp = 0;              /* ??? */
152         FUNLOCKFILE(fp);
153         return (NULL);          /* ??? */