1 #include "../git-compat-util.h"
5 unsigned int _CRT_fmode
= _O_BINARY
;
8 int mingw_open (const char *filename
, int oflags
, ...)
12 va_start(args
, oflags
);
13 mode
= va_arg(args
, int);
16 if (!strcmp(filename
, "/dev/null"))
18 int fd
= open(filename
, oflags
, mode
);
19 if (fd
< 0 && (oflags
& O_CREAT
) && errno
== EACCES
) {
20 DWORD attrs
= GetFileAttributes(filename
);
21 if (attrs
!= INVALID_FILE_ATTRIBUTES
&& (attrs
& FILE_ATTRIBUTE_DIRECTORY
))
27 static inline time_t filetime_to_time_t(const FILETIME
*ft
)
29 long long winTime
= ((long long)ft
->dwHighDateTime
<< 32) + ft
->dwLowDateTime
;
30 winTime
-= 116444736000000000LL; /* Windows to Unix Epoch conversion */
31 winTime
/= 10000000; /* Nano to seconds resolution */
32 return (time_t)winTime
;
35 /* We keep the do_lstat code in a separate function to avoid recursion.
36 * When a path ends with a slash, the stat will fail with ENOENT. In
37 * this case, we strip the trailing slashes and stat again.
39 static int do_lstat(const char *file_name
, struct stat
*buf
)
41 WIN32_FILE_ATTRIBUTE_DATA fdata
;
43 if (!(errno
= get_file_attr(file_name
, &fdata
))) {
48 buf
->st_mode
= file_attr_to_st_mode(fdata
.dwFileAttributes
);
49 buf
->st_size
= fdata
.nFileSizeLow
|
50 (((off_t
)fdata
.nFileSizeHigh
)<<32);
51 buf
->st_dev
= buf
->st_rdev
= 0; /* not used by Git */
52 buf
->st_atime
= filetime_to_time_t(&(fdata
.ftLastAccessTime
));
53 buf
->st_mtime
= filetime_to_time_t(&(fdata
.ftLastWriteTime
));
54 buf
->st_ctime
= filetime_to_time_t(&(fdata
.ftCreationTime
));
60 /* We provide our own lstat/fstat functions, since the provided
61 * lstat/fstat functions are so slow. These stat functions are
62 * tailored for Git's usage (read: fast), and are not meant to be
63 * complete. Note that Git stat()s are redirected to mingw_lstat()
64 * too, since Windows doesn't really handle symlinks that well.
66 int mingw_lstat(const char *file_name
, struct stat
*buf
)
69 static char alt_name
[PATH_MAX
];
71 if (!do_lstat(file_name
, buf
))
74 /* if file_name ended in a '/', Windows returned ENOENT;
75 * try again without trailing slashes
80 namelen
= strlen(file_name
);
81 if (namelen
&& file_name
[namelen
-1] != '/')
83 while (namelen
&& file_name
[namelen
-1] == '/')
85 if (!namelen
|| namelen
>= PATH_MAX
)
88 memcpy(alt_name
, file_name
, namelen
);
89 alt_name
[namelen
] = 0;
90 return do_lstat(alt_name
, buf
);
94 int mingw_fstat(int fd
, struct stat
*buf
)
96 HANDLE fh
= (HANDLE
)_get_osfhandle(fd
);
97 BY_HANDLE_FILE_INFORMATION fdata
;
99 if (fh
== INVALID_HANDLE_VALUE
) {
103 /* direct non-file handles to MS's fstat() */
104 if (GetFileType(fh
) != FILE_TYPE_DISK
)
105 return _fstati64(fd
, buf
);
107 if (GetFileInformationByHandle(fh
, &fdata
)) {
112 buf
->st_mode
= file_attr_to_st_mode(fdata
.dwFileAttributes
);
113 buf
->st_size
= fdata
.nFileSizeLow
|
114 (((off_t
)fdata
.nFileSizeHigh
)<<32);
115 buf
->st_dev
= buf
->st_rdev
= 0; /* not used by Git */
116 buf
->st_atime
= filetime_to_time_t(&(fdata
.ftLastAccessTime
));
117 buf
->st_mtime
= filetime_to_time_t(&(fdata
.ftLastWriteTime
));
118 buf
->st_ctime
= filetime_to_time_t(&(fdata
.ftCreationTime
));
125 static inline void time_t_to_filetime(time_t t
, FILETIME
*ft
)
127 long long winTime
= t
* 10000000LL + 116444736000000000LL;
128 ft
->dwLowDateTime
= winTime
;
129 ft
->dwHighDateTime
= winTime
>> 32;
132 int mingw_utime (const char *file_name
, const struct utimbuf
*times
)
137 /* must have write permission */
138 if ((fh
= open(file_name
, O_RDWR
| O_BINARY
)) < 0)
141 time_t_to_filetime(times
->modtime
, &mft
);
142 time_t_to_filetime(times
->actime
, &aft
);
143 if (!SetFileTime((HANDLE
)_get_osfhandle(fh
), NULL
, &aft
, &mft
)) {
152 unsigned int sleep (unsigned int seconds
)
158 int mkstemp(char *template)
160 char *filename
= mktemp(template);
161 if (filename
== NULL
)
163 return open(filename
, O_RDWR
| O_CREAT
, 0600);
166 int gettimeofday(struct timeval
*tv
, void *tz
)
171 tm
.tm_year
= st
.wYear
-1900;
172 tm
.tm_mon
= st
.wMonth
-1;
173 tm
.tm_mday
= st
.wDay
;
174 tm
.tm_hour
= st
.wHour
;
175 tm
.tm_min
= st
.wMinute
;
176 tm
.tm_sec
= st
.wSecond
;
177 tv
->tv_sec
= tm_to_time_t(&tm
);
180 tv
->tv_usec
= st
.wMilliseconds
*1000;
184 int pipe(int filedes
[2])
189 if (_pipe(filedes
, 8192, 0) < 0)
192 parent
= GetCurrentProcess();
194 if (!DuplicateHandle (parent
, (HANDLE
)_get_osfhandle(filedes
[0]),
195 parent
, &h
[0], 0, FALSE
, DUPLICATE_SAME_ACCESS
)) {
200 if (!DuplicateHandle (parent
, (HANDLE
)_get_osfhandle(filedes
[1]),
201 parent
, &h
[1], 0, FALSE
, DUPLICATE_SAME_ACCESS
)) {
207 fd
= _open_osfhandle((int)h
[0], O_NOINHERIT
);
217 fd
= _open_osfhandle((int)h
[1], O_NOINHERIT
);
229 int poll(struct pollfd
*ufds
, unsigned int nfds
, int timeout
)
238 return errno
= EINVAL
, error("poll timeout not supported");
241 /* When there is only one fd to wait for, then we pretend that
242 * input is available and let the actual wait happen when the
243 * caller invokes read().
246 if (!(ufds
[0].events
& POLLIN
))
247 return errno
= EINVAL
, error("POLLIN not set");
248 ufds
[0].revents
= POLLIN
;
254 for (i
= 0; i
< nfds
; i
++) {
256 HANDLE h
= (HANDLE
) _get_osfhandle(ufds
[i
].fd
);
257 if (h
== INVALID_HANDLE_VALUE
)
258 return -1; /* errno was set */
260 if (!(ufds
[i
].events
& POLLIN
))
261 return errno
= EINVAL
, error("POLLIN not set");
263 /* this emulation works only for pipes */
264 if (!PeekNamedPipe(h
, NULL
, 0, NULL
, &avail
, NULL
)) {
265 int err
= GetLastError();
266 if (err
== ERROR_BROKEN_PIPE
) {
267 ufds
[i
].revents
= POLLHUP
;
271 return error("PeekNamedPipe failed,"
272 " GetLastError: %u", err
);
275 ufds
[i
].revents
= POLLIN
;
281 /* The only times that we spin here is when the process
282 * that is connected through the pipes is waiting for
283 * its own input data to become available. But since
284 * the process (pack-objects) is itself CPU intensive,
285 * it will happily pick up the time slice that we are
286 * relinguishing here.
294 struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
)
296 /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
297 memcpy(result
, gmtime(timep
), sizeof(struct tm
));
301 struct tm
*localtime_r(const time_t *timep
, struct tm
*result
)
303 /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
304 memcpy(result
, localtime(timep
), sizeof(struct tm
));
309 char *mingw_getcwd(char *pointer
, int len
)
312 char *ret
= getcwd(pointer
, len
);
315 for (i
= 0; pointer
[i
]; i
++)
316 if (pointer
[i
] == '\\')
322 char *mingw_getenv(const char *name
)
324 char *result
= getenv(name
);
325 if (!result
&& !strcmp(name
, "TMPDIR")) {
326 /* on Windows it is TMP and TEMP */
327 result
= getenv("TMP");
329 result
= getenv("TEMP");
335 * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
336 * (Parsing C++ Command-Line Arguments)
338 static const char *quote_arg(const char *arg
)
340 /* count chars to quote */
342 int force_quotes
= 0;
345 if (!*p
) force_quotes
= 1;
347 if (isspace(*p
) || *p
== '*' || *p
== '?' || *p
== '{')
351 else if (*p
== '\\') {
365 if (!force_quotes
&& n
== 0)
368 /* insert \ where necessary */
369 d
= q
= xmalloc(len
+n
+3);
374 else if (*arg
== '\\') {
376 while (*arg
== '\\') {
393 static const char *parse_interpreter(const char *cmd
)
395 static char buf
[100];
399 /* don't even try a .exe */
401 if (n
>= 4 && !strcasecmp(cmd
+n
-4, ".exe"))
404 fd
= open(cmd
, O_RDONLY
);
407 n
= read(fd
, buf
, sizeof(buf
)-1);
409 if (n
< 4) /* at least '#!/x' and not error */
412 if (buf
[0] != '#' || buf
[1] != '!')
415 p
= buf
+ strcspn(buf
, "\r\n");
420 if (!(p
= strrchr(buf
+2, '/')) && !(p
= strrchr(buf
+2, '\\')))
423 if ((opt
= strchr(p
+1, ' ')))
429 * Splits the PATH into parts.
431 static char **get_path_split(void)
433 char *p
, **path
, *envpath
= getenv("PATH");
436 if (!envpath
|| !*envpath
)
439 envpath
= xstrdup(envpath
);
445 if (*dir
) { /* not earlier, catches series of ; */
452 path
= xmalloc((n
+1)*sizeof(char*));
457 path
[i
++] = xstrdup(p
);
467 static void free_path_split(char **path
)
479 * exe_only means that we only want to detect .exe files, but not scripts
480 * (which do not have an extension)
482 static char *lookup_prog(const char *dir
, const char *cmd
, int isexe
, int exe_only
)
485 snprintf(path
, sizeof(path
), "%s/%s.exe", dir
, cmd
);
487 if (!isexe
&& access(path
, F_OK
) == 0)
488 return xstrdup(path
);
489 path
[strlen(path
)-4] = '\0';
490 if ((!exe_only
|| isexe
) && access(path
, F_OK
) == 0)
491 if (!(GetFileAttributes(path
) & FILE_ATTRIBUTE_DIRECTORY
))
492 return xstrdup(path
);
497 * Determines the absolute path of cmd using the the split path in path.
498 * If cmd contains a slash or backslash, no lookup is performed.
500 static char *path_lookup(const char *cmd
, char **path
, int exe_only
)
503 int len
= strlen(cmd
);
504 int isexe
= len
>= 4 && !strcasecmp(cmd
+len
-4, ".exe");
506 if (strchr(cmd
, '/') || strchr(cmd
, '\\'))
509 while (!prog
&& *path
)
510 prog
= lookup_prog(*path
++, cmd
, isexe
, exe_only
);
515 static int env_compare(const void *a
, const void *b
)
519 return strcasecmp(*ea
, *eb
);
522 static pid_t
mingw_spawnve(const char *cmd
, const char **argv
, char **env
,
526 PROCESS_INFORMATION pi
;
527 struct strbuf envblk
, args
;
531 /* Determine whether or not we are associated to a console */
532 HANDLE cons
= CreateFile("CONOUT$", GENERIC_WRITE
,
533 FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
534 FILE_ATTRIBUTE_NORMAL
, NULL
);
535 if (cons
== INVALID_HANDLE_VALUE
) {
536 /* There is no console associated with this process.
537 * Since the child is a console process, Windows
538 * would normally create a console window. But
539 * since we'll be redirecting std streams, we do
540 * not need the console.
541 * It is necessary to use DETACHED_PROCESS
542 * instead of CREATE_NO_WINDOW to make ssh
543 * recognize that it has no console.
545 flags
= DETACHED_PROCESS
;
547 /* There is already a console. If we specified
548 * DETACHED_PROCESS here, too, Windows would
549 * disassociate the child from the console.
550 * The same is true for CREATE_NO_WINDOW.
556 memset(&si
, 0, sizeof(si
));
558 si
.dwFlags
= STARTF_USESTDHANDLES
;
559 si
.hStdInput
= (HANDLE
) _get_osfhandle(0);
560 si
.hStdOutput
= (HANDLE
) _get_osfhandle(1);
561 si
.hStdError
= (HANDLE
) _get_osfhandle(2);
563 /* concatenate argv, quoting args as we go */
564 strbuf_init(&args
, 0);
566 char *quoted
= (char *)quote_arg(cmd
);
567 strbuf_addstr(&args
, quoted
);
571 for (; *argv
; argv
++) {
572 char *quoted
= (char *)quote_arg(*argv
);
574 strbuf_addch(&args
, ' ');
575 strbuf_addstr(&args
, quoted
);
582 char **e
, **sorted_env
;
584 for (e
= env
; *e
; e
++)
587 /* environment must be sorted */
588 sorted_env
= xmalloc(sizeof(*sorted_env
) * (count
+ 1));
589 memcpy(sorted_env
, env
, sizeof(*sorted_env
) * (count
+ 1));
590 qsort(sorted_env
, count
, sizeof(*sorted_env
), env_compare
);
592 strbuf_init(&envblk
, 0);
593 for (e
= sorted_env
; *e
; e
++) {
594 strbuf_addstr(&envblk
, *e
);
595 strbuf_addch(&envblk
, '\0');
600 memset(&pi
, 0, sizeof(pi
));
601 ret
= CreateProcess(cmd
, args
.buf
, NULL
, NULL
, TRUE
, flags
,
602 env
? envblk
.buf
: NULL
, NULL
, &si
, &pi
);
605 strbuf_release(&envblk
);
606 strbuf_release(&args
);
612 CloseHandle(pi
.hThread
);
613 return (pid_t
)pi
.hProcess
;
616 pid_t
mingw_spawnvpe(const char *cmd
, const char **argv
, char **env
)
619 char **path
= get_path_split();
620 char *prog
= path_lookup(cmd
, path
, 0);
627 const char *interpr
= parse_interpreter(prog
);
630 const char *argv0
= argv
[0];
631 char *iprog
= path_lookup(interpr
, path
, 1);
638 pid
= mingw_spawnve(iprog
, argv
, env
, 1);
644 pid
= mingw_spawnve(prog
, argv
, env
, 0);
647 free_path_split(path
);
651 static int try_shell_exec(const char *cmd
, char *const *argv
, char **env
)
653 const char *interpr
= parse_interpreter(cmd
);
660 path
= get_path_split();
661 prog
= path_lookup(interpr
, path
, 1);
665 while (argv
[argc
]) argc
++;
666 argv2
= xmalloc(sizeof(*argv
) * (argc
+1));
667 argv2
[0] = (char *)cmd
; /* full path to the script file */
668 memcpy(&argv2
[1], &argv
[1], sizeof(*argv
) * argc
);
669 pid
= mingw_spawnve(prog
, argv2
, env
, 1);
672 if (waitpid(pid
, &status
, 0) < 0)
676 pid
= 1; /* indicate that we tried but failed */
680 free_path_split(path
);
684 static void mingw_execve(const char *cmd
, char *const *argv
, char *const *env
)
686 /* check if git_command is a shell script */
687 if (!try_shell_exec(cmd
, argv
, (char **)env
)) {
690 pid
= mingw_spawnve(cmd
, (const char **)argv
, (char **)env
, 0);
693 if (waitpid(pid
, &status
, 0) < 0)
699 void mingw_execvp(const char *cmd
, char *const *argv
)
701 char **path
= get_path_split();
702 char *prog
= path_lookup(cmd
, path
, 0);
705 mingw_execve(prog
, argv
, environ
);
710 free_path_split(path
);
713 char **copy_environ()
719 env
= xmalloc((i
+1)*sizeof(*env
));
720 for (i
= 0; environ
[i
]; i
++)
721 env
[i
] = xstrdup(environ
[i
]);
726 void free_environ(char **env
)
729 for (i
= 0; env
[i
]; i
++)
734 static int lookup_env(char **env
, const char *name
, size_t nmln
)
738 for (i
= 0; env
[i
]; i
++) {
739 if (0 == strncmp(env
[i
], name
, nmln
)
740 && '=' == env
[i
][nmln
])
748 * If name contains '=', then sets the variable, otherwise it unsets it
750 char **env_setenv(char **env
, const char *name
)
752 char *eq
= strchrnul(name
, '=');
753 int i
= lookup_env(env
, name
, eq
-name
);
757 for (i
= 0; env
[i
]; i
++)
759 env
= xrealloc(env
, (i
+2)*sizeof(*env
));
760 env
[i
] = xstrdup(name
);
767 env
[i
] = xstrdup(name
);
775 /* this is the first function to call into WS_32; initialize it */
777 struct hostent
*mingw_gethostbyname(const char *host
)
781 if (WSAStartup(MAKEWORD(2,2), &wsa
))
782 die("unable to initialize winsock subsystem, error %d",
784 atexit((void(*)(void)) WSACleanup
);
785 return gethostbyname(host
);
788 int mingw_socket(int domain
, int type
, int protocol
)
791 SOCKET s
= WSASocket(domain
, type
, protocol
, NULL
, 0, 0);
792 if (s
== INVALID_SOCKET
) {
794 * WSAGetLastError() values are regular BSD error codes
795 * biased by WSABASEERR.
796 * However, strerror() does not know about networking
797 * specific errors, which are values beginning at 38 or so.
798 * Therefore, we choose to leave the biased error code
799 * in errno so that _if_ someone looks up the code somewhere,
800 * then it is at least the number that are usually listed.
802 errno
= WSAGetLastError();
805 /* convert into a file descriptor */
806 if ((sockfd
= _open_osfhandle(s
, O_RDWR
|O_BINARY
)) < 0) {
808 return error("unable to make a socket file descriptor: %s",
815 int mingw_connect(int sockfd
, struct sockaddr
*sa
, size_t sz
)
817 SOCKET s
= (SOCKET
)_get_osfhandle(sockfd
);
818 return connect(s
, sa
, sz
);
822 int mingw_rename(const char *pold
, const char *pnew
)
827 * Try native rename() first to get errno right.
828 * It is based on MoveFile(), which cannot overwrite existing files.
830 if (!rename(pold
, pnew
))
834 if (MoveFileEx(pold
, pnew
, MOVEFILE_REPLACE_EXISTING
))
836 /* TODO: translate more errors */
837 if (GetLastError() == ERROR_ACCESS_DENIED
&&
838 (attrs
= GetFileAttributes(pnew
)) != INVALID_FILE_ATTRIBUTES
) {
839 if (attrs
& FILE_ATTRIBUTE_DIRECTORY
) {
843 if ((attrs
& FILE_ATTRIBUTE_READONLY
) &&
844 SetFileAttributes(pnew
, attrs
& ~FILE_ATTRIBUTE_READONLY
)) {
845 if (MoveFileEx(pold
, pnew
, MOVEFILE_REPLACE_EXISTING
))
847 /* revert file attributes on failure */
848 SetFileAttributes(pnew
, attrs
);
855 struct passwd
*getpwuid(int uid
)
857 static char user_name
[100];
858 static struct passwd p
;
860 DWORD len
= sizeof(user_name
);
861 if (!GetUserName(user_name
, &len
))
863 p
.pw_name
= user_name
;
864 p
.pw_gecos
= "unknown";
869 static HANDLE timer_event
;
870 static HANDLE timer_thread
;
871 static int timer_interval
;
873 static sig_handler_t timer_fn
= SIG_DFL
;
875 /* The timer works like this:
876 * The thread, ticktack(), is a trivial routine that most of the time
877 * only waits to receive the signal to terminate. The main thread tells
878 * the thread to terminate by setting the timer_event to the signalled
880 * But ticktack() interrupts the wait state after the timer's interval
881 * length to call the signal handler.
884 static __stdcall
unsigned ticktack(void *dummy
)
886 while (WaitForSingleObject(timer_event
, timer_interval
) == WAIT_TIMEOUT
) {
887 if (timer_fn
== SIG_DFL
)
889 if (timer_fn
!= SIG_IGN
)
897 static int start_timer_thread(void)
899 timer_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
901 timer_thread
= (HANDLE
) _beginthreadex(NULL
, 0, ticktack
, NULL
, 0, NULL
);
903 return errno
= ENOMEM
,
904 error("cannot start timer thread");
906 return errno
= ENOMEM
,
907 error("cannot allocate resources for timer");
911 static void stop_timer_thread(void)
914 SetEvent(timer_event
); /* tell thread to terminate */
916 int rc
= WaitForSingleObject(timer_thread
, 1000);
917 if (rc
== WAIT_TIMEOUT
)
918 error("timer thread did not terminate timely");
919 else if (rc
!= WAIT_OBJECT_0
)
920 error("waiting for timer thread failed: %lu",
922 CloseHandle(timer_thread
);
925 CloseHandle(timer_event
);
930 static inline int is_timeval_eq(const struct timeval
*i1
, const struct timeval
*i2
)
932 return i1
->tv_sec
== i2
->tv_sec
&& i1
->tv_usec
== i2
->tv_usec
;
935 int setitimer(int type
, struct itimerval
*in
, struct itimerval
*out
)
937 static const struct timeval zero
;
938 static int atexit_done
;
941 return errno
= EINVAL
,
942 error("setitimer param 3 != NULL not implemented");
943 if (!is_timeval_eq(&in
->it_interval
, &zero
) &&
944 !is_timeval_eq(&in
->it_interval
, &in
->it_value
))
945 return errno
= EINVAL
,
946 error("setitimer: it_interval must be zero or eq it_value");
951 if (is_timeval_eq(&in
->it_value
, &zero
) &&
952 is_timeval_eq(&in
->it_interval
, &zero
))
955 timer_interval
= in
->it_value
.tv_sec
* 1000 + in
->it_value
.tv_usec
/ 1000;
956 one_shot
= is_timeval_eq(&in
->it_interval
, &zero
);
958 atexit(stop_timer_thread
);
961 return start_timer_thread();
964 int sigaction(int sig
, struct sigaction
*in
, struct sigaction
*out
)
967 return errno
= EINVAL
,
968 error("sigaction only implemented for SIGALRM");
970 return errno
= EINVAL
,
971 error("sigaction: param 3 != NULL not implemented");
973 timer_fn
= in
->sa_handler
;
978 sig_handler_t
mingw_signal(int sig
, sig_handler_t handler
)
981 return signal(sig
, handler
);
982 sig_handler_t old
= timer_fn
;
987 static const char *make_backslash_path(const char *path
)
989 static char buf
[PATH_MAX
+ 1];
992 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
)
993 die("Too long path: %.*s", 60, path
);
995 for (c
= buf
; *c
; c
++) {
1002 void mingw_open_html(const char *unixpath
)
1004 const char *htmlpath
= make_backslash_path(unixpath
);
1005 printf("Launching default browser to display HTML ...\n");
1006 ShellExecute(NULL
, "open", htmlpath
, NULL
, "\\", 0);
1009 static int err_win_to_posix(DWORD winerr
)
1013 case ERROR_ACCESS_DENIED
: error
= EACCES
; break;
1014 case ERROR_ACCOUNT_DISABLED
: error
= EACCES
; break;
1015 case ERROR_ACCOUNT_RESTRICTION
: error
= EACCES
; break;
1016 case ERROR_ALREADY_ASSIGNED
: error
= EBUSY
; break;
1017 case ERROR_ALREADY_EXISTS
: error
= EEXIST
; break;
1018 case ERROR_ARITHMETIC_OVERFLOW
: error
= ERANGE
; break;
1019 case ERROR_BAD_COMMAND
: error
= EIO
; break;
1020 case ERROR_BAD_DEVICE
: error
= ENODEV
; break;
1021 case ERROR_BAD_DRIVER_LEVEL
: error
= ENXIO
; break;
1022 case ERROR_BAD_EXE_FORMAT
: error
= ENOEXEC
; break;
1023 case ERROR_BAD_FORMAT
: error
= ENOEXEC
; break;
1024 case ERROR_BAD_LENGTH
: error
= EINVAL
; break;
1025 case ERROR_BAD_PATHNAME
: error
= ENOENT
; break;
1026 case ERROR_BAD_PIPE
: error
= EPIPE
; break;
1027 case ERROR_BAD_UNIT
: error
= ENODEV
; break;
1028 case ERROR_BAD_USERNAME
: error
= EINVAL
; break;
1029 case ERROR_BROKEN_PIPE
: error
= EPIPE
; break;
1030 case ERROR_BUFFER_OVERFLOW
: error
= ENAMETOOLONG
; break;
1031 case ERROR_BUSY
: error
= EBUSY
; break;
1032 case ERROR_BUSY_DRIVE
: error
= EBUSY
; break;
1033 case ERROR_CALL_NOT_IMPLEMENTED
: error
= ENOSYS
; break;
1034 case ERROR_CANNOT_MAKE
: error
= EACCES
; break;
1035 case ERROR_CANTOPEN
: error
= EIO
; break;
1036 case ERROR_CANTREAD
: error
= EIO
; break;
1037 case ERROR_CANTWRITE
: error
= EIO
; break;
1038 case ERROR_CRC
: error
= EIO
; break;
1039 case ERROR_CURRENT_DIRECTORY
: error
= EACCES
; break;
1040 case ERROR_DEVICE_IN_USE
: error
= EBUSY
; break;
1041 case ERROR_DEV_NOT_EXIST
: error
= ENODEV
; break;
1042 case ERROR_DIRECTORY
: error
= EINVAL
; break;
1043 case ERROR_DIR_NOT_EMPTY
: error
= ENOTEMPTY
; break;
1044 case ERROR_DISK_CHANGE
: error
= EIO
; break;
1045 case ERROR_DISK_FULL
: error
= ENOSPC
; break;
1046 case ERROR_DRIVE_LOCKED
: error
= EBUSY
; break;
1047 case ERROR_ENVVAR_NOT_FOUND
: error
= EINVAL
; break;
1048 case ERROR_EXE_MARKED_INVALID
: error
= ENOEXEC
; break;
1049 case ERROR_FILENAME_EXCED_RANGE
: error
= ENAMETOOLONG
; break;
1050 case ERROR_FILE_EXISTS
: error
= EEXIST
; break;
1051 case ERROR_FILE_INVALID
: error
= ENODEV
; break;
1052 case ERROR_FILE_NOT_FOUND
: error
= ENOENT
; break;
1053 case ERROR_GEN_FAILURE
: error
= EIO
; break;
1054 case ERROR_HANDLE_DISK_FULL
: error
= ENOSPC
; break;
1055 case ERROR_INSUFFICIENT_BUFFER
: error
= ENOMEM
; break;
1056 case ERROR_INVALID_ACCESS
: error
= EACCES
; break;
1057 case ERROR_INVALID_ADDRESS
: error
= EFAULT
; break;
1058 case ERROR_INVALID_BLOCK
: error
= EFAULT
; break;
1059 case ERROR_INVALID_DATA
: error
= EINVAL
; break;
1060 case ERROR_INVALID_DRIVE
: error
= ENODEV
; break;
1061 case ERROR_INVALID_EXE_SIGNATURE
: error
= ENOEXEC
; break;
1062 case ERROR_INVALID_FLAGS
: error
= EINVAL
; break;
1063 case ERROR_INVALID_FUNCTION
: error
= ENOSYS
; break;
1064 case ERROR_INVALID_HANDLE
: error
= EBADF
; break;
1065 case ERROR_INVALID_LOGON_HOURS
: error
= EACCES
; break;
1066 case ERROR_INVALID_NAME
: error
= EINVAL
; break;
1067 case ERROR_INVALID_OWNER
: error
= EINVAL
; break;
1068 case ERROR_INVALID_PARAMETER
: error
= EINVAL
; break;
1069 case ERROR_INVALID_PASSWORD
: error
= EPERM
; break;
1070 case ERROR_INVALID_PRIMARY_GROUP
: error
= EINVAL
; break;
1071 case ERROR_INVALID_SIGNAL_NUMBER
: error
= EINVAL
; break;
1072 case ERROR_INVALID_TARGET_HANDLE
: error
= EIO
; break;
1073 case ERROR_INVALID_WORKSTATION
: error
= EACCES
; break;
1074 case ERROR_IO_DEVICE
: error
= EIO
; break;
1075 case ERROR_IO_INCOMPLETE
: error
= EINTR
; break;
1076 case ERROR_LOCKED
: error
= EBUSY
; break;
1077 case ERROR_LOCK_VIOLATION
: error
= EACCES
; break;
1078 case ERROR_LOGON_FAILURE
: error
= EACCES
; break;
1079 case ERROR_MAPPED_ALIGNMENT
: error
= EINVAL
; break;
1080 case ERROR_META_EXPANSION_TOO_LONG
: error
= E2BIG
; break;
1081 case ERROR_MORE_DATA
: error
= EPIPE
; break;
1082 case ERROR_NEGATIVE_SEEK
: error
= ESPIPE
; break;
1083 case ERROR_NOACCESS
: error
= EFAULT
; break;
1084 case ERROR_NONE_MAPPED
: error
= EINVAL
; break;
1085 case ERROR_NOT_ENOUGH_MEMORY
: error
= ENOMEM
; break;
1086 case ERROR_NOT_READY
: error
= EAGAIN
; break;
1087 case ERROR_NOT_SAME_DEVICE
: error
= EXDEV
; break;
1088 case ERROR_NO_DATA
: error
= EPIPE
; break;
1089 case ERROR_NO_MORE_SEARCH_HANDLES
: error
= EIO
; break;
1090 case ERROR_NO_PROC_SLOTS
: error
= EAGAIN
; break;
1091 case ERROR_NO_SUCH_PRIVILEGE
: error
= EACCES
; break;
1092 case ERROR_OPEN_FAILED
: error
= EIO
; break;
1093 case ERROR_OPEN_FILES
: error
= EBUSY
; break;
1094 case ERROR_OPERATION_ABORTED
: error
= EINTR
; break;
1095 case ERROR_OUTOFMEMORY
: error
= ENOMEM
; break;
1096 case ERROR_PASSWORD_EXPIRED
: error
= EACCES
; break;
1097 case ERROR_PATH_BUSY
: error
= EBUSY
; break;
1098 case ERROR_PATH_NOT_FOUND
: error
= ENOENT
; break;
1099 case ERROR_PIPE_BUSY
: error
= EBUSY
; break;
1100 case ERROR_PIPE_CONNECTED
: error
= EPIPE
; break;
1101 case ERROR_PIPE_LISTENING
: error
= EPIPE
; break;
1102 case ERROR_PIPE_NOT_CONNECTED
: error
= EPIPE
; break;
1103 case ERROR_PRIVILEGE_NOT_HELD
: error
= EACCES
; break;
1104 case ERROR_READ_FAULT
: error
= EIO
; break;
1105 case ERROR_SEEK
: error
= EIO
; break;
1106 case ERROR_SEEK_ON_DEVICE
: error
= ESPIPE
; break;
1107 case ERROR_SHARING_BUFFER_EXCEEDED
: error
= ENFILE
; break;
1108 case ERROR_SHARING_VIOLATION
: error
= EACCES
; break;
1109 case ERROR_STACK_OVERFLOW
: error
= ENOMEM
; break;
1110 case ERROR_SWAPERROR
: error
= ENOENT
; break;
1111 case ERROR_TOO_MANY_MODULES
: error
= EMFILE
; break;
1112 case ERROR_TOO_MANY_OPEN_FILES
: error
= EMFILE
; break;
1113 case ERROR_UNRECOGNIZED_MEDIA
: error
= ENXIO
; break;
1114 case ERROR_UNRECOGNIZED_VOLUME
: error
= ENODEV
; break;
1115 case ERROR_WAIT_NO_CHILDREN
: error
= ECHILD
; break;
1116 case ERROR_WRITE_FAULT
: error
= EIO
; break;
1117 case ERROR_WRITE_PROTECT
: error
= EROFS
; break;
1123 int link(const char *oldpath
, const char *newpath
)
1125 typedef BOOL (*T
)(const char*, const char*, LPSECURITY_ATTRIBUTES
);
1126 static T create_hard_link
= NULL
;
1127 if (!create_hard_link
) {
1128 create_hard_link
= (T
) GetProcAddress(
1129 GetModuleHandle("kernel32.dll"), "CreateHardLinkA");
1130 if (!create_hard_link
)
1131 create_hard_link
= (T
)-1;
1133 if (create_hard_link
== (T
)-1) {
1137 if (!create_hard_link(newpath
, oldpath
, NULL
)) {
1138 errno
= err_win_to_posix(GetLastError());