-/* prepend the path part of *path to *link */
-void mutt_expand_link (char *newpath, const char *path, const char *link)
-{
- const char *lb = NULL;
- size_t len;
-
- /* link is full path */
- if (*link == '/')
- {
- strfcpy (newpath, link, _POSIX_PATH_MAX);
- return;
- }
-
- if ((lb = strrchr (path, '/')) == NULL)
- {
- /* no path in link */
- strfcpy (newpath, link, _POSIX_PATH_MAX);
- return;
- }
-
- len = lb - path + 1;
- memcpy (newpath, path, len);
- strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
-}
-
-char *mutt_expand_path (char *s, size_t slen)
-{
- return _mutt_expand_path (s, slen, 0);
-}
-
-char *_mutt_expand_path (char *s, size_t slen, int rx)
-{
- char p[_POSIX_PATH_MAX] = "";
- char q[_POSIX_PATH_MAX] = "";
- char tmp[_POSIX_PATH_MAX];
- char *t;
-
- char *tail = "";
-
- int recurse = 0;
-
- do
- {
- recurse = 0;
-
- switch (*s)
- {
- case '~':
- {
- if (*(s + 1) == '/' || *(s + 1) == 0)
- {
- strfcpy (p, NONULL(Homedir), sizeof (p));
- tail = s + 1;
- }
- else
- {
- struct passwd *pw;
- if ((t = strchr (s + 1, '/')))
- *t = 0;
-
- if ((pw = getpwnam (s + 1)))
- {
- strfcpy (p, pw->pw_dir, sizeof (p));
- if (t)
- {
- *t = '/';
- tail = t;
- }
- else
- tail = "";
- }
- else
- {
- /* user not found! */
- if (t)
- *t = '/';
- *p = '\0';
- tail = s;
- }
- }
- }
- break;
-
- case '=':
- case '+':
- {
-#ifdef USE_IMAP
- /* if folder = {host} or imap[s]://host/: don't append slash */
- if (mx_is_imap (NONULL (Maildir)) &&
- (Maildir[strlen (Maildir) - 1] == '}' ||
- Maildir[strlen (Maildir) - 1] == '/'))
- strfcpy (p, NONULL (Maildir), sizeof (p));
- else
-#endif
- snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
-
- tail = s + 1;
- }
- break;
-
- /* elm compatibility, @ expands alias to user name */
-
- case '@':
- {
- HEADER *h;
- ADDRESS *alias;
-
- if ((alias = mutt_lookup_alias (s + 1)))
- {
- h = mutt_new_header();
- h->env = mutt_new_envelope();
- h->env->from = h->env->to = alias;
- mutt_default_save (p, sizeof (p), h);
- h->env->from = h->env->to = NULL;
- mutt_free_header (&h);
- /* Avoid infinite recursion if the resulting folder starts with '@' */
- if (*p != '@')
- recurse = 1;
-
- tail = "";
- }
- }
- break;
-
- case '>':
- {
- strfcpy (p, NONULL(Inbox), sizeof (p));
- tail = s + 1;
- }
- break;
-
- case '<':
- {
- strfcpy (p, NONULL(Outbox), sizeof (p));
- tail = s + 1;
- }
- break;
-
- case '!':
- {
- if (*(s+1) == '!')
- {
- strfcpy (p, NONULL(LastFolder), sizeof (p));
- tail = s + 2;
- }
- else
- {
- strfcpy (p, NONULL(Spoolfile), sizeof (p));
- tail = s + 1;
- }
- }
- break;
-
- case '-':
- {
- strfcpy (p, NONULL(LastFolder), sizeof (p));
- tail = s + 1;
- }
- break;
-
- default:
- {
- *p = '\0';
- tail = s;
- }
- }
-
- if (rx && *p && !recurse)
- {
- mutt_rx_sanitize_string (q, sizeof (q), p);
- snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
- }
- else
- snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
-
- strfcpy (s, tmp, slen);
- }
- while (recurse);
-
-#ifdef USE_IMAP
- /* Rewrite IMAP path in canonical form - aids in string comparisons of
- * folders. May possibly fail, in which case s should be the same. */
- if (mx_is_imap (s))
- imap_expand_path (s, slen);
-#endif
-
- 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, 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 ((unsigned char) dest[idx]);
+ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
+{
+ char p[_POSIX_PATH_MAX] = "";
+ char tmp[_POSIX_PATH_MAX];
+ const char *tail = "";
+
+ do {
+ const address_t *alias;
+
+ switch (*s) {
+ case '~':
+ if (s[1] == '/' || s[1] == '\0') {
+ m_strcpy(p, sizeof(p), Homedir);
+ tail = s + 1;
+ } else {
+ struct passwd *pw;
+ tail = m_strchrnul(s + 1, '/');
+
+ m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
+
+ if ((pw = getpwnam(tmp))) {
+ m_strcpy(p, sizeof(p), pw->pw_dir);
+ } else {
+ /* user not found! */
+ tail = s;
+ }
+ }
+ break;
+
+ case '=':
+ case '+':
+ /* if folder = imap[s]://host/: don't append slash */
+ if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
+ && Maildir[m_strlen(Maildir) - 1] == '/') {
+ m_strcpy(p, sizeof(p), Maildir);
+ } else {
+ snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
+ }
+
+ tail = s + 1;
+ break;
+
+ /* elm compatibility, @ expands alias to user name */
+
+ case '@':
+ if ((alias = alias_lookup(s + 1))) {
+ HEADER h;
+ header_init(&h);
+ h.env = envelope_new();
+ h.env->from = h.env->to = (address_t *)alias;
+ mutt_default_save(p, sizeof (p), &h);
+ h.env->from = h.env->to = NULL;
+ header_wipe(&h);
+
+ if (*p != '@') {
+ /* recurse iff the result do not starts with '@' */
+ m_strcpy(s, slen, p);
+ continue;
+ }
+ }
+ break;
+
+ case '>':
+ m_strcpy(p, sizeof(p), Inbox);
+ tail = s + 1;
+ break;
+
+ case '<':
+ m_strcpy(p, sizeof(p), Outbox);
+ tail = s + 1;
+ break;
+
+ case '!':
+ if (s[1] == '!') {
+ m_strcpy(p, sizeof(p), LastFolder);
+ tail = s + 2;
+ } else {
+ m_strcpy(p, sizeof(p), Spoolfile);
+ tail = s + 1;
+ }
+ break;
+
+ case '-':
+ m_strcpy(p, sizeof(p), NONULL(LastFolder));
+ tail = s + 1;
+ break;
+
+ case '^':
+ m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
+ tail = s + 1;
+ break;
+
+ default:
+ *p = '\0';
+ tail = s;
+ }
+ } while (0);
+
+ if (rx) {
+ char q[_POSIX_PATH_MAX];
+ rx_sanitize_string(q, sizeof(q), p);
+ snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
+ } else {
+ snprintf(tmp, sizeof(tmp), "%s%s", p, tail);