From 08aa8c17c3241bb5c6a997ed2e01e25a4d0089ce Mon Sep 17 00:00:00 2001 From: Josh Bressers Date: Mon, 20 Feb 2006 03:09:07 +0000 Subject: [PATCH] * sbr/m_draft.c, sbr/utils.c, uip/folder.c, uip/inc.c, uip/mhstoresbr.c, uip/popi.c, uip/refile.c: Add create_folder() function, replacing duplicate code during folder creation. --- ChangeLog | 22 ++++++++++++++-------- sbr/m_draft.c | 12 ++---------- sbr/utils.c | 30 ++++++++++++++++++++++++++++++ uip/folder.c | 17 +---------------- uip/inc.c | 14 ++++---------- uip/mhstoresbr.c | 45 +-------------------------------------------- uip/popi.c | 11 +---------- uip/refile.c | 11 +---------- 8 files changed, 54 insertions(+), 108 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8245154..d4efdf4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,13 +1,19 @@ +2006-02-19 Josh Bressers + + * sbr/m_draft.c, sbr/utils.c, uip/folder.c, uip/inc.c, + uip/mhstoresbr.c, uip/popi.c, uip/refile.c: Add create_folder() + function, replacing duplicate code during folder creation. + 2006-02-18 David Levine - * h/mime.h, h/mhparse.h, uip/mhbuildsbr.c, uip/mhfree.c, - man/mhbuild.man, docs/TODO: added support for an optional - Content-Disposition header in mhbuild (only). Its contents - are supplied between {}, positioned after the optional [], in - a mhbuild directive. If the contents do not contain a - "filename=" parameter, and the directive has a filename, or - something else that ends with "name=", then that will be used - to add a "filename=" parameter to the header. + * h/mime.h, h/mhparse.h, uip/mhbuildsbr.c, uip/mhfree.c, + man/mhbuild.man, docs/TODO: added support for an optional + Content-Disposition header in mhbuild (only). Its contents + are supplied between {}, positioned after the optional [], in + a mhbuild directive. If the contents do not contain a + "filename=" parameter, and the directive has a filename, or + something else that ends with "name=", then that will be used + to add a "filename=" parameter to the header. 2006-02-12 David Levine diff --git a/sbr/m_draft.c b/sbr/m_draft.c index 7aea6f0..b0168e2 100644 --- a/sbr/m_draft.c +++ b/sbr/m_draft.c @@ -34,16 +34,8 @@ m_draft (char *folder, char *msg, int use, int *isdf) chdir (m_maildir ("")); strncpy (buffer, m_maildir (folder), sizeof(buffer)); - if (stat (buffer, &st) == -1) { - if (errno != ENOENT) - adios (buffer, "error on folder"); - cp = concat ("Create folder \"", buffer, "\"? ", NULL); - if (!getanswer (cp)) - done (0); - free (cp); - if (!makedir (buffer)) - adios (NULL, "unable to create folder %s", buffer); - } + + create_folder (buffer, 0, done); if (chdir (buffer) == -1) adios (buffer, "unable to change directory to"); diff --git a/sbr/utils.c b/sbr/utils.c index 8741b20..2a7a1cc 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -12,6 +12,7 @@ #include #include #include +#include /* * Safely call malloc @@ -111,3 +112,32 @@ add (char *s2, char *s1) return cp; } + +/* + * create_folder + * Check to see if a folder exists, if not, prompt the user to create + * it. + */ +void create_folder(char *folder, int autocreate, void (*done_callback)()) +{ + struct stat st; + extern int errno; + char *cp; + + if (stat (folder, &st) == -1) { + if (errno != ENOENT) + adios (folder, "error on folder"); + if (autocreate == 0) { + /* ask before creating folder */ + cp = concat ("Create folder \"", folder, "\"? ", NULL); + if (!getanswer (cp)) + done_callback (1); + free (cp); + } else if (autocreate == -1) { + /* do not create, so exit */ + done_callback (1); + } + if (!makedir (folder)) + adios (NULL, "unable to create folder %s", folder); + } +} diff --git a/uip/folder.c b/uip/folder.c index d6cb5b5..9d85e89 100644 --- a/uip/folder.c +++ b/uip/folder.c @@ -393,22 +393,7 @@ main (int argc, char **argv) * Check if folder exists. If not, then see if * we should create it, or just exit. */ - if (stat (strncpy (buf, m_maildir (folder), sizeof(buf)), &st) == -1) { - if (errno != ENOENT) - adios (buf, "error on folder"); - if (fcreat == 0) { - /* ask before creating folder */ - cp = concat ("Create folder \"", buf, "\"? ", NULL); - if (!getanswer (cp)) - done (1); - free (cp); - } else if (fcreat == -1) { - /* do not create, so exit */ - done (1); - } - if (!makedir (buf)) - adios (NULL, "unable to create folder %s", buf); - } + create_folder (m_maildir (folder), fcreat, done); if (get_folder_info (folder, msg) && argfolder) { /* update current folder */ diff --git a/uip/inc.c b/uip/inc.c index 4903e21..0731a15 100644 --- a/uip/inc.c +++ b/uip/inc.c @@ -562,16 +562,10 @@ main (int argc, char **argv) if ((maildir_copy = strdup(maildir)) == (char *)0) adios (maildir, "error allocating memory to copy maildir"); - if (stat (maildir, &st) == NOTOK) { - if (errno != ENOENT) - adios (maildir, "error on folder"); - cp = concat ("Create folder \"", maildir, "\"? ", NULL); - if (noisy && !getanswer (cp)) - done (1); - free (cp); - if (!makedir (maildir)) - adios (NULL, "unable to create folder %s", maildir); - } + if (noisy) + create_folder(maildir, 0, done); + else + done (1); if (chdir (maildir) == NOTOK) adios (maildir, "unable to change directory to"); diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index 519e481..5776ef3 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -75,7 +75,6 @@ static int store_external (CT); static int ct_compar (CT *, CT *); static int store_content (CT, CT); static int output_content_file (CT, int); -static int check_folder (char *); static int output_content_folder (char *, char *); static int parse_format_string (CT, char *, char *, int, char *); static void get_storeproc (CT); @@ -571,8 +570,7 @@ store_content (CT ct, CT p) folder = getfolder (1); /* Check if folder exists */ - if (check_folder (folder) == NOTOK) - return NOTOK; + create_folder(folder, 0, exit); /* Record the folder name */ ct->c_folder = add (folder, NULL); @@ -864,47 +862,6 @@ losing: /* - * Check if folder exists, and create - * if necessary. - */ - -static int -check_folder (char *folder) -{ - char *folderdir; - struct stat st; - - /* expand path to the folder */ - folderdir = m_mailpath (folder); - - /* Check if folder exists */ - if (stat (folderdir, &st) == NOTOK) { - int answer; - char *ep; - - if (errno != ENOENT) { - advise (folderdir, "error on folder"); - return NOTOK; - } - - ep = concat ("Create folder \"", folderdir, "\"? ", NULL); - answer = getanswer (ep); - free (ep); - - if (!answer) - return NOTOK; - - if (!makedir (folderdir)) { - advise (NULL, "unable to create folder %s", folderdir); - return NOTOK; - } - } - - return OK; -} - - -/* * Add a file to a folder. * * Return the new message number of the file diff --git a/uip/popi.c b/uip/popi.c index ef11a4a..3a03c0a 100644 --- a/uip/popi.c +++ b/uip/popi.c @@ -229,16 +229,7 @@ main (int argc, char **argv) folder = getfolder (0); maildir = m_maildir (folder); - if (stat (maildir, &st) == NOTOK) { - if (errno != ENOENT) - adios (maildir, "error on folder"); - cp = concat ("Create folder \"", maildir, "\"? ", NULL); - if (noisy && !getanswer (cp)) - done (1); - free (cp); - if (!makedir (maildir)) - adios (NULL, "unable to create folder %s", maildir); - } + create_folder(maildir, 0, done); if (chdir (maildir) == NOTOK) adios (maildir, "unable to change directory to"); diff --git a/uip/refile.c b/uip/refile.c index e50253a..bb7f00a 100644 --- a/uip/refile.c +++ b/uip/refile.c @@ -312,16 +312,7 @@ opnfolds (struct st_fold *folders, int nfolders) chdir (m_maildir ("")); strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir)); - if (stat (nmaildir, &st) == NOTOK) { - if (errno != ENOENT) - adios (nmaildir, "error on folder"); - cp = concat ("Create folder \"", nmaildir, "\"? ", NULL); - if (!getanswer (cp)) - done (1); - free (cp); - if (!makedir (nmaildir)) - adios (NULL, "unable to create folder %s", nmaildir); - } + create_folder (nmaildir, 0, done); if (chdir (nmaildir) == NOTOK) adios (nmaildir, "unable to change directory to"); -- 1.7.10.4