source: trunk/essentials/app-arch/cpio/src/dstring.c

Last change on this file was 3332, checked in by bird, 18 years ago

cpio 2.7

File size: 2.7 KB
Line 
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
31char *xmalloc (unsigned n);
32char *xrealloc (char *p, unsigned n);
33
34/* Initialiaze dynamic string STRING with space for SIZE characters. */
35
36void
37ds_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
45void
46ds_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
62char *
63ds_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
93char *
94ds_fgets (FILE *f, dynamic_string *s)
95{
96 return ds_fgetstr (f, s, '\n');
97}
98
99char *
100ds_fgetname (FILE *f, dynamic_string *s)
101{
102 return ds_fgetstr (f, s, '\0');
103}
Note: See TracBrowser for help on using the repository browser.