2 // compat.cpp -- compatibilty layer to make bacula-fd run
3 // natively under windows
5 // Copyright (C)2004 Raider Solutions, Inc
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of
10 // the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public
18 // License along with this program; if not, write to the Free
19 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 // MA 02111-1307, USA.
22 // Author : Christopher S. Hull
23 // Created On : Sat Jan 31 15:55:00 2004
24 // Last Modified By: Christopher S. Hull
25 // Last Modified On: Sun Feb 22 16:08:54 2004
34 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
35 extern DWORD g_platform_id;
37 // from CYGWIN (should be diff between Jan 1 1601 and Jan 1 1970
38 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
40 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
43 cygwin_conv_to_win32_path(const char *name, char *win32_name)
45 const char *fname = name;
47 /* Check for Unix separator and convert to Win32 */
49 *win32_name++ = '\\'; /* convert char */
50 /* If Win32 separated that is "quoted", remove quote */
51 } else if (*name == '\\' && name[1] == '\\') {
53 name++; /* skip first \ */
55 *win32_name++ = *name; /* copy character */
59 /* Strip any trailing slash, if we stored something */
60 if (*fname != 0 && win32_name[-1] == '\\') {
69 wchar_win32_path(const char *name, WCHAR *win32_name)
71 const char *fname = name;
73 /* Check for Unix separator and convert to Win32 */
75 *win32_name++ = '\\'; /* convert char */
76 /* If Win32 separated that is "quoted", remove quote */
77 } else if (*name == '\\' && name[1] == '\\') {
79 name++; /* skip first \ */
81 *win32_name++ = *name; /* copy character */
85 /* Strip any trailing slash, if we stored something */
86 if (*fname != 0 && win32_name[-1] == '\\') {
95 open(const char *file, int flags, int mode)
102 const char *remap = file;
104 if (flags & O_WRONLY) access = GENERIC_WRITE;
105 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
106 else access = GENERIC_READ;
108 if (flags & O_CREAT) create = CREATE_NEW;
109 else create = OPEN_EXISTING;
111 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
113 if (!(flags & O_EXCL))
114 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
116 if (flags & O_APPEND)
118 printf("open...APPEND not implemented yet.");
122 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
123 if (INVALID_HANDLE_VALUE == foo)
134 if (!CloseHandle((HANDLE)fd))
141 write(int fd, const void *data, int32_t len)
145 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
146 if (status) return bwrite;
152 read(int fd, void *data, int32_t len)
157 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
158 if (status) return bread;
170 chmod(const char *, mode_t)
176 chown(const char *k, uid_t, gid_t)
182 lchown(const char *k, uid_t, gid_t)
196 srandom(unsigned int seed)
200 // /////////////////////////////////////////////////////////////////
201 // convert from Windows concept of time to Unix concept of time
202 // /////////////////////////////////////////////////////////////////
204 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
206 uint64_t mstime = time;
207 mstime *= WIN32_FILETIME_SCALE;
208 mstime += WIN32_FILETIME_ADJUST;
210 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
211 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
215 cvt_ftime_to_utime(const FILETIME &time)
217 uint64_t mstime = time.dwHighDateTime;
219 mstime |= time.dwLowDateTime;
221 mstime -= WIN32_FILETIME_ADJUST;
222 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
224 return (time_t) (mstime & 0xffffffff);
232 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
233 FORMAT_MESSAGE_FROM_SYSTEM |
234 FORMAT_MESSAGE_IGNORE_INSERTS,
237 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
242 return (const char *) lpMsgBuf;
246 statDir(const char *file, struct stat *sb)
248 WIN32_FIND_DATA info; // window's file info
250 if (file[1] == ':' && file[2] == 0) {
251 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
252 sb->st_mode = S_IFDIR;
253 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
259 HANDLE h = FindFirstFile(file, &info);
261 if (h == INVALID_HANDLE_VALUE) {
262 const char *err = errorString();
263 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
264 LocalFree((void *)err);
265 errno = GetLastError();
269 sb->st_mode = 0777; /* start with everything */
270 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
271 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
272 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
273 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
274 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
275 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
276 sb->st_mode |= S_IFDIR;
278 sb->st_size = info.nFileSizeHigh;
280 sb->st_size |= info.nFileSizeLow;
281 sb->st_blksize = 4096;
282 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
284 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
285 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
286 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
293 stat2(const char *file, struct stat *sb)
295 BY_HANDLE_FILE_INFORMATION info;
299 cygwin_conv_to_win32_path(file, tmpbuf);
301 DWORD attr = GetFileAttributes(tmpbuf);
304 const char *err = errorString();
305 d_msg(__FILE__, __LINE__, 99,
306 "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
307 LocalFree((void *)err);
308 errno = GetLastError();
312 if (attr & FILE_ATTRIBUTE_DIRECTORY)
313 return statDir(tmpbuf, sb);
315 h = CreateFile(tmpbuf, GENERIC_READ,
316 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
318 if (h == INVALID_HANDLE_VALUE) {
319 const char *err = errorString();
320 d_msg(__FILE__, __LINE__, 99,
321 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
322 LocalFree((void *)err);
324 errno = GetLastError();
328 if (!GetFileInformationByHandle(h, &info)) {
329 const char *err = errorString();
330 d_msg(__FILE__, __LINE__, 99,
331 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
332 LocalFree((void *)err);
334 errno = GetLastError();
338 sb->st_dev = info.dwVolumeSerialNumber;
339 sb->st_ino = info.nFileIndexHigh;
341 sb->st_ino |= info.nFileIndexLow;
342 sb->st_nlink = (short)info.nNumberOfLinks;
344 sb->st_mode = 0777; /* start with everything */
345 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
346 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
347 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
348 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
349 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
350 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
351 sb->st_mode |= S_IFREG;
353 sb->st_size = info.nFileSizeHigh;
355 sb->st_size |= info.nFileSizeLow;
356 sb->st_blksize = 4096;
357 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
358 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
359 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
360 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
368 stat(const char *file, struct stat *sb)
370 WIN32_FILE_ATTRIBUTE_DATA data;
374 memset(sb, 0, sizeof(*sb));
376 if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
377 return stat2(file, sb);
379 // otherwise we're on NT
387 wchar_win32_path(file, buf+4);
389 if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
390 return stat2(file, sb);
392 if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
393 return stat2(file, sb);
396 sb->st_mode = 0777; /* start with everything */
397 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
398 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
399 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
400 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
401 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
402 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
404 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
405 sb->st_mode |= S_IFDIR;
407 sb->st_mode |= S_IFREG;
410 sb->st_size = data.nFileSizeHigh;
412 sb->st_size |= data.nFileSizeLow;
413 sb->st_blksize = 4096;
414 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
415 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
416 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
417 sb->st_ctime = cvt_ftime_to_utime(data.ftLastWriteTime);
422 lstat(const char *file, struct stat *sb)
424 return stat(file, sb);
440 execvp(const char *, char *[]) {
463 waitpid(int, int*, int)
469 readlink(const char *, char *, int)
475 lseek(int fd, int offset, int whence)
483 method = FILE_CURRENT;
492 return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method);
496 strcasecmp(const char *s1, const char *s2)
498 register int ch1, ch2;
501 return 0; /* strings are equal if same object. */
512 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
518 strncasecmp(const char *s1, const char *s2, int len)
520 register int ch1, ch2;
523 return 0; /* strings are equal if same object. */
534 } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
540 gettimeofday(struct timeval *tv, struct timezone *)
546 if (tv == NULL) return -1;
547 if (!SystemTimeToFileTime(&now, &tmp)) return -1;
549 int64_t _100nsec = tmp.dwHighDateTime;
551 _100nsec |= tmp.dwLowDateTime;
552 _100nsec -= WIN32_FILETIME_ADJUST;
554 tv->tv_sec =(long) (_100nsec / 10000000);
555 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
561 syslog(int, const char *, const char *)
579 // implement opendir/readdir/closedir on top of window's API
582 WIN32_FIND_DATA data; // window's file info
583 const char *spec; // the directory we're traversing
584 HANDLE dirh; // the search handle
585 BOOL valid; // the info in data field is valid
586 UINT32 offset; // pseudo offset for d_off
590 opendir(const char *path)
592 int max_len = strlen(path) + 16;
594 if (path == NULL) return NULL;
596 rval = (_dir *)malloc(sizeof(_dir));
597 if (rval == NULL) return NULL;
598 char *tspec = (char *)malloc(max_len);
599 if (tspec == NULL) goto err1;
601 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
602 // allow path to be 32767 bytes
608 cygwin_conv_to_win32_path(path, tspec+4);
610 cygwin_conv_to_win32_path(path, tspec);
612 strncat(tspec, "\\*", max_len);
615 rval->dirh = FindFirstFile(rval->spec, &rval->data);
616 d_msg(__FILE__, __LINE__,
617 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
618 path, rval->spec, rval->dirh);
621 if (rval->dirh == INVALID_HANDLE_VALUE)
624 d_msg(__FILE__, __LINE__,
625 99, "\tFirstFile=%s\n", rval->data.cFileName);
628 free((void *)rval->spec);
637 _dir *dp = (_dir *)dirp;
639 free((void *)dp->spec);
645 typedef struct _WIN32_FIND_DATA {
646 DWORD dwFileAttributes;
647 FILETIME ftCreationTime;
648 FILETIME ftLastAccessTime;
649 FILETIME ftLastWriteTime;
654 TCHAR cFileName[MAX_PATH];
655 TCHAR cAlternateFileName[14];
656 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
660 copyin(struct dirent &dp, const char *fname)
664 char *cp = dp.d_name;
673 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
676 _dir *dp = (_dir *)dirp;
678 entry->d_off = dp->offset;
679 dp->offset += copyin(*entry, dp->data.cFileName);
680 *result = entry; /* return entry address */
681 d_msg(__FILE__, __LINE__,
682 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
683 dirp, entry->d_name, entry->d_reclen, entry->d_off);
685 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
688 dp->valid = FindNextFile(dp->dirh, &dp->data);
694 inet_aton(const char *a, struct in_addr *inp)
698 uint32_t acc = 0, tmp = 0;
700 if (!isdigit(*a)) return 0;
703 tmp = (tmp * 10) + (*cp -'0');
704 else if (*cp == '.') {
705 if (tmp > 255) return 0;
706 acc = (acc << 8) + tmp;
712 if (dotc != 3) return 0;
718 nanosleep(const struct timespec *req, struct timespec *rem)
721 rem->tv_sec = rem->tv_nsec = 0;
722 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
727 init_signals(void terminate(int sig))
733 init_stack_dump(void)
740 pathconf(const char *path, int name)
744 if (strncmp(path, "\\\\?\\", 4) == 0)
756 WORD wVersionRequested = MAKEWORD( 1, 1);
759 int err = WSAStartup(wVersionRequested, &wsaData);
764 printf("Can not start Windows Sockets\n");
773 win32_chdir(const char *dir)
775 if (0 == SetCurrentDirectory(dir)) return -1;
780 win32_getcwd(char *buf, int maxlen)
782 int n = GetCurrentDirectory(maxlen, buf);
784 if (n == 0 || n > maxlen) return NULL;
786 if (n+1 > maxlen) return NULL;
795 #include "mswinver.h"
797 char WIN_VERSION_LONG[64];
798 char WIN_VERSION[32];
805 static winver INIT; // cause constructor to be called before main()
812 const char *version = "";
813 const char *platform = "";
814 OSVERSIONINFO osvinfo;
815 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
817 // Get the current OS version
818 if (!GetVersionEx(&osvinfo)) {
820 platform = "Unknown";
823 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
825 case MS_WINDOWS_95: (version = "Windows 95"); break;
826 case MS_WINDOWS_98: (version = "Windows 98"); break;
827 case MS_WINDOWS_ME: (version = "Windows ME"); break;
828 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
829 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
830 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
831 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
832 default: version = "Windows ??"; break;
835 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
836 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
837 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
840 BPIPE *b = open_bpipe("ls -l", 10, "r");
842 while (!feof(b->rfd)) {
843 fgets(buf, sizeof(buf), b->rfd);
849 BOOL CreateChildProcess(VOID);
850 VOID WriteToPipe(VOID);
851 VOID ReadFromPipe(VOID);
852 VOID ErrorExit(LPTSTR);
853 VOID ErrMsg(LPTSTR, BOOL);
857 getArgv0(const char *cmdline)
859 const char *cp = cmdline;
861 while (*cp && !isspace(*cp)) cp++;
863 int len = cp - cmdline;
864 char *rval = (char *)malloc(len+1);
876 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
878 PROCESS_INFORMATION piProcInfo;
879 STARTUPINFO siStartInfo;
880 BOOL bFuncRetn = FALSE;
882 // Set up members of the PROCESS_INFORMATION structure.
884 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
886 // Set up members of the STARTUPINFO structure.
888 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
889 siStartInfo.cb = sizeof(STARTUPINFO);
890 // setup new process to use supplied handles for stdin,stdout,stderr
891 // if supplied handles are not used the send a copy of our STD_HANDLE
893 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
895 if (in != INVALID_HANDLE_VALUE)
896 siStartInfo.hStdInput = in;
898 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
900 if (out != INVALID_HANDLE_VALUE)
901 siStartInfo.hStdOutput = out;
903 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
904 if (err != INVALID_HANDLE_VALUE)
905 siStartInfo.hStdError = err;
907 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
908 // Create the child process.
912 // retrive the first compont of the command line which should be the
914 const char *exeName = getArgv0(cmdline);
915 // check to see if absolute path was passed to us already?
916 if (exeName[1] != ':'
917 || (strchr(cmdline, '/') == NULL
918 && strchr(cmdline, '\\') == NULL))
920 // only command name so perform search of PATH to find
922 DWORD rval = SearchPath(NULL,
929 return INVALID_HANDLE_VALUE;
930 if (rval > sizeof(exeFile))
931 return INVALID_HANDLE_VALUE;
935 strcpy(exeFile, exeName);
937 // exeFile now has absolute path to program to execute.
938 free((void *)exeName);
939 // copy original command line to pass to create process
940 strcpy(cmdLine, cmdline);
941 // try to execute program
942 bFuncRetn = CreateProcess(exeFile,
943 cmdLine, // command line
944 NULL, // process security attributes
945 NULL, // primary thread security attributes
946 TRUE, // handles are inherited
948 NULL, // use parent's environment
949 NULL, // use parent's current directory
950 &siStartInfo, // STARTUPINFO pointer
951 &piProcInfo); // receives PROCESS_INFORMATION
953 if (bFuncRetn == 0) {
954 ErrorExit("CreateProcess failed\n");
955 return INVALID_HANDLE_VALUE;
957 // we don't need a handle on the process primary thread so we close
959 CloseHandle(piProcInfo.hThread);
961 return piProcInfo.hProcess;
966 ErrorExit (LPTSTR lpszMessage)
968 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
973 typedef struct s_bpipe {
984 CloseIfValid(HANDLE handle)
986 if (handle != INVALID_HANDLE_VALUE)
989 // include <io.h> causes to many conflicts with some of
990 // the fuctions we define here.
992 int __cdecl _open_osfhandle(long, int);
996 open_bpipe(char *prog, int wait, const char *mode)
998 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
999 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1002 SECURITY_ATTRIBUTES saAttr;
1006 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1007 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1008 hInputFile = INVALID_HANDLE_VALUE;
1010 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1011 memset((void *)bpipe, 0, sizeof(BPIPE));
1013 int mode_read = (mode[0] == 'r');
1014 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1017 // Set the bInheritHandle flag so pipe handles are inherited.
1019 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1020 saAttr.bInheritHandle = TRUE;
1021 saAttr.lpSecurityDescriptor = NULL;
1025 // Create a pipe for the child process's STDOUT.
1026 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1027 ErrorExit("Stdout pipe creation failed\n");
1030 // Create noninheritable read handle and close the inheritable read
1033 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1034 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1036 DUPLICATE_SAME_ACCESS);
1038 ErrorExit("DuplicateHandle failed");
1042 CloseHandle(hChildStdoutRd);
1047 // Create a pipe for the child process's STDIN.
1049 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1050 ErrorExit("Stdin pipe creation failed\n");
1054 // Duplicate the write handle to the pipe so it is not inherited.
1055 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1056 GetCurrentProcess(), &hChildStdinWrDup,
1058 FALSE, // not inherited
1059 DUPLICATE_SAME_ACCESS);
1061 ErrorExit("DuplicateHandle failed");
1065 CloseHandle(hChildStdinWr);
1067 // spawn program with redirected handles as appropriate
1068 bpipe->worker_pid = (pid_t)
1069 CreateChildProcess(prog, // commandline
1070 hChildStdinRd, // stdin HANDLE
1071 hChildStdoutWr, // stdout HANDLE
1072 hChildStdoutWr);// stderr HANDLE
1074 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1078 bpipe->worker_stime = time(NULL);
1081 CloseHandle(hChildStdoutWr); // close our write side so when
1082 // process terminates we can
1084 // ugly but convert WIN32 HANDLE to FILE*
1085 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1086 bpipe->rfd = _fdopen(rfd, "r");
1089 CloseHandle(hChildStdinRd); // close our read side so as not
1090 // to interfre with child's copy
1091 // ugly but convert WIN32 HANDLE to FILE*
1092 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1093 bpipe->wfd = _fdopen(wfd, "w");
1097 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1104 CloseIfValid(hChildStdoutRd);
1105 CloseIfValid(hChildStdoutRdDup);
1106 CloseIfValid(hChildStdinWr);
1107 CloseIfValid(hChildStdinWrDup);
1109 free((void *) bpipe);
1115 kill(int pid, int signal)
1118 if (!TerminateProcess((HANDLE)pid, (UINT) signal))
1120 CloseHandle((HANDLE)pid);
1125 close_bpipe(BPIPE *bpipe)
1128 if (bpipe->rfd) fclose(bpipe->rfd);
1129 if (bpipe->wfd) fclose(bpipe->wfd);
1132 int remaining_wait = bpipe->wait;
1136 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
1138 const char *err = errorString();
1139 rval = GetLastError();
1140 d_msg(__FILE__, __LINE__, 0,
1141 "GetExitCode error %s\n", err);
1142 LocalFree((void *)err);
1146 if (exitCode == STILL_ACTIVE) {
1147 bmicrosleep(1, 0); /* wait one second */
1151 } while(remaining_wait);
1154 if (bpipe->timer_id) {
1155 stop_child_timer(bpipe->timer_id);
1157 free((void *)bpipe);
1162 close_wpipe(BPIPE *bpipe)
1168 if (fclose(bpipe->wfd) != 0) {
1176 #include "findlib/find.h"
1179 utime(const char *fname, struct utimbuf *times)
1184 cygwin_conv_to_win32_path(fname, tmpbuf);
1186 cvt_utime_to_ftime(times->actime, acc);
1187 cvt_utime_to_ftime(times->modtime, mod);
1189 HANDLE h = CreateFile(tmpbuf,
1190 FILE_WRITE_ATTRIBUTES,
1197 if (h == INVALID_HANDLE_VALUE) {
1198 const char *err = errorString();
1199 d_msg(__FILE__, __LINE__, 99,
1200 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
1201 LocalFree((void *)err);
1205 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;