diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2017-02-05 20:03:05 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2017-02-05 20:03:05 +0000 |
| commit | f9edb08480901b8c7d85837d72f8702008b0a773 (patch) | |
| tree | c04feb833accf3797a2818bf289559ef157195a4 /libexec/getty/subr.c | |
| parent | 899ca3d65f2b5e1cdf4d563783c61ebcff0862cf (diff) | |
| parent | 9ad221a558f813645e352036ee2445903d9a9b6f (diff) | |
Notes
Diffstat (limited to 'libexec/getty/subr.c')
| -rw-r--r-- | libexec/getty/subr.c | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/libexec/getty/subr.c b/libexec/getty/subr.c index 4716e97d7bff..2496b4c40b2f 100644 --- a/libexec/getty/subr.c +++ b/libexec/getty/subr.c @@ -43,6 +43,7 @@ static const char rcsid[] = #include <sys/time.h> #include <poll.h> +#include <regex.h> #include <stdlib.h> #include <string.h> #include <syslog.h> @@ -53,8 +54,6 @@ static const char rcsid[] = #include "pathnames.h" #include "extern.h" - - /* * Get a table entry. */ @@ -469,42 +468,48 @@ adelay(int ms, struct delayval *dp) char editedhost[MAXHOSTNAMELEN]; void -edithost(const char *pat) +edithost(const char *pattern) { - const char *host = HN; - char *res = editedhost; - - if (!pat) - pat = ""; - while (*pat) { - switch (*pat) { - - case '#': - if (*host) - host++; - break; - - case '@': - if (*host) - *res++ = *host++; - break; + regex_t regex; + regmatch_t *match; + int found; + + if (pattern == NULL || *pattern == '\0') + goto copyasis; + if (regcomp(®ex, pattern, REG_EXTENDED) != 0) + goto copyasis; + + match = calloc(regex.re_nsub + 1, sizeof(*match)); + if (match == NULL) { + regfree(®ex); + goto copyasis; + } - default: - *res++ = *pat; - break; + found = !regexec(®ex, HN, regex.re_nsub + 1, match, 0); + if (found) { + size_t subex, totalsize; - } - if (res == &editedhost[sizeof editedhost - 1]) { - *res = '\0'; - return; - } - pat++; + /* + * We found a match. If there were no parenthesized + * subexpressions in the pattern, use entire matched + * string as ``editedhost''; otherwise use the first + * matched subexpression. + */ + subex = !!regex.re_nsub; + totalsize = match[subex].rm_eo - match[subex].rm_so + 1; + strlcpy(editedhost, HN + match[subex].rm_so, totalsize > + sizeof(editedhost) ? sizeof(editedhost) : totalsize); } - if (*host) - strncpy(res, host, sizeof editedhost - (res - editedhost) - 1); - else - *res = '\0'; - editedhost[sizeof editedhost - 1] = '\0'; + free(match); + regfree(®ex); + if (found) + return; + /* + * In case of any errors, or if the pattern did not match, pass + * the original hostname as is. + */ + copyasis: + strlcpy(editedhost, HN, sizeof(editedhost)); } static struct speedtab { |
