- * Trim the current domain name from fullhost, but only if the result * is less than or equal to hostsize in length. * * This function understands $DISPLAY type fullhosts. * * For example: * * trimdomain("abcde.my.domain", 5) -> "abcde" * trimdomain("abcde.my.domain", 4) -> "abcde.my.domain" * trimdomain("abcde.my.domain:0.0", 9) -> "abcde:0.0" * tri
| 53 | * trimdomain("abcde.my.domain:0.0", 8) -> "abcde.my.domain:0.0" |
| 54 | */ |
| 55 | void |
| 56 | trimdomain(char *fullhost, int hostsize) |
| 57 | { |
| 58 | static size_t dlen; |
| 59 | static int first = 1; |
| 60 | static char domain[MAXHOSTNAMELEN]; |
| 61 | char *end, *s; |
| 62 | size_t len; |
| 63 | |
| 64 | if (first) { |
| 65 | /* XXX: Should we assume that our domain is this persistent ? */ |
| 66 | first = 0; |
| 67 | if (gethostname(domain, sizeof(domain) - 1) == 0 && |
| 68 | (s = strchr(domain, '.')) != NULL) |
| 69 | memmove(domain, s + 1, strlen(s + 1) + 1); |
| 70 | else |
| 71 | domain[0] = '\0'; |
| 72 | dlen = strlen(domain); |
| 73 | } |
| 74 | |
| 75 | if (domain[0] == '\0') |
| 76 | return; |
| 77 | |
| 78 | s = fullhost; |
| 79 | end = s + hostsize + 1; |
| 80 | if ((s = memchr(s, '.', (size_t)(end - s))) != NULL) { |
| 81 | if (strncasecmp(s + 1, domain, dlen) == 0) { |
| 82 | if (s[dlen + 1] == '\0') { |
| 83 | /* Found -- lose the domain. */ |
| 84 | *s = '\0'; |
| 85 | } else if (s[dlen + 1] == ':' && |
| 86 | isDISP(s + dlen + 2) && |
| 87 | (len = strlen(s + dlen + 1)) < (size_t)(end - s)) { |
| 88 | /* Found -- shuffle the DISPLAY back. */ |
| 89 | memmove(s, s + dlen + 1, len + 1); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Is the given string NN or NN.NN where ``NN'' is an all-numeric string ? |