]> git.cworth.org Git - tar/blob - gnu/getdelim.c
upstream: Fix extraction of device nodes.
[tar] / gnu / 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,
5    2009, 2010 Free 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 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
27    optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below.  */
28 #define _GL_ARG_NONNULL(params)
29
30 #include <stdio.h>
31
32 #include <limits.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #ifndef SSIZE_MAX
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 #endif
40
41 #if USE_UNLOCKED_IO
42 # include "unlocked-io.h"
43 # define getc_maybe_unlocked(fp)        getc(fp)
44 #elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
45 # undef flockfile
46 # undef funlockfile
47 # define flockfile(x) ((void) 0)
48 # define funlockfile(x) ((void) 0)
49 # define getc_maybe_unlocked(fp)        getc(fp)
50 #else
51 # define getc_maybe_unlocked(fp)        getc_unlocked(fp)
52 #endif
53
54 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
55    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
56    NULL), pointing to *N characters of space.  It is realloc'ed as
57    necessary.  Returns the number of characters read (not including
58    the null terminator), or -1 on error or EOF.  */
59
60 ssize_t
61 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
62 {
63   ssize_t result;
64   size_t cur_len = 0;
65
66   if (lineptr == NULL || n == NULL || fp == NULL)
67     {
68       errno = EINVAL;
69       return -1;
70     }
71
72   flockfile (fp);
73
74   if (*lineptr == NULL || *n == 0)
75     {
76       char *new_lineptr;
77       *n = 120;
78       new_lineptr = (char *) realloc (*lineptr, *n);
79       if (new_lineptr == NULL)
80         {
81           result = -1;
82           goto unlock_return;
83         }
84       *lineptr = new_lineptr;
85     }
86
87   for (;;)
88     {
89       int i;
90
91       i = getc_maybe_unlocked (fp);
92       if (i == EOF)
93         {
94           result = -1;
95           break;
96         }
97
98       /* Make enough space for len+1 (for final NUL) bytes.  */
99       if (cur_len + 1 >= *n)
100         {
101           size_t needed_max =
102             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
103           size_t needed = 2 * *n + 1;   /* Be generous. */
104           char *new_lineptr;
105
106           if (needed_max < needed)
107             needed = needed_max;
108           if (cur_len + 1 >= needed)
109             {
110               result = -1;
111               errno = EOVERFLOW;
112               goto unlock_return;
113             }
114
115           new_lineptr = (char *) realloc (*lineptr, needed);
116           if (new_lineptr == NULL)
117             {
118               result = -1;
119               goto unlock_return;
120             }
121
122           *lineptr = new_lineptr;
123           *n = needed;
124         }
125
126       (*lineptr)[cur_len] = i;
127       cur_len++;
128
129       if (i == delimiter)
130         break;
131     }
132   (*lineptr)[cur_len] = '\0';
133   result = cur_len ? cur_len : result;
134
135  unlock_return:
136   funlockfile (fp); /* doesn't set errno */
137
138   return result;
139 }