1 /* $NetBSD: decode.c,v 1.1 2015/06/21 06:06:01 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 31 #ifndef lint 32 __RCSID("$NetBSD: decode.c,v 1.1 2015/06/21 06:06:01 mrg Exp $"); 33 #endif 34 35 #include <sys/types.h> 36 #include <sys/time.h> 37 38 #include <ctype.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <limits.h> 42 #include <err.h> 43 44 #include "libaudio.h" 45 46 void 47 decode_int(const char *arg, int *intp) 48 { 49 char *ep; 50 int ret; 51 52 ret = (int)strtoul(arg, &ep, 10); 53 54 if (ep[0] == '\0') { 55 *intp = ret; 56 return; 57 } 58 errx(1, "argument `%s' not a valid integer", arg); 59 } 60 61 void 62 decode_uint(const char *arg, unsigned *intp) 63 { 64 char *ep; 65 unsigned ret; 66 67 ret = (unsigned)strtoul(arg, &ep, 10); 68 69 if (ep[0] == '\0') { 70 *intp = ret; 71 return; 72 } 73 errx(1, "argument `%s' not a valid integer", arg); 74 } 75 76 void 77 decode_time(const char *arg, struct timeval *tvp) 78 { 79 char *s, *colon, *dot; 80 char *copy = strdup(arg); 81 int first; 82 83 if (copy == NULL) 84 err(1, "could not allocate a copy of %s", arg); 85 86 tvp->tv_sec = tvp->tv_usec = 0; 87 s = copy; 88 89 /* handle [hh:]mm:ss.dd */ 90 if ((colon = strchr(s, ':')) != NULL) { 91 *colon++ = '\0'; 92 decode_int(s, &first); 93 tvp->tv_sec = first * 60; /* minutes */ 94 s = colon; 95 96 if ((colon = strchr(s, ':')) != NULL) { 97 *colon++ = '\0'; 98 decode_int(s, &first); 99 tvp->tv_sec += first; /* minutes and hours */ 100 tvp->tv_sec *= 60; 101 s = colon; 102 } 103 } 104 if ((dot = strchr(s, '.')) != NULL) { 105 int i, base = 100000; 106 107 *dot++ = '\0'; 108 109 for (i = 0; i < 6; i++, base /= 10) { 110 if (!dot[i]) 111 break; 112 if (!isdigit((unsigned char)dot[i])) 113 errx(1, "argument `%s' is not a value time specification", arg); 114 tvp->tv_usec += base * (dot[i] - '0'); 115 } 116 } 117 decode_int(s, &first); 118 tvp->tv_sec += first; 119 120 free(copy); 121 } 122