6c0556
commit a7e9dbb7742954814643a8562dcad09abb0b0e5d
6c0556
Author: Alexandra Hájková <ahajkova@redhat.com>
6c0556
Date:   Sat Dec 26 18:45:13 2020 +0100
6c0556
6c0556
    Add xchdir to libsupport.
6c0556
6c0556
diff --git a/support/Makefile b/support/Makefile
6c0556
index dcf3c4baa2a31070..fb95a69ed9158e78 100644
6c0556
--- a/support/Makefile
6c0556
+++ b/support/Makefile
6c0556
@@ -82,6 +82,7 @@ libsupport-routines = \
6c0556
   xasprintf \
6c0556
   xbind \
6c0556
   xcalloc \
6c0556
+  xchdir \
6c0556
   xchroot \
6c0556
   xclose \
6c0556
   xconnect \
6c0556
diff --git a/support/xchdir.c b/support/xchdir.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..beb4feff72832065
6c0556
--- /dev/null
6c0556
+++ b/support/xchdir.c
6c0556
@@ -0,0 +1,28 @@
6c0556
+/* chdir with error checking.
6c0556
+   Copyright (C) 2020 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#include <support/check.h>
6c0556
+#include <support/xunistd.h>
6c0556
+#include <unistd.h>
6c0556
+
6c0556
+void
6c0556
+xchdir (const char *path)
6c0556
+{
6c0556
+  if (chdir (path) != 0)
6c0556
+    FAIL_EXIT1 ("chdir (\"%s\"): %m", path);
6c0556
+}
6c0556
diff --git a/support/xunistd.h b/support/xunistd.h
6c0556
index f99f362cb4763c5b..74fd2771d12c36fe 100644
6c0556
--- a/support/xunistd.h
6c0556
+++ b/support/xunistd.h
6c0556
@@ -44,6 +44,7 @@ long xsysconf (int name);
6c0556
 long long xlseek (int fd, long long offset, int whence);
6c0556
 void xftruncate (int fd, long long length);
6c0556
 void xsymlink (const char *target, const char *linkpath);
6c0556
+void xchdir (const char *path);
6c0556
 
6c0556
 /* Equivalent of "mkdir -p".  */
6c0556
 void xmkdirp (const char *, mode_t);