flockfile(3): Update FreeBSD CVS ID, bump .Dd and add some references.
[dragonfly.git] / contrib / cpio / dstring.c
blob26d6bbcc1dcfcfaf5cef283229d1f952047a8be2
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)
7 any later version.
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. */
18 #include <stdio.h>
19 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
20 #include <string.h>
21 #else
22 #include <strings.h>
23 #endif
24 #include "dstring.h"
26 #if __STDC__
27 # define P_(s) s
28 #else
29 # define P_(s) ()
30 #endif
31 char *xmalloc P_((unsigned n));
32 char *xrealloc P_((char *p, unsigned n));
34 /* Initialiaze dynamic string STRING with space for SIZE characters. */
36 void
37 ds_init (string, size)
38 dynamic_string *string;
39 int size;
41 string->ds_length = size;
42 string->ds_string = (char *) xmalloc (size);
45 /* Expand dynamic string STRING, if necessary, to hold SIZE characters. */
47 void
48 ds_resize (string, size)
49 dynamic_string *string;
50 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. */
66 char *
67 ds_fgetstr (f, s, eos)
68 FILE *f;
69 dynamic_string *s;
70 char eos;
72 int insize; /* Amount needed for line. */
73 int strsize; /* Amount allocated for S. */
74 int next_ch;
76 /* Initialize. */
77 insize = 0;
78 strsize = s->ds_length;
80 /* Read the input string. */
81 next_ch = getc (f);
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;
90 next_ch = getc (f);
92 s->ds_string[insize++] = '\0';
94 if (insize == 1 && next_ch == EOF)
95 return NULL;
96 else
97 return s->ds_string;
100 char *
101 ds_fgets (f, s)
102 FILE *f;
103 dynamic_string *s;
105 return ds_fgetstr (f, s, '\n');
108 char *
109 ds_fgetname (f, s)
110 FILE *f;
111 dynamic_string *s;
113 return ds_fgetstr (f, s, '\0');