1 /* dstring.c - The dynamic string handling routines used by cpio.
2 Copyright (C) 1990, 1991, 1992 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
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
31 char *xmalloc
P_((unsigned n
));
32 char *xrealloc
P_((char *p
, unsigned n
));
34 /* Initialiaze dynamic string STRING with space for SIZE characters. */
37 ds_init (string
, size
)
38 dynamic_string
*string
;
41 string
->ds_length
= size
;
42 string
->ds_string
= (char *) xmalloc (size
);
45 /* Expand dynamic string STRING, if necessary, to hold SIZE characters. */
48 ds_resize (string
, size
)
49 dynamic_string
*string
;
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 (f
, s
, eos
)
72 int insize
; /* Amount needed for line. */
73 int strsize
; /* Amount allocated for S. */
78 strsize
= s
->ds_length
;
80 /* Read the input string. */
82 while (next_ch
!= eos
&& next_ch
!= EOF
)
84 if (insize
>= strsize
- 1)
86 ds_resize (s
, strsize
* 2 + 2);
87 strsize
= s
->ds_length
;
89 s
->ds_string
[insize
++] = next_ch
;
92 s
->ds_string
[insize
++] = '\0';
94 if (insize
== 1 && next_ch
== EOF
)
105 return ds_fgetstr (f
, s
, '\n');
113 return ds_fgetstr (f
, s
, '\0');