1 /* $NetBSD: util.c,v 1.2 2008/08/27 10:18:41 christos Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
33 * Increase the size of a buffer.
35 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
38 binc(SCR
*sp
, void *bp
, size_t *bsizep
, size_t min
)
39 /* sp MAY BE NULL!!! */
45 /* If already larger than the minimum, just return. */
46 if (min
&& *bsizep
>= min
)
49 csize
= *bsizep
+ MAX(min
, 256);
50 REALLOC(sp
, bp
, void *, csize
);
54 * Theoretically, realloc is supposed to leave any already
55 * held memory alone if it can't get more. Don't trust it.
61 * Memory is guaranteed to be zero-filled, various parts of
64 memset((char *)bp
+ *bsizep
, 0, csize
- *bsizep
);
71 * Set the column number of the first non-blank character
72 * including or after the starting column. On error, set
73 * the column to 0, it's safest.
75 * PUBLIC: int nonblank __P((SCR *, db_recno_t, size_t *));
78 nonblank(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
88 /* Get the line, succeeding in an empty file. */
89 if (db_eget(sp
, lno
, &p
, &len
, &isempty
))
93 if (len
== 0 || off
>= len
)
96 for (cnt
= off
, p
= &p
[off
],
97 len
-= off
; len
&& isblank(*p
); ++cnt
, ++p
, --len
);
100 *cnop
= len
? cnt
: cnt
- 1;
106 * Return tail of a path.
108 * PUBLIC: char *tail __P((char *));
111 tail(const char *path
)
115 if ((p
= strrchr(path
, '/')) == NULL
)
122 * Strdup for wide character strings with an associated length.
124 * PUBLIC: char *v_strdup __P((SCR *, const char *, size_t));
127 v_strdup(SCR
*sp
, const char *str
, size_t len
)
131 MALLOC(sp
, copy
, char *, (len
+ 1));
134 memcpy(copy
, str
, len
);
141 * Strdup for wide character strings with an associated length.
143 * PUBLIC: CHAR_T *v_wstrdup __P((SCR *, const CHAR_T *, size_t));
146 v_wstrdup(SCR
*sp
, const CHAR_T
*str
, size_t len
)
150 MALLOC(sp
, copy
, CHAR_T
*, (len
+ 1) * sizeof(CHAR_T
));
153 MEMCPYW(copy
, str
, len
);
160 * Get an unsigned long, checking for overflow.
162 * PUBLIC: enum nresult nget_uslong __P((SCR *, u_long *, const CHAR_T *, CHAR_T **, int));
165 nget_uslong(SCR
*sp
, u_long
*valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
168 *valp
= STRTOUL(p
, (RCHAR_T
**)endp
, base
);
171 if (errno
== ERANGE
&& *valp
== ULONG_MAX
)
178 * Convert a signed long, checking for overflow and underflow.
180 * PUBLIC: enum nresult nget_slong __P((SCR *, long *, const CHAR_T *, CHAR_T **, int));
183 nget_slong(SCR
*sp
, long int *valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
186 *valp
= STRTOL(p
, (RCHAR_T
**)endp
, base
);
189 if (errno
== ERANGE
) {
190 if (*valp
== LONG_MAX
)
192 if (*valp
== LONG_MIN
)