#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "debug.h"
#ifdef DEBUG
int _sndio_debug = -1;
void
_sndio_debug_init(void)
{
char *dbg;
if (_sndio_debug < 0) {
dbg = issetugid() ? NULL : getenv("SNDIO_DEBUG");
if (!dbg || sscanf(dbg, "%u", &_sndio_debug) != 1)
_sndio_debug = 0;
}
}
#endif
const char *
_sndio_parsetype(const char *str, char *type)
{
while (*type) {
if (*type != *str)
return NULL;
type++;
str++;
}
if (*str >= 'a' && *str <= 'z')
return NULL;
return str;
}
const char *
_sndio_parsenum(const char *str, unsigned int *num, unsigned int max)
{
const char *p = str;
unsigned int dig, maxq, maxr, val;
val = 0;
maxq = max / 10;
maxr = max % 10;
for (;;) {
dig = *p - '0';
if (dig >= 10)
break;
if (val > maxq || (val == maxq && dig > maxr)) {
DPRINTF("%s: number too large\n", str);
return NULL;
}
val = val * 10 + dig;
p++;
}
if (p == str) {
DPRINTF("%s: number expected\n", str);
return NULL;
}
*num = val;
return p;
}