1*cca8938dSgson /* $NetBSD: decode.c,v 1.2 2024/02/27 21:05:34 gson Exp $ */
23a70beecSmrg
33a70beecSmrg /*
43a70beecSmrg * Copyright (c) 1999 Matthew R. Green
53a70beecSmrg * All rights reserved.
63a70beecSmrg *
73a70beecSmrg * Redistribution and use in source and binary forms, with or without
83a70beecSmrg * modification, are permitted provided that the following conditions
93a70beecSmrg * are met:
103a70beecSmrg * 1. Redistributions of source code must retain the above copyright
113a70beecSmrg * notice, this list of conditions and the following disclaimer.
123a70beecSmrg * 2. Redistributions in binary form must reproduce the above copyright
133a70beecSmrg * notice, this list of conditions and the following disclaimer in the
143a70beecSmrg * documentation and/or other materials provided with the distribution.
153a70beecSmrg *
163a70beecSmrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173a70beecSmrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183a70beecSmrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193a70beecSmrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203a70beecSmrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
213a70beecSmrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
223a70beecSmrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
233a70beecSmrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
243a70beecSmrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253a70beecSmrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263a70beecSmrg * SUCH DAMAGE.
273a70beecSmrg */
283a70beecSmrg
293a70beecSmrg #include <sys/cdefs.h>
303a70beecSmrg
313a70beecSmrg #ifndef lint
32*cca8938dSgson __RCSID("$NetBSD: decode.c,v 1.2 2024/02/27 21:05:34 gson Exp $");
333a70beecSmrg #endif
343a70beecSmrg
353a70beecSmrg #include <sys/types.h>
363a70beecSmrg #include <sys/time.h>
373a70beecSmrg
383a70beecSmrg #include <ctype.h>
393a70beecSmrg #include <string.h>
403a70beecSmrg #include <stdlib.h>
413a70beecSmrg #include <limits.h>
423a70beecSmrg #include <err.h>
433a70beecSmrg
443a70beecSmrg #include "libaudio.h"
453a70beecSmrg
463a70beecSmrg void
decode_int(const char * arg,int * intp)473a70beecSmrg decode_int(const char *arg, int *intp)
483a70beecSmrg {
493a70beecSmrg char *ep;
503a70beecSmrg int ret;
513a70beecSmrg
523a70beecSmrg ret = (int)strtoul(arg, &ep, 10);
533a70beecSmrg
543a70beecSmrg if (ep[0] == '\0') {
553a70beecSmrg *intp = ret;
563a70beecSmrg return;
573a70beecSmrg }
583a70beecSmrg errx(1, "argument `%s' not a valid integer", arg);
593a70beecSmrg }
603a70beecSmrg
613a70beecSmrg void
decode_uint(const char * arg,unsigned * intp)623a70beecSmrg decode_uint(const char *arg, unsigned *intp)
633a70beecSmrg {
643a70beecSmrg char *ep;
653a70beecSmrg unsigned ret;
663a70beecSmrg
673a70beecSmrg ret = (unsigned)strtoul(arg, &ep, 10);
683a70beecSmrg
693a70beecSmrg if (ep[0] == '\0') {
703a70beecSmrg *intp = ret;
713a70beecSmrg return;
723a70beecSmrg }
733a70beecSmrg errx(1, "argument `%s' not a valid integer", arg);
743a70beecSmrg }
753a70beecSmrg
763a70beecSmrg void
decode_time(const char * arg,struct timeval * tvp)773a70beecSmrg decode_time(const char *arg, struct timeval *tvp)
783a70beecSmrg {
793a70beecSmrg char *s, *colon, *dot;
803a70beecSmrg char *copy = strdup(arg);
813a70beecSmrg int first;
823a70beecSmrg
833a70beecSmrg if (copy == NULL)
843a70beecSmrg err(1, "could not allocate a copy of %s", arg);
853a70beecSmrg
863a70beecSmrg tvp->tv_sec = tvp->tv_usec = 0;
873a70beecSmrg s = copy;
883a70beecSmrg
893a70beecSmrg /* handle [hh:]mm:ss.dd */
903a70beecSmrg if ((colon = strchr(s, ':')) != NULL) {
913a70beecSmrg *colon++ = '\0';
923a70beecSmrg decode_int(s, &first);
933a70beecSmrg tvp->tv_sec = first * 60; /* minutes */
943a70beecSmrg s = colon;
953a70beecSmrg
963a70beecSmrg if ((colon = strchr(s, ':')) != NULL) {
973a70beecSmrg *colon++ = '\0';
983a70beecSmrg decode_int(s, &first);
993a70beecSmrg tvp->tv_sec += first; /* minutes and hours */
1003a70beecSmrg tvp->tv_sec *= 60;
1013a70beecSmrg s = colon;
1023a70beecSmrg }
1033a70beecSmrg }
1043a70beecSmrg if ((dot = strchr(s, '.')) != NULL) {
1053a70beecSmrg int i, base = 100000;
1063a70beecSmrg
1073a70beecSmrg *dot++ = '\0';
1083a70beecSmrg
1093a70beecSmrg for (i = 0; i < 6; i++, base /= 10) {
1103a70beecSmrg if (!dot[i])
1113a70beecSmrg break;
1123a70beecSmrg if (!isdigit((unsigned char)dot[i]))
1133a70beecSmrg errx(1, "argument `%s' is not a value time specification", arg);
1143a70beecSmrg tvp->tv_usec += base * (dot[i] - '0');
1153a70beecSmrg }
1163a70beecSmrg }
1173a70beecSmrg decode_int(s, &first);
1183a70beecSmrg tvp->tv_sec += first;
1193a70beecSmrg
1203a70beecSmrg free(copy);
1213a70beecSmrg }
122