Parent: [c6e228] (diff)

Download this file

safeunistd.h    80 lines (67 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* safeunistd.h: <unistd.h>, but with compat. and large file support for MSVC.
*
* Copyright (C) 2007 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifndef XAPIAN_INCLUDED_SAFEUNISTD_H
#define XAPIAN_INCLUDED_SAFEUNISTD_H
#ifndef _MSC_VER
# include <unistd.h>
#else
// sys/types.h has a typedef for off_t so make sure we've seen that before
// we hide it behind a #define.
# include <sys/types.h>
// MSVC doesn't even HAVE unistd.h - io.h seems the nearest equivalent.
// We also need to do some renaming of functions to get versions which
// work on large files.
# include <io.h>
# ifdef lseek
# undef lseek
# endif
# ifdef off_t
# undef off_t
# endif
# define lseek(FD, OFF, WHENCE) _lseeki64(FD, OFF, WHENCE)
# define off_t __int64
// process.h is needed for getpid().
# include <process.h>
#endif
#ifdef __WIN32__
#ifdef _MSC_VER
/* Recent MinGW versions define this */
inline unsigned int
sleep(unsigned int seconds)
{
// Use our own little helper function to avoid pulling in <windows.h>.
extern void xapian_sleep_milliseconds(unsigned int millisecs);
// Sleep takes a time interval in milliseconds, whereas POSIX sleep takes
// a time interval in seconds, so we need to multiply 'seconds' by 1000.
//
// But make sure the multiplication won't overflow! 4294967 seconds is
// nearly 50 days, so just sleep for that long and return the number of
// seconds left to sleep for. The common case of sleep(CONSTANT) should
// optimise to just xapian_sleep_milliseconds(CONSTANT).
if (seconds > 4294967u) {
xapian_sleep_milliseconds(4294967000u);
return seconds - 4294967u;
}
xapian_sleep_milliseconds(seconds * 1000u);
return 0;
}
#endif /* _MSC_VER*/
#endif /* __WIN32__ */
#endif /* XAPIAN_INCLUDED_SAFEUNISTD_H */