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[] = "@(#)util.c 10.11 (Berkeley) 9/15/96";
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 *, 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_T *v_strdup __P((SCR *, const CHAR_T *, size_t));
129 v_strdup(sp, str, len)
136 MALLOC(sp, copy, CHAR_T *, len + 1);
139 memcpy(copy, str, len * sizeof(CHAR_T));
146 * Get an unsigned long, checking for overflow.
148 * PUBLIC: enum nresult nget_uslong __P((u_long *, const char *, char **, int));
151 nget_uslong(valp, p, endp, base)
158 *valp = strtoul(p, endp, base);
161 if (errno == ERANGE && *valp == ULONG_MAX)
168 * Convert a signed long, checking for overflow and underflow.
170 * PUBLIC: enum nresult nget_slong __P((long *, const char *, char **, int));
173 nget_slong(valp, p, endp, base)
180 *valp = strtol(p, endp, base);
183 if (errno == ERANGE) {
184 if (*valp == LONG_MAX)
186 if (*valp == LONG_MIN)
201 * debugging trace routine.
203 * PUBLIC: void TRACE __P((SCR *, const char *, ...));
207 TRACE(SCR *sp, const char *fmt, ...)
209 TRACE(sp, fmt, va_alist)
218 if ((tfp = sp->gp->tracefp) == NULL)
225 (void)vfprintf(tfp, fmt, ap);