|
Packit Service |
82fcde |
/*
|
|
Packit Service |
82fcde |
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
|
|
Packit Service |
82fcde |
* Copyright (c) 1996,1999 by Internet Software Consortium.
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Permission to use, copy, modify, and distribute this software for any
|
|
Packit Service |
82fcde |
* purpose with or without fee is hereby granted, provided that the above
|
|
Packit Service |
82fcde |
* copyright notice and this permission notice appear in all copies.
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
Packit Service |
82fcde |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
Packit Service |
82fcde |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
Packit Service |
82fcde |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
Packit Service |
82fcde |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
Packit Service |
82fcde |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
Packit Service |
82fcde |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
Packit Service |
82fcde |
* SOFTWARE.
|
|
Packit Service |
82fcde |
*/
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Import. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <arpa/nameser.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <ctype.h>
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <string.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifdef SPRINTF_CHAR
|
|
Packit Service |
82fcde |
# define SPRINTF(x) strlen(sprintf/**/x)
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# define SPRINTF(x) ((size_t)sprintf x)
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Forward. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int fmt1(int t, char s, char **buf, size_t *buflen);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Macros. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define T(x) if ((x) < 0) return (-1); else (void)NULL
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Public. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
int
|
|
Packit Service |
82fcde |
ns_format_ttl(u_long src, char *dst, size_t dstlen) {
|
|
Packit Service |
82fcde |
char *odst = dst;
|
|
Packit Service |
82fcde |
int secs, mins, hours, days, weeks, x;
|
|
Packit Service |
82fcde |
char *p;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
secs = src % 60; src /= 60;
|
|
Packit Service |
82fcde |
mins = src % 60; src /= 60;
|
|
Packit Service |
82fcde |
hours = src % 24; src /= 24;
|
|
Packit Service |
82fcde |
days = src % 7; src /= 7;
|
|
Packit Service |
82fcde |
weeks = src; src = 0;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
x = 0;
|
|
Packit Service |
82fcde |
if (weeks) {
|
|
Packit Service |
82fcde |
T(fmt1(weeks, 'W', &dst, &dstlen));
|
|
Packit Service |
82fcde |
x++;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (days) {
|
|
Packit Service |
82fcde |
T(fmt1(days, 'D', &dst, &dstlen));
|
|
Packit Service |
82fcde |
x++;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (hours) {
|
|
Packit Service |
82fcde |
T(fmt1(hours, 'H', &dst, &dstlen));
|
|
Packit Service |
82fcde |
x++;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (mins) {
|
|
Packit Service |
82fcde |
T(fmt1(mins, 'M', &dst, &dstlen));
|
|
Packit Service |
82fcde |
x++;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (secs || !(weeks || days || hours || mins)) {
|
|
Packit Service |
82fcde |
T(fmt1(secs, 'S', &dst, &dstlen));
|
|
Packit Service |
82fcde |
x++;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (x > 1) {
|
|
Packit Service |
82fcde |
int ch;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
for (p = odst; (ch = *p) != '\0'; p++)
|
|
Packit Service |
82fcde |
if (isascii(ch) && isupper(ch))
|
|
Packit Service |
82fcde |
*p = tolower(ch);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return (dst - odst);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
libresolv_hidden_def (ns_format_ttl)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
// Seems not to be needed. It's not exported from the DSO. Some libresolv.a
|
|
Packit Service |
82fcde |
// might depend on it so we let it in.
|
|
Packit Service |
82fcde |
int
|
|
Packit Service |
82fcde |
ns_parse_ttl(const char *src, u_long *dst) {
|
|
Packit Service |
82fcde |
u_long ttl, tmp;
|
|
Packit Service |
82fcde |
int ch, digits, dirty;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
ttl = 0;
|
|
Packit Service |
82fcde |
tmp = 0;
|
|
Packit Service |
82fcde |
digits = 0;
|
|
Packit Service |
82fcde |
dirty = 0;
|
|
Packit Service |
82fcde |
while ((ch = *src++) != '\0') {
|
|
Packit Service |
82fcde |
if (!isascii(ch) || !isprint(ch))
|
|
Packit Service |
82fcde |
goto einval;
|
|
Packit Service |
82fcde |
if (isdigit(ch)) {
|
|
Packit Service |
82fcde |
tmp *= 10;
|
|
Packit Service |
82fcde |
tmp += (ch - '0');
|
|
Packit Service |
82fcde |
digits++;
|
|
Packit Service |
82fcde |
continue;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (digits == 0)
|
|
Packit Service |
82fcde |
goto einval;
|
|
Packit Service |
82fcde |
if (islower(ch))
|
|
Packit Service |
82fcde |
ch = toupper(ch);
|
|
Packit Service |
82fcde |
switch (ch) {
|
|
Packit Service |
82fcde |
case 'W': tmp *= 7;
|
|
Packit Service |
82fcde |
case 'D': tmp *= 24;
|
|
Packit Service |
82fcde |
case 'H': tmp *= 60;
|
|
Packit Service |
82fcde |
case 'M': tmp *= 60;
|
|
Packit Service |
82fcde |
case 'S': break;
|
|
Packit Service |
82fcde |
default: goto einval;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
ttl += tmp;
|
|
Packit Service |
82fcde |
tmp = 0;
|
|
Packit Service |
82fcde |
digits = 0;
|
|
Packit Service |
82fcde |
dirty = 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (digits > 0) {
|
|
Packit Service |
82fcde |
if (dirty)
|
|
Packit Service |
82fcde |
goto einval;
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
ttl += tmp;
|
|
Packit Service |
82fcde |
} else if (!dirty)
|
|
Packit Service |
82fcde |
goto einval;
|
|
Packit Service |
82fcde |
*dst = ttl;
|
|
Packit Service |
82fcde |
return (0);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
einval:
|
|
Packit Service |
82fcde |
__set_errno (EINVAL);
|
|
Packit Service |
82fcde |
return (-1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Private. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
fmt1(int t, char s, char **buf, size_t *buflen) {
|
|
Packit Service |
82fcde |
char tmp[50];
|
|
Packit Service |
82fcde |
size_t len;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
len = SPRINTF((tmp, "%d%c", t, s));
|
|
Packit Service |
82fcde |
if (len + 1 > *buflen)
|
|
Packit Service |
82fcde |
return (-1);
|
|
Packit Service |
82fcde |
strcpy(*buf, tmp);
|
|
Packit Service |
82fcde |
*buf += len;
|
|
Packit Service |
82fcde |
*buflen -= len;
|
|
Packit Service |
82fcde |
return (0);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/*! \file */
|