+ fprintf (stdlis, _("Renaming %s back to %s\n"),
+ quote_n (0, after_backup_name),
+ quote_n (1, before_backup_name));
+ assign_string (&after_backup_name, 0);
+ }
+}
+
+/* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
+int
+deref_stat (bool deref, char const *name, struct stat *buf)
+{
+ return deref ? stat (name, buf) : lstat (name, buf);
+}
+
+/* Set FD's (i.e., FILE's) access time to TIMESPEC[0]. If that's not
+ possible to do by itself, set its access and data modification
+ times to TIMESPEC[0] and TIMESPEC[1], respectively. */
+int
+set_file_atime (int fd, char const *file, struct timespec const timespec[2])
+{
+#ifdef _FIOSATIME
+ if (0 <= fd)
+ {
+ struct timeval timeval;
+ timeval.tv_sec = timespec[0].tv_sec;
+ timeval.tv_usec = timespec[0].tv_nsec / 1000;
+ if (ioctl (fd, _FIOSATIME, &timeval) == 0)
+ return 0;
+ }
+#endif
+
+ return futimens (fd, file, timespec);
+}
+
+/* A description of a working directory. */
+struct wd
+{
+ char const *name;
+ int saved;
+ struct saved_cwd saved_cwd;
+};
+
+/* A vector of chdir targets. wd[0] is the initial working directory. */
+static struct wd *wd;
+
+/* The number of working directories in the vector. */
+static size_t wds;
+
+/* The allocated size of the vector. */
+static size_t wd_alloc;
+
+/* DIR is the operand of a -C option; add it to vector of chdir targets,
+ and return the index of its location. */
+int
+chdir_arg (char const *dir)
+{
+ if (wds == wd_alloc)
+ {
+ wd_alloc = 2 * (wd_alloc + 1);
+ wd = xrealloc (wd, sizeof *wd * wd_alloc);
+ if (! wds)
+ {
+ wd[wds].name = ".";
+ wd[wds].saved = 0;
+ wds++;
+ }
+ }
+
+ /* Optimize the common special case of the working directory,
+ or the working directory as a prefix. */
+ if (dir[0])
+ {
+ while (dir[0] == '.' && ISSLASH (dir[1]))
+ for (dir += 2; ISSLASH (*dir); dir++)
+ continue;
+ if (! dir[dir[0] == '.'])
+ return wds - 1;
+ }
+
+ wd[wds].name = dir;
+ wd[wds].saved = 0;
+ return wds++;
+}
+
+/* Change to directory I. If I is 0, change to the initial working
+ directory; otherwise, I must be a value returned by chdir_arg. */
+void
+chdir_do (int i)
+{
+ static int previous;
+
+ if (previous != i)
+ {
+ struct wd *prev = &wd[previous];
+ struct wd *curr = &wd[i];
+
+ if (! prev->saved)
+ {
+ prev->saved = 1;
+ if (save_cwd (&prev->saved_cwd) != 0)
+ FATAL_ERROR ((0, 0, _("Cannot save working directory")));
+ }
+
+ if (curr->saved)
+ {
+ if (restore_cwd (&curr->saved_cwd))
+ FATAL_ERROR ((0, 0, _("Cannot change working directory")));
+ }
+ else
+ {
+ if (i && ! ISSLASH (curr->name[0]))
+ chdir_do (i - 1);
+ if (chdir (curr->name) != 0)
+ chdir_fatal (curr->name);
+ }
+
+ previous = i;