2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: util.c,v 10.18 2000/07/20 19:21:53 skimo Exp $ (Berkeley) $Date: 2000/07/20 19:21:53 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
31 * Increase the size of a buffer.
33 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
36 binc(sp
, bp
, bsizep
, min
)
37 SCR
*sp
; /* sp MAY BE NULL!!! */
43 /* If already larger than the minimum, just return. */
44 if (min
&& *bsizep
>= min
)
47 csize
= *bsizep
+ MAX(min
, 256);
48 REALLOC(sp
, bp
, void *, csize
);
52 * Theoretically, realloc is supposed to leave any already
53 * held memory alone if it can't get more. Don't trust it.
59 * Memory is guaranteed to be zero-filled, various parts of
62 memset((char *)bp
+ *bsizep
, 0, csize
- *bsizep
);
69 * Set the column number of the first non-blank character
70 * including or after the starting column. On error, set
71 * the column to 0, it's safest.
73 * PUBLIC: int nonblank __P((SCR *, db_recno_t, size_t *));
76 nonblank(sp
, lno
, cnop
)
89 /* Get the line, succeeding in an empty file. */
90 if (db_eget(sp
, lno
, &p
, &len
, &isempty
))
94 if (len
== 0 || off
>= len
)
97 for (cnt
= off
, p
= &p
[off
],
98 len
-= off
; len
&& isblank(*p
); ++cnt
, ++p
, --len
);
100 /* Set the return. */
101 *cnop
= len
? cnt
: cnt
- 1;
107 * Return tail of a path.
109 * PUBLIC: char *tail __P((char *));
117 if ((p
= strrchr(path
, '/')) == NULL
)
124 * Strdup for wide character strings with an associated length.
126 * PUBLIC: char *v_strdup __P((SCR *, const char *, size_t));
129 v_strdup(sp
, str
, len
)
136 MALLOC(sp
, copy
, char *, (len
+ 1));
139 memcpy(copy
, str
, len
);
146 * Strdup for wide character strings with an associated length.
148 * PUBLIC: CHAR_T *v_wstrdup __P((SCR *, const CHAR_T *, size_t));
151 v_wstrdup(sp
, str
, len
)
158 MALLOC(sp
, copy
, CHAR_T
*, (len
+ 1) * sizeof(CHAR_T
));
161 MEMCPYW(copy
, str
, len
);
167 * PUBLIC: size_t v_strlen __P((const CHAR_T *str));
170 v_strlen(const CHAR_T
*str
)
179 * PUBLIC: void * v_charset __P((CHAR_T *s, CHAR_T c, size_t n));
182 v_charset(CHAR_T
*s
, CHAR_T c
, size_t n
)
186 while (n
--) *s
++ = c
;
191 * PUBLIC: int v_strcmp __P((const CHAR_T *s1, const CHAR_T *s2))
194 v_strcmp(const CHAR_T
*s1
, const CHAR_T
*s2
)
196 while (*s1
&& *s2
&& *s1
== *s2
) s1
++, s2
++;
202 * Get an unsigned long, checking for overflow.
204 * PUBLIC: enum nresult nget_uslong __P((SCR *, u_long *, const CHAR_T *, CHAR_T **, int));
207 nget_uslong(sp
, valp
, p
, endp
, base
)
218 INT2CHAR(sp
, p
, v_strlen(p
) + 1, np
, nlen
);
220 *valp
= strtoul(np
, &endnp
, base
);
221 *endp
= (CHAR_T
*)p
+ (endnp
- np
);
224 if (errno
== ERANGE
&& *valp
== ULONG_MAX
)
231 * Convert a signed long, checking for overflow and underflow.
233 * PUBLIC: enum nresult nget_slong __P((SCR *, long *, const CHAR_T *, CHAR_T **, int));
236 nget_slong(sp
, valp
, p
, endp
, base
)
247 INT2CHAR(sp
, p
, v_strlen(p
) + 1, np
, nlen
);
249 *valp
= strtol(np
, &endnp
, base
);
250 *endp
= (CHAR_T
*)p
+ (endnp
- np
);
253 if (errno
== ERANGE
) {
254 if (*valp
== LONG_MAX
)
256 if (*valp
== LONG_MIN
)