xref: /titanic_51/usr/src/lib/libresolv2/common/nameser/ns_ttl.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #ifndef lint
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* Import. */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "port_after.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
367c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
377c478bd9Sstevel@tonic-gate #else
387c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /* Forward. */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static int	fmt1(int t, char s, char **buf, size_t *buflen);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* Macros. */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define T(x) if ((x) < 0) return (-1); else (void)NULL
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* Public. */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int
527c478bd9Sstevel@tonic-gate ns_format_ttl(u_long src, char *dst, size_t dstlen) {
537c478bd9Sstevel@tonic-gate 	char *odst = dst;
547c478bd9Sstevel@tonic-gate 	int secs, mins, hours, days, weeks, x;
557c478bd9Sstevel@tonic-gate 	char *p;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	secs = src % 60;   src /= 60;
587c478bd9Sstevel@tonic-gate 	mins = src % 60;   src /= 60;
597c478bd9Sstevel@tonic-gate 	hours = src % 24;  src /= 24;
607c478bd9Sstevel@tonic-gate 	days = src % 7;    src /= 7;
617c478bd9Sstevel@tonic-gate 	weeks = src;       src = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	x = 0;
647c478bd9Sstevel@tonic-gate 	if (weeks) {
657c478bd9Sstevel@tonic-gate 		T(fmt1(weeks, 'W', &dst, &dstlen));
667c478bd9Sstevel@tonic-gate 		x++;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	if (days) {
697c478bd9Sstevel@tonic-gate 		T(fmt1(days, 'D', &dst, &dstlen));
707c478bd9Sstevel@tonic-gate 		x++;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	if (hours) {
737c478bd9Sstevel@tonic-gate 		T(fmt1(hours, 'H', &dst, &dstlen));
747c478bd9Sstevel@tonic-gate 		x++;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	if (mins) {
777c478bd9Sstevel@tonic-gate 		T(fmt1(mins, 'M', &dst, &dstlen));
787c478bd9Sstevel@tonic-gate 		x++;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	if (secs || !(weeks || days || hours || mins)) {
817c478bd9Sstevel@tonic-gate 		T(fmt1(secs, 'S', &dst, &dstlen));
827c478bd9Sstevel@tonic-gate 		x++;
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (x > 1) {
867c478bd9Sstevel@tonic-gate 		int ch;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		for (p = odst; (ch = *p) != '\0'; p++)
897c478bd9Sstevel@tonic-gate 			if (isascii(ch) && isupper(ch))
907c478bd9Sstevel@tonic-gate 				*p = tolower(ch);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	return (dst - odst);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate int
977c478bd9Sstevel@tonic-gate ns_parse_ttl(const char *src, u_long *dst) {
987c478bd9Sstevel@tonic-gate 	u_long ttl, tmp;
997c478bd9Sstevel@tonic-gate 	int ch, digits, dirty;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	ttl = 0;
1027c478bd9Sstevel@tonic-gate 	tmp = 0;
1037c478bd9Sstevel@tonic-gate 	digits = 0;
1047c478bd9Sstevel@tonic-gate 	dirty = 0;
1057c478bd9Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1067c478bd9Sstevel@tonic-gate 		if (!isascii(ch) || !isprint(ch))
1077c478bd9Sstevel@tonic-gate 			goto einval;
1087c478bd9Sstevel@tonic-gate 		if (isdigit(ch)) {
1097c478bd9Sstevel@tonic-gate 			tmp *= 10;
1107c478bd9Sstevel@tonic-gate 			tmp += (ch - '0');
1117c478bd9Sstevel@tonic-gate 			digits++;
1127c478bd9Sstevel@tonic-gate 			continue;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		if (digits == 0)
1157c478bd9Sstevel@tonic-gate 			goto einval;
1167c478bd9Sstevel@tonic-gate 		if (islower(ch))
1177c478bd9Sstevel@tonic-gate 			ch = toupper(ch);
1187c478bd9Sstevel@tonic-gate 		switch (ch) {
1197c478bd9Sstevel@tonic-gate 		case 'W':  tmp *= 7;
1207c478bd9Sstevel@tonic-gate 		case 'D':  tmp *= 24;
1217c478bd9Sstevel@tonic-gate 		case 'H':  tmp *= 60;
1227c478bd9Sstevel@tonic-gate 		case 'M':  tmp *= 60;
1237c478bd9Sstevel@tonic-gate 		case 'S':  break;
1247c478bd9Sstevel@tonic-gate 		default:   goto einval;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		ttl += tmp;
1277c478bd9Sstevel@tonic-gate 		tmp = 0;
1287c478bd9Sstevel@tonic-gate 		digits = 0;
1297c478bd9Sstevel@tonic-gate 		dirty = 1;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	if (digits > 0) {
1327c478bd9Sstevel@tonic-gate 		if (dirty)
1337c478bd9Sstevel@tonic-gate 			goto einval;
1347c478bd9Sstevel@tonic-gate 		else
1357c478bd9Sstevel@tonic-gate 			ttl += tmp;
136*9525b14bSRao Shoaib 	} else if (!dirty)
137*9525b14bSRao Shoaib 		goto einval;
1387c478bd9Sstevel@tonic-gate 	*dst = ttl;
1397c478bd9Sstevel@tonic-gate 	return (0);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate  einval:
1427c478bd9Sstevel@tonic-gate 	errno = EINVAL;
1437c478bd9Sstevel@tonic-gate 	return (-1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* Private. */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static int
1497c478bd9Sstevel@tonic-gate fmt1(int t, char s, char **buf, size_t *buflen) {
1507c478bd9Sstevel@tonic-gate 	char tmp[50];
1517c478bd9Sstevel@tonic-gate 	size_t len;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	len = SPRINTF((tmp, "%d%c", t, s));
1547c478bd9Sstevel@tonic-gate 	if (len + 1 > *buflen)
1557c478bd9Sstevel@tonic-gate 		return (-1);
1567c478bd9Sstevel@tonic-gate 	strcpy(*buf, tmp);
1577c478bd9Sstevel@tonic-gate 	*buf += len;
1587c478bd9Sstevel@tonic-gate 	*buflen -= len;
1597c478bd9Sstevel@tonic-gate 	return (0);
1607c478bd9Sstevel@tonic-gate }
161*9525b14bSRao Shoaib 
162*9525b14bSRao Shoaib /*! \file */
163