- address_t *head = NULL, *last = NULL, *t, *w;
- LIST *u;
- char i;
- const char *fqdn;
-
- while (a) {
- if (!a->group && !a->personal && a->mailbox
- && strchr (a->mailbox, '@') == NULL) {
- t = alias_lookup(Aliases, a->mailbox);
-
- if (t) {
- i = 0;
- for (u = *expn; u; u = u->next) {
- if (m_strcmp(a->mailbox, u->data) == 0) { /* alias already found */
- debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
- i = 1;
- break;
- }
- }
-
- if (!i) {
- u = p_new(LIST, 1);
- u->data = m_strdup(a->mailbox);
- u->next = *expn;
- *expn = u;
- w = address_list_dup (t);
- w = mutt_expand_aliases_r (w, expn);
- if (head)
- last->next = w;
- else
- head = last = w;
- while (last && last->next)
- last = last->next;
+ address_t *pop, *head = NULL;
+ address_t **last = &head;
+
+ while ((pop = address_list_pop(&a))) {
+ if (!pop->group && !pop->personal
+ && pop->mailbox && !strchr(pop->mailbox, '@'))
+ {
+ const address_t *t = alias_lookup(Aliases, pop->mailbox);
+
+ if (t) {
+ LIST *u;
+
+ for (u = *expn; u; u = u->next) {
+ if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
+ address_delete(&pop);
+ continue;
+ }
+ }
+
+ /* save the fact we saw it */
+ u = mutt_new_list();
+ u->data = m_strdup(pop->mailbox);
+ u->next = *expn;
+ *expn = u;
+ address_delete(&pop);
+
+ /* recurse */
+ last = address_list_last(last);
+ *last = mutt_expand_aliases_r(address_list_dup(t), expn);
+ continue;
+ } else {
+ struct passwd *pw = getpwnam(pop->mailbox);
+
+ if (pw) {
+ char namebuf[STRING];
+ mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
+ m_strreplace(&pop->personal, namebuf);
+ }
+ }