floor, ceil, trunc, truncf, truncl: Defeat GCC optimizations.
[gnulib.git] / lib / link.c
blobcff54a26befaba3718dea7b1cc5545e9fa33c176
1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <unistd.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
27 #if !HAVE_LINK
28 # if defined _WIN32 && ! defined __CYGWIN__
30 # define WIN32_LEAN_AND_MEAN
31 # include <windows.h>
33 /* Avoid warnings from gcc -Wcast-function-type. */
34 # define GetProcAddress \
35 (void *) GetProcAddress
37 /* CreateHardLink was introduced only in Windows 2000. */
38 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
39 LPCTSTR lpExistingFileName,
40 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
41 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
42 static BOOL initialized = FALSE;
44 static void
45 initialize (void)
47 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
48 if (kernel32 != NULL)
50 CreateHardLinkFunc =
51 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
53 initialized = TRUE;
56 int
57 link (const char *file1, const char *file2)
59 char *dir;
60 size_t len1 = strlen (file1);
61 size_t len2 = strlen (file2);
62 if (!initialized)
63 initialize ();
64 if (CreateHardLinkFunc == NULL)
66 /* System does not support hard links. */
67 errno = EPERM;
68 return -1;
70 /* Reject trailing slashes on non-directories; mingw does not
71 support hard-linking directories. */
72 if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
73 || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
75 struct stat st;
76 if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
77 errno = EPERM;
78 else
79 errno = ENOTDIR;
80 return -1;
82 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
83 that dirname(file2) exists. */
84 dir = strdup (file2);
85 if (!dir)
86 return -1;
88 struct stat st;
89 char *p = strchr (dir, '\0');
90 while (dir < p && (*--p != '/' && *p != '\\'));
91 *p = '\0';
92 if (p != dir && stat (dir, &st) == -1)
94 int saved_errno = errno;
95 free (dir);
96 errno = saved_errno;
97 return -1;
99 free (dir);
101 /* Now create the link. */
102 if (CreateHardLinkFunc (file2, file1, NULL) == 0)
104 /* It is not documented which errors CreateHardLink() can produce.
105 * The following conversions are based on tests on a Windows XP SP2
106 * system. */
107 DWORD err = GetLastError ();
108 switch (err)
110 case ERROR_ACCESS_DENIED:
111 errno = EACCES;
112 break;
114 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
115 errno = EPERM;
116 break;
118 case ERROR_NOT_SAME_DEVICE:
119 errno = EXDEV;
120 break;
122 case ERROR_PATH_NOT_FOUND:
123 case ERROR_FILE_NOT_FOUND:
124 errno = ENOENT;
125 break;
127 case ERROR_INVALID_PARAMETER:
128 errno = ENAMETOOLONG;
129 break;
131 case ERROR_TOO_MANY_LINKS:
132 errno = EMLINK;
133 break;
135 case ERROR_ALREADY_EXISTS:
136 errno = EEXIST;
137 break;
139 default:
140 errno = EIO;
142 return -1;
145 return 0;
148 # else /* !Windows */
150 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
152 # endif /* !Windows */
153 #else /* HAVE_LINK */
155 # undef link
157 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
159 rpl_link (char const *file1, char const *file2)
161 size_t len1;
162 size_t len2;
163 struct stat st;
165 /* Don't allow IRIX to dereference dangling file2 symlink. */
166 if (!lstat (file2, &st))
168 errno = EEXIST;
169 return -1;
172 /* Reject trailing slashes on non-directories. */
173 len1 = strlen (file1);
174 len2 = strlen (file2);
175 if ((len1 && file1[len1 - 1] == '/')
176 || (len2 && file2[len2 - 1] == '/'))
178 /* Let link() decide whether hard-linking directories is legal.
179 If stat() fails, then link() should fail for the same reason
180 (although on Solaris 9, link("file/","oops") mistakenly
181 succeeds); if stat() succeeds, require a directory. */
182 if (stat (file1, &st))
183 return -1;
184 if (!S_ISDIR (st.st_mode))
186 errno = ENOTDIR;
187 return -1;
190 else
192 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
193 char *dir = strdup (file2);
194 char *p;
195 if (!dir)
196 return -1;
197 /* We already know file2 does not end in slash. Strip off the
198 basename, then check that the dirname exists. */
199 p = strrchr (dir, '/');
200 if (p)
202 *p = '\0';
203 if (stat (dir, &st) == -1)
205 int saved_errno = errno;
206 free (dir);
207 errno = saved_errno;
208 return -1;
211 free (dir);
213 return link (file1, file2);
215 #endif /* HAVE_LINK */