1 /* dstring.c - The dynamic string handling routines used by cpio.
2 Copyright (C) 1990, 1991, 1992, 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA. */
19 #if defined(HAVE_CONFIG_H)
24 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
31 char *xmalloc (unsigned n);
32 char *xrealloc (char *p, unsigned n);
34 /* Initialiaze dynamic string STRING with space for SIZE characters. */
37 ds_init (dynamic_string *string, int size)
39 string->ds_length = size;
40 string->ds_string = (char *) xmalloc (size);
43 /* Expand dynamic string STRING, if necessary, to hold SIZE characters. */
46 ds_resize (dynamic_string *string, int size)
48 if (size > string->ds_length)
50 string->ds_length = size;
51 string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
55 /* Dynamic string S gets a string terminated by the EOS character
56 (which is removed) from file F. S will increase
57 in size during the function if the string from F is longer than
58 the current size of S.
59 Return NULL if end of file is detected. Otherwise,
60 Return a pointer to the null-terminated string in S. */
63 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
65 int insize; /* Amount needed for line. */
66 int strsize; /* Amount allocated for S. */
71 strsize = s->ds_length;
73 /* Read the input string. */
75 while (next_ch != eos && next_ch != EOF)
77 if (insize >= strsize - 1)
79 ds_resize (s, strsize * 2 + 2);
80 strsize = s->ds_length;
82 s->ds_string[insize++] = next_ch;
85 s->ds_string[insize++] = '\0';
87 if (insize == 1 && next_ch == EOF)
94 ds_fgets (FILE *f, dynamic_string *s)
96 return ds_fgetstr (f, s, '\n');
100 ds_fgetname (FILE *f, dynamic_string *s)
102 return ds_fgetstr (f, s, '\0');