source: rtems-libbsd/freebsd-userspace/lib/libutil/trimdomain.c @ 4b8600a

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 4b8600a was 4b8600a, checked in by Joel Sherrill <joel.sherrill@…>, on 10/22/12 at 15:51:44

trimdomain.c: New file in libutil

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#ifdef __rtems__
2#include "port_before.h"
3#endif
4/*-
5 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
6 *   Based on original work by Atsushi Murai <amurai@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36
37#include <libutil.h>
38#include <string.h>
39#include <unistd.h>
40
41static int      isDISP(const char *);
42
43/*-
44 * Trim the current domain name from fullhost, but only if the result
45 * is less than or equal to hostsize in length.
46 *
47 * This function understands $DISPLAY type fullhosts.
48 *
49 * For example:
50 *
51 *     trimdomain("abcde.my.domain", 5)       ->   "abcde"
52 *     trimdomain("abcde.my.domain", 4)       ->   "abcde.my.domain"
53 *     trimdomain("abcde.my.domain:0.0", 9)   ->   "abcde:0.0"
54 *     trimdomain("abcde.my.domain:0.0", 8)   ->   "abcde.my.domain:0.0"
55 */
56void
57trimdomain(char *fullhost, int hostsize)
58{
59        static size_t dlen;
60        static int first = 1;
61        static char domain[MAXHOSTNAMELEN];
62        char *end, *s;
63        size_t len;
64
65        if (first) {
66                /* XXX: Should we assume that our domain is this persistent ? */
67                first = 0;
68                if (gethostname(domain, sizeof(domain) - 1) == 0 &&
69                    (s = strchr(domain, '.')) != NULL)
70                        memmove(domain, s + 1, strlen(s + 1) + 1);
71                else
72                        domain[0] = '\0';
73                dlen = strlen(domain);
74        }
75
76        if (domain[0] == '\0')
77                return;
78
79        s = fullhost;
80        end = s + hostsize + 1;
81        if ((s = memchr(s, '.', (size_t)(end - s))) != NULL) {
82                if (strncasecmp(s + 1, domain, dlen) == 0) {
83                        if (s[dlen + 1] == '\0') {
84                                /* Found -- lose the domain. */
85                                *s = '\0';
86                        } else if (s[dlen + 1] == ':' &&
87                            isDISP(s + dlen + 2) &&
88                            (len = strlen(s + dlen + 1)) < (size_t)(end - s)) {
89                                /* Found -- shuffle the DISPLAY back. */
90                                memmove(s, s + dlen + 1, len + 1);
91                        }
92                }
93        }
94}
95
96/*
97 * Is the given string NN or NN.NN where ``NN'' is an all-numeric string ?
98 */
99static int
100isDISP(const char *disp)
101{
102        size_t w;
103        int res;
104
105        w = strspn(disp, "0123456789");
106        res = 0;
107        if (w > 0) {
108                if (disp[w] == '\0')
109                        res = 1;        /* NN */
110                else if (disp[w] == '.') {
111                        disp += w + 1;
112                        w = strspn(disp, "0123456789");
113                        if (w > 0 && disp[w] == '\0')
114                                res = 1;        /* NN.NN */
115                }
116        }
117        return (res);
118}
Note: See TracBrowser for help on using the repository browser.