]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/cpio/src/dstring.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / cpio / src / dstring.c
1 /* dstring.c - The dynamic string handling routines used by cpio.
2    Copyright (C) 1990, 1991, 1992, 2004 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #if defined(HAVE_CONFIG_H)
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
24 #include <string.h>
25 #else
26 #include <strings.h>
27 #endif
28 #include "dstring.h"
29
30 #if __STDC__
31 # define P_(s) s
32 #else
33 # define P_(s) ()
34 #endif
35 char *xmalloc P_((unsigned n));
36 char *xrealloc P_((char *p, unsigned n));
37
38 /* Initialiaze dynamic string STRING with space for SIZE characters.  */
39
40 void
41 ds_init (dynamic_string *string, int size)
42 {
43   string->ds_length = size;
44   string->ds_string = (char *) xmalloc (size);
45 }
46
47 /* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
48
49 void
50 ds_resize (dynamic_string *string, int size)
51 {
52   if (size > string->ds_length)
53     {
54       string->ds_length = size;
55       string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
56     }
57 }
58
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.  */
65
66 char *
67 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
68 {
69   int insize;                   /* Amount needed for line.  */
70   int strsize;                  /* Amount allocated for S.  */
71   int next_ch;
72
73   /* Initialize.  */
74   insize = 0;
75   strsize = s->ds_length;
76
77   /* Read the input string.  */
78   next_ch = getc (f);
79   while (next_ch != eos && next_ch != EOF)
80     {
81       if (insize >= strsize - 1)
82         {
83           ds_resize (s, strsize * 2 + 2);
84           strsize = s->ds_length;
85         }
86       s->ds_string[insize++] = next_ch;
87       next_ch = getc (f);
88     }
89   s->ds_string[insize++] = '\0';
90
91   if (insize == 1 && next_ch == EOF)
92     return NULL;
93   else
94     return s->ds_string;
95 }
96
97 char *
98 ds_fgets (FILE *f, dynamic_string *s)
99 {
100   return ds_fgetstr (f, s, '\n');
101 }
102
103 char *
104 ds_fgetname (FILE *f, dynamic_string *s)
105 {
106   return ds_fgetstr (f, s, '\0');
107 }