#include "config.h"
#include <sys/queue.h>
#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
void *
binc(SCR *sp, void *bp, size_t *bsizep, size_t min)
{
size_t csize;
if (min && *bsizep >= min)
return (bp);
csize = *bsizep + MAXIMUM(min, 256);
REALLOC(sp, bp, csize);
if (bp == NULL) {
*bsizep = 0;
return (NULL);
}
memset((char *)bp + *bsizep, 0, csize - *bsizep);
*bsizep = csize;
return (bp);
}
int
nonblank(SCR *sp, recno_t lno, size_t *cnop)
{
char *p;
size_t cnt, len, off;
int isempty;
off = *cnop;
*cnop = 0;
if (db_eget(sp, lno, &p, &len, &isempty))
return (!isempty);
if (len == 0 || off >= len)
return (0);
for (cnt = off, p = &p[off],
len -= off; len && isblank(*p); ++cnt, ++p, --len);
*cnop = len ? cnt : cnt - 1;
return (0);
}
CHAR_T *
v_strdup(SCR *sp, const CHAR_T *str, size_t len)
{
CHAR_T *copy;
MALLOC(sp, copy, len + 1);
if (copy == NULL)
return (NULL);
memcpy(copy, str, len * sizeof(CHAR_T));
copy[len] = '\0';
return (copy);
}
enum nresult
nget_uslong(u_long *valp, const char *p, char **endp, int base)
{
errno = 0;
*valp = strtoul(p, endp, base);
if (errno == 0)
return (NUM_OK);
if (errno == ERANGE && *valp == ULONG_MAX)
return (NUM_OVER);
return (NUM_ERR);
}
enum nresult
nget_slong(long *valp, const char *p, char **endp, int base)
{
errno = 0;
*valp = strtol(p, endp, base);
if (errno == 0)
return (NUM_OK);
if (errno == ERANGE) {
if (*valp == LONG_MAX)
return (NUM_OVER);
if (*valp == LONG_MIN)
return (NUM_UNDER);
}
return (NUM_ERR);
}
#ifdef DEBUG
#include <stdarg.h>
void
TRACE(SCR *sp, const char *fmt, ...)
{
FILE *tfp;
va_list ap;
if ((tfp = sp->gp->tracefp) == NULL)
return;
va_start(ap, fmt);
(void)vfprintf(tfp, fmt, ap);
fflush(tfp);
va_end(ap);
(void)fflush(tfp);
}
#endif