]> granicus.if.org Git - mutt/commitdiff
Add mutt_gecos_name function which centrally handles the GECOS
authorThomas Roessler <roessler@does-not-exist.org>
Sat, 20 May 2000 07:48:26 +0000 (07:48 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Sat, 20 May 2000 07:48:26 +0000 (07:48 +0000)
processing.

alias.c
browser.c
init.c
lib.c
mutt.h
muttlib.c
protos.h

diff --git a/alias.c b/alias.c
index a5199d9ef58746309e3ec663b23ce9d243c7bfe1..d2d6033bc4ac0ac319599fa8cb39135f2d8599dc 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -20,7 +20,6 @@
 #include "mutt_regex.h"
 #include "mutt_curses.h"
 
-#include <pwd.h>
 #include <string.h>
 
 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
index ebb4f5e40c31deafc12c7616de6f9e2e5cefe9b5..9adee806afcc39705157093c64ee1663c6059a33 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -34,8 +34,6 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <sys/stat.h>
-#include <pwd.h>
-#include <grp.h>
 
 static struct mapping_t FolderHelp[] = {
   { N_("Exit"),  OP_EXIT },
diff --git a/init.c b/init.c
index 7e155e6da25820da4895d0c1951209dfaa0fa76c..7f0bef29d871fa428e08543d694f29adc7e2e2c5 100644 (file)
--- a/init.c
+++ b/init.c
@@ -42,7 +42,6 @@
 #include "init.h"
 #include "mailbox.h"
 
-#include <pwd.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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 dec3e66c2957438b44d2bfbddb3105987f29b6b5..7e2466993989a34bf7e856d165c4fac5feae64fb 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -32,9 +32,9 @@
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <errno.h>
-#include <pwd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <pwd.h>
 
 #include "lib.h"
 
diff --git a/mutt.h b/mutt.h
index 37ed6a9af82697b2a4a222f5b31293035083fa16..75652cf5cd106a8d93f5d7ec44a7a865a7cb0029 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -36,6 +36,9 @@
 #include <posix1_lim.h>
 #endif
 
+#include <pwd.h>
+#include <grp.h>
+
 #include "rfc822.h"
 #include "hash.h"
 
index ad02ab53e00041bb7cdb3c936b097a0d7a7a4189..6a44e70a3dedcb06eff7283b1722057889212ef5 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <errno.h>
-#include <pwd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#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)
index 5ccdf4d5bfdc2552c8c10e7f548e0361a6b6eff4..63879250592250a91e2fbdd26e87212c55bfc075 100644 (file)
--- 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 *);