xref: /openbsd-src/lib/libsndio/debug.c (revision 8522ebc41ac3035a480709de2deac640d46877db)
1*8522ebc4Smiko /*	$OpenBSD: debug.c,v 1.5 2018/09/26 08:33:22 miko Exp $	*/
282bfc72bSratchov /*
382bfc72bSratchov  * Copyright (c) 2011 Alexandre Ratchov <alex@caoua.org>
482bfc72bSratchov  *
582bfc72bSratchov  * Permission to use, copy, modify, and distribute this software for any
682bfc72bSratchov  * purpose with or without fee is hereby granted, provided that the above
782bfc72bSratchov  * copyright notice and this permission notice appear in all copies.
882bfc72bSratchov  *
982bfc72bSratchov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1082bfc72bSratchov  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1182bfc72bSratchov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1282bfc72bSratchov  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1382bfc72bSratchov  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1482bfc72bSratchov  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1582bfc72bSratchov  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1682bfc72bSratchov  */
1782bfc72bSratchov 
1882bfc72bSratchov #include <stdio.h>
1982bfc72bSratchov #include <stdlib.h>
2082bfc72bSratchov #include <unistd.h>
2182bfc72bSratchov 
2282bfc72bSratchov #include "debug.h"
2382bfc72bSratchov 
2482bfc72bSratchov #ifdef DEBUG
2582bfc72bSratchov /*
2682bfc72bSratchov  * debug level, -1 means uninitialized
2782bfc72bSratchov  */
28d418f94bSratchov int _sndio_debug = -1;
2982bfc72bSratchov 
3082bfc72bSratchov void
_sndio_debug_init(void)31d418f94bSratchov _sndio_debug_init(void)
3282bfc72bSratchov {
3382bfc72bSratchov 	char *dbg;
3482bfc72bSratchov 
35d418f94bSratchov 	if (_sndio_debug < 0) {
3682bfc72bSratchov 		dbg = issetugid() ? NULL : getenv("SNDIO_DEBUG");
37d418f94bSratchov 		if (!dbg || sscanf(dbg, "%u", &_sndio_debug) != 1)
38d418f94bSratchov 			_sndio_debug = 0;
3982bfc72bSratchov 	}
4082bfc72bSratchov }
4182bfc72bSratchov #endif
42b3956098Sratchov 
43b3956098Sratchov const char *
_sndio_parsetype(const char * str,char * type)44d418f94bSratchov _sndio_parsetype(const char *str, char *type)
45b3956098Sratchov {
46b3956098Sratchov 	while (*type) {
47b3956098Sratchov 		if (*type != *str)
48b3956098Sratchov 			return NULL;
49b3956098Sratchov 		type++;
50b3956098Sratchov 		str++;
51b3956098Sratchov 	}
52b3956098Sratchov 	if (*str >= 'a' && *str <= 'z')
53b3956098Sratchov 		return NULL;
54b3956098Sratchov 	return str;
55b3956098Sratchov }
56b44cb2caSratchov 
57b44cb2caSratchov const char *
_sndio_parsenum(const char * str,unsigned int * num,unsigned int max)58b44cb2caSratchov _sndio_parsenum(const char *str, unsigned int *num, unsigned int max)
59b44cb2caSratchov {
60b44cb2caSratchov 	const char *p = str;
61b44cb2caSratchov 	unsigned int dig, maxq, maxr, val;
62b44cb2caSratchov 
63b44cb2caSratchov 	val = 0;
64b44cb2caSratchov 	maxq = max / 10;
65b44cb2caSratchov 	maxr = max % 10;
66b44cb2caSratchov 	for (;;) {
67b44cb2caSratchov 		dig = *p - '0';
68b44cb2caSratchov 		if (dig >= 10)
69b44cb2caSratchov 			break;
70*8522ebc4Smiko 		if (val > maxq || (val == maxq && dig > maxr)) {
71*8522ebc4Smiko 			DPRINTF("%s: number too large\n", str);
72b44cb2caSratchov 			return NULL;
73*8522ebc4Smiko 		}
74b44cb2caSratchov 		val = val * 10 + dig;
75b44cb2caSratchov 		p++;
76b44cb2caSratchov 	}
77*8522ebc4Smiko 	if (p == str) {
78*8522ebc4Smiko 		DPRINTF("%s: number expected\n", str);
79b44cb2caSratchov 		return NULL;
80*8522ebc4Smiko 	}
81b44cb2caSratchov 	*num = val;
82b44cb2caSratchov 	return p;
83b44cb2caSratchov }
84