Fix a bug when '-f -H' is used and the target already exists. cpdup was
[dragonfly.git] / contrib / tar / lib / getstr.c
1 /* getstr.c -- core function for GNU C library getline replacement function
2
3    Copyright (C) 1993, 1996-2000 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #include <assert.h>
29
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 #else
33 char *malloc (), *realloc ();
34 #endif
35
36 /* Always add at least this many bytes when extending the buffer.  */
37 #define MIN_CHUNK 64
38
39 /* Read up to (and including) a delimiter DELIM1 from STREAM into *LINEPTR
40    + OFFSET (and NUL-terminate it).  If DELIM2 is non-zero, then read up
41    and including the first occurrence of DELIM1 or DELIM2.  *LINEPTR is
42    a pointer returned from malloc (or NULL), pointing to *N characters of
43    space.  It is realloc'd as necessary.  Return the number of characters
44    read (not including the NUL terminator), or -1 on error or EOF.  */
45
46 int
47 getstr (char **lineptr, size_t *n, FILE *stream, int delim1, int delim2,
48         size_t offset)
49 {
50   int nchars_avail;             /* Allocated but unused chars in *LINEPTR.  */
51   char *read_pos;               /* Where we're reading into *LINEPTR. */
52   int ret;
53
54   if (!lineptr || !n || !stream)
55     return -1;
56
57   if (!*lineptr)
58     {
59       *n = MIN_CHUNK;
60       *lineptr = malloc (*n);
61       if (!*lineptr)
62         return -1;
63     }
64
65   nchars_avail = *n - offset;
66   read_pos = *lineptr + offset;
67
68   for (;;)
69     {
70       register int c = getc (stream);
71
72       /* We always want at least one char left in the buffer, since we
73          always (unless we get an error while reading the first char)
74          NUL-terminate the line buffer.  */
75
76       assert(*n - nchars_avail == read_pos - *lineptr);
77       if (nchars_avail < 2)
78         {
79           if (*n > MIN_CHUNK)
80             *n *= 2;
81           else
82             *n += MIN_CHUNK;
83
84           nchars_avail = *n + *lineptr - read_pos;
85           *lineptr = realloc (*lineptr, *n);
86           if (!*lineptr)
87             return -1;
88           read_pos = *n - nchars_avail + *lineptr;
89           assert(*n - nchars_avail == read_pos - *lineptr);
90         }
91
92       if (c == EOF || ferror (stream))
93         {
94           /* Return partial line, if any.  */
95           if (read_pos == *lineptr)
96             return -1;
97           else
98             break;
99         }
100
101       *read_pos++ = c;
102       nchars_avail--;
103
104       if (c == delim1 || (delim2 && c == delim2))
105         /* Return the line.  */
106         break;
107     }
108
109   /* Done - NUL terminate and return the number of chars read.  */
110   *read_pos = '\0';
111
112   ret = read_pos - (*lineptr + offset);
113   return ret;
114 }