* @param[in] attname Attachment name
* @param[in] path Path to save the file
* @param[out] fname Buffer for filename
- * @param[out] flen Length of buffer
* @param[out] opt Save option, see #SaveAttach
* @param[out] directory Directory to save under (OPTIONAL)
* @retval 0 Success
* @retval -1 Abort
* @retval 1 Error
*/
-int mutt_check_overwrite(const char *attname, const char *path, char *fname,
- size_t flen, enum SaveAttach *opt, char **directory)
+int mutt_check_overwrite(const char *attname, const char *path, struct Buffer *fname,
+ enum SaveAttach *opt, char **directory)
{
struct stat st;
- mutt_str_strfcpy(fname, path, flen);
- if (access(fname, F_OK) != 0)
+ mutt_buffer_strcpy (fname, path);
+ if (access (mutt_b2s (fname), F_OK) != 0)
return 0;
- if (stat(fname, &st) != 0)
+ if (stat (mutt_b2s (fname), &st) != 0)
return -1;
if (S_ISDIR(st.st_mode))
{
(_("File is a directory, save under it: (y)es, (n)o, (a)ll?"), _("yna")))
{
case 3: /* all */
- mutt_str_replace(directory, fname);
+ mutt_str_replace (directory, mutt_b2s (fname));
break;
case 1: /* yes */
FREE(directory);
else if ((ans = mutt_yesorno(_("File is a directory, save under it?"), MUTT_YES)) != MUTT_YES)
return (ans == MUTT_NO) ? 1 : -1;
- char tmp[PATH_MAX];
- mutt_str_strfcpy(tmp, mutt_path_basename(NONULL(attname)), sizeof(tmp));
- if ((mutt_get_field(_("File under directory: "), tmp, sizeof(tmp),
- MUTT_FILE | MUTT_CLEAR) != 0) ||
- !tmp[0])
+ struct Buffer *tmp = mutt_buffer_pool_get ();
+ mutt_buffer_strcpy (tmp, mutt_path_basename (NONULL (attname)));
+ if (mutt_get_field (_("File under directory: "), tmp->data, tmp->dsize,
+ MUTT_FILE | MUTT_CLEAR) != 0)
{
+ mutt_buffer_pool_release (&tmp);
return -1;
}
- mutt_path_concat(fname, path, tmp, flen);
+ mutt_buffer_fix_dptr (tmp);
+ if (mutt_buffer_is_empty(tmp))
+ {
+ mutt_buffer_pool_release (&tmp);
+ return (-1);
+ }
+ mutt_buffer_concat_path (fname, path, mutt_b2s (tmp));
+ mutt_buffer_pool_release (&tmp);
}
- if ((*opt == MUTT_SAVE_NO_FLAGS) && (access(fname, F_OK) == 0))
+ if ((*opt == MUTT_SAVE_NO_FLAGS) && (access(mutt_b2s(fname), F_OK) == 0))
{
switch (
mutt_multi_choice(_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"),
/**
* prepend_savedir - Add #C_AttachSaveDir to the beginning of a path
- * @param buf Buffer for the result, must be valid
- * @param bufsize Size of the buffer
+ * @param buf Buffer for the result
*/
-static void prepend_savedir(char *buf, size_t bufsize)
+static void prepend_savedir(struct Buffer *buf)
{
- const char *savedir = C_AttachSaveDir;
-
- if (buf[0] == '/')
+ if (!buf || !buf->data || (buf->data[0] == '/'))
return;
- if (!savedir || !*savedir)
- savedir = "./";
-
- size_t savedirlen = strlen(savedir);
- size_t buflen = strlen(buf);
- bool addsep = (savedir[savedirlen - 1] != '/');
- size_t newbuflen = savedirlen + buflen + addsep;
-
- if (bufsize < newbuflen)
+ struct Buffer *tmp = mutt_buffer_pool_get();
+ if (C_AttachSaveDir)
{
- return;
+ mutt_buffer_addstr(tmp, C_AttachSaveDir);
+ if (tmp->dptr[-1] != '/')
+ mutt_buffer_addch(tmp, '/');
}
+ else
+ mutt_buffer_addstr(tmp, "./");
- memmove(buf + savedirlen + addsep, buf, buflen);
- memcpy(buf, savedir, savedirlen);
- if (addsep)
- {
- buf[savedirlen] = '/';
- }
- buf[newbuflen] = '\0';
+ mutt_buffer_addstr(tmp, mutt_b2s(buf));
+ mutt_buffer_strcpy(buf, mutt_b2s(tmp));
+ mutt_buffer_pool_release(&tmp);
}
/**
static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, char **directory)
{
char *prompt = NULL;
- char buf[PATH_MAX], tfile[PATH_MAX];
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
- int rc;
+ int rc = -1;
+
+ struct Buffer *buf = mutt_buffer_pool_get();
+ struct Buffer *tfile = mutt_buffer_pool_get();
if (body->filename)
{
if (directory && *directory)
{
- mutt_path_concat(buf, *directory, mutt_path_basename(body->filename), sizeof(buf));
+ mutt_buffer_concat_path(buf, *directory, mutt_path_basename(body->filename));
}
else
- mutt_str_strfcpy(buf, body->filename, sizeof(buf));
+ mutt_buffer_strcpy(buf, body->filename);
}
else if (has_a_message(body))
{
- mutt_default_save(buf, sizeof(buf), body->email);
+ mutt_default_save(buf->data, buf->dsize, body->email);
+ mutt_buffer_fix_dptr(buf);
}
- else
- buf[0] = '\0';
- prepend_savedir(buf, sizeof(buf));
+ prepend_savedir(buf);
prompt = _("Save to file: ");
while (prompt)
{
- if ((mutt_get_field(prompt, buf, sizeof(buf), MUTT_FILE) != 0) || (buf[0] == '\0'))
+ if (mutt_get_field(prompt, buf->data, buf->dsize, MUTT_FILE | MUTT_CLEAR) != 0)
{
mutt_clear_error();
- return -1;
+ goto cleanup;
}
+ mutt_buffer_fix_dptr(buf);
+ if (mutt_buffer_is_empty(buf))
+ goto cleanup;
prompt = NULL;
- mutt_expand_path(buf, sizeof(buf));
+ mutt_buffer_expand_path(buf);
bool is_message = (fp && has_a_message(body));
struct stat st;
/* check to make sure that this file is really the one the user wants */
- rc = mutt_save_confirm(buf, &st);
+ rc = mutt_save_confirm(mutt_b2s(buf), &st);
if (rc == 1)
{
prompt = _("Save to file: ");
continue;
}
else if (rc == -1)
- return -1;
- mutt_str_strfcpy(tfile, buf, sizeof(tfile));
+ goto cleanup;
+ mutt_buffer_strcpy(tfile, mutt_b2s(buf));
}
else
{
- rc = mutt_check_overwrite(body->filename, buf, tfile, sizeof(tfile), &opt, directory);
+ rc = mutt_check_overwrite(body->filename, mutt_b2s(buf), tfile, &opt, directory);
if (rc == -1)
- return -1;
+ goto cleanup;
else if (rc == 1)
{
prompt = _("Save to file: ");
}
mutt_message(_("Saving..."));
- if (mutt_save_attachment(fp, body, tfile, opt, (e || !is_message) ? e : body->email) == 0)
+ if (mutt_save_attachment(fp, body, mutt_b2s(tfile), opt,
+ (e || !is_message) ? e : body->email) == 0)
{
mutt_message(_("Attachment saved"));
- return 0;
+ rc = 0;
+ goto cleanup;
}
else
{
continue;
}
}
- return 0;
+
+cleanup:
+ mutt_buffer_pool_release(&buf);
+ mutt_buffer_pool_release(&tfile);
+ return rc;
}
/**
*/
static int save_without_prompting(FILE *fp, struct Body *body, struct Email *e)
{
- char buf[PATH_MAX], tfile[PATH_MAX];
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
- int rc;
+ int rc = -1;
+ struct Buffer *buf = mutt_buffer_pool_get();
+ struct Buffer *tfile = mutt_buffer_pool_get();
if (body->filename)
{
- mutt_str_strfcpy(buf, body->filename, sizeof(buf));
+ mutt_buffer_strcpy(buf, body->filename);
}
else if (has_a_message(body))
{
- mutt_default_save(buf, sizeof(buf), body->email);
- }
- else
- {
- buf[0] = '\0';
+ mutt_default_save(buf->data, buf->dsize, body->email);
}
- prepend_savedir(buf, sizeof(buf));
- mutt_expand_path(buf, sizeof(buf));
+ prepend_savedir(buf);
+ mutt_buffer_expand_path(buf);
bool is_message = (fp && has_a_message(body));
if (is_message)
{
- mutt_str_strfcpy(tfile, buf, sizeof(tfile));
+ mutt_buffer_strcpy(tfile, mutt_b2s(buf));
}
else
{
- rc = mutt_check_overwrite(body->filename, buf, tfile, sizeof(tfile), &opt, NULL);
+ rc = mutt_check_overwrite(body->filename, mutt_b2s(buf), tfile, &opt, NULL);
if (rc == -1) // abort or cancel
- {
- return -1;
- }
+ goto cleanup;
}
- return mutt_save_attachment(fp, body, tfile, opt, (e || !is_message) ? e : body->email);
+ rc = mutt_save_attachment(fp, body, mutt_b2s(tfile), opt,
+ (e || !is_message) ? e : body->email);
+
+cleanup:
+ mutt_buffer_pool_release(&buf);
+ mutt_buffer_pool_release(&tfile);
+ return rc;
}
+
/**
* mutt_save_attachment_list - Save a list of attachments
* @param actx Attachment context
void mutt_save_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag,
struct Body *top, struct Email *e, struct Menu *menu)
{
- char buf[PATH_MAX], tfile[PATH_MAX];
char *directory = NULL;
int rc = 1;
int last = menu ? menu->current : -1;
FILE *fp_out = NULL;
int saved_attachments = 0;
- buf[0] = '\0';
+ struct Buffer *buf = mutt_buffer_pool_get ();
+ struct Buffer *tfile = mutt_buffer_pool_get ();
for (int i = 0; !tag || (i < actx->idxlen); i++)
{
{
if (!C_AttachSplit)
{
- if (buf[0] == '\0')
+ if (mutt_buffer_is_empty (buf))
{
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
- mutt_str_strfcpy(buf, mutt_path_basename(NONULL(top->filename)), sizeof(buf));
- prepend_savedir(buf, sizeof(buf));
+ mutt_buffer_strcpy (buf, mutt_path_basename (NONULL (top->filename)));
+ prepend_savedir(buf);
- if ((mutt_get_field(_("Save to file: "), buf, sizeof(buf), MUTT_FILE) != 0) ||
- !buf[0])
+ if (mutt_get_field (_("Save to file: "), buf->data, buf->dsize, MUTT_FILE | MUTT_CLEAR) != 0)
{
- return;
+ goto cleanup;
}
- mutt_expand_path(buf, sizeof(buf));
- if (mutt_check_overwrite(top->filename, buf, tfile, sizeof(tfile), &opt, NULL))
- return;
- rc = mutt_save_attachment(fp, top, tfile, opt, e);
- if ((rc == 0) && C_AttachSep && (fp_out = fopen(tfile, "a")))
+ mutt_buffer_fix_dptr (buf);
+ if (mutt_buffer_is_empty (buf))
+ goto cleanup;
+ mutt_buffer_expand_path (buf);
+ if (mutt_check_overwrite (top->filename, mutt_b2s (buf), tfile, &opt, NULL))
+ goto cleanup;
+ rc = mutt_save_attachment (fp, top, mutt_b2s (tfile), opt, e);
+ if ((rc == 0) && C_AttachSep && (fp_out = fopen(mutt_b2s(tfile), "a")))
{
fprintf(fp_out, "%s", C_AttachSep);
mutt_file_fclose(&fp_out);
}
else
{
- rc = mutt_save_attachment(fp, top, tfile, MUTT_SAVE_APPEND, e);
- if ((rc == 0) && C_AttachSep && (fp_out = fopen(tfile, "a")))
+ rc = mutt_save_attachment (fp, top, mutt_b2s (tfile), MUTT_SAVE_APPEND, e);
+ if ((rc == 0) && C_AttachSep && (fp_out = fopen(mutt_b2s(tfile), "a")))
{
fprintf(fp_out, "%s", C_AttachSep);
mutt_file_fclose(&fp_out);
mutt_message(ngettext("Attachment saved", "%d attachments saved", saved_attachments),
saved_attachments);
}
+
+cleanup:
+ mutt_buffer_pool_release (&buf);
+ mutt_buffer_pool_release (&tfile);
}
/**