From: Thomas Roessler Date: Mon, 10 Jan 2000 10:35:48 +0000 (+0000) Subject: Add (and use) a function mutt_str_replace, which essentially X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e059042e2422bd48e51ee3f6fbe273ac42ae9918;p=neomutt Add (and use) a function mutt_str_replace, which essentially replaces the sequence: safe_free (&s); s = safe_strdup (t); --- diff --git a/attach.c b/attach.c index 93658e440..57c3bde90 100644 --- a/attach.c +++ b/attach.c @@ -65,8 +65,7 @@ int mutt_get_tmp_attachment (BODY *a) if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) { mutt_copy_stream (fpin, fpout); - FREE(&a->filename); - a->filename = safe_strdup(tempfile); + mutt_str_replace (&a->filename, tempfile); a->unlink = 1; if(a->stamp >= st.st_mtime) diff --git a/browser.c b/browser.c index bb538160c..d1ae8decb 100644 --- a/browser.c +++ b/browser.c @@ -932,10 +932,9 @@ void _mutt_select_file (char *f, size_t flen, int buffy, } else { - safe_free ((void **) &Mask.pattern); + mutt_str_replace (&Mask.pattern, buf); regfree (Mask.rx); safe_free ((void **) &Mask.rx); - Mask.pattern = safe_strdup (buf); Mask.rx = rx; Mask.not = not; diff --git a/compose.c b/compose.c index d49a22d7c..9b55143af 100644 --- a/compose.c +++ b/compose.c @@ -152,8 +152,8 @@ static int pgp_send_menu (int bits, int *redraw) if ((p = pgp_ask_for_key (_("Sign as: "), NULL, KEYFLAG_CANSIGN, PGP_PUBRING))) { snprintf (input_signas, sizeof (input_signas), "0x%s", pgp_keyid (p)); - safe_free((void **) &PgpSignAs); PgpSignAs = safe_strdup (input_signas); - safe_free((void **) &PgpSignMicalg); PgpSignMicalg = safe_strdup (pgp_pkalg_to_mic (p->algorithm)); + mutt_str_replace (&PgpSignAs, input_signas); + mutt_str_replace (&PgpSignMicalg, pgp_pkalg_to_mic (p->algorithm)); pgp_free_key (&p); bits |= PGPSIGN; @@ -188,10 +188,7 @@ static int pgp_send_menu (int bits, int *redraw) mutt_error _("Unknown MIC algorithm, valid ones are: pgp-md5, pgp-sha1, pgp-rmd160"); } else - { - safe_free ((void **) &PgpSignMicalg); - PgpSignMicalg = safe_strdup (input_micalg); - } + mutt_str_replace (&PgpSignMicalg, input_micalg); } } break; @@ -487,8 +484,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */ buf[0] = 0; if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0) { - safe_free ((void **) &msg->env->subject); - msg->env->subject = safe_strdup (buf); + mutt_str_replace (&msg->env->subject, buf); move (HDR_SUBJECT, HDR_XOFFSET); clrtoeol (); if (msg->env->subject) @@ -825,8 +821,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */ /* header names should not be translated */ if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0) { - safe_free ((void **) &idx[menu->current]->content->description); - idx[menu->current]->content->description = safe_strdup (buf); + mutt_str_replace (&idx[menu->current]->content->description, buf); menu->redraw = REDRAW_CURRENT; } break; @@ -964,8 +959,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */ if(mutt_rename_file (idx[menu->current]->content->filename, fname)) break; - safe_free ((void **) &idx[menu->current]->content->filename); - idx[menu->current]->content->filename = safe_strdup (fname); + mutt_str_replace (&idx[menu->current]->content->filename, fname); menu->redraw = REDRAW_CURRENT; if(idx[menu->current]->content->stamp >= st.st_mtime) @@ -1036,8 +1030,7 @@ int mutt_compose_menu (HEADER *msg, /* structure for new message */ menu->max = idxlen; idx[menu->current]->content->type = itype; - safe_free ((void **) &idx[menu->current]->content->subtype); - idx[menu->current]->content->subtype = safe_strdup (p); + mutt_str_replace (&idx[menu->current]->content->subtype, p); idx[menu->current]->content->unlink = 1; menu->redraw |= REDRAW_INDEX | REDRAW_STATUS; diff --git a/curs_main.c b/curs_main.c index af3520274..f8e2b5371 100644 --- a/curs_main.c +++ b/curs_main.c @@ -918,12 +918,8 @@ int mutt_index_menu (void) if (Context) { - FREE (&LastFolder); - LastFolder = safe_strdup (Context->path); - } + mutt_str_replace (&LastFolder, Context->path); - if (Context) - { if (mx_close_mailbox (Context) != 0) { menu->redraw = REDRAW_INDEX | REDRAW_STATUS; diff --git a/edit.c b/edit.c index 92f21dfe9..73be6b85c 100644 --- a/edit.c +++ b/edit.c @@ -253,10 +253,7 @@ static void be_edit_header (ENVELOPE *e, int force) addstr ("Subject: "); strfcpy (tmp, e->subject ? e->subject: "", sizeof (tmp)); if (mutt_enter_string ((unsigned char *) tmp, sizeof (tmp), LINES-1, 9, 0) == 0) - { - safe_free ((void **) &e->subject); - e->subject = safe_strdup (tmp); - } + mutt_str_replace (&e->subject, tmp); addch ('\n'); } @@ -391,8 +388,7 @@ int mutt_builtin_editor (const char *path, HEADER *msg, HEADER *cur) addstr (_("missing filename.\n")); break; case 's': - safe_free ((void **) &msg->env->subject); - msg->env->subject = safe_strdup (p); + mutt_str_replace (&msg->env->subject, p); break; case 't': msg->env->to = rfc822_parse_adrlist (msg->env->to, p); diff --git a/history.c b/history.c index dfd2234e4..f38b49699 100644 --- a/history.c +++ b/history.c @@ -79,8 +79,7 @@ void mutt_history_add (history_class_t hclass, const char *s) if (prev < 0) prev = HistSize - 1; if (!h->hist[prev] || mutt_strcmp (h->hist[prev], s) != 0) { - safe_free ((void **) &h->hist[h->last]); - h->hist[h->last++] = safe_strdup (s); + mutt_str_replace (&h->hist[h->last++], s); if (h->last > HistSize - 1) h->last = 0; } diff --git a/init.c b/init.c index c824c047b..56e651059 100644 --- a/init.c +++ b/init.c @@ -653,20 +653,16 @@ static void mutt_restore_default (struct option_t *p) { case DT_STR: if (p->init) - { - FREE (p->data); - *((char **) p->data) = safe_strdup ((char *) p->init); - } + mutt_str_replace ((char **) p->data, (char *) p->init); break; case DT_PATH: if (p->init) { char path[_POSIX_PATH_MAX]; - FREE (p->data); strfcpy (path, (char *) p->init, sizeof (path)); mutt_expand_path (path, sizeof (path)); - *((char **) p->data) = safe_strdup (path); + mutt_str_replace ((char **) p->data, path); } break; case DT_ADDR: diff --git a/lib.c b/lib.c index dff66a2f4..d16a5e148 100644 --- a/lib.c +++ b/lib.c @@ -143,6 +143,13 @@ char *safe_strdup (const char *s) return (p); } +void mutt_str_replace (char **p, const char *s) +{ + if (*p) + free (*p); + *p = safe_strdup (s); +} + /* convert all characters in the string to lowercase */ char *mutt_strlower (char *s) { diff --git a/main.c b/main.c index 477190c10..46a70af45 100644 --- a/main.c +++ b/main.c @@ -396,8 +396,7 @@ int main (int argc, char **argv) break; case 'F': - FREE (&Muttrc); - Muttrc = safe_strdup (optarg); + mutt_str_replace (&Muttrc, optarg); break; case 'f': diff --git a/menu.c b/menu.c index 809a941e2..759bdcdd3 100644 --- a/menu.c +++ b/menu.c @@ -658,8 +658,7 @@ static int menu_search (MUTTMENU *menu, int op) _("Reverse search for: "), buf, sizeof (buf), M_CLEAR) != 0 || !buf[0]) return (-1); - safe_free ((void **) &menu->searchBuf); - menu->searchBuf = safe_strdup (buf); + mutt_str_replace (&menu->searchBuf, buf); menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP; } else diff --git a/mh.c b/mh.c index a4d667870..a4805978f 100644 --- a/mh.c +++ b/mh.c @@ -500,10 +500,7 @@ int maildir_commit_message (CONTEXT *ctx, MESSAGE *msg, HEADER *hdr) if (safe_rename (msg->path, full) == 0) { if (hdr) - { - FREE (&hdr->path); - hdr->path = safe_strdup (path); - } + mutt_str_replace (&hdr->path, path); FREE (&msg->path); return 0; } @@ -575,10 +572,7 @@ int mh_commit_message (CONTEXT *ctx, MESSAGE *msg, HEADER *hdr) if (safe_rename (msg->path, path) == 0) { if (hdr) - { - FREE (&hdr->path); - hdr->path = safe_strdup (tmp); - } + mutt_str_replace (&hdr->path, tmp); FREE (&msg->path); return 0; } @@ -648,10 +642,7 @@ static int mh_sync_message (CONTEXT *ctx, int msgno) { snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path); if ((rc = safe_rename (newpath, oldpath)) == 0) - { - FREE (&h->path); - h->path = safe_strdup (partpath); - } + mutt_str_replace (&h->path, partpath); } } else mx_close_message (&dest); @@ -719,8 +710,7 @@ static int maildir_sync_message (CONTEXT *ctx, int msgno) mutt_perror ("rename"); return (-1); } - safe_free ((void **) &h->path); - h->path = safe_strdup (partpath); + mutt_str_replace (&h->path, partpath); } return (0); } @@ -928,10 +918,7 @@ int mh_check_mailbox(CONTEXT *ctx, int *index_hint) ctx->hdrs[i]->read ? "R" : "")); if(mutt_strcmp(ctx->hdrs[i]->path, p->h->path)) - { - safe_free((void **) &ctx->hdrs[i]->path); - ctx->hdrs[i]->path = safe_strdup(p->h->path); - } + mutt_str_replace (&ctx->hdrs[i]->path, p->h->path); if(modified) { diff --git a/muttlib.c b/muttlib.c index ae7d7eb68..8d7ed9e90 100644 --- a/muttlib.c +++ b/muttlib.c @@ -403,8 +403,7 @@ void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p { if (mutt_strcasecmp (attribute, q->attribute) == 0) { - safe_free((void **) &q->value); - q->value = safe_strdup(value); + mutt_str_replace (&q->value, value); return; } } diff --git a/parse.c b/parse.c index 93b3854c2..0f6a82c82 100644 --- a/parse.c +++ b/parse.c @@ -348,8 +348,7 @@ static void parse_content_disposition (char *s, BODY *ct) * have allocated space if a "name=filename" parameter was * specified. */ - safe_free ((void **) &ct->filename); - ct->filename = safe_strdup (s); + mutt_str_replace (&ct->filename, s); } if ((s = mutt_get_parameter ("name", parms)) != 0) ct->form_name = safe_strdup (s); @@ -406,8 +405,7 @@ BODY *mutt_read_mime_header (FILE *fp, int digest) parse_content_disposition (c, p); else if (!mutt_strcasecmp ("description", line + 8)) { - safe_free ((void **) &p->description); - p->description = safe_strdup (c); + mutt_str_replace (&p->description, c); rfc2047_decode (p->description, p->description, mutt_strlen (p->description) + 1); } } @@ -455,8 +453,7 @@ void mutt_parse_part (FILE *fp, BODY *b) if (!b->parts) { b->type = TYPETEXT; - safe_free ((void **) &b->subtype); - b->subtype = safe_strdup ("plain"); + mutt_str_replace (&b->subtype, "plain"); } } @@ -988,8 +985,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs, { if (hdr) { - safe_free ((void **) &hdr->content->description); - hdr->content->description = safe_strdup (p); + mutt_str_replace (&hdr->content->description, p); rfc2047_decode (hdr->content->description, hdr->content->description, mutt_strlen (hdr->content->description) + 1); @@ -1008,8 +1004,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs, case 'd': if (!mutt_strcasecmp ("ate", line + 1)) { - safe_free((void **)&e->date); - e->date = safe_strdup(p); + mutt_str_replace (&e->date, p); if (hdr) hdr->date_sent = mutt_parse_date (p, hdr); matched = 1; diff --git a/pgpkey.c b/pgpkey.c index d2fd5503b..c9b28df5a 100644 --- a/pgpkey.c +++ b/pgpkey.c @@ -618,10 +618,7 @@ pgp_key_t *pgp_ask_for_key (char *tag, char *whatfor, if (whatfor) { if (l) - { - safe_free ((void **) &l->dflt); - l->dflt = safe_strdup (resp); - } + mutt_str_replace (&l->dflt, resp); else { l = safe_malloc (sizeof (struct pgp_cache)); diff --git a/postpone.c b/postpone.c index d386bcc63..743aa0ab0 100644 --- a/postpone.c +++ b/postpone.c @@ -482,17 +482,11 @@ int mutt_parse_pgp_hdr (char *p, int set_signas) } if (set_signas || *pgp_sign_as) - { - safe_free((void **) &PgpSignAs); - PgpSignAs = safe_strdup(pgp_sign_as); - } + mutt_str_replace (&PgpSignAs, pgp_sign_as); /* the micalg field must not be empty */ if (set_signas && *pgp_sign_micalg) - { - safe_free((void **) &PgpSignMicalg); - PgpSignMicalg = safe_strdup(pgp_sign_micalg); - } + mutt_str_replace (&PgpSignMicalg, pgp_sign_micalg); return pgp; } @@ -624,8 +618,7 @@ int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr, return -1; } - safe_free ((void *) &b->filename); - b->filename = safe_strdup (file); + mutt_str_replace (&b->filename, file); b->unlink = 1; if (mutt_is_text_type (b->type, b->subtype)) diff --git a/protos.h b/protos.h index 7efd31245..cc6727469 100644 --- a/protos.h +++ b/protos.h @@ -194,6 +194,7 @@ void mutt_shell_escape (void); void mutt_show_error (void); void mutt_signal_init (void); void mutt_stamp_attachment (BODY *a); +void mutt_str_replace (char **p, const char *s); void mutt_tabs_to_spaces (char *); void mutt_tag_set_flag (int, int); void mutt_unblock_signals (void); diff --git a/recvattach.c b/recvattach.c index 09b034c85..81acec92b 100644 --- a/recvattach.c +++ b/recvattach.c @@ -88,10 +88,7 @@ void mutt_update_tree (ATTACHPTR **idx, short idxlen) if (idx[x]->tree) { if (mutt_strcmp (idx[x]->tree, buf) != 0) - { - safe_free ((void **) &idx[x]->tree); - idx[x]->tree = safe_strdup (buf); - } + mutt_str_replace (&idx[x]->tree, buf); } else idx[x]->tree = safe_strdup (buf); diff --git a/rfc1524.c b/rfc1524.c index cc727aab5..4e20b4a80 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -150,8 +150,7 @@ static int get_field_text (char *field, char **entry, { field++; field = mutt_skip_whitespace (field); - safe_free ((void **) entry); - *entry = safe_strdup (field); + mutt_str_replace (entry, field); } return 1; } diff --git a/rfc2047.c b/rfc2047.c index e8a3e6ed0..89cd09ae0 100644 --- a/rfc2047.c +++ b/rfc2047.c @@ -259,15 +259,13 @@ void rfc2047_encode_adrlist (ADDRESS *addr) if (ptr->personal) { rfc2047_encode_string (buffer, sizeof (buffer), (const unsigned char *)ptr->personal); - safe_free ((void **) &ptr->personal); - ptr->personal = safe_strdup (buffer); + mutt_str_replace (&ptr->personal, buffer); } #ifdef EXACT_ADDRESS if (ptr->val) { rfc2047_encode_string (buffer, sizeof (buffer), (const unsigned char *)ptr->val); - safe_free ((void **) &ptr->val); - ptr->val = safe_strdup (buffer); + mutt_str_replace (&ptr->val, buffer); } #endif ptr = ptr->next; diff --git a/send.c b/send.c index 65a977d21..c631fc57e 100644 --- a/send.c +++ b/send.c @@ -234,8 +234,7 @@ static int edit_envelope (ENVELOPE *en) mutt_message _("No subject, aborting."); return (-1); } - safe_free ((void **) &en->subject); - en->subject = safe_strdup (buf); + mutt_str_replace (&en->subject, buf); return 0; } @@ -914,8 +913,7 @@ static void encode_descriptions (BODY *b) if (t->description) { rfc2047_encode_string (tmp, sizeof (tmp), (unsigned char *) t->description); - safe_free ((void **) &t->description); - t->description = safe_strdup (tmp); + mutt_str_replace (&t->description, tmp); } if (t->parts) encode_descriptions (t->parts); diff --git a/sendlib.c b/sendlib.c index 255f38cce..4ce2d873d 100644 --- a/sendlib.c +++ b/sendlib.c @@ -1381,8 +1381,7 @@ static void encode_headers (LIST *h) snprintf (tmp, sizeof (tmp), "%s: ", h->data); len = mutt_strlen (tmp); rfc2047_encode_string (tmp + len, sizeof (tmp) - len, (unsigned char *) p); - safe_free ((void **) &h->data); - h->data = safe_strdup (tmp); + mutt_str_replace (&h->data, tmp); } } } @@ -1811,8 +1810,7 @@ void mutt_prepare_envelope (ENVELOPE *env) { rfc2047_encode_string (buffer, sizeof (buffer) - 1, (unsigned char *) env->subject); - safe_free ((void **) &env->subject); - env->subject = safe_strdup (buffer); + mutt_str_replace (&env->subject, buffer); } encode_headers (env->userhdrs);