Clarify what new code should not cast unused
[dragonfly.git] / contrib / cpio / dstring.c
1 /* dstring.c - The dynamic string handling routines used by cpio.
2    Copyright (C) 1990, 1991, 1992 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
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
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"
25
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));
33
34 /* Initialiaze dynamic string STRING with space for SIZE characters.  */
35
36 void
37 ds_init (string, size)
38      dynamic_string *string;
39      int size;
40 {
41   string->ds_length = size;
42   string->ds_string = (char *) xmalloc (size);
43 }
44
45 /* Expand dynamic string STRING, if necessary, to hold SIZE characters.  */
46
47 void
48 ds_resize (string, size)
49      dynamic_string *string;
50      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 (f, s, eos)
68      FILE *f;
69      dynamic_string *s;
70      char eos;
71 {
72   int insize;                   /* Amount needed for line.  */
73   int strsize;                  /* Amount allocated for S.  */
74   int next_ch;
75
76   /* Initialize.  */
77   insize = 0;
78   strsize = s->ds_length;
79
80   /* Read the input string.  */
81   next_ch = getc (f);
82   while (next_ch != eos && next_ch != EOF)
83     {
84       if (insize >= strsize - 1)
85         {
86           ds_resize (s, strsize * 2 + 2);
87           strsize = s->ds_length;
88         }
89       s->ds_string[insize++] = next_ch;
90       next_ch = getc (f);
91     }
92   s->ds_string[insize++] = '\0';
93
94   if (insize == 1 && next_ch == EOF)
95     return NULL;
96   else
97     return s->ds_string;
98 }
99
100 char *
101 ds_fgets (f, s)
102      FILE *f;
103      dynamic_string *s;
104 {
105   return ds_fgetstr (f, s, '\n');
106 }
107
108 char *
109 ds_fgetname (f, s)
110      FILE *f;
111      dynamic_string *s;
112 {
113   return ds_fgetstr (f, s, '\0');
114 }