+/* Options passed to subsidiary functions. */
+struct mkdir_options
+{
+ /* Function to make an ancestor, or NULL if ancestors should not be
+ made. */
+ int (*make_ancestor_function) (char const *, char const *, void *);
+
+ /* Mode for ancestor directory. */
+ mode_t ancestor_mode;
+
+ /* Mode for directory itself. */
+ mode_t mode;
+
+ /* File mode bits affected by MODE. */
+ mode_t mode_bits;
+
+ /* If not null, format to use when reporting newly made directories. */
+ char const *created_directory_format;
+};
+
+/* Report that directory DIR was made, if OPTIONS requests this. */
+static void
+announce_mkdir (char const *dir, void *options)
+{
+ struct mkdir_options const *o = options;
+ if (o->created_directory_format)
+ prog_fprintf (stdout, o->created_directory_format, quote (dir));
+}
+
+/* Make ancestor directory DIR, whose last component is COMPONENT,
+ with options OPTIONS. Assume the working directory is COMPONENT's
+ parent. Return 0 if successful and the resulting directory is
+ readable, 1 if successful but the resulting directory is not
+ readable, -1 (setting errno) otherwise. */
+static int
+make_ancestor (char const *dir, char const *component, void *options)
+{
+ struct mkdir_options const *o = options;
+ int r = mkdir (component, o->ancestor_mode);
+ if (r == 0)
+ {
+ r = ! (o->ancestor_mode & S_IRUSR);
+ announce_mkdir (dir, options);
+ }
+ return r;
+}
+
+/* Process a command-line file name. */
+static int
+process_dir (char *dir, struct savewd *wd, void *options)
+{
+ struct mkdir_options const *o = options;
+ return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
+ o->mode, announce_mkdir,
+ o->mode_bits, (uid_t) -1, (gid_t) -1, true)
+ ? EXIT_SUCCESS
+ : EXIT_FAILURE);
+}
+