1 /* $OpenBSD: debug.c,v 1.5 2018/09/26 08:33:22 miko Exp $ */ 2 /* 3 * Copyright (c) 2011 Alexandre Ratchov <alex@caoua.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 22 #include "debug.h" 23 24 #ifdef DEBUG 25 /* 26 * debug level, -1 means uninitialized 27 */ 28 int _sndio_debug = -1; 29 30 void 31 _sndio_debug_init(void) 32 { 33 char *dbg; 34 35 if (_sndio_debug < 0) { 36 dbg = issetugid() ? NULL : getenv("SNDIO_DEBUG"); 37 if (!dbg || sscanf(dbg, "%u", &_sndio_debug) != 1) 38 _sndio_debug = 0; 39 } 40 } 41 #endif 42 43 const char * 44 _sndio_parsetype(const char *str, char *type) 45 { 46 while (*type) { 47 if (*type != *str) 48 return NULL; 49 type++; 50 str++; 51 } 52 if (*str >= 'a' && *str <= 'z') 53 return NULL; 54 return str; 55 } 56 57 const char * 58 _sndio_parsenum(const char *str, unsigned int *num, unsigned int max) 59 { 60 const char *p = str; 61 unsigned int dig, maxq, maxr, val; 62 63 val = 0; 64 maxq = max / 10; 65 maxr = max % 10; 66 for (;;) { 67 dig = *p - '0'; 68 if (dig >= 10) 69 break; 70 if (val > maxq || (val == maxq && dig > maxr)) { 71 DPRINTF("%s: number too large\n", str); 72 return NULL; 73 } 74 val = val * 10 + dig; 75 p++; 76 } 77 if (p == str) { 78 DPRINTF("%s: number expected\n", str); 79 return NULL; 80 } 81 *num = val; 82 return p; 83 } 84