forked from snarfed/p4sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys.cpp
More file actions
139 lines (115 loc) · 3.58 KB
/
Copy pathsys.cpp
File metadata and controls
139 lines (115 loc) · 3.58 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* p4sync
* http://snarfed.org/p4sync
*
* Copyright 2005 Ryan Barrett <[email protected]>
*
* @file sys.cpp
*
* Contain functions for cross-platform portability.
*
* 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., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "sys.h"
#include <cassert>
#ifdef _WIN32
#include <errno.h>
#include <Winsock2.h>
/**
* Closes a socket or file, given a file descriptor.
*
* closesocket on win32 does the exact same thing as close(2) on *nix, it's
* just named differently. sigh.
*/
int close(int sock)
{
closesocket(sock);
}
/**
* gettimeofday() -- gets the current time in elapsed seconds and
* microsends since GMT Jan 1, 1970.
*
* ARGUMENTS: - Pointer to a timeval struct to return the time into
*
* RETURN CODES: - 0 on success
* -1 on failure
*
* We got this code from:
* http://www.freecode.com/viewsource/webstone/WebStone2.5-src::src::gettimeofday.c/
*/
int gettimeofday(struct timespec *curTimeP, int *junk)
{
struct _timeb localTime;
if (curTimeP == NULL) {
errno = EFAULT;
return (-1);
}
/**
* Compute the elapsed time since Jan 1, 1970 by first
* obtaining the elapsed time from the system using the
* _ftime(..) call and then convert to the "timeval"
* equivalent.
*/
_ftime(&localTime);
curTimeP->tv_sec = localTime.time + localTime.timezone;
curTimeP->tv_nsec = localTime.millitm * 1000;
return(0);
}
#endif // _WIN32
/** sleep_ms
*
* Like sleep(1), but allows millisecond sleep times. Uses select(2) under the
* covers.
*/
void sleep_ms(int ms) {
assert(ms >= 0);
struct timeval timeout = { ms / 1000, (ms % 1000) * 1000 };
select(0, 0, 0, 0, &timeout);
}
// note that this is not a substitue implementation on platforms without
// gettimeofday. it's an *overloaded* version that's more user friendly.
struct timeval gettimeofday() {
struct timeval tv;
gettimeofday(&tv, 0);
return tv;
}
long timeval_to_ms(const struct timeval &time) {
return time.tv_sec * 1000 + time.tv_usec / 1000;
}
struct timeval timeval_subtract(const struct timeval &x,
const struct timeval &y)
{
struct timeval result, u(x), v(y);
/* Perform the carry for the later subtraction by updating v. */
if (u.tv_usec < v.tv_usec) {
int nsec = (v.tv_usec - u.tv_usec) / 1000000 + 1;
v.tv_usec -= 1000000 * nsec;
v.tv_sec += nsec;
}
if (u.tv_usec - v.tv_usec > 1000000) {
int nsec = (u.tv_usec - v.tv_usec) / 1000000;
v.tv_usec += 1000000 * nsec;
v.tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result.tv_sec = u.tv_sec - v.tv_sec;
result.tv_usec = u.tv_usec - v.tv_usec;
return result;
}
struct timeval timeval_add(const struct timeval &x, const struct timeval &y) {
struct timeval sum = { x.tv_sec + y.tv_sec, x.tv_usec + y.tv_usec };
return sum;
}