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