Commit | Line | Data |
---|---|---|
41c799d4 C |
1 | /* buffer.c |
2 | Manipulate buffers used to hold strings. | |
3 | ||
3469b437 | 4 | Copyright (C) 1992, 1993 Ian Lance Taylor |
41c799d4 C |
5 | |
6 | This file is part of Taylor UUCP. | |
7 | ||
8 | This library is free software; you can redistribute it and/or | |
9 | modify it under the terms of the GNU Library General Public License | |
10 | as published by the Free Software Foundation; either version 2 of | |
11 | the License, or (at your option) any later version. | |
12 | ||
13 | This library is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Library General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Library General Public | |
19 | License along with this library; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | The author of the program may be contacted at ian@airs.com or | |
3469b437 | 23 | c/o Cygnus Support, Building 200, 1 Kendall Square, Cambridge, MA 02139. |
41c799d4 C |
24 | */ |
25 | ||
26 | #include "uucp.h" | |
27 | ||
28 | #include "uudefs.h" | |
29 | ||
30 | /* We keep a linked list of buffers. The union is a hack because the | |
31 | default definition of offsetof, in uucp.h, takes the address of the | |
32 | field, and some C compilers will not let you take the address of an | |
33 | array. */ | |
34 | ||
35 | struct sbuf | |
36 | { | |
37 | struct sbuf *qnext; | |
38 | size_t c; | |
39 | union | |
40 | { | |
41 | char ab[4]; | |
42 | char bdummy; | |
43 | } | |
44 | u; | |
45 | }; | |
46 | ||
47 | static struct sbuf *qBlist; | |
48 | ||
49 | /* Get a buffer of a given size. The buffer is returned with the | |
50 | ubuffree function. */ | |
51 | ||
52 | char * | |
53 | zbufalc (c) | |
54 | size_t c; | |
55 | { | |
56 | register struct sbuf *q; | |
57 | ||
58 | if (qBlist == NULL) | |
59 | { | |
60 | q = (struct sbuf *) xmalloc (sizeof (struct sbuf) + c - 4); | |
61 | q->c = c; | |
62 | } | |
63 | else | |
64 | { | |
65 | q = qBlist; | |
66 | qBlist = q->qnext; | |
67 | if (q->c < c) | |
68 | { | |
69 | q = (struct sbuf *) xrealloc ((pointer) q, | |
70 | sizeof (struct sbuf) + c - 4); | |
71 | q->c = c; | |
72 | } | |
73 | } | |
74 | return q->u.ab; | |
75 | } | |
76 | ||
77 | /* Get a buffer holding a given string. */ | |
78 | ||
79 | char * | |
80 | zbufcpy (z) | |
81 | const char *z; | |
82 | { | |
83 | size_t csize; | |
84 | char *zret; | |
85 | ||
86 | if (z == NULL) | |
87 | return NULL; | |
88 | csize = strlen (z) + 1; | |
89 | zret = zbufalc (csize); | |
90 | memcpy (zret, z, csize); | |
91 | return zret; | |
92 | } | |
93 | ||
94 | /* Free up a buffer back onto the linked list. */ | |
95 | ||
96 | void | |
97 | ubuffree (z) | |
98 | char *z; | |
99 | { | |
41c799d4 | 100 | struct sbuf *q; |
3469b437 AC |
101 | /* The type of ioff should be size_t, but making it int avoids a bug |
102 | in some versions of the HP/UX compiler, and will always work. */ | |
103 | int ioff; | |
41c799d4 C |
104 | |
105 | if (z == NULL) | |
106 | return; | |
107 | ioff = offsetof (struct sbuf, u); | |
108 | q = (struct sbuf *) (pointer) (z - ioff); | |
3469b437 AC |
109 | |
110 | #ifdef DEBUG_BUFFER | |
111 | { | |
112 | struct sbuf *qlook; | |
113 | ||
114 | for (qlook = qBlist; qlook != NULL; qlook = qlook->qnext) | |
115 | { | |
116 | if (qlook == q) | |
117 | { | |
118 | ulog (LOG_ERROR, "ubuffree: Attempt to free buffer twice"); | |
119 | abort (); | |
120 | } | |
121 | } | |
122 | } | |
123 | #endif | |
124 | ||
41c799d4 C |
125 | q->qnext = qBlist; |
126 | qBlist = q; | |
127 | } |