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.22 2001/06/25 15:19:12 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:12 $";
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(SCR
*sp
, void *bp
, size_t *bsizep
, size_t min
)
37 /* 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(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
86 /* Get the line, succeeding in an empty file. */
87 if (db_eget(sp
, lno
, &p
, &len
, &isempty
))
91 if (len
== 0 || off
>= len
)
94 for (cnt
= off
, p
= &p
[off
],
95 len
-= off
; len
&& isblank(*p
); ++cnt
, ++p
, --len
);
98 *cnop
= len
? cnt
: cnt
- 1;
104 * Return tail of a path.
106 * PUBLIC: char *tail __P((char *));
113 if ((p
= strrchr(path
, '/')) == NULL
)
120 * Strdup for wide character strings with an associated length.
122 * PUBLIC: char *v_strdup __P((SCR *, const char *, size_t));
125 v_strdup(SCR
*sp
, const char *str
, size_t len
)
129 MALLOC(sp
, copy
, char *, (len
+ 1));
132 memcpy(copy
, str
, len
);
139 * Strdup for wide character strings with an associated length.
141 * PUBLIC: CHAR_T *v_wstrdup __P((SCR *, const CHAR_T *, size_t));
144 v_wstrdup(SCR
*sp
, const CHAR_T
*str
, size_t len
)
148 MALLOC(sp
, copy
, CHAR_T
*, (len
+ 1) * sizeof(CHAR_T
));
151 MEMCPYW(copy
, str
, len
);
158 * Get an unsigned long, checking for overflow.
160 * PUBLIC: enum nresult nget_uslong __P((SCR *, u_long *, const CHAR_T *, CHAR_T **, int));
163 nget_uslong(SCR
*sp
, u_long
*valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
166 *valp
= STRTOUL(p
, endp
, base
);
169 if (errno
== ERANGE
&& *valp
== ULONG_MAX
)
176 * Convert a signed long, checking for overflow and underflow.
178 * PUBLIC: enum nresult nget_slong __P((SCR *, long *, const CHAR_T *, CHAR_T **, int));
181 nget_slong(SCR
*sp
, long int *valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
184 *valp
= STRTOL(p
, endp
, base
);
187 if (errno
== ERANGE
) {
188 if (*valp
== LONG_MAX
)
190 if (*valp
== LONG_MIN
)