]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/cpio/src/dstring.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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
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.  */
18
19 #if defined(HAVE_CONFIG_H)
20 # include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
25 #include <string.h>
26 #else
27 #include <strings.h>
28 #endif
29 #include "dstring.h"
30
31 char *xmalloc (unsigned n);
32 char *xrealloc (char *p, unsigned n);
33
34 /* Initialiaze dynamic string STRING with space for SIZE characters.  */
35
36 void
37 ds_init (dynamic_string *string, int size)
38 {
39   string->ds_length = size;
40   string->ds_string = (char *) xmalloc (size);
41 }
42
43 /* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
44
45 void
46 ds_resize (dynamic_string *string, int size)
47 {
48   if (size > string->ds_length)
49     {
50       string->ds_length = size;
51       string->ds_string = (char *) xrealloc ((char *) string->ds_string, size);
52     }
53 }
54
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.  */
61
62 char *
63 ds_fgetstr (FILE *f, dynamic_string *s, char eos)
64 {
65   int insize;                   /* Amount needed for line.  */
66   int strsize;                  /* Amount allocated for S.  */
67   int next_ch;
68
69   /* Initialize.  */
70   insize = 0;
71   strsize = s->ds_length;
72
73   /* Read the input string.  */
74   next_ch = getc (f);
75   while (next_ch != eos && next_ch != EOF)
76     {
77       if (insize >= strsize - 1)
78         {
79           ds_resize (s, strsize * 2 + 2);
80           strsize = s->ds_length;
81         }
82       s->ds_string[insize++] = next_ch;
83       next_ch = getc (f);
84     }
85   s->ds_string[insize++] = '\0';
86
87   if (insize == 1 && next_ch == EOF)
88     return NULL;
89   else
90     return s->ds_string;
91 }
92
93 char *
94 ds_fgets (FILE *f, dynamic_string *s)
95 {
96   return ds_fgetstr (f, s, '\n');
97 }
98
99 char *
100 ds_fgetname (FILE *f, dynamic_string *s)
101 {
102   return ds_fgetstr (f, s, '\0');
103 }