1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1996,1999 by Internet Software Consortium. 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 ISC DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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 15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include "port_before.h" 19 20 #include <arpa/nameser.h> 21 22 #include <ctype.h> 23 #include <errno.h> 24 #include <stdio.h> 25 #include <string.h> 26 27 #include "port_after.h" 28 29 #ifdef SPRINTF_CHAR 30 # define SPRINTF(x) strlen(sprintf/**/x) 31 #else 32 # define SPRINTF(x) ((size_t)sprintf x) 33 #endif 34 35 /* Forward. */ 36 37 static int fmt1(int t, char s, char **buf, size_t *buflen); 38 39 /* Macros. */ 40 41 #define T(x) if ((x) < 0) return (-1); else (void)NULL 42 43 /* Public. */ 44 45 int 46 ns_format_ttl(u_long src, char *dst, size_t dstlen) { 47 char *odst = dst; 48 int secs, mins, hours, days, weeks, x; 49 char *p; 50 51 secs = src % 60; src /= 60; 52 mins = src % 60; src /= 60; 53 hours = src % 24; src /= 24; 54 days = src % 7; src /= 7; 55 weeks = src; src = 0; 56 57 x = 0; 58 if (weeks) { 59 T(fmt1(weeks, 'W', &dst, &dstlen)); 60 x++; 61 } 62 if (days) { 63 T(fmt1(days, 'D', &dst, &dstlen)); 64 x++; 65 } 66 if (hours) { 67 T(fmt1(hours, 'H', &dst, &dstlen)); 68 x++; 69 } 70 if (mins) { 71 T(fmt1(mins, 'M', &dst, &dstlen)); 72 x++; 73 } 74 if (secs || !(weeks || days || hours || mins)) { 75 T(fmt1(secs, 'S', &dst, &dstlen)); 76 x++; 77 } 78 79 if (x > 1) { 80 int ch; 81 82 for (p = odst; (ch = *p) != '\0'; p++) 83 if (isascii(ch) && isupper(ch)) 84 *p = tolower(ch); 85 } 86 87 return (dst - odst); 88 } 89 90 int 91 ns_parse_ttl(const char *src, u_long *dst) { 92 u_long ttl, tmp; 93 int ch, digits, dirty; 94 95 ttl = 0; 96 tmp = 0; 97 digits = 0; 98 dirty = 0; 99 while ((ch = *src++) != '\0') { 100 if (!isascii(ch) || !isprint(ch)) 101 goto einval; 102 if (isdigit(ch)) { 103 tmp *= 10; 104 tmp += (ch - '0'); 105 digits++; 106 continue; 107 } 108 if (digits == 0) 109 goto einval; 110 if (islower(ch)) 111 ch = toupper(ch); 112 switch (ch) { 113 case 'W': tmp *= 7; 114 case 'D': tmp *= 24; 115 case 'H': tmp *= 60; 116 case 'M': tmp *= 60; 117 case 'S': break; 118 default: goto einval; 119 } 120 ttl += tmp; 121 tmp = 0; 122 digits = 0; 123 dirty = 1; 124 } 125 if (digits > 0) { 126 if (dirty) 127 goto einval; 128 else 129 ttl += tmp; 130 } else if (!dirty) 131 goto einval; 132 *dst = ttl; 133 return (0); 134 135 einval: 136 errno = EINVAL; 137 return (-1); 138 } 139 140 /* Private. */ 141 142 static int 143 fmt1(int t, char s, char **buf, size_t *buflen) { 144 char tmp[50]; 145 size_t len; 146 147 len = SPRINTF((tmp, "%d%c", t, s)); 148 if (len + 1 > *buflen) 149 return (-1); 150 strcpy(*buf, tmp); 151 *buf += len; 152 *buflen -= len; 153 return (0); 154 } 155 156 /*! \file */ 157