From d04295aaf918032c1c2fafc94ddf637cf23341e9 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 12 Nov 2006 14:25:41 +0100 Subject: [PATCH] reimplement mutt_gecos_name. Signed-off-by: Pierre Habouzit --- alias.c | 6 +++-- init.c | 3 ++- lib-sys/Makefile.am | 7 ++--- lib-sys/mutt_tunnel.c | 17 ++++++------ lib-sys/unix.c | 63 +++++++++++++++++++++++++++++++++++++++++++ lib-sys/unix.h | 30 +++++++++++++++++++++ muttlib.c | 45 ------------------------------- protos.h | 1 - 8 files changed, 112 insertions(+), 60 deletions(-) create mode 100644 lib-sys/unix.c create mode 100644 lib-sys/unix.h diff --git a/alias.c b/alias.c index cefe0ec..fa83c6f 100644 --- a/alias.c +++ b/alias.c @@ -23,6 +23,8 @@ #include #include +#include + #include #include #include @@ -100,8 +102,8 @@ static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn) if (pw) { char namebuf[STRING]; - mutt_gecos_name (namebuf, sizeof (namebuf), pw); - m_strreplace (&a->personal, namebuf); + mutt_gecos_name(namebuf, sizeof (namebuf), pw, GecosMask.rx); + m_strreplace(&a->personal, namebuf); } } } diff --git a/init.c b/init.c index bb8bedb..5151f5a 100644 --- a/init.c +++ b/init.c @@ -2608,7 +2608,8 @@ void mutt_init (int skip_sys_rc, LIST * commands) if (!Homedir) Homedir = m_strdup(pw->pw_dir); - Realname = m_strdup(mutt_gecos_name (rnbuf, sizeof(rnbuf), pw)); + mutt_gecos_name(rnbuf, sizeof(rnbuf), pw, GecosMask.rx); + Realname = m_strdup(rnbuf); Shell = m_strdup(pw->pw_shell); endpwent (); } diff --git a/lib-sys/Makefile.am b/lib-sys/Makefile.am index 5e896e5..18d56c7 100644 --- a/lib-sys/Makefile.am +++ b/lib-sys/Makefile.am @@ -1,11 +1,12 @@ noinst_LIBRARIES = libsys.a -libsys_a_SOURCES = exit.h \ - exit.c \ +libsys_a_SOURCES = exit.h unix.h \ + exit.c unix.c \ $(___networking_part____) \ mutt_socket.h mutt_tunnel.c mutt_ssl.h \ mutt_socket.c mutt_tunnel.h mutt_ssl.c mutt_ssl_gnutls.c -noinst_HEADERS = mutt_socket.h mutt_tunnel.c mutt_ssl.h +noinst_HEADERS = exit.h unix.h \ + mutt_socket.h mutt_tunnel.c mutt_ssl.h -include ../cflags.mk diff --git a/lib-sys/mutt_tunnel.c b/lib-sys/mutt_tunnel.c index d82ec63..b74c5c5 100644 --- a/lib-sys/mutt_tunnel.c +++ b/lib-sys/mutt_tunnel.c @@ -12,22 +12,23 @@ # include "config.h" #endif +#include +#include +#include +#include +#include +#include + #include #include #include #include "mutt.h" + +#include "exit.h" #include "mutt_socket.h" #include "mutt_tunnel.h" - -#include -#include -#include -#include -#include -#include - /* -- data types -- */ typedef struct { pid_t pid; diff --git a/lib-sys/unix.c b/lib-sys/unix.c new file mode 100644 index 0000000..372ff06 --- /dev/null +++ b/lib-sys/unix.c @@ -0,0 +1,63 @@ +/* + * Copyright notice from original mutt: + * Copyright (C) 1996-2000 Michael R. Elkins + * Copyright (C) 1999-2000 Thomas Roessler + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ + +#include + +#include +#include +#include + +#include "unix.h" + +/* Extract the real name from /etc/passwd's GECOS field. + * When set, honor the regular expression in rx, + * otherwise assume that the GECOS field is a comma-separated list. + * Replace "&" by a capitalized version of the user's login name. + */ +ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx) +{ + const char *p, *end; + ssize_t len; + + *dst = '\0'; + len = 0; + + if (!pw->pw_gecos) + return 0; + + if (rx) { + regmatch_t pat_match[1]; + + if (regexec(rx->rx, pw->pw_gecos, 1, pat_match, 0)) { + return 0; + } + + p = pw->pw_gecos + pat_match[0].rm_so; + end = pw->pw_gecos + pat_match[0].rm_so; + } else { + p = pw->pw_gecos; + end = m_strchrnul(pw->pw_gecos, ','); + } + + for (;;) { + const char *q = MIN(end, m_strchrnul(p, '&')); + + len += m_strncpy(dst + len, n - len, p, q - p); + p = q + 1; + + if (!p[-1] || p >= end) + break; + + /* p[0] == '&' */ + len += m_strcpy(dst + len, n - len, pw->pw_name); + } + + return len; +} diff --git a/lib-sys/unix.h b/lib-sys/unix.h new file mode 100644 index 0000000..b43077a --- /dev/null +++ b/lib-sys/unix.h @@ -0,0 +1,30 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_SYS_UNIX_H +#define MUTT_LIB_SYS_UNIX_H + +#include +#include + +#include + +ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx); + +#endif /* MUTT_LIB_SYS_UNIX_H */ diff --git a/muttlib.c b/muttlib.c index 729c050..1c54b13 100644 --- a/muttlib.c +++ b/muttlib.c @@ -400,51 +400,6 @@ char *_mutt_expand_path (char *s, size_t slen, int rx) return (s); } -/* Extract the real name from /etc/passwd's GECOS field. - * When set, honor the regular expression in GecosMask, - * otherwise assume that the GECOS field is a - * comma-separated list. - * Replace "&" by a capitalized version of the user's login - * name. - */ - -char *mutt_gecos_name (char *dest, ssize_t destlen, struct passwd *pw) -{ - regmatch_t pat_match[1]; - ssize_t pwnl; - int idx; - char *p; - - if (!pw || !pw->pw_gecos) - return NULL; - - p_clear(dest, destlen); - - if (GecosMask.rx) { - if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0) - m_strcpy(dest, MIN(pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen), - pw->pw_gecos + pat_match[0].rm_so); - } - else if ((p = strchr (pw->pw_gecos, ','))) - m_strcpy(dest, MIN(destlen, p - pw->pw_gecos + 1), pw->pw_gecos); - else - m_strcpy(dest, destlen, pw->pw_gecos); - - pwnl = m_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 ((unsigned char) 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 8148476..d72450d 100644 --- a/protos.h +++ b/protos.h @@ -83,7 +83,6 @@ char *mutt_iconv_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 *, ssize_t, struct passwd *); char *mutt_gen_msgid (void); char *mutt_get_body_charset (char *, size_t, BODY *); const char *mutt_get_name (address_t *); -- 2.20.1