2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* written by Jim Meyering */
31 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
35 #define _(msgid) gettext (msgid)
37 #include "openat-priv.h"
39 /* Solaris 10 has no function like this.
40 Create a subdirectory, FILE, with mode MODE, in the directory
41 open on descriptor FD. If possible, do it without changing the
42 working directory. Otherwise, resort to using save_cwd/fchdir,
43 then mkdir/restore_cwd. If either the save_cwd or the restore_cwd
44 fails, then give a diagnostic and exit nonzero. */
46 mkdirat (int fd, char const *file, mode_t mode)
48 struct saved_cwd saved_cwd;
52 if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
53 return mkdir (file, mode);
57 BUILD_PROC_NAME (proc_file, fd, file);
58 err = mkdir (proc_file, mode);
59 /* If the syscall succeeds, or if it fails with an unexpected
60 errno value, then return right away. Otherwise, fall through
61 and resort to using save_cwd/restore_cwd. */
62 if (0 <= err || ! EXPECTED_ERRNO (errno))
66 if (save_cwd (&saved_cwd) != 0)
67 openat_save_fail (errno);
72 free_cwd (&saved_cwd);
77 err = mkdir (file, mode);
78 saved_errno = (err < 0 ? errno : 0);
80 if (restore_cwd (&saved_cwd) != 0)
81 openat_restore_fail (errno);
83 free_cwd (&saved_cwd);