From: David Levine Date: Sun, 26 Jan 2014 17:26:11 +0000 (-0600) Subject: Merge branch 'tmpfiles' X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/6bc64765f773ce75454ec1592a86779e3547fe46?ds=inline;hp=--cc Merge branch 'tmpfiles' --- 6bc64765f773ce75454ec1592a86779e3547fe46 diff --git a/docs/README.developers b/docs/README.developers index 76f650dc..1d3fd737 100644 --- a/docs/README.developers +++ b/docs/README.developers @@ -22,6 +22,15 @@ commit checklist 8. notify nmh-users? +--------------------------------- +C library/system call usage notes +--------------------------------- +* Use m_mktemp2() or m_mktemp() instead of mkstemp(3) (see section on + nmh temporary files below). +* Use m_unlink() instead of unlink(3). +* Use done() instead of _exit(3) except after a fork(3). + + ------------------------- autoconf & automake files ------------------------- @@ -162,6 +171,21 @@ OS function nmh-local version to use instead getpass() nmh_getpass() +------------------- +nmh temporary files +------------------- + +To create a temporary file, use m_mktemp2() or m_mktemp(). They use +mkstemp(3), but they also register the temporary file for removal on +program termination. So, do not use mkstemp() directly. + +To further support this, nmh_init() must be called at the beginning of +main(). And, if a child process is not going to immediately call one +of the exec(3) functions or _exit(3) after a fork(3), it should call +unregister_for_removal(0). Finally, nmh_init() sets up signal handlers +for several signals: these signal handlers should not be disabled. + + -------------- nmh test suite -------------- diff --git a/h/mh.h b/h/mh.h index 999d600a..1ecb4882 100644 --- a/h/mh.h +++ b/h/mh.h @@ -172,6 +172,7 @@ svector_t svector_create (size_t); void svector_free (svector_t); char *svector_push_back (svector_t, char *); char *svector_at (svector_t, size_t); +char **svector_find(svector_t, const char *); char **svector_strs (svector_t); size_t svector_size (svector_t); diff --git a/h/prototypes.h b/h/prototypes.h index a4184312..563a0d5d 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -191,6 +191,7 @@ int m_putenv (char *, char *); int m_rand (unsigned char *, size_t); char *m_mktemp(const char *, int *, FILE **); char *m_mktemp2(const char *, const char *, int *, FILE **); +char *get_temp_dir(); void m_unknown(m_getfld_state_t *, FILE *); int makedir (char *); char *message_id (time_t, int); @@ -370,3 +371,10 @@ int writeBase64raw (unsigned char *, size_t, unsigned char *); */ void init_credentials_file (); int nmh_get_credentials (char *, char *, int, nmh_creds_t); + +/* + * temporary file management + */ +int nmh_init(const char *argv0, int read_context); +int m_unlink(const char *); +void unregister_for_removal(int remove_files); diff --git a/sbr/context_save.c b/sbr/context_save.c index 5224af4c..d7d87e45 100644 --- a/sbr/context_save.c +++ b/sbr/context_save.c @@ -53,6 +53,8 @@ context_save (void) sigprocmask (SIG_SETMASK, &oset, &set); /* reset the signal mask */ if (action == 0) + /* This must be _exit(), not exit(), because the child didn't + call unregister_for_removal() in m_chkids(). */ _exit (0); /* we are child, time to die */ } @@ -83,6 +85,8 @@ m_chkids (void) break; case 0: + /* It's not necessary to call unregister_for_removal(0) + because the child calls _exit() in context_save(). */ setgid (getgid ()); setuid (getuid ()); break; diff --git a/sbr/folder_delmsgs.c b/sbr/folder_delmsgs.c index ee4bd9e9..db593f80 100644 --- a/sbr/folder_delmsgs.c +++ b/sbr/folder_delmsgs.c @@ -112,7 +112,7 @@ folder_delmsgs (struct msgs *mp, int unlink_msgs, int nohook) if (unlink_msgs) { /* just unlink the messages */ - if (unlink (dp) == -1) { + if (m_unlink (dp) == -1) { admonish (dp, "unable to unlink"); retval = -1; continue; diff --git a/sbr/lock_file.c b/sbr/lock_file.c index 01d7ae49..1a562f9b 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -335,7 +335,7 @@ lkclose_dot (int fd, const char *file) lockname (file, &lkinfo, 0); /* get name of lock file */ #if !defined(HAVE_LIBLOCKFILE) - unlink (lkinfo.curlock); /* remove lock file */ + (void) m_unlink (lkinfo.curlock); /* remove lock file */ #else lockfile_remove(lkinfo.curlock); #endif /* HAVE_LIBLOCKFILE */ @@ -513,7 +513,7 @@ lkopen_dot (const char *file, int access, mode_t mode) /* check for stale lockfile, else sleep */ if (curtime > st.st_ctime + RSECS) - unlink (lkinfo.curlock); + (void) m_unlink (lkinfo.curlock); else sleep (5); } @@ -543,17 +543,18 @@ static int lockit (struct lockinfo *li) { int fd; - char *curlock, *tmplock; + char *curlock, *tmpfile; #if 0 char buffer[128]; #endif curlock = li->curlock; - tmplock = li->tmplock; - if ((fd = mkstemp(tmplock)) == -1) + if ((tmpfile = m_mktemp(li->tmplock, &fd, NULL)) == NULL) { + advise(NULL, "unable to create temporary file in %s", get_temp_dir()); return -1; + } #if 0 /* write our process id into lock file */ @@ -567,8 +568,8 @@ lockit (struct lockinfo *li) * Now try to create the real lock file * by linking to the temporary file. */ - fd = link(tmplock, curlock); - unlink(tmplock); + fd = link(tmpfile, curlock); + (void) m_unlink(tmpfile); return (fd == -1 ? -1 : 0); } diff --git a/sbr/m_backup.c b/sbr/m_backup.c index c34c4097..5e24a033 100644 --- a/sbr/m_backup.c +++ b/sbr/m_backup.c @@ -23,6 +23,6 @@ m_backup (char *file) snprintf(buffer, sizeof(buffer), "%.*s%s%s", (int)(cp - file), file, BACKUP_PREFIX, cp); - unlink(buffer); + (void) m_unlink(buffer); return buffer; } diff --git a/sbr/m_mktemp.c b/sbr/m_mktemp.c index ca4ab29c..ac05306a 100644 --- a/sbr/m_mktemp.c +++ b/sbr/m_mktemp.c @@ -7,8 +7,11 @@ */ #include +#include +#include + +static void register_for_removal(const char *); -static char *get_temp_dir(); /* Create a temporary file. If pfx_in is null, the temporary file * will be created in the temporary directory (more on that later). @@ -26,7 +29,7 @@ static char *get_temp_dir(); * * When pfx_in is null, the temporary directory is determined as * follows, in order: - * + * * MHTMPDIR envvar * TMPDIR envvar * TMP envvar @@ -58,10 +61,14 @@ m_mktemp ( } fd = mkstemp(tmpfil); + if (fd < 0) { umask(oldmode); return NULL; } + + register_for_removal(tmpfil); + if (fd_ret != NULL) { *fd_ret = fd; keep_open = 1; @@ -70,7 +77,7 @@ m_mktemp ( FILE *fp = fdopen(fd, "w+"); if (fp == NULL) { int olderr = errno; - unlink(tmpfil); + (void) m_unlink(tmpfil); close(fd); errno = olderr; umask(oldmode); @@ -90,7 +97,7 @@ m_mktemp ( * by created based on a given pathname. Although m_mktemp() technically * supports this, this version is when the directory is defined by * a separate variable from the prefix, eliminating the caller from having - * to do string manipulation to generate the desired. pathname prefix. + * to do string manipulation to generate the desired pathname prefix. * * The pfx_in parameter specifies a basename prefix for the file. If dir_in * is NULL, then the defined temporary directory (see comments to m_mktemp() @@ -126,7 +133,7 @@ m_mktemp2 ( } -static char * +char * get_temp_dir() { /* Ignore envvars if we are setuid */ @@ -143,3 +150,125 @@ get_temp_dir() } return m_maildir(""); } + + +/* + * Array of files (full pathnames) to remove on process exit. + * Instead of removing slots from the array, just set to NULL + * to indicate that the file should no longer be removed. + */ +static svector_t exit_filelist = NULL; + +/* + * Register a file for removal at program termination. + */ +static void +register_for_removal(const char *pathname) { + if (exit_filelist == NULL) exit_filelist = svector_create(20); + (void) svector_push_back(exit_filelist, add(pathname, NULL)); +} + +/* + * Unregister all files that were registered to be removed at program + * termination. When called with remove_files of 0, this function is + * intended for use by one of the programs after a fork(3) without a + * subsequent call to one of the exec(3) functions or _exit(3). When + * called with non-0 remove_files argument, it is intended for use by + * an atexit() function. + * + * Right after a fork() and before calling exec() or _exit(), if the + * child catches one of the appropriate signals, it will remove + * all the registered temporary files. Some of those may be needed by + * the parent. Some nmh programs ignore or block some of the signals + * in the child right after fork(). For the other programs, rather + * than complicate things by preventing that, just take the chance + * that it might happen. It is harmless to attempt to unlink a + * temporary file twice, assuming another one wasn't created too + * quickly created with the same name. + */ +void +unregister_for_removal(int remove_files) { + if (exit_filelist) { + size_t i; + + for (i = 0; i < svector_size(exit_filelist); ++i) { + char *filename = svector_at(exit_filelist, i); + + if (filename) { + if (remove_files) (void) unlink(filename); + free(filename); + } + } + + svector_free(exit_filelist); + exit_filelist = NULL; + } +} + +/* + * If the file was registered for removal, deregister it. In + * any case, unlink it. + */ +int +m_unlink(const char *pathname) { + if (exit_filelist) { + char **slot = svector_find(exit_filelist, pathname); + + if (slot && *slot) { + free(*slot); + *slot = NULL; + } + } + + return unlink(pathname); + /* errno should be set to ENOENT if file was not found */ +} + +/* + * Remove all registered temporary files. + */ +void +remove_registered_files_atexit() { + unregister_for_removal(1); +} + +/* + * Remove all registered temporary files. Suitable for use as a + * signal handler. If the signal is one of the usual process + * termination signals, calls exit(). Otherwise, disables itself + * by restoring the default action and then re-raises the signal, + * in case the use was expecting a core dump. + */ +void +remove_registered_files(int sig) { + struct sigaction act; + + /* + * Ignore this signal for the duration. And we either exit() or + * disable this signal handler below, so don't restore this handler. + */ + act.sa_handler = SIG_IGN; + (void) sigemptyset(&act.sa_mask); + act.sa_flags = 0; + (void) sigaction(sig, &act, 0); + + if (sig == SIGHUP || sig == SIGINT || sig == SIGQUIT || sig == SIGTERM) { + /* + * We don't need to call remove_registered_files_atexit() if + * it was registered with atexit(), but let's not assume that. + * It's harmless to call it twice. + */ + remove_registered_files_atexit(); + + exit(1); + } else { + act.sa_handler = SIG_DFL; + (void) sigemptyset(&act.sa_mask); + act.sa_flags = 0; + (void) sigaction(sig, &act, 0); + + remove_registered_files_atexit(); + + (void) raise(sig); + } +} diff --git a/sbr/push.c b/sbr/push.c index b4e3f498..39c58acc 100644 --- a/sbr/push.c +++ b/sbr/push.c @@ -37,6 +37,9 @@ push(void) SIGNAL (SIGTTIN, SIG_IGN); SIGNAL (SIGTTOU, SIG_IGN); #endif + + unregister_for_removal(0); + freopen ("/dev/null", "r", stdin); freopen ("/dev/null", "w", stdout); break; diff --git a/sbr/seq_save.c b/sbr/seq_save.c index 83753afb..5b06dcdf 100644 --- a/sbr/seq_save.c +++ b/sbr/seq_save.c @@ -91,7 +91,7 @@ priv: rewind(fp); ftruncate(fileno(fp), 0); } else if ((fp = lkfopendata (seqfile, "w")) == NULL - && (unlink (seqfile) == -1 || + && (m_unlink (seqfile) == -1 || (fp = lkfopendata (seqfile, "w")) == NULL)) { admonish (attr, "unable to write"); goto priv; @@ -118,7 +118,7 @@ priv: * public sequences, then remove that file. */ if (!is_readonly(mp)) - unlink (seqfile); + (void) m_unlink (seqfile); } /* diff --git a/sbr/signals.c b/sbr/signals.c index eb26e632..7645c1cc 100644 --- a/sbr/signals.c +++ b/sbr/signals.c @@ -10,6 +10,9 @@ #include #include +/* sbr/m_mktemp.c */ +extern void remove_registered_files(int); + /* * A version of the function `signal' that uses reliable @@ -78,3 +81,30 @@ SIGNAL2 (int sig, SIGNAL_HANDLER func) return (oact.sa_handler); } + +/* + * For use by nmh_init(). + */ +int +setup_signal_handlers() { + /* + * Catch HUP, INT, QUIT, and TERM so that we can clean up tmp + * files when the user terminates the process early. And also a + * few other common signals that can be thrown due to bugs, stack + * overflow, etc. + */ + + if (SIGNAL(SIGHUP, remove_registered_files) == SIG_ERR || + SIGNAL(SIGINT, remove_registered_files) == SIG_ERR || + SIGNAL(SIGQUIT, remove_registered_files) == SIG_ERR || + SIGNAL(SIGTERM, remove_registered_files) == SIG_ERR || + SIGNAL(SIGILL, remove_registered_files) == SIG_ERR || +# ifdef SIGBUS + SIGNAL(SIGBUS, remove_registered_files) == SIG_ERR || +# endif + SIGNAL(SIGSEGV, remove_registered_files) == SIG_ERR) { + return NOTOK; + } + + return OK; +} diff --git a/sbr/utils.c b/sbr/utils.c index 7f0760a0..d8b060fb 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -11,6 +11,13 @@ #include #include +/* sbr/signals.c */ +extern int setup_signal_handlers(); + +/* sbr/m_mktemp.c */ +extern void remove_registered_files_atexit(); + + /* * We allocate space for messages (msgs array) * this number of elements at a time. @@ -317,3 +324,31 @@ nmh_strcasestr (const char *s1, const char *s2) { return NULL; } + + +int +nmh_init(const char *argv0, int read_context) { + setlocale(LC_ALL, ""); + + invo_name = r1bindex ((char *) argv0, '/'); + + if (setup_signal_handlers()) { + admonish("sigaction", "unable to set up signal handlers"); + } + + /* POSIX atexit() does not define any error conditions. */ + if (atexit(remove_registered_files_atexit)) { + admonish("atexit", "unable to register atexit function"); + } + + if (read_context) { + context_read(); + return OK; + } else { + int status = context_foil(NULL); + if (status != OK) { + advise("", "failed to create minimal profile/conext"); + } + return status; + } +} diff --git a/sbr/vector.c b/sbr/vector.c index ca8c894e..10058d4d 100644 --- a/sbr/vector.c +++ b/sbr/vector.c @@ -174,6 +174,24 @@ svector_at (svector_t vec, size_t i) { return vec->strs[i]; } +/* + * Return address of first element that stringwise matches s. + * The caller can replace the contents of the return address. + */ +char ** +svector_find (svector_t vec, const char *s) { + size_t i; + char **str = vec->strs; + + for (i = 0; i < vec->size; ++i, ++str) { + if (*str && ! strcmp(*str, s)) { + return str; + } + } + + return NULL; +} + char ** svector_strs (svector_t vec) { return vec->strs; diff --git a/uip/ali.c b/uip/ali.c index 534c25d8..e7c865be 100644 --- a/uip/ali.c +++ b/uip/ali.c @@ -53,11 +53,7 @@ main (int argc, char **argv) char **vec = mh_xmalloc (argc * sizeof (char *)), **arguments; struct aka *ak; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); diff --git a/uip/anno.c b/uip/anno.c index 75bcc682..26706eda 100644 --- a/uip/anno.c +++ b/uip/anno.c @@ -94,11 +94,7 @@ main (int argc, char **argv) int list = 0; /* list header elements if set */ int number = 0; /* delete specific number of like elements if set */ - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/annosbr.c b/uip/annosbr.c index ae9df280..88c90f44 100644 --- a/uip/annosbr.c +++ b/uip/annosbr.c @@ -184,7 +184,10 @@ annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, in mode = fstat (fd, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot (); - strncpy (tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil)); + if ((cp = m_mktemp2(file, "annotate", NULL, &tmp)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } + strncpy (tmpfil, cp, sizeof(tmpfil)); chmod (tmpfil, mode); /* @@ -412,13 +415,13 @@ annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, in cpydata (tmpfd, fd, tmpfil, file); close (tmpfd); - unlink (tmpfil); + (void) m_unlink (tmpfil); } else { strncpy (buffer, m_backup (file), sizeof(buffer)); if (rename (file, buffer) == NOTOK) { switch (errno) { case ENOENT: /* unlinked early - no annotations */ - unlink (tmpfil); + (void) m_unlink (tmpfil); break; default: diff --git a/uip/ap.c b/uip/ap.c index a8f9169f..c8cf4b25 100644 --- a/uip/ap.c +++ b/uip/ap.c @@ -53,11 +53,7 @@ main (int argc, char **argv) char buf[BUFSIZ], **argp; char **arguments, *addrs[NADDRS]; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); diff --git a/uip/burst.c b/uip/burst.c index e838422b..ff5af237 100644 --- a/uip/burst.c +++ b/uip/burst.c @@ -75,11 +75,7 @@ main (int argc, char **argv) struct smsg *smsgs; struct msgs *mp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -462,8 +458,14 @@ burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst, i = inplace ? msgnum + numburst : mp->hghmsg; for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) { + char *tempfile; + + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + strncpy (f2, tempfile, sizeof(f2)); strncpy (f1, m_name (i), sizeof(f1)); - strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2)); if (verbosw && i != msgnum) printf ("message %d of digest %d becomes message %d\n", j, msgnum, i); diff --git a/uip/comp.c b/uip/comp.c index a7300c34..02feefaa 100644 --- a/uip/comp.c +++ b/uip/comp.c @@ -83,11 +83,7 @@ main (int argc, char **argv) struct format *fmt; struct stat st; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/conflict.c b/uip/conflict.c index 9174a39b..4c140022 100644 --- a/uip/conflict.c +++ b/uip/conflict.c @@ -68,12 +68,7 @@ main (int argc, char **argv) char *cp, **argp, **arguments; char buf[BUFSIZ], *akv[50]; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* foil search of user profile/context */ - if (context_foil (NULL) == -1) - done (1); + if (nmh_init(argv[0], 0 /* use context_foil() */)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 0); diff --git a/uip/dist.c b/uip/dist.c index 00503be5..5a35e59a 100644 --- a/uip/dist.c +++ b/uip/dist.c @@ -83,11 +83,7 @@ main (int argc, char **argv) struct msgs *mp = NULL; struct stat st; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/distsbr.c b/uip/distsbr.c index d7a27120..2d9239e9 100644 --- a/uip/distsbr.c +++ b/uip/distsbr.c @@ -87,7 +87,7 @@ distout (char *drft, char *msgnam, char *backup) leave_bad: ; fclose (ifp); fclose (ofp); - unlink (drft); + (void) m_unlink (drft); if (rename (backup, drft) == NOTOK) adios (drft, "unable to rename %s to", backup); return NOTOK; @@ -104,7 +104,7 @@ process: ; if (!resent) { advise (NULL, BADMSG, "draft"); fclose (ofp); - unlink (drft); + (void) m_unlink (drft); if (rename (backup, drft) == NOTOK) adios (drft, "unable to rename %s to", backup); return NOTOK; @@ -141,14 +141,13 @@ ready_msg (char *msgnam) cp = m_mktemp2(NULL, "dist", &hdrfd, NULL); if (cp == NULL) { - adios("distsbr", "unable to create temporary file"); + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); } - fchmod(hdrfd, 0600); strncpy(tmpfil, cp, sizeof(tmpfil)); if ((out = dup (hdrfd)) == NOTOK || (ofp = fdopen (out, "w")) == NULL) adios (NULL, "no file descriptors -- you lose big"); - unlink (tmpfil); + (void) m_unlink (tmpfil); for (;;) { int buffersz = sizeof buffer; @@ -170,14 +169,15 @@ ready_msg (char *msgnam) cp = m_mktemp2(NULL, "dist", &txtfd, NULL); if (cp == NULL) { - adios("distsbr", "unable to create temporary file"); + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); } fchmod(txtfd, 0600); strncpy (tmpfil, cp, sizeof(tmpfil)); if ((out = dup (txtfd)) == NOTOK || (ofp = fdopen (out, "w")) == NULL) adios (NULL, "no file descriptors -- you lose big"); - unlink (tmpfil); + (void) m_unlink (tmpfil); fprintf (ofp, "\n%s", buffer); while (state == BODY) { buffersz = sizeof buffer; diff --git a/uip/dp.c b/uip/dp.c index 8245f60a..dabdd74c 100644 --- a/uip/dp.c +++ b/uip/dp.c @@ -51,11 +51,7 @@ main (int argc, char **argv) char buf[BUFSIZ], **argp, **arguments; char *dates[NDATES]; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/dropsbr.c b/uip/dropsbr.c index 2cf5b598..da2752d1 100644 --- a/uip/dropsbr.c +++ b/uip/dropsbr.c @@ -567,7 +567,7 @@ map_write (char *mailbox, int md, int id, long last, off_t start, clear = 1; if (!clear && map_chk (file, fd, &d1, pos, noisy)) { - unlink (file); + (void) m_unlink (file); mbx_close (file, fd); if ((fd = map_open (file, md)) == NOTOK) return NOTOK; diff --git a/uip/flist.c b/uip/flist.c index cea1ca7c..829500d6 100644 --- a/uip/flist.c +++ b/uip/flist.c @@ -115,11 +115,7 @@ main(int argc, char **argv) char **arguments; char buf[BUFSIZ]; - setlocale(LC_ALL, ""); - invo_name = r1bindex(argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } /* * If program was invoked with name ending diff --git a/uip/fmtdump.c b/uip/fmtdump.c index 289f1e66..f1df6f35 100644 --- a/uip/fmtdump.c +++ b/uip/fmtdump.c @@ -51,11 +51,7 @@ main (int argc, char **argv) char buf[BUFSIZ], *nfs, **argp, **arguments; struct format *fmt; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/fmttest.c b/uip/fmttest.c index a3a436f4..2f45e7b5 100644 --- a/uip/fmttest.c +++ b/uip/fmttest.c @@ -121,11 +121,7 @@ main (int argc, char **argv) int dat[5]; struct fmt_callbacks cb, *cbp = NULL; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/folder.c b/uip/folder.c index a2e6a4a8..98ecde68 100644 --- a/uip/folder.c +++ b/uip/folder.c @@ -101,11 +101,7 @@ main (int argc, char **argv) char *cp, *dp, *msg = NULL, *argfolder = NULL; char **ap, **argp, buf[BUFSIZ], **arguments; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } /* * If program was invoked with name ending diff --git a/uip/forw.c b/uip/forw.c index 9eeb6123..2139d35c 100644 --- a/uip/forw.c +++ b/uip/forw.c @@ -116,14 +116,9 @@ main (int argc, char **argv) char **argp, **arguments; struct stat st; struct msgs_array msgs = { 0, 0, NULL }; - int buildsw = 0; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/forwsbr.c b/uip/forwsbr.c index 48fecb17..295507d6 100644 --- a/uip/forwsbr.c +++ b/uip/forwsbr.c @@ -174,9 +174,11 @@ finished: } cp = m_mktemp2(NULL, invo_name, NULL, &tmp); - if (cp == NULL) adios("forw", "unable to create temporary file"); + if (cp == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (tmpfil, cp, sizeof(tmpfil)); - unlink (tmpfil); + (void) m_unlink (tmpfil); if ((in = dup (fileno (tmp))) == NOTOK) adios ("dup", "unable to"); diff --git a/uip/inc.c b/uip/inc.c index 3aa5e50f..b76f1cd9 100644 --- a/uip/inc.c +++ b/uip/inc.c @@ -197,11 +197,7 @@ main (int argc, char **argv) SAVEGROUPPRIVS(); TRYDROPGROUPPRIVS(); - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); @@ -660,7 +656,7 @@ go_to_it: } else { if (ferror(pf) || fclose (pf)) { int e = errno; - unlink (cp); + (void) m_unlink (cp); pop_quit (); errno = e; adios (cp, "write error on"); @@ -755,7 +751,7 @@ go_to_it: break; if (ferror(sf) || fflush(pf) || ferror(pf)) { int e = errno; - fclose(pf); fclose(sf); unlink(cp); + fclose(pf); fclose(sf); (void) m_unlink(cp); errno = e; adios(cp, "copy error %s -> %s", sp, cp); } @@ -802,14 +798,14 @@ go_to_it: } if (ferror(pf) || fclose (pf)) { int e = errno; - unlink (cp); + (void) m_unlink (cp); errno = e; adios (cp, "write error on"); } pf = NULL; free (cp); - if (trnflag && unlink (sp) == NOTOK) + if (trnflag && m_unlink (sp) == NOTOK) adios (sp, "couldn't unlink"); free (sp); /* Free Maildir[i]->filename */ } @@ -851,7 +847,7 @@ go_to_it: close (newfd); else admonish (newmail, "error zero'ing"); - unlink(map_name(newmail)); + (void) m_unlink(map_name(newmail)); } } else { if (noisy) diff --git a/uip/install-mh.c b/uip/install-mh.c index 0fc97c45..12377352 100644 --- a/uip/install-mh.c +++ b/uip/install-mh.c @@ -42,8 +42,8 @@ main (int argc, char **argv) FILE *in, *out; int check; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 0 /* use context_foil() */ )) { return 1; } + arguments = getarguments (invo_name, argc, argv, 0); argp = arguments; diff --git a/uip/mark.c b/uip/mark.c index 439ea407..da25f0b3 100644 --- a/uip/mark.c +++ b/uip/mark.c @@ -52,11 +52,7 @@ main (int argc, char **argv) struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/mhbuild.c b/uip/mhbuild.c index 85b5a21b..de849a39 100644 --- a/uip/mhbuild.c +++ b/uip/mhbuild.c @@ -66,9 +66,6 @@ DEFINE_SWITCH_ENUM(MIMEENCODING); DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches); #undef X -/* mhbuildsbr.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -114,13 +111,9 @@ main (int argc, char **argv) FILE *fp_out = NULL; int header_encoding = CE_UNKNOWN; - done=unlink_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=unlink_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -309,16 +302,6 @@ main (int argc, char **argv) cache_private = ".cache"; cache_private = getcpy (m_maildir (cache_private)); - /* - * Check for storage directory. If defined, we - * will store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); @@ -330,8 +313,13 @@ main (int argc, char **argv) * Process the composition file from standard input. */ if (compfile[0] == '-' && compfile[1] == '\0') { + if ((cp = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + /* copy standard input to temporary file */ - strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile)); + strncpy (infile, cp, sizeof(infile)); while (fgets (buffer, BUFSIZ, stdin)) fputs (buffer, fp); fclose (fp); @@ -357,7 +345,7 @@ main (int argc, char **argv) free_content (ct); } - unlink (infile); + (void) m_unlink (infile); unlink_infile = 0; done (0); @@ -382,8 +370,10 @@ main (int argc, char **argv) cts[1] = NULL; /* output MIME message to this temporary file */ - strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out), - sizeof(outfile)); + if ((cp = m_mktemp2(compfile, invo_name, NULL, &fp_out)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } + strncpy(outfile, cp, sizeof(outfile)); unlink_outfile = 1; /* output the message */ @@ -424,9 +414,9 @@ unlink_done (int status) * temporary files. */ if (unlink_infile) - unlink (infile); + (void) m_unlink (infile); if (unlink_outfile) - unlink (outfile); + (void) m_unlink (outfile); exit (status); } diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index e5909229..70747c85 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -42,12 +42,6 @@ extern int contentidsw; extern int rcachesw; /* mhcachesbr.c */ extern int wcachesw; /* mhcachesbr.c */ -/* - * Directory to place tmp files. This must - * be set before these routines are called. - */ -char *tmp; - pid_t xpid = 0; static char prefix[] = "----- =_aaaaaaaaaa"; @@ -580,8 +574,10 @@ user_content (FILE *in, char *buf, CT *ctp) FILE *out; char *cp; - cp = m_mktemp2(NULL, invo_name, NULL, &out); - if (cp == NULL) adios("mhbuildsbr", "unable to create temporary file"); + if ((cp = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) { + adios("mhbuildsbr", "unable to create temporary file in %s", + get_temp_dir()); + } /* use a temp file to collect the plain text lines */ ce->ce_file = add (cp, NULL); @@ -1140,10 +1136,10 @@ compose_content (CT ct) if (!(cp = ci->ci_magic)) adios (NULL, "internal error(5)"); - tfile = m_mktemp2(NULL, invo_name, NULL, NULL); - if (tfile == NULL) { - adios("mhbuildsbr", "unable to create temporary file"); - } + if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios("mhbuildsbr", "unable to create temporary file in %s", + get_temp_dir()); + } ce->ce_file = add (tfile, NULL); ce->ce_unlink = 1; diff --git a/uip/mhcachesbr.c b/uip/mhcachesbr.c index 0bf725c6..136b5678 100644 --- a/uip/mhcachesbr.c +++ b/uip/mhcachesbr.c @@ -143,11 +143,11 @@ cache_content (CT ct) if (ferror (gp)) { admonish (ce->ce_file, "error reading"); - unlink (cachefile); + (void) m_unlink (cachefile); } else { if (ferror (fp)) { admonish (cachefile, "error writing"); - unlink (cachefile); + (void) m_unlink (cachefile); } } fclose (fp); @@ -273,7 +273,7 @@ got_it: if (status == OK && writing) { if (*writing && strchr(buffer, '/')) make_intermediates (buffer); - unlink (buffer); + (void) m_unlink (buffer); } free (id); diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index bafd78e2..20162508 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -53,7 +53,6 @@ int debugsw; /* Needed by mhparse.c. */ #define quitser pipeser /* mhparse.c */ -extern char *tmp; /* directory to place tmp files */ extern int skip_mp_cte_check; /* flag to InitMultiPart */ extern int suppress_bogus_mp_content_warning; /* flag to InitMultiPart */ extern int bogus_mp_content; /* flag from InitMultiPart */ @@ -135,13 +134,9 @@ main (int argc, char **argv) { fx.decodetext = CE_8BIT; fx.textcodeset = NULL; - done = freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done = freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -264,16 +259,6 @@ main (int argc, char **argv) { fclose (fp); } - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - suppress_bogus_mp_content_warning = skip_mp_cte_check = 1; if (! context_find ("path")) @@ -298,17 +283,17 @@ main (int argc, char **argv) { using_stdin = 1; - if ((cp = m_mktemp2 (tmp, invo_name, &fd, NULL)) == NULL) { - adios (NULL, "unable to create temporary file"); + if ((cp = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) { + adios (NULL, "unable to create temporary file in %s", + get_temp_dir()); } else { free (file); file = add (cp, NULL); - chmod (file, 0600); cpydata (STDIN_FILENO, fd, "-", file); } if (close (fd)) { - unlink (file); + (void) m_unlink (file); adios (NULL, "failed to write temporary file"); } } @@ -371,7 +356,7 @@ main (int argc, char **argv) { status += mhfixmsgsbr (ctp, &fx, outfile); if (using_stdin) { - unlink (file); + (void) m_unlink (file); if (! outfile) { /* Just calling m_backup() unlinks the backup file. */ @@ -384,7 +369,6 @@ main (int argc, char **argv) { } free (outfile); - free (tmp); free (file); /* done is freects_done, which will clean up all of cts. */ @@ -406,7 +390,12 @@ mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) { modify_inplace = 1; if ((*ctp)->c_file) { - outfile = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL); + char *tempfile; + if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) { + adios (NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + outfile = add (tempfile, NULL); } else { adios (NULL, "missing both input and output filenames\n"); } @@ -468,7 +457,7 @@ mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) { } if (modify_inplace) { - if (status != OK) unlink (outfile); + if (status != OK) (void) m_unlink (outfile); free (outfile); outfile = NULL; } @@ -524,7 +513,7 @@ fix_boundary (CT *ct, int *message_mods) { if (get_multipart_boundary (*ct, &part_boundary) == OK) { char *fixed; - if ((fixed = m_mktemp2 (tmp, invo_name, NULL, &(*ct)->c_fp))) { + if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) { if (replace_boundary (*ct, fixed, part_boundary) == OK) { char *filename = add ((*ct)->c_file, NULL); @@ -544,7 +533,8 @@ fix_boundary (CT *ct, int *message_mods) { status = NOTOK; } } else { - advise (NULL, "unable to create temporary file"); + advise (NULL, "unable to create temporary file in %s", + get_temp_dir()); status = NOTOK; } @@ -1029,7 +1019,13 @@ build_text_plain_part (CT encoded_part) { contains the decoded contents. And the decoding function, such as openQuoted, will have set ...->ce_unlink to 1 so that it will be unlinked by free_content (). */ - tmp_plain_file = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL); + char *tempfile; + + if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) { + advise (NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + tmp_plain_file = add (tempfile, NULL); if (reformat_part (tp_part, tmp_plain_file, tp_part->c_ctinfo.ci_type, tp_part->c_ctinfo.ci_subtype, @@ -1039,7 +1035,7 @@ build_text_plain_part (CT encoded_part) { } free_content (tp_part); - unlink (tmp_plain_file); + (void) m_unlink (tmp_plain_file); free (tmp_plain_file); return NULL; @@ -1100,13 +1096,17 @@ static int decode_part (CT ct) { char *tmp_decoded; int status; + char *tempfile; - tmp_decoded = add (m_mktemp2 (tmp, invo_name, NULL, NULL), NULL); + if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) { + adios (NULL, "unable to create temporary file in %s", get_temp_dir()); + } + tmp_decoded = add (tempfile, NULL); /* The following call will load ct->c_cefile.ce_file with the tmp filename of the decoded content. tmp_decoded will contain the encoded output, get rid of that. */ status = output_message (ct, tmp_decoded); - unlink (tmp_decoded); + (void) m_unlink (tmp_decoded); free (tmp_decoded); return status; @@ -1166,7 +1166,7 @@ reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) { /* Unlink decoded content tmp file and free its filename to avoid leaks. The file stream should already have been closed. */ if (ct->c_cefile.ce_unlink) { - unlink (ct->c_cefile.ce_file); + (void) m_unlink (ct->c_cefile.ce_file); free (ct->c_cefile.ce_file); ct->c_cefile.ce_file = NULL; ct->c_cefile.ce_unlink = 0; @@ -1486,7 +1486,7 @@ decode_text_parts (CT ct, int encoding, int *message_mods) { : ct->c_ctline ? ct->c_ctline : ""); } - unlink (ct->c_cefile.ce_file); + (void) m_unlink (ct->c_cefile.ce_file); free (ct->c_cefile.ce_file); ct->c_cefile.ce_file = NULL; } else if (ct->c_encoding == CE_QUOTED && @@ -1500,7 +1500,7 @@ decode_text_parts (CT ct, int encoding, int *message_mods) { : ct->c_ctline ? ct->c_ctline : ""); } - unlink (ct->c_cefile.ce_file); + (void) m_unlink (ct->c_cefile.ce_file); free (ct->c_cefile.ce_file); ct->c_cefile.ce_file = NULL; } else { @@ -1689,8 +1689,14 @@ strip_crs (CT ct, int *message_mods) { if (has_crs) { int fd; - char *stripped_content_file = - add (m_mktemp2 (tmp, invo_name, &fd, NULL), NULL); + char *stripped_content_file; + char *tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL); + + if (tempfile == NULL) { + adios (NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + stripped_content_file = add (tempfile, NULL); /* Strip each CR before a LF from the content. */ fseeko (*fp, begin, SEEK_SET); @@ -1717,13 +1723,13 @@ strip_crs (CT ct, int *message_mods) { if (close (fd)) { admonish (NULL, "unable to write temporary file %s", stripped_content_file); - unlink (stripped_content_file); + (void) m_unlink (stripped_content_file); status = NOTOK; } else { /* Replace the decoded file with the converted one. */ if (ct->c_cefile.ce_file) { if (ct->c_cefile.ce_unlink) { - unlink (ct->c_cefile.ce_file); + (void) m_unlink (ct->c_cefile.ce_file); } free (ct->c_cefile.ce_file); } @@ -1842,6 +1848,7 @@ convert_codeset (CT ct, char *dest_codeset, int *message_mods) { int opened_input_file = 0; char src_buffer[BUFSIZ]; HF hf; + char *tempfile; if ((conv_desc = iconv_open (dest_codeset, src_codeset)) == (iconv_t) -1) { @@ -1849,7 +1856,11 @@ convert_codeset (CT ct, char *dest_codeset, int *message_mods) { return -1; } - dest = add (m_mktemp2 (tmp, invo_name, &fd, NULL), NULL); + if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) { + adios (NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + dest = add (tempfile, NULL); if (ct->c_cefile.ce_file) { file = &ct->c_cefile.ce_file; @@ -1912,7 +1923,7 @@ convert_codeset (CT ct, char *dest_codeset, int *message_mods) { /* Replace the decoded file with the converted one. */ if (ct->c_cefile.ce_file) { if (ct->c_cefile.ce_unlink) { - unlink (ct->c_cefile.ce_file); + (void) m_unlink (ct->c_cefile.ce_file); } free (ct->c_cefile.ce_file); } @@ -1951,7 +1962,7 @@ convert_codeset (CT ct, char *dest_codeset, int *message_mods) { } } } else { - unlink (dest); + (void) m_unlink (dest); } #else /* ! HAVE_ICONV */ NMH_UNUSED (message_mods); @@ -2001,7 +2012,7 @@ write_content (CT ct, char *input_filename, char *outfile, int modify_inplace, } if (new != -1) close (new); if (old != -1) close (old); - unlink (outfile); + (void) m_unlink (outfile); if (i < 0) { /* The -file argument processing used path() to @@ -2017,7 +2028,7 @@ write_content (CT ct, char *input_filename, char *outfile, int modify_inplace, } else { admonish (NULL, "unable to remove input file %s, " "not modifying it", infile); - unlink (outfile); + (void) m_unlink (outfile); status = NOTOK; } @@ -2027,7 +2038,7 @@ write_content (CT ct, char *input_filename, char *outfile, int modify_inplace, } } else { /* No modifications and didn't need the tmp outfile. */ - unlink (outfile); + (void) m_unlink (outfile); } } else { /* Output is going to some file. Produce it whether or not diff --git a/uip/mhfree.c b/uip/mhfree.c index 5fa2a59c..29756876 100644 --- a/uip/mhfree.c +++ b/uip/mhfree.c @@ -106,7 +106,7 @@ free_content (CT ct) if (ct->c_file) { if (ct->c_unlink) - unlink (ct->c_file); + (void) m_unlink (ct->c_file); free (ct->c_file); } if (ct->c_fp) @@ -270,7 +270,7 @@ free_encoding (CT ct, int toplevel) if (ce->ce_file) { if (ce->ce_unlink) - unlink (ce->ce_file); + (void) m_unlink (ce->ce_file); free (ce->ce_file); ce->ce_file = NULL; } diff --git a/uip/mhl.c b/uip/mhl.c index d4c5308b..652fb11b 100644 --- a/uip/mhl.c +++ b/uip/mhl.c @@ -13,7 +13,8 @@ int main (int argc, char **argv) { - setlocale(LC_ALL, ""); + if (nmh_init(argv[0], 1)) { return 1; } + done (mhl (argc, argv)); return 1; } diff --git a/uip/mhlist.c b/uip/mhlist.c index 70924348..f37041c3 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -47,9 +47,6 @@ DEFINE_SWITCH_ARRAY(MHLIST, switches); #undef X -/* mhparse.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -107,13 +104,9 @@ main (int argc, char **argv) struct msgs *mp = NULL; CT ct, *ctp; - done=freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -243,16 +236,6 @@ do_cache: cache_private = ".cache"; cache_private = getcpy (m_maildir (cache_private)); - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index 6a1c3db7..f4df180a 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -349,11 +349,6 @@ mhl (int argc, char **argv) char buf[BUFSIZ], *files[MAXARGS]; char **argp, **arguments; - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); - arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/mhn.c b/uip/mhn.c index 06aeee53..08405d4d 100644 --- a/uip/mhn.c +++ b/uip/mhn.c @@ -77,9 +77,6 @@ DEFINE_SWITCH_ARRAY(MHN, switches); #undef X -/* mhparse.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -167,13 +164,9 @@ main (int argc, char **argv) CT ct, *ctp; FILE *fp; - done=freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -411,16 +404,6 @@ do_cache: */ cwd = getcpy (pwd()); - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); diff --git a/uip/mhparam.c b/uip/mhparam.c index b0c119cf..8a276f72 100644 --- a/uip/mhparam.c +++ b/uip/mhparam.c @@ -131,10 +131,7 @@ main(int argc, char **argv) char *cp, buf[BUFSIZ], **argp; char **arguments, *comps[MAXARGS]; - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/mhparse.c b/uip/mhparse.c index 5e35cbbf..716a22f7 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -28,12 +28,6 @@ extern int wcachesw; /* mhcachesbr.c */ int checksw = 0; /* check Content-MD5 field */ -/* - * Directory to place temp files. This must - * be set before these routines are called. - */ -char *tmp; - /* * These are for mhfixmsg to: * 1) Instruct parser not to detect invalid Content-Transfer-Encoding @@ -200,23 +194,23 @@ parse_mime (char *file) if ((is_stdin = !(strcmp (file, "-")))) { char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp); if (tfile == NULL) { - advise("mhparse", "unable to create temporary file"); + advise("mhparse", "unable to create temporary file in %s", + get_temp_dir()); return NULL; } file = add (tfile, NULL); - chmod (file, 0600); while (fgets (buffer, sizeof(buffer), stdin)) fputs (buffer, fp); fflush (fp); if (ferror (stdin)) { - unlink (file); + (void) m_unlink (file); advise ("stdin", "error reading"); return NULL; } if (ferror (fp)) { - unlink (file); + (void) m_unlink (file); advise (file, "error writing"); return NULL; } @@ -228,7 +222,7 @@ parse_mime (char *file) if (!(ct = get_content (fp, file, 1))) { if (is_stdin) - unlink (file); + (void) m_unlink (file); advise (NULL, "unable to decode %s", file); return NULL; } @@ -1652,7 +1646,12 @@ openBase64 (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -1670,17 +1669,7 @@ openBase64 (CT ct, char **file) cp = context_find (buffer); } if (cp != NULL && *cp != '\0') { - if (ce->ce_unlink) { - /* Temporary file already exists, so we rename to - version with extension. */ - char *file_org = strdup(ce->ce_file); - ce->ce_file = add (cp, ce->ce_file); - if (rename(file_org, ce->ce_file)) { - adios (ce->ce_file, "unable to rename %s to ", file_org); - } - free(file_org); - - } else { + if (! ce->ce_unlink) { ce->ce_file = add (cp, ce->ce_file); } } @@ -1886,7 +1875,12 @@ openQuoted (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -1904,17 +1898,7 @@ openQuoted (CT ct, char **file) cp = context_find (buffer); } if (cp != NULL && *cp != '\0') { - if (ce->ce_unlink) { - /* Temporary file already exists, so we rename to - version with extension. */ - char *file_org = strdup(ce->ce_file); - ce->ce_file = add (cp, ce->ce_file); - if (rename(file_org, ce->ce_file)) { - adios (ce->ce_file, "unable to rename %s to ", file_org); - } - free(file_org); - - } else { + if (! ce->ce_unlink) { ce->ce_file = add (cp, ce->ce_file); } } @@ -2110,7 +2094,12 @@ open7Bit (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -2128,17 +2117,7 @@ open7Bit (CT ct, char **file) cp = context_find (buffer); } if (cp != NULL && *cp != '\0') { - if (ce->ce_unlink) { - /* Temporary file already exists, so we rename to - version with extension. */ - char *file_org = strdup(ce->ce_file); - ce->ce_file = add (cp, ce->ce_file); - if (rename(file_org, ce->ce_file)) { - adios (ce->ce_file, "unable to rename %s to ", file_org); - } - free(file_org); - - } else { + if (! ce->ce_unlink) { ce->ce_file = add (cp, ce->ce_file); } } @@ -2361,12 +2340,12 @@ openFile (CT ct, char **file) if (ferror (gp)) { admonish (ce->ce_file, "error reading"); - unlink (cachefile); + (void) m_unlink (cachefile); } else if (ferror (fp)) { admonish (cachefile, "error writing"); - unlink (cachefile); + (void) m_unlink (cachefile); } fclose (fp); } @@ -2499,8 +2478,14 @@ openFTP (CT ct, char **file) ce->ce_file = add (*file, NULL); else if (caching) ce->ce_file = add (cachefile, NULL); - else - ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); + else { + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); + } if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { content_error (ce->ce_file, ct, "unable to fopen for reading/writing"); @@ -2571,12 +2556,12 @@ openFTP (CT ct, char **file) if (ferror (gp)) { admonish (ce->ce_file, "error reading"); - unlink (cachefile); + (void) m_unlink (cachefile); } else if (ferror (fp)) { admonish (cachefile, "error writing"); - unlink (cachefile); + (void) m_unlink (cachefile); } fclose (fp); } @@ -2688,7 +2673,12 @@ openMail (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -2781,8 +2771,14 @@ openURL (CT ct, char **file) ce->ce_file = add(*file, NULL); else if (caching) ce->ce_file = add(cachefile, NULL); - else - ce->ce_file = add(m_mktemp(tmp, NULL, NULL), NULL); + else { + char *tempfile; + if ((tempfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + ce->ce_file = add (tempfile, NULL); + } if ((ce->ce_fp = fopen(ce->ce_file, "w+")) == NULL) { content_error(ce->ce_file, ct, "unable to fopen for read/writing"); @@ -2835,7 +2831,7 @@ openURL (CT ct, char **file) if (ferror(gp)) { admonish(ce->ce_file, "error reading"); - unlink(cachefile); + (void) m_unlink (cachefile); } } umask(mask); diff --git a/uip/mhpath.c b/uip/mhpath.c index 29b59f4a..b9506d73 100644 --- a/uip/mhpath.c +++ b/uip/mhpath.c @@ -32,11 +32,7 @@ main(int argc, char **argv) struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/mhshow.c b/uip/mhshow.c index f19f9a9d..f83236b7 100644 --- a/uip/mhshow.c +++ b/uip/mhshow.c @@ -56,9 +56,6 @@ DEFINE_SWITCH_ARRAY(MHSHOW, switches); #undef X -/* mhparse.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -117,13 +114,9 @@ main (int argc, char **argv) CT ct, *ctp; FILE *fp; - done=freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -291,16 +284,6 @@ do_cache: cache_private = ".cache"; cache_private = getcpy (m_maildir (cache_private)); - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); diff --git a/uip/mhstore.c b/uip/mhstore.c index fff480ae..fb5349fa 100644 --- a/uip/mhstore.c +++ b/uip/mhstore.c @@ -48,9 +48,6 @@ DEFINE_SWITCH_ARRAY(MHSTORE, switches); int save_clobber_policy (const char *); extern int files_not_clobbered; -/* mhparse.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -106,13 +103,9 @@ main (int argc, char **argv) CT ct, *ctp; FILE *fp; - done=freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -267,16 +260,6 @@ do_cache: */ cwd = getcpy (pwd()); - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index 93d3e902..7b1dbd51 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -562,7 +562,10 @@ store_content (CT ct, CT p) char *tmpfilenam, *folder; /* Store content in temporary file for now */ - tmpfilenam = m_mktemp(invo_name, NULL, NULL); + if ((tmpfilenam = m_mktemp(invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } ct->c_storage = add (tmpfilenam, NULL); /* Get the folder name */ @@ -623,7 +626,7 @@ got_filename: */ if (ct->c_folder && (!is_partial || last_partial)) { msgnum = output_content_folder (ct->c_folder, ct->c_storage); - unlink (ct->c_storage); + (void) m_unlink (ct->c_storage); if (msgnum == NOTOK) return NOTOK; } diff --git a/uip/mhtest.c b/uip/mhtest.c index 092b542a..b85a91ea 100644 --- a/uip/mhtest.c +++ b/uip/mhtest.c @@ -42,9 +42,6 @@ DEFINE_SWITCH_ARRAY(MHTEST, switches); #undef X -/* mhparse.c */ -extern char *tmp; /* directory to place temp files */ - /* mhcachesbr.c */ extern int rcachesw; extern int wcachesw; @@ -102,13 +99,9 @@ main (int argc, char **argv) struct msgs *mp = NULL; CT ct, *ctp; - done=freects_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=freects_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -226,16 +219,6 @@ do_cache: cache_private = ".cache"; cache_private = getcpy (m_maildir (cache_private)); - /* - * Check for storage directory. If specified, - * then store temporary files there. Else we - * store them in standard nmh directory. - */ - if ((cp = context_find (nmhstorage)) && *cp) - tmp = concat (cp, "/", invo_name, NULL); - else - tmp = add (m_maildir (invo_name), NULL); - if (!context_find ("path")) free (path ("./", TFOLDER)); @@ -360,7 +343,6 @@ static void pipeser (int i) { if (i == SIGQUIT) { - unlink ("core"); fflush (stdout); fprintf (stderr, "\n"); fflush (stderr); diff --git a/uip/msgchk.c b/uip/msgchk.c index 39a2918d..49e758f0 100644 --- a/uip/msgchk.c +++ b/uip/msgchk.c @@ -85,11 +85,7 @@ main (int argc, char **argv) char **argp, **arguments, *vec[MAXVEC]; struct passwd *pw; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); diff --git a/uip/msh.c b/uip/msh.c index 75371bad..e91924c3 100644 --- a/uip/msh.c +++ b/uip/msh.c @@ -203,11 +203,7 @@ main (int argc, char **argv) char *cp, *file = NULL, *folder = NULL; char **argp, **arguments, buf[BUFSIZ]; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc,argv, 1); @@ -1118,7 +1114,7 @@ quit (void) close (i); else advise (mp->foldpath, "error zero'ing"); - unlink (map_name (mp->foldpath));/* XXX */ + (void) m_unlink (map_name (mp->foldpath));/* XXX */ } goto release; } @@ -1135,8 +1131,8 @@ quit (void) for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) { mbx_close (tmpfil, md); - unlink (tmpfil); - unlink (map_name (tmpfil)); + (void) m_unlink (tmpfil); + (void) m_unlink (map_name (tmpfil)); goto release; } mbx_close (tmpfil, md); @@ -1149,8 +1145,8 @@ quit (void) if (rename (map1, map2) == NOTOK) { admonish (map2, "unable to rename %s to", map1); - unlink (map1); - unlink (map2); + (void) m_unlink (map1); + (void) m_unlink (map2); } } diff --git a/uip/mshcmds.c b/uip/mshcmds.c index 3a5f98fa..6627c6ab 100644 --- a/uip/mshcmds.c +++ b/uip/mshcmds.c @@ -1011,7 +1011,7 @@ forw (char *proc, char *filter, int vecp, char **vec) break; } - unlink (tmpfil); + (void) m_unlink (tmpfil); } @@ -2842,7 +2842,7 @@ ready: ; } if (!fmsh) - unlink (tmpfil); + (void) m_unlink (tmpfil); return status; } diff --git a/uip/new.c b/uip/new.c index 59b5b8cf..6f998252 100644 --- a/uip/new.c +++ b/uip/new.c @@ -422,11 +422,7 @@ main(int argc, char **argv) char *unseen; struct node *folder; - setlocale(LC_ALL, ""); - invo_name = r1bindex(argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/packf.c b/uip/packf.c index 6f951914..30f0f484 100644 --- a/uip/packf.c +++ b/uip/packf.c @@ -46,13 +46,9 @@ main (int argc, char **argv) struct msgs *mp; struct stat st; - done=mbxclose_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=mbxclose_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/pick.c b/uip/pick.c index a9159208..740f6f0a 100644 --- a/uip/pick.c +++ b/uip/pick.c @@ -66,13 +66,9 @@ main (int argc, char **argv) struct msgs *mp, *mp2; register FILE *fp; - done=putzero_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=putzero_done; arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/post.c b/uip/post.c index f6e14fb4..9b358521 100644 --- a/uip/post.c +++ b/uip/post.c @@ -283,12 +283,7 @@ main (int argc, char **argv) FILE *in, *out; m_getfld_state_t gstate = 0; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* foil search of user profile/context */ - if (context_foil (NULL) == -1) - done (1); + if (nmh_init(argv[0], 0 /* use context_foil() */)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 0); @@ -533,15 +528,12 @@ main (int argc, char **argv) if ((out = fopen ("/dev/null", "w")) == NULL) adios ("/dev/null", "unable to open"); } else { - char *cp = m_mktemp(m_maildir(invo_name), NULL, &out); - if (cp == NULL) { - cp = m_mktemp2(NULL, invo_name, NULL, &out); - if (cp == NULL) { - adios ("post", "unable to create temporary file"); - } - } + char *cp = m_mktemp2(NULL, invo_name, NULL, &out); + if (cp == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } strncpy(tmpfil, cp, sizeof(tmpfil)); - chmod (tmpfil, 0600); } } @@ -638,13 +630,13 @@ main (int argc, char **argv) post (tmpfil, 0, verbose, envelope); } post (bccfil, 1, verbose, envelope); - unlink (bccfil); + (void) m_unlink (bccfil); } else { post (tmpfil, 0, isatty (1), envelope); } p_refile (tmpfil); - unlink (tmpfil); + (void) m_unlink (tmpfil); if (verbose) { if (partno) @@ -1280,8 +1272,9 @@ make_bcc_file (int dashstuff) FILE *out; char *tfile = NULL, *program; - tfile = m_mktemp2(NULL, "bccs", NULL, &out); - if (tfile == NULL) adios("bcc", "unable to create temporary file"); + if ((tfile = m_mktemp2(NULL, "bccs", NULL, &out)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (bccfil, tfile, sizeof(bccfil)); fprintf (out, "From: %s\n", fullfrom); @@ -1712,9 +1705,9 @@ sigser (int i) { NMH_UNUSED (i); - unlink (tmpfil); + (void) m_unlink (tmpfil); if (msgflags & MINV) - unlink (bccfil); + (void) m_unlink (bccfil); if (!whomsw || checksw) sm_end (NOTOK); @@ -1836,9 +1829,9 @@ die (char *what, char *fmt, ...) { va_list ap; - unlink (tmpfil); + (void) m_unlink (tmpfil); if (msgflags & MINV) - unlink (bccfil); + (void) m_unlink (bccfil); if (!whomsw || checksw) sm_end (NOTOK); diff --git a/uip/prompter.c b/uip/prompter.c index 7923e84b..37f5960b 100644 --- a/uip/prompter.c +++ b/uip/prompter.c @@ -78,11 +78,7 @@ main (int argc, char **argv) char *tmpfil; m_getfld_state_t gstate = 0; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -152,9 +148,9 @@ main (int argc, char **argv) if ((in = fopen (drft, "r")) == NULL) adios (drft, "unable to open"); - tmpfil = m_mktemp2(NULL, invo_name, NULL, &out); - if (tmpfil == NULL) adios("prompter", "unable to create temporary file"); - chmod (tmpfil, 0600); + if ((tmpfil = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } /* * Are we changing the kill or erase character? @@ -231,7 +227,7 @@ abort: if (killp || erasep) { tcsetattr(0, TCSADRAIN, &tio); } - unlink (tmpfil); + (void) m_unlink (tmpfil); done (1); } if (i != 0 || (field[0] != '\n' && field[0] != 0)) { @@ -320,7 +316,7 @@ abort: cpydata (fdi, fdo, tmpfil, drft); close (fdi); close (fdo); - unlink (tmpfil); + (void) m_unlink (tmpfil); context_save (); /* save the context file */ done (0); diff --git a/uip/rcvdist.c b/uip/rcvdist.c index 8b672cd7..85baf33d 100644 --- a/uip/rcvdist.c +++ b/uip/rcvdist.c @@ -48,13 +48,9 @@ main (int argc, char **argv) FILE *fp; char *tfile = NULL; - done=unlink_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=unlink_done; /* * Configure this now, since any unknown switches to rcvdist get @@ -102,15 +98,17 @@ main (int argc, char **argv) umask (~m_gmprot ()); - tfile = m_mktemp2(NULL, invo_name, NULL, &fp); - if (tfile == NULL) adios("rcvdist", "unable to create temporary file"); + if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (tmpfil, tfile, sizeof(tmpfil)); cpydata (fileno (stdin), fileno (fp), "message", tmpfil); fseek (fp, 0L, SEEK_SET); - tfile = m_mktemp2(NULL, invo_name, NULL, NULL); - if (tfile == NULL) adios("forw", "unable to create temporary file"); + if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (drft, tfile, sizeof(tmpfil)); rcvdistout (fp, form, addrs); @@ -255,11 +253,11 @@ static void unlink_done (int status) { if (backup[0]) - unlink (backup); + (void) m_unlink (backup); if (drft[0]) - unlink (drft); + (void) m_unlink (drft); if (tmpfil[0]) - unlink (tmpfil); + (void) m_unlink (tmpfil); exit (status ? RCV_MBX : RCV_MOK); } diff --git a/uip/rcvpack.c b/uip/rcvpack.c index 08e21b9c..bbd0e08e 100644 --- a/uip/rcvpack.c +++ b/uip/rcvpack.c @@ -40,11 +40,7 @@ main (int argc, char **argv) char *cp, *file = NULL, buf[BUFSIZ]; char **argp, **arguments; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); diff --git a/uip/rcvstore.c b/uip/rcvstore.c index 98f17503..be0ac396 100644 --- a/uip/rcvstore.c +++ b/uip/rcvstore.c @@ -54,13 +54,9 @@ main (int argc, char **argv) struct msgs *mp; struct stat st; - done=unlink_done; - - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); + if (nmh_init(argv[0], 1)) { return 1; } - /* read user profile/context */ - context_read(); + done=unlink_done; mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); @@ -162,7 +158,7 @@ main (int argc, char **argv) /* create a temporary file */ tmpfilenam = m_mktemp (invo_name, &fd, NULL); if (tmpfilenam == NULL) { - adios ("rcvstore", "unable to create temporary file"); + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); } chmod (tmpfilenam, m_gmprot()); @@ -170,7 +166,7 @@ main (int argc, char **argv) cpydata (fileno (stdin), fd, "standard input", tmpfilenam); if (fstat (fd, &st) == NOTOK) { - unlink (tmpfilenam); + (void) m_unlink (tmpfilenam); adios (tmpfilenam, "unable to fstat"); } if (close (fd) == NOTOK) @@ -178,7 +174,7 @@ main (int argc, char **argv) /* don't add file if it is empty */ if (st.st_size == 0) { - unlink (tmpfilenam); + (void) m_unlink (tmpfilenam); advise (NULL, "empty file"); done (0); } @@ -216,7 +212,7 @@ main (int argc, char **argv) folder_free (mp); /* free folder/message structure */ context_save (); /* save the global context file */ - unlink (tmpfilenam); /* remove temporary file */ + (void) m_unlink (tmpfilenam); /* remove temporary file */ tmpfilenam = NULL; done (0); @@ -230,6 +226,6 @@ static void unlink_done(int status) { if (tmpfilenam && *tmpfilenam) - unlink (tmpfilenam); + (void) m_unlink (tmpfilenam); exit (status); } diff --git a/uip/rcvtty.c b/uip/rcvtty.c index 447c6e40..3bee075e 100644 --- a/uip/rcvtty.c +++ b/uip/rcvtty.c @@ -82,11 +82,7 @@ main (int argc, char **argv) char **argp, **arguments, *vec[MAXARGS]; struct utmpx *utp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); @@ -188,13 +184,15 @@ message_fd (char **vec) { pid_t child_id; int bytes, seconds; - /* volatile to prevent "might be clobbered" warning from gcc: */ - volatile int fd; - char tmpfil[BUFSIZ]; + int fd; + char *tfile; struct stat st; - fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil))); - unlink (tmpfil); + if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) { + advise(NULL, "unable to create temporary file in %s", get_temp_dir()); + return NOTOK; + } + (void) m_unlink(tfile); /* Use fd, no longer need the file name. */ if ((child_id = fork()) == NOTOK) { /* fork error */ @@ -254,9 +252,11 @@ header_fd (void) char *nfs; char *tfile = NULL; - tfile = m_mktemp2(NULL, invo_name, &fd, NULL); - if (tfile == NULL) return NOTOK; - unlink (tfile); + if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) { + advise(NULL, "unable to create temporary file in %s", get_temp_dir()); + return NOTOK; + } + (void) m_unlink(tfile); /* Use fd, no longer need the file name. */ rewind (stdin); diff --git a/uip/refile.c b/uip/refile.c index 776c6fdf..ecbe3466 100644 --- a/uip/refile.c +++ b/uip/refile.c @@ -68,11 +68,7 @@ main (int argc, char **argv) struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -342,7 +338,7 @@ remove_files (int filep, char **files) /* Else just unlink the files */ files++; /* advance past filevec[0] */ for (i = 0; i < filep; i++) { - if (unlink (files[i]) == NOTOK) + if (m_unlink (files[i]) == NOTOK) admonish (files[i], "unable to unlink"); } } diff --git a/uip/repl.c b/uip/repl.c index d1c8ebfa..7b0ab4a0 100644 --- a/uip/repl.c +++ b/uip/repl.c @@ -129,11 +129,7 @@ main (int argc, char **argv) int buildsw = 0; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/rmf.c b/uip/rmf.c index 735cf774..bb03805a 100644 --- a/uip/rmf.c +++ b/uip/rmf.c @@ -37,11 +37,7 @@ main (int argc, char **argv) char *cp, *folder = NULL, newfolder[BUFSIZ]; char buf[BUFSIZ], **argp, **arguments; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -183,7 +179,7 @@ rmf (char *folder) others++; continue; } - if (unlink (dp->d_name) == NOTOK) { + if (m_unlink (dp->d_name) == NOTOK) { admonish (dp->d_name, "unable to unlink %s:", folder); others++; } diff --git a/uip/rmm.c b/uip/rmm.c index 8882b8f6..8644a02f 100644 --- a/uip/rmm.c +++ b/uip/rmm.c @@ -37,11 +37,7 @@ main (int argc, char **argv) struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/scan.c b/uip/scan.c index 31309220..20668691 100644 --- a/uip/scan.c +++ b/uip/scan.c @@ -52,11 +52,7 @@ main (int argc, char **argv) struct msgs *mp; FILE *in; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 1); diff --git a/uip/send.c b/uip/send.c index 626eba16..5885c98e 100644 --- a/uip/send.c +++ b/uip/send.c @@ -118,11 +118,7 @@ main (int argc, char **argv) struct msgs *mp; struct stat st; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -382,8 +378,12 @@ go_to_it: && (distsw = atoi (cp)) && altmsg) { vec[vecp++] = "-dist"; - distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL)); - unlink(distfile); + if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + distfile = getcpy (cp); + (void) m_unlink(distfile); if (link (altmsg, distfile) == NOTOK) { /* Cygwin with FAT32 filesystem produces EPERM. */ if (errno != EXDEV && errno != EPERM @@ -393,7 +393,11 @@ go_to_it: ) adios (distfile, "unable to link %s to", altmsg); free (distfile); - distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL)); + if ((cp = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + distfile = getcpy (cp); { int in, out; struct stat st; diff --git a/uip/sendsbr.c b/uip/sendsbr.c index aa4426e1..020949d0 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -99,10 +99,11 @@ sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st, * rename the draft file. I'm not quite sure why. */ if (pushsw && unique) { - char *cp = m_mktemp2(drft, invo_name, NULL, NULL); - if (cp == NULL) { - adios ("sendsbr", "unable to create temporary file"); - } + char *cp = m_mktemp2(drft, invo_name, NULL, NULL); + if (cp == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK) adios (file, "unable to rename %s to", drft); drft = file; @@ -134,7 +135,7 @@ sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st, done=exit; if (distfile) - unlink (distfile); + (void) m_unlink (distfile); return status; } @@ -286,10 +287,10 @@ splitmsg (char **vec, int vecp, char *program, char *drft, char *cp = m_mktemp2(drft, invo_name, NULL, &out); if (cp == NULL) { - adios (drft, "unable to create temporary file for"); + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); } strncpy(tmpdrf, cp, sizeof(tmpdrf)); - chmod (tmpdrf, 0600); /* * Output the header fields @@ -353,7 +354,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft, snprintf (partnum, sizeof(partnum), "%d", partno); status = sendaux (vec, vecp, program, tmpdrf, st); - unlink (tmpdrf); + (void) m_unlink (tmpdrf); if (status != OK) break; @@ -395,7 +396,8 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st) snprintf (buf, sizeof(buf), "%d", fd2); vec[vecp++] = buf; } else { - admonish (NULL, "unable to create file for annotation list"); + admonish (NULL, "unable to create temporary file in %s " + "for annotation list", get_temp_dir()); } } if (distfile && distout (drft, distfile, backup) == NOTOK) @@ -450,7 +452,7 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st) if (annotext && fd2 != NOTOK) close (fd2); if (distfile) { - unlink (drft); + (void) m_unlink (drft); if (rename (backup, drft) == NOTOK) advise (drft, "unable to rename %s to", backup); } @@ -536,16 +538,14 @@ static int tmp_fd (void) { int fd; - char *tfile = NULL; + char *tfile; - tfile = m_mktemp2(NULL, invo_name, &fd, NULL); - if (tfile == NULL) return NOTOK; - fchmod(fd, 0600); + if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK; if (debugsw) advise (NULL, "temporary file %s selected", tfile); else - if (unlink (tfile) == NOTOK) + if (m_unlink (tfile) == NOTOK) advise (tfile, "unable to remove"); return fd; @@ -588,6 +588,8 @@ anno (int fd, struct stat *st) sigaddset (&set, SIGTERM); sigprocmask (SIG_BLOCK, &set, &oset); + unregister_for_removal(0); + annoaux (fd); if (child_id == OK) _exit (0); diff --git a/uip/show.c b/uip/show.c index 14d41911..acd08afb 100644 --- a/uip/show.c +++ b/uip/show.c @@ -61,11 +61,7 @@ main (int argc, char **argv) struct msgs_array msgs = { 0, 0, NULL }; struct msgs_array vec = { 0, 0, NULL }; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } if (!strcasecmp (invo_name, "next")) { mode = NEXT; diff --git a/uip/slocal.c b/uip/slocal.c index 8dace059..eaa7cb7c 100644 --- a/uip/slocal.c +++ b/uip/slocal.c @@ -188,12 +188,7 @@ main (int argc, char **argv) char mailbox[BUFSIZ], tmpfil[BUFSIZ]; char **argp, **arguments; - setlocale(LC_ALL, ""); - invo_name = r1bindex (*argv, '/'); - - /* foil search of user profile/context */ - if (context_foil (NULL) == -1) - done (1); + if (nmh_init(argv[0], 0 /* use context_foil() */)) { return 1; } mts_init (invo_name); arguments = getarguments (invo_name, argc, argv, 0); @@ -315,14 +310,16 @@ main (int argc, char **argv) if (debug) debug_printf ("retrieving message from file \"%s\"\n", file); if ((fd = copy_message (tempfd, tmpfil, 1)) == -1) - adios (NULL, "unable to create temporary file"); + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); close (tempfd); } else { /* getting message from stdin */ if (debug) debug_printf ("retrieving message from stdin\n"); if ((fd = copy_message (fileno (stdin), tmpfil, 1)) == -1) - adios (NULL, "unable to create temporary file"); + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); } if (debug) @@ -339,7 +336,7 @@ main (int argc, char **argv) thing would be to delay this unlink() until later if debug == 1, but I'll leave that for someone who cares about the temp-file-accessing functionality (they'll have to watch out for cases where we adios()). */ - unlink (tmpfil); + (void) m_unlink (tmpfil); if (!(fp = fdopen (fd, "r+"))) adios (NULL, "unable to access temporary file"); @@ -1196,7 +1193,6 @@ copy_message (int qd, char *tmpfil, int fold) tfile = m_mktemp2(NULL, invo_name, &fd1, NULL); if (tfile == NULL) return -1; - fchmod(fd1, 0600); strncpy (tmpfil, tfile, BUFSIZ); if (!fold) { @@ -1204,7 +1200,7 @@ copy_message (int qd, char *tmpfil, int fold) if (write (fd1, buffer, i) != i) { you_lose: close (fd1); - unlink (tmpfil); + (void) m_unlink (tmpfil); return -1; } if (i == -1) diff --git a/uip/sortm.c b/uip/sortm.c index f6a66f16..3df18859 100644 --- a/uip/sortm.c +++ b/uip/sortm.c @@ -78,11 +78,7 @@ main (int argc, char **argv) struct smsg **dlist; int checksw = 0; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; diff --git a/uip/viamail.c b/uip/viamail.c index 13447382..97dee86b 100644 --- a/uip/viamail.c +++ b/uip/viamail.c @@ -57,10 +57,7 @@ main (int argc, char **argv) char *cp, buf[BUFSIZ]; char **argp, **arguments; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 0); argp = arguments; @@ -158,9 +155,9 @@ via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw, umask (~m_gmprot ()); - tfile = m_mktemp2(NULL, invo_name, NULL, &fp); - if (tfile == NULL) adios("viamail", "unable to create temporary file"); - chmod(tfile, 0600); + if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (tmpfil, tfile, sizeof(tmpfil)); if (!strchr(mailsw, '@')) @@ -227,8 +224,8 @@ via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw, } fclose (fp); - if (unlink (tmpfil) == -1) - advise (NULL, "unable to remove temp file %s", tmpfil); + if (m_unlink (tmpfil) == -1) + advise (tmpfil, "unable to remove temp file %s", tmpfil); done (status); return 1; } diff --git a/uip/whatnow.c b/uip/whatnow.c index 4950514e..031ff3c8 100644 --- a/uip/whatnow.c +++ b/uip/whatnow.c @@ -13,6 +13,7 @@ int main (int argc, char **argv) { - setlocale(LC_ALL, ""); + if (nmh_init(argv[0], 1)) { return 1; } + return WhatNow (argc, argv); } diff --git a/uip/whatnowsbr.c b/uip/whatnowsbr.c index 005095a8..4a3c2647 100644 --- a/uip/whatnowsbr.c +++ b/uip/whatnowsbr.c @@ -129,11 +129,6 @@ WhatNow (int argc, char **argv) char *l; /* set on -l to alist command */ int n; /* set on -n to alist command */ - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); - arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -238,7 +233,7 @@ WhatNow (int argc, char **argv) #else /* ! READLINE_SUPPORT */ if (!(argp = getans (prompt, aleqs))) { #endif /* READLINE_SUPPORT */ - unlink (LINK); + (void) m_unlink (LINK); done (1); } switch (smatch (*argp, aleqs)) { @@ -681,7 +676,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, snprintf (linkpath, sizeof(linkpath), "%s/%s", cwd, LINK); if (atfile) { - unlink (linkpath); + (void) m_unlink (linkpath); if (link (altpath, linkpath) == NOTOK) { symlink (altpath, linkpath); slinked = 1; @@ -747,7 +742,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, && copyf (linkpath, altpath) == NOTOK : stat (linkpath, &st) != NOTOK && st.st_nlink == 1 - && (unlink (altpath) == NOTOK + && (m_unlink (altpath) == NOTOK || link (linkpath, altpath) == NOTOK))) advise (linkpath, "unable to update %s from", altmsg); } @@ -758,7 +753,7 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, *ed = NULL; if (altmsg && atfile) - unlink (linkpath); + (void) m_unlink (linkpath); return status; } @@ -1194,8 +1189,12 @@ sendit (char *sp, char **arg, char *file, int pushed) #endif /* not lint */ && altmsg) { vec[vecp++] = "-dist"; - distfile = getcpy (m_mktemp2(altmsg, invo_name, NULL, NULL)); - unlink(distfile); + if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) { + adios(NULL, "unable to create temporary file in %s", + get_temp_dir()); + } + distfile = getcpy (cp); + (void) m_unlink(distfile); if (link (altmsg, distfile) == NOTOK) adios (distfile, "unable to link %s to", altmsg); } else { @@ -1261,7 +1260,7 @@ whomfile (char **arg, char *file) static int removefile (char *drft) { - if (unlink (drft) == NOTOK) + if (m_unlink (drft) == NOTOK) adios (drft, "unable to unlink"); return OK; diff --git a/uip/whom.c b/uip/whom.c index 8973c762..2a36025a 100644 --- a/uip/whom.c +++ b/uip/whom.c @@ -63,11 +63,7 @@ main (int argc, char **argv) char *msg = NULL, **ap, **argp, backup[BUFSIZ]; char buf[BUFSIZ], **arguments, *vec[MAXARGS]; - setlocale(LC_ALL, ""); - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); + if (nmh_init(argv[0], 1)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -200,7 +196,7 @@ main (int argc, char **argv) status = pidwait(child_id, OK); - unlink (msg); + (void) m_unlink (msg); if (rename (backup, msg) == NOTOK) adios (msg, "unable to rename %s to", backup); done (status);