Back to index

glibc  2.9
ns_ttl.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (c) 1996,1999 by Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00010  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00011  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00012  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00013  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00014  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00015  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00016  * SOFTWARE.
00017  */
00018 
00019 #if !defined(_LIBC) && !defined(lint)
00020 static const char rcsid[] = "$BINDId: ns_ttl.c,v 8.8 1999/10/13 16:39:36 vixie Exp $";
00021 #endif
00022 
00023 /* Import. */
00024 
00025 #include <arpa/nameser.h>
00026 
00027 #include <ctype.h>
00028 #include <errno.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 
00032 #ifdef SPRINTF_CHAR
00033 # define SPRINTF(x) strlen(sprintfx)
00034 #else
00035 # define SPRINTF(x) ((size_t)sprintf x)
00036 #endif
00037 
00038 /* Forward. */
00039 
00040 static int    fmt1(int t, char s, char **buf, size_t *buflen);
00041 
00042 /* Macros. */
00043 
00044 #define T(x) if ((x) < 0) return (-1); else (void)NULL
00045 
00046 /* Public. */
00047 
00048 int
00049 ns_format_ttl(u_long src, char *dst, size_t dstlen) {
00050        char *odst = dst;
00051        int secs, mins, hours, days, weeks, x;
00052        char *p;
00053 
00054        secs = src % 60;   src /= 60;
00055        mins = src % 60;   src /= 60;
00056        hours = src % 24;  src /= 24;
00057        days = src % 7;    src /= 7;
00058        weeks = src;       src = 0;
00059 
00060        x = 0;
00061        if (weeks) {
00062               T(fmt1(weeks, 'W', &dst, &dstlen));
00063               x++;
00064        }
00065        if (days) {
00066               T(fmt1(days, 'D', &dst, &dstlen));
00067               x++;
00068        }
00069        if (hours) {
00070               T(fmt1(hours, 'H', &dst, &dstlen));
00071               x++;
00072        }
00073        if (mins) {
00074               T(fmt1(mins, 'M', &dst, &dstlen));
00075               x++;
00076        }
00077        if (secs || !(weeks || days || hours || mins)) {
00078               T(fmt1(secs, 'S', &dst, &dstlen));
00079               x++;
00080        }
00081 
00082        if (x > 1) {
00083               int ch;
00084 
00085               for (p = odst; (ch = *p) != '\0'; p++)
00086                      if (isascii(ch) && isupper(ch))
00087                             *p = tolower(ch);
00088        }
00089 
00090        return (dst - odst);
00091 }
00092 libresolv_hidden_def (ns_format_ttl)
00093 
00094 // Seems not to be needed.  It's not exported from the DSO.  Some libresolv.a
00095 // might depend on it so we let it in.
00096 int
00097 ns_parse_ttl(const char *src, u_long *dst) {
00098        u_long ttl, tmp;
00099        int ch, digits, dirty;
00100 
00101        ttl = 0;
00102        tmp = 0;
00103        digits = 0;
00104        dirty = 0;
00105        while ((ch = *src++) != '\0') {
00106               if (!isascii(ch) || !isprint(ch))
00107                      goto einval;
00108               if (isdigit(ch)) {
00109                      tmp *= 10;
00110                      tmp += (ch - '0');
00111                      digits++;
00112                      continue;
00113               }
00114               if (digits == 0)
00115                      goto einval;
00116               if (islower(ch))
00117                      ch = toupper(ch);
00118               switch (ch) {
00119               case 'W':  tmp *= 7;
00120               case 'D':  tmp *= 24;
00121               case 'H':  tmp *= 60;
00122               case 'M':  tmp *= 60;
00123               case 'S':  break;
00124               default:   goto einval;
00125               }
00126               ttl += tmp;
00127               tmp = 0;
00128               digits = 0;
00129               dirty = 1;
00130        }
00131        if (digits > 0) {
00132               if (dirty)
00133                      goto einval;
00134               else
00135                      ttl += tmp;
00136        } else if (!dirty)
00137               goto einval;
00138        *dst = ttl;
00139        return (0);
00140 
00141  einval:
00142        __set_errno (EINVAL);
00143        return (-1);
00144 }
00145 
00146 /* Private. */
00147 
00148 static int
00149 fmt1(int t, char s, char **buf, size_t *buflen) {
00150        char tmp[50];
00151        size_t len;
00152 
00153        len = SPRINTF((tmp, "%d%c", t, s));
00154        if (len + 1 > *buflen)
00155               return (-1);
00156        strcpy(*buf, tmp);
00157        *buf += len;
00158        *buflen -= len;
00159        return (0);
00160 }
00161