vma-iter: Add support for Android.
[gnulib.git] / lib / symlinkat.c
blob7d58cd974c6808b8a4fec5e1cb9454c44315c423
1 /* Create a symlink relative to an open directory.
2 Copyright (C) 2009-2019 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 3 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
17 /* written by Eric Blake */
19 #include <config.h>
21 #include <unistd.h>
22 #include <errno.h>
24 #if HAVE_SYMLINKAT
25 # undef symlinkat
27 #include <sys/stat.h>
28 #include <string.h>
30 /* Create a symlink, but reject trailing slash. */
31 int
32 rpl_symlinkat (char const *contents, int fd, char const *name)
34 size_t len = strlen (name);
35 if (len && name[len - 1] == '/')
37 struct stat st;
38 if (fstatat (fd, name, &st, 0) == 0)
39 errno = EEXIST;
40 return -1;
42 return symlinkat (contents, fd, name);
45 #elif !HAVE_SYMLINK
46 /* Mingw lacks symlink, and it is more efficient to provide a trivial
47 wrapper than to go through at-func.c to call rpl_symlink. */
49 int
50 symlinkat (char const *path1 _GL_UNUSED, int fd _GL_UNUSED,
51 char const *path2 _GL_UNUSED)
53 errno = ENOSYS;
54 return -1;
57 #else /* HAVE_SYMLINK */
59 /* Our openat helper functions expect the directory parameter first,
60 not second. These shims make life easier. */
62 /* Like symlink, but with arguments reversed. */
63 static int
64 symlink_reversed (char const *file, char const *contents)
66 return symlink (contents, file);
69 /* Like symlinkat, but with arguments reversed. */
71 static int
72 symlinkat_reversed (int fd, char const *file, char const *contents);
74 # define AT_FUNC_NAME symlinkat_reversed
75 # define AT_FUNC_F1 symlink_reversed
76 # define AT_FUNC_POST_FILE_PARAM_DECLS , char const *contents
77 # define AT_FUNC_POST_FILE_ARGS , contents
78 # include "at-func.c"
79 # undef AT_FUNC_NAME
80 # undef AT_FUNC_F1
81 # undef AT_FUNC_POST_FILE_PARAM_DECLS
82 # undef AT_FUNC_POST_FILE_ARGS
84 /* Create a symlink FILE, in the directory open on descriptor FD,
85 holding CONTENTS. If possible, do it without changing the
86 working directory. Otherwise, resort to using save_cwd/fchdir,
87 then symlink/restore_cwd. If either the save_cwd or the restore_cwd
88 fails, then give a diagnostic and exit nonzero. */
90 int
91 symlinkat (char const *contents, int fd, char const *file)
93 return symlinkat_reversed (fd, file, contents);
96 #endif /* HAVE_SYMLINK */