From: Thomas Roessler Date: Sat, 20 May 2000 07:48:26 +0000 (+0000) Subject: Add mutt_gecos_name function which centrally handles the GECOS X-Git-Tag: mutt-1-3-1-rel~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ac4ab5a0f4df5650685ef1459f4319a3d96c0082;p=mutt Add mutt_gecos_name function which centrally handles the GECOS processing. --- diff --git a/alias.c b/alias.c index a5199d9e..d2d6033b 100644 --- a/alias.c +++ b/alias.c @@ -20,7 +20,6 @@ #include "mutt_regex.h" #include "mutt_curses.h" -#include #include ADDRESS *mutt_lookup_alias (const char *s) @@ -86,22 +85,11 @@ static ADDRESS *mutt_expand_aliases_r (ADDRESS *a, LIST **expn) if (pw) { - regmatch_t pat_match[1]; - - /* Use regular expression to parse Gecos field. This result of the - * parsing will be used as the personal ID string when the alias is - * expanded. - */ - if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0) - { - /* Malloc enough for the matching pattern + terminating NULL */ - a->personal = safe_malloc ((pat_match[0].rm_eo - - pat_match[0].rm_so) + 1); - - strfcpy (a->personal, pw->pw_gecos + pat_match[0].rm_so, - pat_match[0].rm_eo - pat_match[0].rm_so + 1); - } - + char namebuf[STRING]; + + mutt_gecos_name (namebuf, sizeof (namebuf), pw); + mutt_str_replace (&a->personal, namebuf); + #ifdef EXACT_ADDRESS FREE (&a->val); #endif diff --git a/browser.c b/browser.c index ebb4f5e4..9adee806 100644 --- a/browser.c +++ b/browser.c @@ -34,8 +34,6 @@ #include #include #include -#include -#include static struct mapping_t FolderHelp[] = { { N_("Exit"), OP_EXIT }, diff --git a/init.c b/init.c index 7e155e6d..7f0bef29 100644 --- a/init.c +++ b/init.c @@ -42,7 +42,6 @@ #include "init.h" #include "mailbox.h" -#include #include #include #include @@ -1710,13 +1709,13 @@ void mutt_init (int skip_sys_rc, LIST *commands) /* Get some information about the user */ if ((pw = getpwuid (getuid ()))) { + char rnbuf[STRING]; + Username = safe_strdup (pw->pw_name); if (!Homedir) Homedir = safe_strdup (pw->pw_dir); - if ((p = strchr (pw->pw_gecos, ','))) - Realname = mutt_substrdup (pw->pw_gecos, p); - else - Realname = safe_strdup (pw->pw_gecos); + + Realname = safe_strdup (mutt_gecos_name (rnbuf, sizeof (rnbuf), pw)); Shell = safe_strdup (pw->pw_shell); } else diff --git a/lib.c b/lib.c index dec3e66c..7e246699 100644 --- a/lib.c +++ b/lib.c @@ -32,9 +32,9 @@ #include #include #include -#include #include #include +#include #include "lib.h" diff --git a/mutt.h b/mutt.h index 37ed6a9a..75652cf5 100644 --- a/mutt.h +++ b/mutt.h @@ -36,6 +36,9 @@ #include #endif +#include +#include + #include "rfc822.h" #include "hash.h" diff --git a/muttlib.c b/muttlib.c index ad02ab53..6a44e70a 100644 --- a/muttlib.c +++ b/muttlib.c @@ -33,10 +33,12 @@ #include #include #include -#include #include #include +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a,b) ((a) < (b) ? (b) : (a)) + BODY *mutt_new_body (void) { BODY *p = (BODY *) safe_calloc (1, sizeof (BODY)); @@ -453,6 +455,46 @@ char *_mutt_expand_path (char *s, size_t slen, int rx) } +char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw) +{ + regmatch_t pat_match[1]; + size_t pwnl; + int idx; + char *p; + + if (!pw || !pw->pw_gecos) + return NULL; + + memset (dest, 0, destlen); + + if (GecosMask.rx) + { + if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0) + strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so, + MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen)); + } + else if ((p = strchr (pw->pw_gecos, ','))) + strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1)); + else + strfcpy (dest, pw->pw_gecos, destlen); + + pwnl = strlen (pw->pw_name); + + for (idx = 0; dest[idx]; idx++) + { + if (dest[idx] == '&') + { + memmove (&dest[idx + pwnl], &dest[idx + 1], + MAX(destlen - idx - pwnl - 1, 0)); + memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl)); + dest[idx] = toupper (dest[idx]); + } + } + + return dest; +} + + char *mutt_get_parameter (const char *s, PARAMETER *p) { for (; p; p = p->next) diff --git a/protos.h b/protos.h index 5ccdf4d5..63879250 100644 --- a/protos.h +++ b/protos.h @@ -119,6 +119,7 @@ char *mutt_charset_hook (const char *); char *mutt_expand_path (char *, size_t); char *_mutt_expand_path (char *, size_t, int); char *mutt_find_hook (int, const char *); +char *mutt_gecos_name (char *, size_t, struct passwd *); char *mutt_gen_msgid (void); char *mutt_get_name (ADDRESS *); char *mutt_get_parameter (const char *, PARAMETER *);