xref: /onnv-gate/usr/src/lib/libresolv2/common/nameser/ns_ttl.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #ifndef lint
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ns_ttl.c,v 1.4 2005/07/28 06:51:49 marka Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Import. */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <arpa/nameser.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "port_after.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef SPRINTF_CHAR
360Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
370Sstevel@tonic-gate #else
380Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /* Forward. */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int	fmt1(int t, char s, char **buf, size_t *buflen);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* Macros. */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define T(x) if ((x) < 0) return (-1); else (void)NULL
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* Public. */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int
ns_format_ttl(u_long src,char * dst,size_t dstlen)520Sstevel@tonic-gate ns_format_ttl(u_long src, char *dst, size_t dstlen) {
530Sstevel@tonic-gate 	char *odst = dst;
540Sstevel@tonic-gate 	int secs, mins, hours, days, weeks, x;
550Sstevel@tonic-gate 	char *p;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	secs = src % 60;   src /= 60;
580Sstevel@tonic-gate 	mins = src % 60;   src /= 60;
590Sstevel@tonic-gate 	hours = src % 24;  src /= 24;
600Sstevel@tonic-gate 	days = src % 7;    src /= 7;
610Sstevel@tonic-gate 	weeks = src;       src = 0;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	x = 0;
640Sstevel@tonic-gate 	if (weeks) {
650Sstevel@tonic-gate 		T(fmt1(weeks, 'W', &dst, &dstlen));
660Sstevel@tonic-gate 		x++;
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 	if (days) {
690Sstevel@tonic-gate 		T(fmt1(days, 'D', &dst, &dstlen));
700Sstevel@tonic-gate 		x++;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	if (hours) {
730Sstevel@tonic-gate 		T(fmt1(hours, 'H', &dst, &dstlen));
740Sstevel@tonic-gate 		x++;
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	if (mins) {
770Sstevel@tonic-gate 		T(fmt1(mins, 'M', &dst, &dstlen));
780Sstevel@tonic-gate 		x++;
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 	if (secs || !(weeks || days || hours || mins)) {
810Sstevel@tonic-gate 		T(fmt1(secs, 'S', &dst, &dstlen));
820Sstevel@tonic-gate 		x++;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (x > 1) {
860Sstevel@tonic-gate 		int ch;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		for (p = odst; (ch = *p) != '\0'; p++)
890Sstevel@tonic-gate 			if (isascii(ch) && isupper(ch))
900Sstevel@tonic-gate 				*p = tolower(ch);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	return (dst - odst);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
ns_parse_ttl(const char * src,u_long * dst)970Sstevel@tonic-gate ns_parse_ttl(const char *src, u_long *dst) {
980Sstevel@tonic-gate 	u_long ttl, tmp;
990Sstevel@tonic-gate 	int ch, digits, dirty;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	ttl = 0;
1020Sstevel@tonic-gate 	tmp = 0;
1030Sstevel@tonic-gate 	digits = 0;
1040Sstevel@tonic-gate 	dirty = 0;
1050Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1060Sstevel@tonic-gate 		if (!isascii(ch) || !isprint(ch))
1070Sstevel@tonic-gate 			goto einval;
1080Sstevel@tonic-gate 		if (isdigit(ch)) {
1090Sstevel@tonic-gate 			tmp *= 10;
1100Sstevel@tonic-gate 			tmp += (ch - '0');
1110Sstevel@tonic-gate 			digits++;
1120Sstevel@tonic-gate 			continue;
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 		if (digits == 0)
1150Sstevel@tonic-gate 			goto einval;
1160Sstevel@tonic-gate 		if (islower(ch))
1170Sstevel@tonic-gate 			ch = toupper(ch);
1180Sstevel@tonic-gate 		switch (ch) {
1190Sstevel@tonic-gate 		case 'W':  tmp *= 7;
1200Sstevel@tonic-gate 		case 'D':  tmp *= 24;
1210Sstevel@tonic-gate 		case 'H':  tmp *= 60;
1220Sstevel@tonic-gate 		case 'M':  tmp *= 60;
1230Sstevel@tonic-gate 		case 'S':  break;
1240Sstevel@tonic-gate 		default:   goto einval;
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 		ttl += tmp;
1270Sstevel@tonic-gate 		tmp = 0;
1280Sstevel@tonic-gate 		digits = 0;
1290Sstevel@tonic-gate 		dirty = 1;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	if (digits > 0) {
1320Sstevel@tonic-gate 		if (dirty)
1330Sstevel@tonic-gate 			goto einval;
1340Sstevel@tonic-gate 		else
1350Sstevel@tonic-gate 			ttl += tmp;
136*11038SRao.Shoaib@Sun.COM 	} else if (!dirty)
137*11038SRao.Shoaib@Sun.COM 		goto einval;
1380Sstevel@tonic-gate 	*dst = ttl;
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate  einval:
1420Sstevel@tonic-gate 	errno = EINVAL;
1430Sstevel@tonic-gate 	return (-1);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* Private. */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate static int
fmt1(int t,char s,char ** buf,size_t * buflen)1490Sstevel@tonic-gate fmt1(int t, char s, char **buf, size_t *buflen) {
1500Sstevel@tonic-gate 	char tmp[50];
1510Sstevel@tonic-gate 	size_t len;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	len = SPRINTF((tmp, "%d%c", t, s));
1540Sstevel@tonic-gate 	if (len + 1 > *buflen)
1550Sstevel@tonic-gate 		return (-1);
1560Sstevel@tonic-gate 	strcpy(*buf, tmp);
1570Sstevel@tonic-gate 	*buf += len;
1580Sstevel@tonic-gate 	*buflen -= len;
1590Sstevel@tonic-gate 	return (0);
1600Sstevel@tonic-gate }
161*11038SRao.Shoaib@Sun.COM 
162*11038SRao.Shoaib@Sun.COM /*! \file */
163