stdlib: Remove use of mergesort on qsort (BZ 21719)
[glibc.git] / resolv / ns_makecanon.c
bloba6c3c2737df3fb7a4e84d729a2f5ef8269a70734
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.
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.
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.
19 #include <arpa/nameser.h>
20 #include <errno.h>
21 #include <string.h>
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
30 __libc_ns_makecanon (const char *src, char *dst, size_t dstsize)
32 size_t n = strlen (src);
34 if (n + sizeof "." > dstsize) /* sizeof == 2. */
36 __set_errno (EMSGSIZE);
37 return -1;
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;
50 libc_hidden_def (__libc_ns_makecanon)