blob: 99b7300d372b21ce4abdfa3b350ce737f2f515fa [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2001-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <cc++/config.h>
40#include <cc++/export.h>
41#include <cc++/strchar.h>
42
43#include <cstdio>
44#include <cstdlib>
45#include <cstring>
46
47#ifdef WIN32
48#ifndef _WIN32_WCE
49#include <sys/timeb.h>
50#endif
51#else
52
53#ifdef HAVE_SYS_PARAM_H
54#include <sys/param.h>
55#endif
56
57#ifdef HAVE_SYS_FILE_H
58#include <sys/file.h>
59#endif
60
61#ifdef HAVE_SYS_LOCKF_H
62#include <sys/lockf.h>
63#endif
64
65#ifdef COMMON_AIX_FIXES
66#undef LOCK_EX
67#undef LOCK_SH
68#endif
69
70#ifndef F_LOCK
71#define MISSING_LOCKF
72
73enum {
74 F_ULOCK = 1,
75 F_LOCK,
76 F_TLOCK,
77 F_TEST
78};
79#endif
80
81#endif
82
83#ifdef CCXX_NAMESPACES
84namespace ost {
85using namespace std;
86#endif
87
88#ifdef WIN32
89#ifdef _WIN32_WCE
90int gettimeofday(struct timeval *tv_, void *tz_)
91{
92 // We could use _ftime(), but it is not available on WinCE.
93 // (WinCE also lacks time.h)
94 // Note also that the average error of _ftime is around 20 ms :)
95 DWORD ms = GetTickCount();
96 tv_->tv_sec = ms / 1000;
97 tv_->tv_usec = ms * 1000;
98 return 0;
99}
100#else
101
102int gettimeofday(struct timeval *tv_, void *tz_)
103{
104#if defined(_MSC_VER) && _MSC_VER >= 1300
105 struct __timeb64 tb;
106 _ftime64(&tb);
107#else
108# ifndef __BORLANDC__
109 struct _timeb tb;
110 _ftime(&tb);
111# else
112 struct timeb tb;
113 ftime(&tb);
114# endif
115#endif
116 tv_->tv_sec = (long)tb.time;
117 tv_->tv_usec = tb.millitm * 1000;
118 return 0;
119}
120#endif
121#endif
122
123#ifndef WIN32
124#ifdef HAVE_GETTIMEOFDAY
125
126unsigned long getTicks(void)
127{
128 unsigned long ticks;
129
130 struct timeval now;
131 gettimeofday(&now, NULL);
132 ticks = now.tv_sec * 1000l;
133 ticks += now.tv_usec / 1000l;
134 return ticks;
135}
136
137#endif
138#else
139
140DWORD getTicks(void)
141{
142 return GetTickCount();
143}
144
145#endif
146
147#ifndef HAVE_STRDUP
148char *strdup(const char *str)
149{
150 if(!str)
151 return NULL;
152
153 size_t len = strlen(str) + 1;
154 char *dest = (char *)malloc(len);
155
156 if(!dest)
157 return NULL;
158
159 return setString(dest, len, str);
160}
161#endif
162
163#ifndef HAVE_MEMMOVE
164void *memmove (char *dest, const char *source, size_t length)
165{
166 char *save = dest;
167 if (source < dest) {
168 for (source += length, dest += length; length; --length)
169 *--dest = *--source;
170 }
171 else if (source != dest) {
172 for (; length; --length)
173 *dest++ = *source++;
174 }
175 return (void *) save;
176}
177#endif
178
179#ifndef HAVE_LOCKF
180int lockf(int fd, int cmd, long len)
181{
182 struct flock lck;
183
184 lck.l_start = 0l;
185 lck.l_whence = SEEK_CUR;
186 lck.l_len = len;
187
188 switch(cmd) {
189 case F_ULOCK:
190 lck.l_type = F_UNLCK;
191 return fcntl(fd, F_SETLK, &lck);
192 case F_LOCK:
193 lck.l_type = F_WRLCK;
194 return fcntl(fd, F_SETLKW, &lck);
195 case F_TLOCK:
196 lck.l_type = F_WRLCK;
197 return fcntl(fd, F_SETLK, &lck);
198 case F_TEST:
199 lck.l_type = F_WRLCK;
200 fcntl(fd, F_GETLK, &lck);
201 if(lck.l_type == F_UNLCK)
202 return 0;
203 return -1;
204 }
205}
206#endif
207
208#ifdef CCXX_NAMESPACES
209}
210#endif