+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+#include "save-cwd.h"
+#include "dirname.h"
+#include "error.h"
+#include "quote.h"
+#include "stat-macros.h"
+
+#define WX_USR (S_IWUSR | S_IXUSR)
+
+#define CLEANUP_CWD \
+ do \
+ { \
+ /* We're done operating on basename_dir. \
+ Restore working directory. */ \
+ if (do_chdir) \
+ { \
+ if (restore_cwd (&cwd) != 0) \
+ { \
+ int _saved_errno = errno; \
+ error (0, errno, \
+ _("failed to return to initial working directory")); \
+ free_cwd (&cwd); \
+ errno = _saved_errno; \
+ return 1; \
+ } \
+ free_cwd (&cwd); \
+ } \
+ } \
+ while (0)
+
+#define CLEANUP \
+ do \
+ { \
+ umask (oldmask); \
+ CLEANUP_CWD; \
+ } \
+ while (0)
+
+/* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
+ If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P if this
+ function creates DIR and clear it otherwise. Give a diagnostic and
+ return false if DIR cannot be created or cannot be determined to
+ exist already. Use DIRPATH in any diagnostic, not DIR.
+ Note that if DIR already exists, this function returns true
+ (indicating success) and clears *CREATED_DIR_P. */
+
+bool
+make_dir (const char *dir, const char *dirpath, mode_t mode,
+ bool *created_dir_p)
+{
+ bool ok = true;
+ bool created_dir;