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 12:55:40 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, size_t len)
145 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
146 if (status) return bwrite;
152 read(int fd, void *data, size_t len)
157 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
158 if (status) return bread;
169 chmod(const char *, mode_t)
175 chown(const char *k, uid_t, gid_t)
181 lchown(const char *k, uid_t, gid_t)
195 srandom(unsigned int seed)
199 // /////////////////////////////////////////////////////////////////
200 // convert from Windows concept of time to Unix concept of time
201 // /////////////////////////////////////////////////////////////////
203 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
205 uint64_t mstime = time;
206 mstime *= WIN32_FILETIME_SCALE;
207 mstime += WIN32_FILETIME_ADJUST;
209 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
210 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
214 cvt_ftime_to_utime(const FILETIME &time)
216 uint64_t mstime = time.dwHighDateTime;
218 mstime |= time.dwLowDateTime;
220 mstime -= WIN32_FILETIME_ADJUST;
221 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
223 return (time_t) (mstime & 0xffffffff);
231 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
232 FORMAT_MESSAGE_FROM_SYSTEM |
233 FORMAT_MESSAGE_IGNORE_INSERTS,
236 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
241 return (const char *) lpMsgBuf;
245 statDir(const char *file, struct stat *sb)
247 WIN32_FIND_DATA info; // window's file info
249 if (file[1] == ':' && file[2] == 0) {
250 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
251 sb->st_mode = S_IFDIR;
252 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
258 HANDLE h = FindFirstFile(file, &info);
260 if (h == INVALID_HANDLE_VALUE) {
261 const char *err = errorString();
262 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
263 LocalFree((void *)err);
264 errno = GetLastError();
268 sb->st_mode = 0777; /* start with everything */
269 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
270 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
271 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
272 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
273 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
274 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
275 sb->st_mode |= S_IFDIR;
277 sb->st_size = info.nFileSizeHigh;
279 sb->st_size |= info.nFileSizeLow;
280 sb->st_blksize = 4096;
281 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
283 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
284 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
285 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
292 stat2(const char *file, struct stat *sb)
294 BY_HANDLE_FILE_INFORMATION info;
298 cygwin_conv_to_win32_path(file, tmpbuf);
300 DWORD attr = GetFileAttributes(tmpbuf);
303 const char *err = errorString();
304 d_msg(__FILE__, __LINE__, 99,
305 "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
306 LocalFree((void *)err);
307 errno = GetLastError();
311 if (attr & FILE_ATTRIBUTE_DIRECTORY)
312 return statDir(tmpbuf, sb);
314 h = CreateFile(tmpbuf, GENERIC_READ,
315 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
317 if (h == INVALID_HANDLE_VALUE) {
318 const char *err = errorString();
319 d_msg(__FILE__, __LINE__, 99,
320 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
321 LocalFree((void *)err);
323 errno = GetLastError();
327 if (!GetFileInformationByHandle(h, &info)) {
328 const char *err = errorString();
329 d_msg(__FILE__, __LINE__, 99,
330 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
331 LocalFree((void *)err);
333 errno = GetLastError();
337 sb->st_dev = info.dwVolumeSerialNumber;
338 sb->st_ino = info.nFileIndexHigh;
340 sb->st_ino |= info.nFileIndexLow;
341 sb->st_nlink = (short)info.nNumberOfLinks;
343 sb->st_mode = 0777; /* start with everything */
344 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
345 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
346 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
347 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
348 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
349 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
350 sb->st_mode |= S_IFREG;
352 sb->st_size = info.nFileSizeHigh;
354 sb->st_size |= info.nFileSizeLow;
355 sb->st_blksize = 4096;
356 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
357 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
358 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
359 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
367 stat(const char *file, struct stat *sb)
369 WIN32_FILE_ATTRIBUTE_DATA data;
373 memset(sb, 0, sizeof(*sb));
375 if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
376 return stat2(file, sb);
378 // otherwise we're on NT
386 wchar_win32_path(file, buf+4);
388 if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
389 return stat2(file, sb);
391 if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
392 return stat2(file, sb);
395 sb->st_mode = 0777; /* start with everything */
396 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
397 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
398 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
399 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
400 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
401 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
403 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
404 sb->st_mode |= S_IFDIR;
406 sb->st_mode |= S_IFREG;
409 sb->st_size = data.nFileSizeHigh;
411 sb->st_size |= data.nFileSizeLow;
412 sb->st_blksize = 4096;
413 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
414 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
415 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
416 sb->st_ctime = cvt_ftime_to_utime(data.ftLastWriteTime);
421 lstat(const char *file, struct stat *sb)
423 return stat(file, sb);
439 execvp(const char *, char *[]) {
462 waitpid(int, int*, int)
468 readlink(const char *, char *, int)
474 lseek(int fd, off_t offset, int whence)
482 method = FILE_CURRENT;
491 return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method);
495 strcasecmp(const char *s1, const char *s2)
497 register int ch1, ch2;
500 return 0; /* strings are equal if same object. */
511 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
517 strncasecmp(const char *s1, const char *s2, int len)
519 register int ch1, ch2;
522 return 0; /* strings are equal if same object. */
533 } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
539 gettimeofday(struct timeval *tv, struct timezone *)
545 if (tv == NULL) return -1;
546 if (!SystemTimeToFileTime(&now, &tmp)) return -1;
548 int64_t _100nsec = tmp.dwHighDateTime;
550 _100nsec |= tmp.dwLowDateTime;
551 _100nsec -= WIN32_FILETIME_ADJUST;
553 tv->tv_sec =(long) (_100nsec / 10000000);
554 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
560 syslog(int, const char *, const char *)
578 // implement opendir/readdir/closedir on top of window's API
581 WIN32_FIND_DATA data; // window's file info
582 const char *spec; // the directory we're traversing
583 HANDLE dirh; // the search handle
584 BOOL valid; // the info in data field is valid
585 UINT32 offset; // pseudo offset for d_off
589 opendir(const char *path)
591 int max_len = strlen(path) + 16;
593 if (path == NULL) return NULL;
595 rval = (_dir *)malloc(sizeof(_dir));
596 if (rval == NULL) return NULL;
597 char *tspec = (char *)malloc(max_len);
598 if (tspec == NULL) goto err1;
600 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
601 // allow path to be 32767 bytes
607 cygwin_conv_to_win32_path(path, tspec+4);
609 cygwin_conv_to_win32_path(path, tspec);
611 strncat(tspec, "\\*", max_len);
614 rval->dirh = FindFirstFile(rval->spec, &rval->data);
615 d_msg(__FILE__, __LINE__,
616 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
617 path, rval->spec, rval->dirh);
620 if (rval->dirh == INVALID_HANDLE_VALUE)
623 d_msg(__FILE__, __LINE__,
624 99, "\tFirstFile=%s\n", rval->data.cFileName);
627 free((void *)rval->spec);
636 _dir *dp = (_dir *)dirp;
638 free((void *)dp->spec);
644 typedef struct _WIN32_FIND_DATA {
645 DWORD dwFileAttributes;
646 FILETIME ftCreationTime;
647 FILETIME ftLastAccessTime;
648 FILETIME ftLastWriteTime;
653 TCHAR cFileName[MAX_PATH];
654 TCHAR cAlternateFileName[14];
655 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
659 copyin(struct dirent &dp, const char *fname)
663 char *cp = dp.d_name;
672 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
675 _dir *dp = (_dir *)dirp;
677 entry->d_off = dp->offset;
678 dp->offset += copyin(*entry, dp->data.cFileName);
679 *result = entry; /* return entry address */
680 d_msg(__FILE__, __LINE__,
681 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
682 dirp, entry->d_name, entry->d_reclen, entry->d_off);
684 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
687 dp->valid = FindNextFile(dp->dirh, &dp->data);
693 inet_aton(const char *a, struct in_addr *inp)
697 uint32_t acc = 0, tmp = 0;
699 if (!isdigit(*a)) return 0;
702 tmp = (tmp * 10) + (*cp -'0');
703 else if (*cp == '.') {
704 if (tmp > 255) return 0;
705 acc = (acc << 8) + tmp;
711 if (dotc != 3) return 0;
717 nanosleep(const struct timespec *req, struct timespec *rem)
720 rem->tv_sec = rem->tv_nsec = 0;
721 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
726 init_signals(void terminate(int sig))
732 init_stack_dump(void)
739 pathconf(const char *path, int name)
743 if (strncmp(path, "\\\\?\\", 4) == 0)
755 WORD wVersionRequested = MAKEWORD( 1, 1);
758 int err = WSAStartup(wVersionRequested, &wsaData);
763 printf("Can not start Windows Sockets\n");
772 win32_chdir(const char *dir)
774 if (0 == SetCurrentDirectory(dir)) return -1;
779 win32_getcwd(char *buf, int maxlen)
781 int n = GetCurrentDirectory(maxlen, buf);
783 if (n == 0 || n > maxlen) return NULL;
785 if (n+1 > maxlen) return NULL;
794 #include "mswinver.h"
796 char WIN_VERSION_LONG[64];
797 char WIN_VERSION[32];
804 static winver INIT; // cause constructor to be called before main()
811 const char *version = "";
812 const char *platform = "";
813 OSVERSIONINFO osvinfo;
814 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
816 // Get the current OS version
817 if (!GetVersionEx(&osvinfo)) {
819 platform = "Unknown";
822 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
824 case MS_WINDOWS_95: (version = "Windows 95"); break;
825 case MS_WINDOWS_98: (version = "Windows 98"); break;
826 case MS_WINDOWS_ME: (version = "Windows ME"); break;
827 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
828 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
829 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
830 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
831 default: version = "Windows ??"; break;
834 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
835 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
836 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
839 BPIPE *b = open_bpipe("ls -l", 10, "r");
841 while (!feof(b->rfd)) {
842 fgets(buf, sizeof(buf), b->rfd);
848 BOOL CreateChildProcess(VOID);
849 VOID WriteToPipe(VOID);
850 VOID ReadFromPipe(VOID);
851 VOID ErrorExit(LPTSTR);
852 VOID ErrMsg(LPTSTR, BOOL);
856 getArgv0(const char *cmdline)
858 const char *cp = cmdline;
860 while (*cp && !isspace(*cp)) cp++;
862 int len = cp - cmdline;
863 char *rval = (char *)malloc(len+1);
875 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
877 PROCESS_INFORMATION piProcInfo;
878 STARTUPINFO siStartInfo;
879 BOOL bFuncRetn = FALSE;
881 // Set up members of the PROCESS_INFORMATION structure.
883 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
885 // Set up members of the STARTUPINFO structure.
887 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
888 siStartInfo.cb = sizeof(STARTUPINFO);
889 // setup new process to use supplied handles for stdin,stdout,stderr
890 // if supplied handles are not used the send a copy of our STD_HANDLE
892 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
894 if (in != INVALID_HANDLE_VALUE)
895 siStartInfo.hStdInput = in;
897 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
899 if (out != INVALID_HANDLE_VALUE)
900 siStartInfo.hStdOutput = out;
902 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
903 if (err != INVALID_HANDLE_VALUE)
904 siStartInfo.hStdError = err;
906 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
907 // Create the child process.
911 // retrive the first compont of the command line which should be the
913 const char *exeName = getArgv0(cmdline);
914 // check to see if absolute path was passed to us already?
915 if (exeName[1] != ':'
916 || (strchr(cmdline, '/') == NULL
917 && strchr(cmdline, '\\') == NULL))
919 // only command name so perform search of PATH to find
921 DWORD rval = SearchPath(NULL,
928 return INVALID_HANDLE_VALUE;
929 if (rval > sizeof(exeFile))
930 return INVALID_HANDLE_VALUE;
934 strcpy(exeFile, exeName);
936 // exeFile now has absolute path to program to execute.
937 free((void *)exeName);
938 // copy original command line to pass to create process
939 strcpy(cmdLine, cmdline);
940 // try to execute program
941 bFuncRetn = CreateProcess(exeFile,
942 cmdLine, // command line
943 NULL, // process security attributes
944 NULL, // primary thread security attributes
945 TRUE, // handles are inherited
947 NULL, // use parent's environment
948 NULL, // use parent's current directory
949 &siStartInfo, // STARTUPINFO pointer
950 &piProcInfo); // receives PROCESS_INFORMATION
952 if (bFuncRetn == 0) {
953 ErrorExit("CreateProcess failed\n");
954 return INVALID_HANDLE_VALUE;
956 // we don't need a handle on the process primary thread so we close
958 CloseHandle(piProcInfo.hThread);
960 return piProcInfo.hProcess;
965 ErrorExit (LPTSTR lpszMessage)
967 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
972 typedef struct s_bpipe {
983 CloseIfValid(HANDLE handle)
985 if (handle != INVALID_HANDLE_VALUE)
988 // include <io.h> causes to many conflicts with some of
989 // the fuctions we define here.
991 int __cdecl _open_osfhandle(long, int);
995 open_bpipe(char *prog, int wait, const char *mode)
997 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
998 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1001 SECURITY_ATTRIBUTES saAttr;
1005 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1006 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1007 hInputFile = INVALID_HANDLE_VALUE;
1009 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1010 memset((void *)bpipe, 0, sizeof(BPIPE));
1012 int mode_read = (mode[0] == 'r');
1013 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1016 // Set the bInheritHandle flag so pipe handles are inherited.
1018 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1019 saAttr.bInheritHandle = TRUE;
1020 saAttr.lpSecurityDescriptor = NULL;
1024 // Create a pipe for the child process's STDOUT.
1025 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1026 ErrorExit("Stdout pipe creation failed\n");
1029 // Create noninheritable read handle and close the inheritable read
1032 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1033 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1035 DUPLICATE_SAME_ACCESS);
1037 ErrorExit("DuplicateHandle failed");
1041 CloseHandle(hChildStdoutRd);
1046 // Create a pipe for the child process's STDIN.
1048 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1049 ErrorExit("Stdin pipe creation failed\n");
1053 // Duplicate the write handle to the pipe so it is not inherited.
1054 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1055 GetCurrentProcess(), &hChildStdinWrDup,
1057 FALSE, // not inherited
1058 DUPLICATE_SAME_ACCESS);
1060 ErrorExit("DuplicateHandle failed");
1064 CloseHandle(hChildStdinWr);
1066 // spawn program with redirected handles as appropriate
1067 bpipe->worker_pid = (pid_t)
1068 CreateChildProcess(prog, // commandline
1069 hChildStdinRd, // stdin HANDLE
1070 hChildStdoutWr, // stdout HANDLE
1071 hChildStdoutWr);// stderr HANDLE
1073 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1077 bpipe->worker_stime = time(NULL);
1080 CloseHandle(hChildStdoutWr); // close our write side so when
1081 // process terminates we can
1083 // ugly but convert WIN32 HANDLE to FILE*
1084 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1085 bpipe->rfd = _fdopen(rfd, "r");
1088 CloseHandle(hChildStdinRd); // close our read side so as not
1089 // to interfre with child's copy
1090 // ugly but convert WIN32 HANDLE to FILE*
1091 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1092 bpipe->wfd = _fdopen(wfd, "w");
1096 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1103 CloseIfValid(hChildStdoutRd);
1104 CloseIfValid(hChildStdoutRdDup);
1105 CloseIfValid(hChildStdinWr);
1106 CloseIfValid(hChildStdinWrDup);
1108 free((void *) bpipe);
1114 kill(int pid, int signal)
1117 if (!TerminateProcess((HANDLE)pid, (UINT) signal))
1119 CloseHandle((HANDLE)pid);
1124 close_bpipe(BPIPE *bpipe)
1127 if (bpipe->rfd) fclose(bpipe->rfd);
1128 if (bpipe->wfd) fclose(bpipe->wfd);
1131 int remaining_wait = bpipe->wait;
1135 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
1137 const char *err = errorString();
1138 rval = GetLastError();
1139 d_msg(__FILE__, __LINE__, 0,
1140 "GetExitCode error %s\n", err);
1141 LocalFree((void *)err);
1145 if (exitCode == STILL_ACTIVE) {
1146 bmicrosleep(1, 0); /* wait one second */
1150 } while(remaining_wait);
1153 if (bpipe->timer_id) {
1154 stop_child_timer(bpipe->timer_id);
1156 free((void *)bpipe);
1161 close_wpipe(BPIPE *bpipe)
1167 if (fclose(bpipe->wfd) != 0) {
1175 #include "findlib/find.h"
1178 utime(const char *fname, struct utimbuf *times)
1183 cygwin_conv_to_win32_path(fname, tmpbuf);
1185 cvt_utime_to_ftime(times->actime, acc);
1186 cvt_utime_to_ftime(times->modtime, mod);
1188 HANDLE h = CreateFile(tmpbuf,
1189 FILE_WRITE_ATTRIBUTES,
1196 if (h == INVALID_HANDLE_VALUE) {
1197 const char *err = errorString();
1198 d_msg(__FILE__, __LINE__, 99,
1199 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
1200 LocalFree((void *)err);
1204 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;