]> git.cworth.org Git - tar/blob - lib/getdelim.c
Imported Upstream version 1.20
[tar] / lib / getdelim.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* getdelim.c --- Implementation of replacement getdelim function.
4    Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007, 2008 Free
5    Software Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 3, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21
22 /* Ported from glibc by Simon Josefsson. */
23
24 #include <config.h>
25
26 #include <stdio.h>
27
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #ifndef SIZE_MAX
33 # define SIZE_MAX ((size_t) -1)
34 #endif
35 #ifndef SSIZE_MAX
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
37 #endif
38 #if !HAVE_FLOCKFILE
39 # undef flockfile
40 # define flockfile(x) ((void) 0)
41 #endif
42 #if !HAVE_FUNLOCKFILE
43 # undef funlockfile
44 # define funlockfile(x) ((void) 0)
45 #endif
46
47 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
48    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
49    NULL), pointing to *N characters of space.  It is realloc'ed as
50    necessary.  Returns the number of characters read (not including
51    the null terminator), or -1 on error or EOF.  */
52
53 ssize_t
54 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
55 {
56   ssize_t result;
57   size_t cur_len = 0;
58
59   if (lineptr == NULL || n == NULL || fp == NULL)
60     {
61       errno = EINVAL;
62       return -1;
63     }
64
65   flockfile (fp);
66
67   if (*lineptr == NULL || *n == 0)
68     {
69       char *new_lineptr;
70       *n = 120;
71       new_lineptr = (char *) realloc (*lineptr, *n);
72       if (new_lineptr == NULL)
73         {
74           result = -1;
75           goto unlock_return;
76         }
77       *lineptr = new_lineptr;
78     }
79
80   for (;;)
81     {
82       int i;
83
84       i = getc (fp);
85       if (i == EOF)
86         {
87           result = -1;
88           break;
89         }
90
91       /* Make enough space for len+1 (for final NUL) bytes.  */
92       if (cur_len + 1 >= *n)
93         {
94           size_t needed_max =
95             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
96           size_t needed = 2 * *n + 1;   /* Be generous. */
97           char *new_lineptr;
98
99           if (needed_max < needed)
100             needed = needed_max;
101           if (cur_len + 1 >= needed)
102             {
103               result = -1;
104               errno = EOVERFLOW;
105               goto unlock_return;
106             }
107
108           new_lineptr = (char *) realloc (*lineptr, needed);
109           if (new_lineptr == NULL)
110             {
111               result = -1;
112               goto unlock_return;
113             }
114
115           *lineptr = new_lineptr;
116           *n = needed;
117         }
118
119       (*lineptr)[cur_len] = i;
120       cur_len++;
121
122       if (i == delimiter)
123         break;
124     }
125   (*lineptr)[cur_len] = '\0';
126   result = cur_len ? cur_len : result;
127
128  unlock_return:
129   funlockfile (fp); /* doesn't set errno */
130
131   return result;
132 }