ns_ttl.c revision 1.1
11.1Schristos/* $NetBSD: ns_ttl.c,v 1.1 2004/05/20 20:01:31 christos Exp $ */ 21.1Schristos 31.1Schristos/* 41.1Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 51.1Schristos * Copyright (c) 1996,1999 by Internet Software Consortium. 61.1Schristos * 71.1Schristos * Permission to use, copy, modify, and distribute this software for any 81.1Schristos * purpose with or without fee is hereby granted, provided that the above 91.1Schristos * copyright notice and this permission notice appear in all copies. 101.1Schristos * 111.1Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 121.1Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 131.1Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 141.1Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 151.1Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 161.1Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 171.1Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 181.1Schristos */ 191.1Schristos 201.1Schristos#ifndef lint 211.1Schristosstatic const char rcsid[] = "Id: ns_ttl.c,v 1.1.206.1 2004/03/09 08:33:45 marka Exp"; 221.1Schristos#endif 231.1Schristos 241.1Schristos/* Import. */ 251.1Schristos 261.1Schristos#include "port_before.h" 271.1Schristos 281.1Schristos#include <arpa/nameser.h> 291.1Schristos 301.1Schristos#include <ctype.h> 311.1Schristos#include <errno.h> 321.1Schristos#include <stdio.h> 331.1Schristos#include <string.h> 341.1Schristos 351.1Schristos#include "port_after.h" 361.1Schristos 371.1Schristos#ifdef SPRINTF_CHAR 381.1Schristos# define SPRINTF(x) strlen(sprintf/**/x) 391.1Schristos#else 401.1Schristos# define SPRINTF(x) ((size_t)sprintf x) 411.1Schristos#endif 421.1Schristos 431.1Schristos/* Forward. */ 441.1Schristos 451.1Schristosstatic int fmt1(int t, char s, char **buf, size_t *buflen); 461.1Schristos 471.1Schristos/* Macros. */ 481.1Schristos 491.1Schristos#define T(x) if ((x) < 0) return (-1); else (void)NULL 501.1Schristos 511.1Schristos/* Public. */ 521.1Schristos 531.1Schristosint 541.1Schristosns_format_ttl(u_long src, char *dst, size_t dstlen) { 551.1Schristos char *odst = dst; 561.1Schristos int secs, mins, hours, days, weeks, x; 571.1Schristos char *p; 581.1Schristos 591.1Schristos secs = src % 60; src /= 60; 601.1Schristos mins = src % 60; src /= 60; 611.1Schristos hours = src % 24; src /= 24; 621.1Schristos days = src % 7; src /= 7; 631.1Schristos weeks = src; src = 0; 641.1Schristos 651.1Schristos x = 0; 661.1Schristos if (weeks) { 671.1Schristos T(fmt1(weeks, 'W', &dst, &dstlen)); 681.1Schristos x++; 691.1Schristos } 701.1Schristos if (days) { 711.1Schristos T(fmt1(days, 'D', &dst, &dstlen)); 721.1Schristos x++; 731.1Schristos } 741.1Schristos if (hours) { 751.1Schristos T(fmt1(hours, 'H', &dst, &dstlen)); 761.1Schristos x++; 771.1Schristos } 781.1Schristos if (mins) { 791.1Schristos T(fmt1(mins, 'M', &dst, &dstlen)); 801.1Schristos x++; 811.1Schristos } 821.1Schristos if (secs || !(weeks || days || hours || mins)) { 831.1Schristos T(fmt1(secs, 'S', &dst, &dstlen)); 841.1Schristos x++; 851.1Schristos } 861.1Schristos 871.1Schristos if (x > 1) { 881.1Schristos int ch; 891.1Schristos 901.1Schristos for (p = odst; (ch = *p) != '\0'; p++) 911.1Schristos if (isascii(ch) && isupper(ch)) 921.1Schristos *p = tolower(ch); 931.1Schristos } 941.1Schristos 951.1Schristos return (dst - odst); 961.1Schristos} 971.1Schristos 981.1Schristosint 991.1Schristosns_parse_ttl(const char *src, u_long *dst) { 1001.1Schristos u_long ttl, tmp; 1011.1Schristos int ch, digits, dirty; 1021.1Schristos 1031.1Schristos ttl = 0; 1041.1Schristos tmp = 0; 1051.1Schristos digits = 0; 1061.1Schristos dirty = 0; 1071.1Schristos while ((ch = *src++) != '\0') { 1081.1Schristos if (!isascii(ch) || !isprint(ch)) 1091.1Schristos goto einval; 1101.1Schristos if (isdigit(ch)) { 1111.1Schristos tmp *= 10; 1121.1Schristos tmp += (ch - '0'); 1131.1Schristos digits++; 1141.1Schristos continue; 1151.1Schristos } 1161.1Schristos if (digits == 0) 1171.1Schristos goto einval; 1181.1Schristos if (islower(ch)) 1191.1Schristos ch = toupper(ch); 1201.1Schristos switch (ch) { 1211.1Schristos case 'W': tmp *= 7; 1221.1Schristos case 'D': tmp *= 24; 1231.1Schristos case 'H': tmp *= 60; 1241.1Schristos case 'M': tmp *= 60; 1251.1Schristos case 'S': break; 1261.1Schristos default: goto einval; 1271.1Schristos } 1281.1Schristos ttl += tmp; 1291.1Schristos tmp = 0; 1301.1Schristos digits = 0; 1311.1Schristos dirty = 1; 1321.1Schristos } 1331.1Schristos if (digits > 0) { 1341.1Schristos if (dirty) 1351.1Schristos goto einval; 1361.1Schristos else 1371.1Schristos ttl += tmp; 1381.1Schristos } 1391.1Schristos *dst = ttl; 1401.1Schristos return (0); 1411.1Schristos 1421.1Schristos einval: 1431.1Schristos errno = EINVAL; 1441.1Schristos return (-1); 1451.1Schristos} 1461.1Schristos 1471.1Schristos/* Private. */ 1481.1Schristos 1491.1Schristosstatic int 1501.1Schristosfmt1(int t, char s, char **buf, size_t *buflen) { 1511.1Schristos char tmp[50]; 1521.1Schristos size_t len; 1531.1Schristos 1541.1Schristos len = SPRINTF((tmp, "%d%c", t, s)); 1551.1Schristos if (len + 1 > *buflen) 1561.1Schristos return (-1); 1571.1Schristos strcpy(*buf, tmp); 1581.1Schristos *buf += len; 1591.1Schristos *buflen -= len; 1601.1Schristos return (0); 1611.1Schristos} 162