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 License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #if defined(HAVE_CONFIG_H)
23 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
35 char *xmalloc P_((unsigned n));
36 char *xrealloc P_((char *p, unsigned n));
38 /* Initialiaze dynamic string STRING with space for SIZE characters. */
41 ds_init (dynamic_string *string, int size)
43 string->ds_length = size;
44 string->ds_string = (char *) xmalloc (size);
47 /* Expand dynamic string STRING, if necessary, to hold SIZE characters. */
50 ds_resize (dynamic_string *string, int size)
52 if (size > string->ds_length)
54 string->ds_length = size;
55 string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
59 /* Dynamic string S gets a string terminated by the EOS character
60 (which is removed) from file F. S will increase
61 in size during the function if the string from F is longer than
62 the current size of S.
63 Return NULL if end of file is detected. Otherwise,
64 Return a pointer to the null-terminated string in S. */
67 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
69 int insize; /* Amount needed for line. */
70 int strsize; /* Amount allocated for S. */
75 strsize = s->ds_length;
77 /* Read the input string. */
79 while (next_ch != eos && next_ch != EOF)
81 if (insize >= strsize - 1)
83 ds_resize (s, strsize * 2 + 2);
84 strsize = s->ds_length;
86 s->ds_string[insize++] = next_ch;
89 s->ds_string[insize++] = '\0';
91 if (insize == 1 && next_ch == EOF)
98 ds_fgets (FILE *f, dynamic_string *s)
100 return ds_fgetstr (f, s, '\n');
104 ds_fgetname (FILE *f, dynamic_string *s)
106 return ds_fgetstr (f, s, '\0');