1 /* Add missing "." to domain names.
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1995,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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16  * SOFTWARE.
17  */
18 
19 #include <arpa/nameser.h>
20 #include <errno.h>
21 #include <string.h>
22 
23 /* Make a canonical copy of domain name SRC in DST.  Behavior:
24       foo -> foo.
25       foo. -> foo.
26       foo.. -> foo.
27       foo\. -> foo\..
28       foo\\. -> foo\\.  */
29 int
__libc_ns_makecanon(const char * src,char * dst,size_t dstsize)30 __libc_ns_makecanon (const char *src, char *dst, size_t dstsize)
31 {
32   size_t n = strlen (src);
33 
34   if (n + sizeof "." > dstsize) /* sizeof == 2.  */
35     {
36       __set_errno (EMSGSIZE);
37       return -1;
38     }
39   strcpy (dst, src);
40   while (n >= 1U && dst[n - 1] == '.')   /* Ends in ".".  */
41     if (n >= 2U && dst[n - 2] == '\\' && /* Ends in "\.".  */
42         (n < 3U || dst[n - 3] != '\\'))  /* But not "\\.".  */
43       break;
44     else
45       dst[--n] = '\0';
46   dst[n++] = '.';
47   dst[n] = '\0';
48   return 0;
49 }
50 libc_hidden_def (__libc_ns_makecanon)
51