1 /* $NetBSD: util.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: util.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
37 * Increase the size of a buffer.
39 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
42 binc(SCR
*sp
, void *bp
, size_t *bsizep
, size_t min
)
43 /* sp MAY BE NULL!!! */
49 /* If already larger than the minimum, just return. */
50 if (min
&& *bsizep
>= min
)
53 csize
= *bsizep
+ MAX(min
, 256);
54 REALLOC(sp
, bp
, void *, csize
);
58 * Theoretically, realloc is supposed to leave any already
59 * held memory alone if it can't get more. Don't trust it.
65 * Memory is guaranteed to be zero-filled, various parts of
68 memset((char *)bp
+ *bsizep
, 0, csize
- *bsizep
);
75 * Set the column number of the first non-blank character
76 * including or after the starting column. On error, set
77 * the column to 0, it's safest.
79 * PUBLIC: int nonblank __P((SCR *, db_recno_t, size_t *));
82 nonblank(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
92 /* Get the line, succeeding in an empty file. */
93 if (db_eget(sp
, lno
, &p
, &len
, &isempty
))
97 if (len
== 0 || off
>= len
)
100 for (cnt
= off
, p
= &p
[off
],
101 len
-= off
; len
&& ISBLANK((UCHAR_T
)*p
); ++cnt
, ++p
, --len
);
103 /* Set the return. */
104 *cnop
= len
? cnt
: cnt
- 1;
110 * Return tail of a path.
112 * PUBLIC: const char *tail __P((const char *));
115 tail(const char *path
)
119 if ((p
= strrchr(path
, '/')) == NULL
)
126 * Strdup for wide character strings with an associated length.
128 * PUBLIC: char *v_strdup __P((SCR *, const char *, size_t));
131 v_strdup(SCR
*sp
, const char *str
, size_t len
)
135 MALLOC(sp
, copy
, char *, (len
+ 1));
138 memcpy(copy
, str
, len
);
145 * Strdup for wide character strings with an associated length.
147 * PUBLIC: CHAR_T *v_wstrdup __P((SCR *, const CHAR_T *, size_t));
150 v_wstrdup(SCR
*sp
, const CHAR_T
*str
, size_t len
)
154 MALLOC(sp
, copy
, CHAR_T
*, (len
+ 1) * sizeof(CHAR_T
));
157 MEMCPYW(copy
, str
, len
);
164 * Get an unsigned long, checking for overflow.
166 * PUBLIC: enum nresult nget_uslong __P((SCR *, u_long *, const CHAR_T *, CHAR_T **, int));
169 nget_uslong(SCR
*sp
, u_long
*valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
172 *valp
= STRTOUL(p
, (RCHAR_T
**)endp
, base
);
175 if (errno
== ERANGE
&& *valp
== ULONG_MAX
)
182 * Convert a signed long, checking for overflow and underflow.
184 * PUBLIC: enum nresult nget_slong __P((SCR *, long *, const CHAR_T *, CHAR_T **, int));
187 nget_slong(SCR
*sp
, long int *valp
, const CHAR_T
*p
, CHAR_T
**endp
, int base
)
190 *valp
= STRTOL(p
, (RCHAR_T
**)endp
, base
);
193 if (errno
== ERANGE
) {
194 if (*valp
== LONG_MAX
)
196 if (*valp
== LONG_MIN
)