1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* $OpenBSD: util.c,v 1.2 2001/07/04 08:44:59 niklas Exp $ */
/* --------------------------------------------------
| NAME
| util
| PURPOSE
| provide some standard useful utility functions.
| NOTES
|
| COPYRIGHT
| Copyright (C) 1993 Christian E. Hopps
|
| This program is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2 of the License, or
| (at your option) any later version.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; if not, write to the Free Software
| Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
| HISTORY
| chopps - Oct 9, 1993: Created.
+--------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <error.h>
#include <ctype.h>
#include <stdarg.h>
#include "util.h"
#include "string.h"
/* Utility functions */
/* string functions.
*/
int
string_to_number(char *s, unsigned long *num)
{
char *ns;
int base, errsave = errno;
unsigned long res;
int len;
s = stpblk(s);
ns = s;
len = strlen (s);
if((!strnicmp(s,"0x",2))) { /* check 0[xX]nnnnnnnn */
/* Hex number */
s += 2;
ns += 2;
base = 16;
} else if((!strnicmp(s,"x",1)) || /* check [xX]nnnnnnnn */
(s[0] == '$')) { /* check $nnnnnnnn */
/* Hex number */
s++;
ns++;
base = 16;
} else if((len && tolower (s[len-1]) == 'h') || /* check nnnnnnnn[Hh] */
(len>1 && tolower (s[len-2]) == 'h')) {
/* Hex number */
base = 16;
} else if((len && tolower (s[len-1]) == 'o') || /* check nnnnnnnnnn[Oo] */
(len>1 && tolower (s[len-2]) == 'o')) {
/* octal */
base = 8;
} else if( (len && tolower (s[len-1]) == 'b') || /* check nnnnnnnnnn[Bb] */
(len>1 && tolower (s[len-2]) == 'b')) {
/* binary */
base = 2;
} else {
/* assume decimal */
base = 10;
}
errno = 0;
len = strlen (s);
res = strtoul(s,&ns,base);
if(ns == s || (res == 0xffffffff && errno == ERANGE)) {
errno = errsave;
return(0); /* failed */
} else {
errno = errsave;
if ((len && s[len-1] == 'M') ||
(len>1 && s[len-2] == 'M')) {
/* result should be in Megabytes */
if (res <= 0xfff) {
res <<= 20;
} else {
errno = ERANGE;
return (0);
}
} else if ((len && tolower (s[len-1]) == 'k') ||
(len>1 && tolower (s[len-2]) == 'k')) {
/* result should be in kilobytes */
if (res <= 0x3ffffff) {
res <<= 10;
} else {
errno = ERANGE;
return (0);
}
}
*num = res;
}
return(ns - s); /* it worked */
}
char *
stripws (char *s)
{
while (isspace (*s)) {
s++;
}
return (s);
}
/* string = fgetline(fileptr) :: replacement for fgets. no length limits. */
/* ------------------------- */
/* fgetline function returns a dynamic string of any length. The string is */
/* the next line from ``fp'' arg. Returns NULL for failure or EOF the */
/* reason can be detirmened by feof() and errno. On an error that is not */
/* EOF will flush the buffer to EOL if possible. The returned string has */
/* the newline stripped. */
/* sorry about the asm like comments I wrote this for a school project and */
/* the prof is decidedly in favor of verbosity. I think the code is clear */
/* enough alone, and most of these comments clutter the clarity. Oh well.*/
char *fgetline(FILE *fp)
{
enum local_constants { locbufsize = 40 };
char *retstr = NULL, *temp;
char locbuf[locbufsize];
char locbuflen = 0;
while(1) { /* do forever. */
while(locbuflen < (locbufsize-1)) {
int ch = fgetc(fp); /* get next character from */
/* stream. */
if(ch == EOF) { /* check for end of file. */
free_string(retstr); /* free_string retstr */
/* if EOF. */
return(NULL); /* and return NULL. */
} else if( ch == '\n' ) {
locbuf[locbuflen] = 0; /* got newline null term. */
temp = concat_strings(retstr,locbuf); /* and concat local */
/* buffer. */
free_string(retstr);
return(temp); /* return new string. */
} else {
locbuf[locbuflen++] = ch; /* add to local buffer */
}
}
/* we need to reset out local buffer. */
locbuf[locbuflen] = 0; /* null terminate. */
temp = retstr;
retstr = concat_strings(retstr,locbuf); /* concatenate locbuf to */
/* older string. */
locbuflen = 0; /* zero local buffer. */
free_string(temp); /* free old string. */
if(retstr == NULL) {
flush_to_eol(fp); /* flush to EOL on fail. */
return(NULL); /* and return NULL. */
}
}
}
/* flush ``fp'' to end of line, if possible. returns 0 on success or EOF for */
/* error. */
int flush_to_eol(FILE *fp)
{
int ch;
while(EOF != (ch = fgetc(fp))) { /* loop until EOF */
if(ch == '\n') { /* if newline, return. */
return(0);
}
}
return(EOF);
}
/* Concatenate 2 strings into a new one. Both or either of the inputs */
/* ``before'' and ``after'' can be NULL. returns NULL for failure */
/* setting errno acordingly. */
char *concat_strings(const char *before, const char *after)
{
char *string = NULL;
int len1 = 0, len2 = 0;
if(before) /* if non null */
len1 = strlen(before); /* get length */
if(after) /* if non null */
len2 = strlen(after); /* get length */
string = malloc(len1 + len2 + 1); /* allocate storage for */
/* new string. */
if(string) {
memcpy(string,before,len1); /* copy ``before'' */
memcpy(&string[len1],after,len2); /* cat ``after'' */
string[len1+len2] = '\0'; /* null terminate. */
}
return(string); /* return string (or NULL) */
}
/* free_string() - frees a string gotten from misc string routines. input */
/* can be NULL. */
void free_string(char *string)
{
if(string) /* if non NULL */
free(string); /* free string. */
}
char *
alloc_string (char *s)
{
char *d = malloc (strlen (s) + 1);
if (d) {
strcpy (d, s);
}
return (d);
}
int
ask_bool (int def, int other, char *f, ...)
{
char buffer[20];
va_list ap;
va_start (ap, f);
vfprintf (mout, f, ap);
fprintf (mout, "? [%lc%lc]:",toupper (def),tolower (other));
fflush (mout);
if (fgets (buffer, 18, min)) {
char *s = stripws (buffer);
if (s[0] != 0 && s[0] != '\n') {
def = (int) s[0];
}
}
if (buffer[strlen (buffer)-1] != '\n') {
flush_to_eol (min);
}
return (def);
}
void *
zmalloc (size_t b)
{
void *mem = malloc (b);
if (mem) {
memset (mem, 0, b);
}
return (mem);
}
void
zfree (void *mem)
{
if (mem)
free (mem);
}
struct Node *
find_name (struct List *l, char *s)
{
struct Node *n = l->lh_Head;
while (n->ln_Succ) {
if (!stricmp (s, n->ln_Name)) {
return (n);
}
n = n->ln_Succ;
}
return (NULL);
}
void
verbose_message (char *f, ...)
{
if (opt_verbose) {
va_list ap;
va_start (ap, f);
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
}
void
debug_message (char *f, ...)
{
if (opt_debug) {
va_list ap;
va_start (ap, f);
fprintf (mout, "debug: ");
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
}
void
verbose_debug_message (char *f, ...)
{
if (opt_verbose && opt_debug) {
va_list ap;
va_start (ap, f);
fprintf (mout, "debug: ");
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
}
void
message (char *f, ...)
{
va_list ap;
va_start (ap, f);
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
void
warn_message (char *f, ...)
{
va_list ap;
va_start (ap, f);
fprintf (mout, "warn: ");
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
void
vmessage (char *f, va_list ap)
{
vfprintf (mout, f, ap);
fprintf (mout, "\n");
}
|