]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/compat.cpp
Update projects
[bacula/bacula] / bacula / src / win32 / compat / compat.cpp
1 //                              -*- Mode: C++ -*-
2 // compat.cpp -- compatibilty layer to make bacula-fd run
3 //               natively under windows
4 //
5 // Copyright transferred from Raider Solutions, Inc to
6 //   Kern Sibbald and John Walker by express permission.
7 //
8 // Copyright (C) 2004 Kern Sibbald and John Walker
9 //
10 //   This program is free software; you can redistribute it and/or
11 //   modify it under the terms of the GNU General Public License as
12 //   published by the Free Software Foundation; either version 2 of
13 //   the License, or (at your option) any later version.
14 //
15 //   This program is distributed in the hope that it will be useful,
16 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 //   General Public License for more details.
19 //
20 //   You should have received a copy of the GNU General Public
21 //   License along with this program; if not, write to the Free
22 //   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 //   MA 02111-1307, USA.
24 //
25 // Author          : Christopher S. Hull
26 // Created On      : Sat Jan 31 15:55:00 2004
27 // $Id$
28
29 #include <stdio.h>
30
31 #include "compat.h"
32 #include "pthread.h"
33
34 #define b_errno_win32 (1<<29)
35
36 #define USE_WIN32_COMPAT_IO 1
37
38 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
39 extern DWORD   g_platform_id;
40
41 // from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970
42 #ifdef HAVE_MINGW
43 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000UL //Not sure it works
44 #else
45 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
46 #endif
47
48 #define WIN32_FILETIME_SCALE  10000000             // 100ns/second
49
50 extern "C" void
51 cygwin_conv_to_win32_path(const char *name, char *win32_name)
52 {
53     const char *fname = name;
54     while (*name) {
55         /* Check for Unix separator and convert to Win32 */
56         if (*name == '/') {
57             *win32_name++ = '\\';     /* convert char */
58         /* If Win32 separated that is "quoted", remove quote */
59         } else if (*name == '\\' && name[1] == '\\') {
60             *win32_name++ = '\\';
61             name++;                   /* skip first \ */
62         } else {
63             *win32_name++ = *name;    /* copy character */
64         }
65         name++;
66     }
67     /* Strip any trailing slash, if we stored something */
68     if (*fname != 0 && win32_name[-1] == '\\') {
69         win32_name[-1] = 0;
70     } else {
71         *win32_name = 0;
72     }
73 }
74
75 #if USE_WIN32_UNICODE
76
77 int 
78 wchar_2_UTF8(char *pszUTF, const WCHAR *pszUCS, int cchChar)
79 {
80         /* the return value is the number of bytes written to the buffer. 
81            The number includes the byte for the null terminator. */
82
83         return WideCharToMultiByte (CP_UTF8,0,pszUCS,-1,pszUTF,cchChar,NULL,NULL);
84 }
85
86 int 
87 UTF8_2_wchar(WCHAR *pszUCS, const char *pszUTF, int cchWideChar)
88 {
89         /*  the return value is the number of wide characters written to the buffer. */
90         /* convert null terminated string from utf-8 to ucs2*/
91         return MultiByteToWideChar(CP_UTF8, 0, pszUTF, -1, pszUCS,cchWideChar);
92 }
93
94 #endif
95
96
97 void
98 wchar_win32_path(const char *name, WCHAR *win32_name)
99 {
100     const char *fname = name;
101     while (*name) {
102         /* Check for Unix separator and convert to Win32 */
103         if (*name == '/') {
104             *win32_name++ = '\\';     /* convert char */
105         /* If Win32 separated that is "quoted", remove quote */
106         } else if (*name == '\\' && name[1] == '\\') {
107             *win32_name++ = '\\';
108             name++;                   /* skip first \ */
109         } else {
110             *win32_name++ = *name;    /* copy character */
111         }
112         name++;
113     }
114     /* Strip any trailing slash, if we stored something */
115     if (*fname != 0 && win32_name[-1] == '\\') {
116         win32_name[-1] = 0;
117     } else {
118         *win32_name = 0;
119     }
120 }
121
122 int umask(int)
123 {
124    return 0;
125 }
126
127 int chmod(const char *, mode_t)
128 {
129    return 0;
130 }
131
132 int chown(const char *k, uid_t, gid_t)
133 {
134    return 0;
135 }
136
137 int lchown(const char *k, uid_t, gid_t)
138 {
139    return 0;
140 }
141
142 #ifdef needed
143 bool fstype(const char *fname, char *fs, int fslen)
144 {
145    return true;                       /* accept anything */
146 }
147 #endif
148
149
150 long int
151 random(void)
152 {
153     return rand();
154 }
155
156 void
157 srandom(unsigned int seed)
158 {
159    srand(seed);
160 }
161 // /////////////////////////////////////////////////////////////////
162 // convert from Windows concept of time to Unix concept of time
163 // /////////////////////////////////////////////////////////////////
164 void
165 cvt_utime_to_ftime(const time_t  &time, FILETIME &wintime)
166 {
167     uint64_t mstime = time;
168     mstime *= WIN32_FILETIME_SCALE;
169     mstime += WIN32_FILETIME_ADJUST;
170
171     #ifdef HAVE_MINGW
172     wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL);
173     #else
174     wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
175     #endif
176     wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
177 }
178
179 time_t
180 cvt_ftime_to_utime(const FILETIME &time)
181 {
182     uint64_t mstime = time.dwHighDateTime;
183     mstime <<= 32;
184     mstime |= time.dwLowDateTime;
185
186     mstime -= WIN32_FILETIME_ADJUST;
187     mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
188
189     return (time_t) (mstime & 0xffffffff);
190 }
191
192 static const char *
193 errorString(void)
194 {
195    LPVOID lpMsgBuf;
196
197    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
198                  FORMAT_MESSAGE_FROM_SYSTEM |
199                  FORMAT_MESSAGE_IGNORE_INSERTS,
200                  NULL,
201                  GetLastError(),
202                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
203                  (LPTSTR) &lpMsgBuf,
204                  0,
205                  NULL);
206
207    return (const char *) lpMsgBuf;
208 }
209
210 #ifndef HAVE_MINGW
211
212 static int
213 statDir(const char *file, struct stat *sb)
214 {
215 #if USE_WIN32_UNICODE
216         WIN32_FIND_DATAW info;       // window's file info
217 #else
218     WIN32_FIND_DATA info;       // window's file info
219 #endif
220
221     if (file[1] == ':' && file[2] == 0) {
222         d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
223         sb->st_mode = S_IFDIR;
224         sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
225         time(&sb->st_ctime);
226         time(&sb->st_mtime);
227         time(&sb->st_atime);
228         return 0;
229     }
230
231 #if USE_WIN32_UNICODE
232         WCHAR szBuf[MAX_PATH_UNICODE];
233         UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
234         
235         HANDLE h = FindFirstFileW(szBuf, &info);
236 #else
237     HANDLE h = FindFirstFile(file, &info);
238 #endif
239
240     if (h == INVALID_HANDLE_VALUE) {
241         const char *err = errorString();
242         d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
243         LocalFree((void *)err);
244         errno = b_errno_win32;
245         return -1;
246     }
247
248     sb->st_mode = 0777;               /* start with everything */
249     if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
250         sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
251     if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
252         sb->st_mode &= ~S_IRWXO; /* remove everything for other */
253     if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
254         sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
255     sb->st_mode |= S_IFDIR;
256
257     sb->st_size = info.nFileSizeHigh;
258     sb->st_size <<= 32;
259     sb->st_size |= info.nFileSizeLow;
260     sb->st_blksize = 4096;
261     sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
262
263     sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
264     sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
265     sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
266     FindClose(h);
267
268     return 0;
269 }
270
271 static int
272 stat2(const char *file, struct stat *sb)
273 {
274     BY_HANDLE_FILE_INFORMATION info;
275     HANDLE h;
276     int rval = 0;
277     char tmpbuf[1024];
278     cygwin_conv_to_win32_path(file, tmpbuf);
279
280 #if USE_WIN32_UNICODE
281         WCHAR szBuf[MAX_PATH_UNICODE];
282         UTF8_2_wchar(szBuf, tmpbuf, MAX_PATH_UNICODE);
283         
284         DWORD attr = GetFileAttributesW(szBuf);
285 #else
286     DWORD attr = GetFileAttributes(tmpbuf);
287 #endif
288
289     if (attr == -1) {
290         const char *err = errorString();
291         d_msg(__FILE__, __LINE__, 99,
292               "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
293         LocalFree((void *)err);
294         errno = b_errno_win32;
295         return -1;
296     }
297
298     if (attr & FILE_ATTRIBUTE_DIRECTORY)
299         return statDir(tmpbuf, sb);
300
301     h = CreateFile(tmpbuf, GENERIC_READ,
302                    FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
303
304     if (h == INVALID_HANDLE_VALUE) {
305         const char *err = errorString();
306         d_msg(__FILE__, __LINE__, 99,
307               "Cannot open file for stat (%s):%s\n", tmpbuf, err);
308         LocalFree((void *)err);
309         rval = -1;
310         errno = b_errno_win32;
311         goto error;
312     }
313
314     if (!GetFileInformationByHandle(h, &info)) {
315         const char *err = errorString();
316         d_msg(__FILE__, __LINE__, 99,
317               "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
318         LocalFree((void *)err);
319         rval = -1;
320         errno = b_errno_win32;
321         goto error;
322     }
323
324     sb->st_dev = info.dwVolumeSerialNumber;
325     sb->st_ino = info.nFileIndexHigh;
326     sb->st_ino <<= 32;
327     sb->st_ino |= info.nFileIndexLow;
328     sb->st_nlink = (short)info.nNumberOfLinks;
329     if (sb->st_nlink > 1) {
330        d_msg(__FILE__, __LINE__, 99,  "st_nlink=%d\n", sb->st_nlink);
331     }
332
333     sb->st_mode = 0777;               /* start with everything */
334     if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
335         sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
336     if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
337         sb->st_mode &= ~S_IRWXO; /* remove everything for other */
338     if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
339         sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
340     sb->st_mode |= S_IFREG;
341
342     sb->st_size = info.nFileSizeHigh;
343     sb->st_size <<= 32;
344     sb->st_size |= info.nFileSizeLow;
345     sb->st_blksize = 4096;
346     sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
347     sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
348     sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
349     sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
350
351 error:
352     CloseHandle(h);
353     return rval;
354 }
355
356 int
357 stat(const char *file, struct stat *sb)
358 {
359     WIN32_FILE_ATTRIBUTE_DATA data;
360     errno = 0;
361
362
363     memset(sb, 0, sizeof(*sb));
364
365     if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
366         return stat2(file, sb);
367
368     // otherwise we're on NT
369 #if 0
370     WCHAR buf[32767];
371     buf[0] = '\\';
372     buf[1] = '\\';
373     buf[2] = '?';
374     buf[3] = '\\';
375
376     wchar_win32_path(file, buf+4);
377
378     if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
379         return stat2(file, sb);
380 #else
381 #if USE_WIN32_UNICODE
382         WCHAR buf[MAX_PATH_UNICODE];
383         UTF8_2_wchar(buf, file, MAX_PATH_UNICODE);
384         if (!GetFileAttributesExW(buf, GetFileExInfoStandard, &data))
385         return stat2(file, sb);
386 #else
387     if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
388         return stat2(file, sb);
389 #endif
390 #endif
391
392     sb->st_mode = 0777;               /* start with everything */
393     if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
394         sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
395     if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
396         sb->st_mode &= ~S_IRWXO; /* remove everything for other */
397     if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
398         sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
399
400     if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
401         sb->st_mode |= S_IFDIR;
402     else
403         sb->st_mode |= S_IFREG;
404
405     sb->st_nlink = 1;
406     sb->st_size = data.nFileSizeHigh;
407     sb->st_size <<= 32;
408     sb->st_size |= data.nFileSizeLow;
409     sb->st_blksize = 4096;
410     sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
411     sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
412     sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
413     sb->st_ctime = cvt_ftime_to_utime(data.ftCreationTime);
414     return 0;
415 }
416
417 #endif //HAVE_MINGW
418
419 int
420 lstat(const char *file, struct stat *sb)
421 {
422     return stat(file, sb);
423 }
424
425 void
426 sleep(int sec)
427 {
428     Sleep(sec * 1000);
429 }
430
431 int
432 geteuid(void)
433 {
434     return 0;
435 }
436
437 int
438 execvp(const char *, char *[]) {
439     errno = ENOSYS;
440     return -1;
441 }
442
443
444 int
445 fork(void)
446 {
447     errno = ENOSYS;
448     return -1;
449 }
450
451 int
452 pipe(int[])
453 {
454     errno = ENOSYS;
455     return -1;
456 }
457
458 int
459 waitpid(int, int*, int)
460 {
461     errno = ENOSYS;
462     return -1;
463 }
464
465 int
466 readlink(const char *, char *, int)
467 {
468     errno = ENOSYS;
469     return -1;
470 }
471
472
473 #ifndef HAVE_MINGW
474 int
475 strcasecmp(const char *s1, const char *s2)
476 {
477     register int ch1, ch2;
478
479     if (s1==s2)
480         return 0;       /* strings are equal if same object. */
481     else if (!s1)
482         return -1;
483     else if (!s2)
484         return 1;
485     do
486     {
487         ch1 = *s1;
488         ch2 = *s2;
489         s1++;
490         s2++;
491     } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
492
493   return(ch1 - ch2);
494 }
495 #endif //HAVE_MINGW
496
497 int
498 strncasecmp(const char *s1, const char *s2, int len)
499 {
500     register int ch1, ch2;
501
502     if (s1==s2)
503         return 0;       /* strings are equal if same object. */
504     else if (!s1)
505         return -1;
506     else if (!s2)
507         return 1;
508     while (len--)
509     {
510         ch1 = *s1;
511         ch2 = *s2;
512         s1++;
513         s2++;
514         if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break;
515     } 
516
517     return (ch1 - ch2);
518 }
519
520 int
521 gettimeofday(struct timeval *tv, struct timezone *)
522 {
523     SYSTEMTIME now;
524     FILETIME tmp;
525     GetSystemTime(&now);
526
527     if (tv == NULL) {
528        errno = EINVAL;
529        return -1;
530     }
531     if (!SystemTimeToFileTime(&now, &tmp)) {
532        errno = b_errno_win32;
533        return -1;
534     }
535
536     int64_t _100nsec = tmp.dwHighDateTime;
537     _100nsec <<= 32;
538     _100nsec |= tmp.dwLowDateTime;
539     _100nsec -= WIN32_FILETIME_ADJUST;
540
541     tv->tv_sec =(long) (_100nsec / 10000000);
542     tv->tv_usec = (long) ((_100nsec % 10000000)/10);
543     return 0;
544
545 }
546
547 int
548 syslog(int type, const char *fmt, const char *msg)
549 {
550 /*#ifndef HAVE_CONSOLE
551     MessageBox(NULL, msg, "Bacula", MB_OK);
552 #endif*/
553     return 0;
554 }
555
556 struct passwd *
557 getpwuid(uid_t)
558 {
559     return NULL;
560 }
561
562 struct group *
563 getgrgid(uid_t)
564 {
565     return NULL;
566 }
567
568 // implement opendir/readdir/closedir on top of window's API
569 typedef struct _dir
570 {
571 #if USE_WIN32_UNICODE
572     WIN32_FIND_DATAW data;       // window's file info
573 #else
574     WIN32_FIND_DATA data;       // window's file info
575 #endif
576     const char *spec;           // the directory we're traversing
577     HANDLE      dirh;           // the search handle
578     BOOL        valid;          // the info in data field is valid
579     UINT32      offset;         // pseudo offset for d_off
580 } _dir;
581
582 DIR *
583 opendir(const char *path)
584 {
585     int max_len = strlen(path) + 16;
586     _dir *rval = NULL;
587     if (path == NULL) {
588        errno = ENOENT;
589        return NULL;
590     }
591
592     rval = (_dir *)malloc(sizeof(_dir));
593     if (rval == NULL) return NULL;
594     char *tspec = (char *)malloc(max_len);
595     if (tspec == NULL) return NULL;
596
597     if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
598         // allow path to be 32767 bytes
599         tspec[0] = '\\';
600         tspec[1] = '\\';
601         tspec[2] = '?';
602         tspec[3] = '\\';
603         tspec[4] = 0;
604         cygwin_conv_to_win32_path(path, tspec+4);
605     } else {
606         cygwin_conv_to_win32_path(path, tspec);
607     }
608
609     strncat(tspec, "\\*", max_len);
610     rval->spec = tspec;
611
612         // convert to WCHAR
613 #if USE_WIN32_UNICODE
614         WCHAR wcBuf[MAX_PATH_UNICODE];
615         UTF8_2_wchar(wcBuf,rval->spec, MAX_PATH_UNICODE);
616         rval->dirh = FindFirstFileW(wcBuf, &rval->data);        
617 #else
618     rval->dirh = FindFirstFile(rval->spec, &rval->data);
619 #endif
620
621     d_msg(__FILE__, __LINE__,
622           99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
623           path, rval->spec, rval->dirh);
624
625     rval->offset = 0;
626     if (rval->dirh == INVALID_HANDLE_VALUE)
627         goto err;
628     rval->valid = 1;
629     d_msg(__FILE__, __LINE__,
630           99, "\tFirstFile=%s\n", rval->data.cFileName);
631     return (DIR *)rval;
632
633 err:
634     free((void *)rval->spec);
635     free(rval);
636     errno = b_errno_win32;
637     return NULL;
638 }
639
640 int
641 closedir(DIR *dirp)
642 {
643     _dir *dp = (_dir *)dirp;
644     FindClose(dp->dirh);
645     free((void *)dp->spec);
646     free((void *)dp);
647     return 0;
648 }
649
650 /*
651   typedef struct _WIN32_FIND_DATA {
652     DWORD dwFileAttributes;
653     FILETIME ftCreationTime;
654     FILETIME ftLastAccessTime;
655     FILETIME ftLastWriteTime;
656     DWORD nFileSizeHigh;
657     DWORD nFileSizeLow;
658     DWORD dwReserved0;
659     DWORD dwReserved1;
660     TCHAR cFileName[MAX_PATH];
661     TCHAR cAlternateFileName[14];
662 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
663 */
664
665 static int
666 copyin(struct dirent &dp, const char *fname)
667 {
668     dp.d_ino = 0;
669     dp.d_reclen = 0;
670     char *cp = dp.d_name;
671     while (*fname) {
672         *cp++ = *fname++;
673         dp.d_reclen++;
674     }
675         *cp = 0;
676     return dp.d_reclen;
677 }
678
679 int
680 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
681 {
682     _dir *dp = (_dir *)dirp;
683     if (dp->valid) {
684         entry->d_off = dp->offset;
685                 
686 #if USE_WIN32_UNICODE
687                 char szBuf[MAX_PATH_UTF8];
688                 wchar_2_UTF8(szBuf,dp->data.cFileName);
689                 dp->offset += copyin(*entry, szBuf);
690 #else
691         dp->offset += copyin(*entry, dp->data.cFileName);
692 #endif
693                 
694         *result = entry;              /* return entry address */
695         d_msg(__FILE__, __LINE__,
696               99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
697               dirp, entry->d_name, entry->d_reclen, entry->d_off);
698     } else {
699 //      d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
700         errno = b_errno_win32;
701         return -1;
702     }
703
704 #if USE_WIN32_UNICODE
705         dp->valid = FindNextFileW(dp->dirh, &dp->data);
706 #else
707         dp->valid = FindNextFileA(dp->dirh, &dp->data);
708 #endif
709     
710     return 0;
711 }
712
713 /*
714  * Dotted IP address to network address
715  *
716  * Returns 1 if  OK
717  *         0 on error
718  */
719 int
720 inet_aton(const char *a, struct in_addr *inp)
721 {
722    const char *cp = a;
723    uint32_t acc = 0, tmp = 0;
724    int dotc = 0;
725
726    if (!isdigit(*cp)) {         /* first char must be digit */
727       return 0;                 /* error */
728    }
729    do {
730       if (isdigit(*cp)) {
731          tmp = (tmp * 10) + (*cp -'0');
732       } else if (*cp == '.' || *cp == 0) {
733          if (tmp > 255) {
734             return 0;           /* error */
735          }
736          acc = (acc << 8) + tmp;
737          dotc++;
738          tmp = 0;
739       } else {
740          return 0;              /* error */
741       }
742    } while (*cp++ != 0);
743    if (dotc != 4) {              /* want 3 .'s plus EOS */
744       return 0;                  /* error */
745    }
746    inp->s_addr = htonl(acc);     /* store addr in network format */
747    return 1;
748 }
749
750 int
751 nanosleep(const struct timespec *req, struct timespec *rem)
752 {
753     if (rem)
754         rem->tv_sec = rem->tv_nsec = 0;
755     Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
756     return 0;
757 }
758
759 void
760 init_signals(void terminate(int sig))
761 {
762
763 }
764
765 void
766 init_stack_dump(void)
767 {
768
769 }
770
771
772 long
773 pathconf(const char *path, int name)
774 {
775     switch(name) {
776     case _PC_PATH_MAX :
777         if (strncmp(path, "\\\\?\\", 4) == 0)
778             return 32767;
779     case _PC_NAME_MAX :
780         return 255;
781     }
782     errno = ENOSYS;                                                         
783     return -1;
784 }
785
786 int
787 WSA_Init(void)
788 {
789     WORD wVersionRequested = MAKEWORD( 1, 1);
790     WSADATA wsaData;
791
792     int err = WSAStartup(wVersionRequested, &wsaData);
793
794
795     if (err != 0) {
796         printf("Can not start Windows Sockets\n");
797         errno = ENOSYS;
798         return -1;
799     }
800
801     return 0;
802 }
803
804
805 int
806 win32_chdir(const char *dir)
807 {
808 #if USE_WIN32_UNICODE
809         WCHAR szBuf[MAX_PATH_UNICODE];
810         UTF8_2_wchar(szBuf, dir, MAX_PATH_UNICODE);
811
812         if (0 == SetCurrentDirectoryW(szBuf)) {
813        errno = b_errno_win32;
814        return -1;
815     }
816 #else
817         if (0 == SetCurrentDirectoryA(dir)) {
818        errno = b_errno_win32;
819        return -1;
820     }
821 #endif
822    
823     return 0;
824 }
825
826 int
827 win32_mkdir(const char *dir)
828 {
829 #if USE_WIN32_UNICODE
830         WCHAR szBuf[MAX_PATH_UNICODE];
831         UTF8_2_wchar(szBuf, dir, MAX_PATH_UNICODE);
832
833         return _wmkdir(szBuf);
834 #else
835         return _mkdir(dir);     
836 #endif
837 }
838
839
840 char *
841 win32_getcwd(char *buf, int maxlen)
842 {
843 #if USE_WIN32_UNICODE
844         WCHAR szBuf[MAX_PATH_UNICODE];
845         int n = GetCurrentDirectoryW(maxlen, szBuf);
846         n = wchar_2_UTF8 (buf, szBuf, maxlen)-1;
847 #else
848    int n =  GetCurrentDirectory(maxlen, buf);
849 #endif
850
851    if (n == 0 || n > maxlen) return NULL;
852
853    if (n+1 > maxlen) return NULL;
854    if (n != 3) {
855        buf[n] = '\\';
856        buf[n+1] = 0;
857    }
858    return buf;
859 }
860
861 #include "mswinver.h"
862
863 char WIN_VERSION_LONG[64];
864 char WIN_VERSION[32];
865
866 class winver {
867 public:
868     winver(void);
869 };
870
871 static winver INIT;                     // cause constructor to be called before main()
872
873 #include "bacula.h"
874 #include "jcr.h"
875
876 winver::winver(void)
877 {
878     const char *version = "";
879     const char *platform = "";
880     OSVERSIONINFO osvinfo;
881     osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
882
883     // Get the current OS version
884     if (!GetVersionEx(&osvinfo)) {
885         version = "Unknown";
886         platform = "Unknown";
887     }
888     else
889         switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
890         {
891         case MS_WINDOWS_95: (version =  "Windows 95"); break;
892         case MS_WINDOWS_98: (version =  "Windows 98"); break;
893         case MS_WINDOWS_ME: (version =  "Windows ME"); break;
894         case MS_WINDOWS_NT4:(version =  "Windows NT 4.0"); platform = "NT"; break;
895         case MS_WINDOWS_2K: (version =  "Windows 2000");platform = "NT"; break;
896         case MS_WINDOWS_XP: (version =  "Windows XP");platform = "NT"; break;
897         case MS_WINDOWS_S2003: (version =  "Windows Server 2003");platform = "NT"; break;
898         default: version = "Windows ??"; break;
899         }
900
901     bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
902     snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
903              platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
904
905 #if 0
906     HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
907     CloseHandle(h);
908 #endif
909 #if 0
910     BPIPE *b = open_bpipe("ls -l", 10, "r");
911     char buf[1024];
912     while (!feof(b->rfd)) {
913         fgets(buf, sizeof(buf), b->rfd);
914     }
915     close_bpipe(b);
916 #endif
917 }
918
919 BOOL CreateChildProcess(VOID);
920 VOID WriteToPipe(VOID);
921 VOID ReadFromPipe(VOID);
922 VOID ErrorExit(LPTSTR);
923 VOID ErrMsg(LPTSTR, BOOL);
924
925 /**
926  * Check for a quoted path,  if an absolute path name is given and it contains
927  * spaces it will need to be quoted.  i.e.  "c:/Program Files/foo/bar.exe"
928  * CreateProcess() says the best way to ensure proper results with executables
929  * with spaces in path or filename is to quote the string.
930  */
931 const char *
932 getArgv0(const char *cmdline)
933 {
934
935     int inquote = 0;
936     for (const char *cp = cmdline; *cp; cp++)
937     {
938         if (*cp == '"') {
939             inquote = !inquote;
940         }
941         if (!inquote && isspace(*cp))
942             break;
943     }
944
945         
946     int len = cp - cmdline;
947     char *rval = (char *)malloc(len+1);
948
949     cp = cmdline;
950     char *rp = rval;
951     
952     while (len--)
953         *rp++ = *cp++;
954
955     *rp = 0;
956     return rval;
957 }
958
959
960 /**
961  * OK, so it would seem CreateProcess only handles true executables:
962  *  .com or .exe files.
963  * So test to see whether we're getting a .bat file and if so grab
964  * $COMSPEC value and pass batch file to it.
965  */
966 HANDLE
967 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
968 {
969     PROCESS_INFORMATION piProcInfo;
970     STARTUPINFO siStartInfo;
971     BOOL bFuncRetn = FALSE;
972
973     // Set up members of the PROCESS_INFORMATION structure.
974
975     ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
976
977     // Set up members of the STARTUPINFO structure.
978
979     ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
980     siStartInfo.cb = sizeof(STARTUPINFO);
981     // setup new process to use supplied handles for stdin,stdout,stderr
982     // if supplied handles are not used the send a copy of our STD_HANDLE
983     // as appropriate
984     siStartInfo.dwFlags = STARTF_USESTDHANDLES;
985
986     if (in != INVALID_HANDLE_VALUE)
987         siStartInfo.hStdInput = in;
988     else
989         siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
990
991     if (out != INVALID_HANDLE_VALUE)
992         siStartInfo.hStdOutput = out;
993     else
994         siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
995     if (err != INVALID_HANDLE_VALUE)
996         siStartInfo.hStdError = err;
997     else
998         siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
999     // Create the child process.
1000
1001     char exeFile[256];
1002
1003     const char *comspec = getenv("COMSPEC");
1004     
1005     if (comspec == NULL) // should never happen
1006         return INVALID_HANDLE_VALUE;
1007
1008     char *cmdLine = (char *)alloca(strlen(cmdline) + strlen(comspec) + 16);
1009     
1010     strcpy(exeFile, comspec);
1011     strcpy(cmdLine, comspec);
1012     strcat(cmdLine, " /c ");
1013     strcat(cmdLine, cmdline);
1014
1015     // try to execute program
1016     bFuncRetn = CreateProcess(exeFile,
1017                               cmdLine, // command line
1018                               NULL, // process security attributes
1019                               NULL, // primary thread security attributes
1020                               TRUE, // handles are inherited
1021                               0, // creation flags
1022                               NULL, // use parent's environment
1023                               NULL, // use parent's current directory
1024                               &siStartInfo, // STARTUPINFO pointer
1025                               &piProcInfo); // receives PROCESS_INFORMATION
1026
1027     if (bFuncRetn == 0) {
1028         ErrorExit("CreateProcess failed\n");
1029         const char *err = errorString();
1030         d_msg(__FILE__, __LINE__, 99,
1031               "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err);
1032         LocalFree((void *)err);
1033         return INVALID_HANDLE_VALUE;
1034     }
1035     // we don't need a handle on the process primary thread so we close
1036     // this now.
1037     CloseHandle(piProcInfo.hThread);
1038
1039     return piProcInfo.hProcess;
1040 }
1041
1042
1043 void
1044 ErrorExit (LPTSTR lpszMessage)
1045 {
1046     d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
1047 }
1048
1049
1050 /*
1051 typedef struct s_bpipe {
1052    pid_t worker_pid;
1053    time_t worker_stime;
1054    int wait;
1055    btimer_t *timer_id;
1056    FILE *rfd;
1057    FILE *wfd;
1058 } BPIPE;
1059 */
1060
1061 static void
1062 CloseIfValid(HANDLE handle)
1063 {
1064     if (handle != INVALID_HANDLE_VALUE)
1065         CloseHandle(handle);
1066 }
1067
1068 #ifndef HAVE_MINGW
1069 BPIPE *
1070 open_bpipe(char *prog, int wait, const char *mode)
1071 {
1072     HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
1073         hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1074         hInputFile;
1075
1076     SECURITY_ATTRIBUTES saAttr;
1077
1078     BOOL fSuccess;
1079
1080     hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1081         hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1082         hInputFile = INVALID_HANDLE_VALUE;
1083
1084     BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1085     memset((void *)bpipe, 0, sizeof(BPIPE));
1086
1087     int mode_read = (mode[0] == 'r');
1088     int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1089
1090
1091     // Set the bInheritHandle flag so pipe handles are inherited.
1092
1093     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1094     saAttr.bInheritHandle = TRUE;
1095     saAttr.lpSecurityDescriptor = NULL;
1096
1097     if (mode_read) {
1098
1099         // Create a pipe for the child process's STDOUT.
1100         if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1101             ErrorExit("Stdout pipe creation failed\n");
1102             goto cleanup;
1103         }
1104         // Create noninheritable read handle and close the inheritable read
1105         // handle.
1106
1107         fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1108                                    GetCurrentProcess(), &hChildStdoutRdDup , 0,
1109                                    FALSE,
1110                                    DUPLICATE_SAME_ACCESS);
1111         if ( !fSuccess ) {
1112             ErrorExit("DuplicateHandle failed");
1113             goto cleanup;
1114         }
1115
1116         CloseHandle(hChildStdoutRd);
1117         hChildStdoutRd = INVALID_HANDLE_VALUE;
1118     }
1119
1120     if (mode_write) {
1121
1122         // Create a pipe for the child process's STDIN.
1123
1124         if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1125             ErrorExit("Stdin pipe creation failed\n");
1126             goto cleanup;
1127         }
1128
1129         // Duplicate the write handle to the pipe so it is not inherited.
1130         fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1131                                    GetCurrentProcess(), &hChildStdinWrDup,
1132                                    0,
1133                                    FALSE,                  // not inherited
1134                                    DUPLICATE_SAME_ACCESS);
1135         if (!fSuccess) {
1136             ErrorExit("DuplicateHandle failed");
1137             goto cleanup;
1138         }
1139
1140         CloseHandle(hChildStdinWr);
1141         hChildStdinWr = INVALID_HANDLE_VALUE;
1142     }
1143     // spawn program with redirected handles as appropriate
1144     bpipe->worker_pid = (pid_t)
1145         CreateChildProcess(prog,             // commandline
1146                            hChildStdinRd,    // stdin HANDLE
1147                            hChildStdoutWr,   // stdout HANDLE
1148                            hChildStdoutWr);  // stderr HANDLE
1149
1150     if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1151         goto cleanup;
1152
1153     bpipe->wait = wait;
1154     bpipe->worker_stime = time(NULL);
1155
1156     if (mode_read) {
1157         CloseHandle(hChildStdoutWr); // close our write side so when
1158                                      // process terminates we can
1159                                      // detect eof.
1160         // ugly but convert WIN32 HANDLE to FILE*
1161         int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1162         if (rfd >= 0) {
1163            bpipe->rfd = _fdopen(rfd, "r");
1164         }
1165     }
1166     if (mode_write) {
1167         CloseHandle(hChildStdinRd); // close our read side so as not
1168                                     // to interfre with child's copy
1169         // ugly but convert WIN32 HANDLE to FILE*
1170         int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1171         if (wfd >= 0) {
1172            bpipe->wfd = _fdopen(wfd, "w");
1173         }
1174     }
1175
1176     if (wait > 0) {
1177         bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1178     }
1179
1180     return bpipe;
1181
1182 cleanup:
1183
1184     CloseIfValid(hChildStdoutRd);
1185     CloseIfValid(hChildStdoutRdDup);
1186     CloseIfValid(hChildStdinWr);
1187     CloseIfValid(hChildStdinWrDup);
1188
1189     free((void *) bpipe);
1190     errno = b_errno_win32;            /* do GetLastError() for error code */
1191     return NULL;
1192 }
1193
1194 #endif //HAVE_MINGW
1195
1196 int
1197 kill(int pid, int signal)
1198 {
1199    int rval = 0;
1200    if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
1201       rval = -1;
1202       errno = b_errno_win32;
1203    }
1204    CloseHandle((HANDLE)pid);
1205    return rval;
1206 }
1207
1208 #ifndef HAVE_MINGW
1209
1210 int
1211 close_bpipe(BPIPE *bpipe)
1212 {
1213    int rval = 0;
1214    int32_t remaining_wait = bpipe->wait;
1215
1216    if (remaining_wait == 0) {         /* wait indefinitely */
1217       remaining_wait = INT32_MAX;
1218    }
1219    for ( ;; ) {
1220       DWORD exitCode;
1221       if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
1222          const char *err = errorString();
1223          rval = b_errno_win32;
1224          d_msg(__FILE__, __LINE__, 0,
1225                "GetExitCode error %s\n", err);
1226          LocalFree((void *)err);
1227          break;
1228       }
1229       if (exitCode == STILL_ACTIVE) {
1230          if (remaining_wait <= 0) {
1231             rval = ETIME;             /* timed out */
1232             break;
1233          }
1234          bmicrosleep(1, 0);           /* wait one second */
1235          remaining_wait--;
1236       } else if (exitCode != 0) {
1237          /* Truncate exit code as it doesn't seem to be correct */
1238          rval = (exitCode & 0xFF) | b_errno_exit;
1239          break;
1240       } else {
1241          break;                       /* Shouldn't get here */
1242       }
1243    }
1244
1245    if (bpipe->timer_id) {
1246        stop_child_timer(bpipe->timer_id);
1247    }
1248    if (bpipe->rfd) fclose(bpipe->rfd);
1249    if (bpipe->wfd) fclose(bpipe->wfd);
1250    free((void *)bpipe);
1251    return rval;
1252 }
1253
1254 int
1255 close_wpipe(BPIPE *bpipe)
1256 {
1257     int stat = 1;
1258
1259     if (bpipe->wfd) {
1260         fflush(bpipe->wfd);
1261         if (fclose(bpipe->wfd) != 0) {
1262             stat = 0;
1263         }
1264         bpipe->wfd = NULL;
1265     }
1266     return stat;
1267 }
1268
1269 #include "findlib/find.h"
1270
1271 int
1272 utime(const char *fname, struct utimbuf *times)
1273 {
1274     FILETIME acc, mod;
1275     char tmpbuf[1024];
1276
1277     cygwin_conv_to_win32_path(fname, tmpbuf);
1278
1279     cvt_utime_to_ftime(times->actime, acc);
1280     cvt_utime_to_ftime(times->modtime, mod);
1281
1282
1283 #if USE_WIN32_UNICODE
1284         WCHAR szBuf[MAX_PATH_UNICODE];
1285         UTF8_2_wchar(szBuf, tmpbuf, MAX_PATH_UNICODE);
1286
1287         HANDLE h = CreateFileW(szBuf,
1288                           FILE_WRITE_ATTRIBUTES,
1289                           FILE_SHARE_WRITE,
1290                           NULL,
1291                           OPEN_EXISTING,
1292                           0,
1293                           NULL);
1294 #else
1295     HANDLE h = CreateFile(tmpbuf,
1296                           FILE_WRITE_ATTRIBUTES,
1297                           FILE_SHARE_WRITE,
1298                           NULL,
1299                           OPEN_EXISTING,
1300                           0,
1301                           NULL);
1302
1303 #endif
1304
1305
1306     if (h == INVALID_HANDLE_VALUE) {
1307         const char *err = errorString();
1308         d_msg(__FILE__, __LINE__, 99,
1309               "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err);
1310         LocalFree((void *)err);
1311         errno = b_errno_win32;
1312         return -1;
1313     }
1314
1315     int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1316     CloseHandle(h);
1317     if (rval == -1) {
1318        errno = b_errno_win32;
1319     }
1320     return rval;
1321 }
1322
1323 #if USE_WIN32_COMPAT_IO
1324
1325 int
1326 open(const char *file, int flags, int mode)
1327 {
1328 #if USE_WIN32_UNICODE
1329         WCHAR szBuf[MAX_PATH_UNICODE];
1330         UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
1331
1332         return _wopen(szBuf, flags|_O_BINARY, mode);
1333 #else
1334     return _open(file, flags|_O_BINARY, mode);
1335 #endif
1336 }
1337
1338 /*
1339  * Note, this works only for a file. If you want
1340  *   to close a socket, use closesocket(). 
1341  *   Bacula has been modified in src/lib/bnet.c
1342  *   to use closesocket().
1343  */
1344 int
1345 close(int fd)
1346 {
1347     return _close(fd);
1348 }
1349
1350 #ifndef HAVE_WXCONSOLE
1351 ssize_t
1352 read(int fd, void *buf, ssize_t len)
1353 {
1354     return _read(fd, buf, (size_t)len);
1355 }
1356
1357 ssize_t
1358 write(int fd, const void *buf, ssize_t len)
1359 {
1360     return _write(fd, buf, (size_t)len);
1361 }
1362 #endif
1363
1364 off_t
1365 lseek(int fd, off_t offset, int whence)
1366 {
1367     return _lseeki64(fd, offset, whence);
1368 }
1369
1370 int
1371 dup2(int fd1, int fd2)
1372 {
1373     return _dup2(fd1, fd2);
1374 }
1375 #else
1376 int
1377 open(const char *file, int flags, int mode)
1378 {
1379     DWORD access = 0;
1380     DWORD shareMode = 0;
1381     DWORD create = 0;
1382     DWORD msflags = 0;
1383     HANDLE foo;
1384     const char *remap = file;
1385
1386     if (flags & O_WRONLY) access = GENERIC_WRITE;
1387     else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1388     else access = GENERIC_READ;
1389
1390     if (flags & O_CREAT) create = CREATE_NEW;
1391     else create = OPEN_EXISTING;
1392
1393     if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
1394
1395     if (!(flags & O_EXCL))
1396         shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
1397
1398     if (flags & O_APPEND) {
1399         printf("open...APPEND not implemented yet.");
1400         exit(-1);
1401     }
1402
1403
1404 #if USE_WIN32_UNICODE
1405         WCHAR szBuf[MAX_PATH_UNICODE];
1406         UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
1407
1408         foo = CreateFileW(szBuf, access, shareMode, NULL, create, msflags, NULL);
1409 #else
1410     foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1411 #endif
1412
1413
1414     if (INVALID_HANDLE_VALUE == foo) {
1415         errno = b_errno_win32;
1416         return(int) -1;
1417     }
1418     return (int)foo;
1419
1420 }
1421
1422
1423 int
1424 close(int fd)
1425 {
1426     if (!CloseHandle((HANDLE)fd)) {
1427         errno = b_errno_win32;
1428         return -1;
1429     }
1430
1431     return 0;
1432 }
1433
1434 ssize_t
1435 write(int fd, const void *data, ssize_t len)
1436 {
1437     BOOL status;
1438     DWORD bwrite;
1439     status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1440     if (status) return bwrite;
1441     errno = b_errno_win32;
1442     return -1;
1443 }
1444
1445
1446 ssize_t
1447 read(int fd, void *data, ssize_t len)
1448 {
1449     BOOL status;
1450     DWORD bread;
1451
1452     status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1453     if (status) return bread;
1454     errno = b_errno_win32;
1455     return -1;
1456 }
1457
1458 off_t
1459 lseek(int fd, off_t offset, int whence)
1460 {
1461     DWORD method = 0;
1462     DWORD val;
1463     switch (whence) {
1464     case SEEK_SET :
1465         method = FILE_BEGIN;
1466         break;
1467     case SEEK_CUR:
1468         method = FILE_CURRENT;
1469         break;
1470     case SEEK_END:
1471         method = FILE_END;
1472         break;
1473     default:
1474         errno = EINVAL;
1475         return -1;
1476     }
1477
1478     if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) {
1479        errno = b_errno_win32;
1480        return -1;
1481     }
1482     /* ***FIXME*** I doubt this works right */
1483     return val;
1484 }
1485
1486 int
1487 dup2(int, int)
1488 {
1489     errno = ENOSYS;
1490     return -1;
1491 }
1492
1493
1494 #endif
1495
1496 #endif //HAVE_MINGW
1497
1498 #ifdef HAVE_MINGW
1499 /* syslog function, added by Nicolas Boichat */
1500 void closelog() {}
1501 #endif //HAVE_MINGW