Sat, 31 Jan 2004 02:02:46 +0000
[gaim-migrate @ 8911]
Daniel Atallah noticed a problem with my changes to the disconnect
account dialog and submitted a patch. I did this instead because
I think it's cleaner, but it might be an iota or three slower. But
really, if you think about it, what's just one little iota when you
sleep for BILLIONS of iotas a DAY?
| 3712 | 1 | /* |
|
5913
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
2 | * gaim |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
3 | * |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
4 | * File: libc_interface.c |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
5 | * Date: October 14, 2002 |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
6 | * Description: libc interface for Windows api |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
7 | * |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
8 | * Copyright (C) 2002-2003, Herman Bloggs <hermanator12002@yahoo.com> |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
9 | * |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
10 | * This program is free software; you can redistribute it and/or modify |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
11 | * it under the terms of the GNU General Public License as published by |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
12 | * the Free Software Foundation; either version 2 of the License, or |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
13 | * (at your option) any later version. |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
14 | * |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
15 | * This program is distributed in the hope that it will be useful, |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
18 | * GNU General Public License for more details. |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
19 | * |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
20 | * You should have received a copy of the GNU General Public License |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
21 | * along with this program; if not, write to the Free Software |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
23 | * |
| 3712 | 24 | */ |
| 25 | #include <winsock.h> | |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
26 | #include <io.h> |
| 3712 | 27 | #include <stdlib.h> |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
28 | #include <stdio.h> |
| 3712 | 29 | #include <errno.h> |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
30 | #include <sys/timeb.h> |
|
4905
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
31 | #include <sys/stat.h> |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
32 | #include <time.h> |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5913
diff
changeset
|
33 | #include "debug.h" |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
34 | #include "libc_internal.h" |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
35 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
36 | /* |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
37 | * PROTOS |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
38 | */ |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
39 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
40 | /* |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
41 | * LOCALS |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
42 | */ |
| 3712 | 43 | |
| 44 | static char errbuf[1024]; | |
| 45 | ||
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
46 | /* |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
47 | * CODE |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
48 | */ |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
49 | |
| 3712 | 50 | /* helpers */ |
| 51 | static int wgaim_is_socket( int fd ) { | |
| 52 | int optval; | |
| 53 | unsigned int optlen = sizeof(int); | |
| 54 | ||
| 55 | if( (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&optval, &optlen)) == SOCKET_ERROR ) { | |
| 56 | int error = WSAGetLastError(); | |
| 57 | if( error == WSAENOTSOCK ) | |
| 58 | return FALSE; | |
| 59 | else { | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5913
diff
changeset
|
60 | gaim_debug(GAIM_DEBUG_WARNING, "wgaim", "wgaim_is_socket: getsockopt returned error: %d\n", error); |
| 3712 | 61 | return FALSE; |
| 62 | } | |
| 63 | } | |
| 64 | return TRUE; | |
| 65 | } | |
| 66 | ||
| 67 | /* socket.h */ | |
| 68 | int wgaim_socket (int namespace, int style, int protocol) { | |
| 69 | int ret; | |
| 70 | ||
| 71 | ret = socket( namespace, style, protocol ); | |
| 72 | ||
| 73 | if( ret == INVALID_SOCKET ) { | |
| 74 | errno = WSAGetLastError(); | |
| 75 | return -1; | |
| 76 | } | |
| 77 | return ret; | |
| 78 | } | |
| 79 | ||
| 80 | int wgaim_connect(int socket, struct sockaddr *addr, u_long length) { | |
| 81 | int ret; | |
| 82 | ||
| 83 | ret = connect( socket, addr, length ); | |
| 84 | ||
| 85 | if( ret == SOCKET_ERROR ) { | |
| 86 | errno = WSAGetLastError(); | |
| 87 | if( errno == WSAEWOULDBLOCK ) | |
| 88 | errno = WSAEINPROGRESS; | |
| 89 | return -1; | |
| 90 | } | |
| 91 | return 0; | |
| 92 | } | |
| 93 | ||
| 94 | int wgaim_getsockopt(int socket, int level, int optname, void *optval, unsigned int *optlenptr) { | |
| 95 | int ret; | |
| 96 | ||
| 97 | ret = getsockopt( socket, level, optname, optval, optlenptr ); | |
| 98 | if( ret == SOCKET_ERROR ) { | |
| 99 | errno = WSAGetLastError(); | |
| 100 | return -1; | |
| 101 | } | |
| 102 | ||
| 103 | return 0; | |
| 104 | } | |
| 105 | ||
| 106 | /* fcntl.h */ | |
| 107 | /* This is not a full implementation of fcntl. Update as needed.. */ | |
| 108 | int wgaim_fcntl(int socket, int command, int val) { | |
| 109 | switch( command ) { | |
| 110 | case F_SETFL: | |
| 111 | { | |
| 112 | int ret=0; | |
| 113 | ||
| 114 | switch( val ) { | |
| 115 | case O_NONBLOCK: | |
| 116 | { | |
| 117 | u_long imode=1; | |
| 118 | ret = ioctlsocket(socket, FIONBIO, &imode); | |
| 119 | break; | |
| 120 | } | |
| 121 | case 0: | |
| 122 | { | |
| 123 | u_long imode=0; | |
| 124 | ret = ioctlsocket(socket, FIONBIO, &imode); | |
| 125 | break; | |
| 126 | } | |
| 127 | default: | |
| 128 | errno = EINVAL; | |
| 129 | return -1; | |
| 130 | }/*end switch*/ | |
| 131 | if( ret == SOCKET_ERROR ) { | |
| 132 | errno = WSAGetLastError(); | |
| 133 | return -1; | |
| 134 | } | |
| 135 | return 0; | |
| 136 | } | |
| 137 | default: | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5913
diff
changeset
|
138 | gaim_debug(GAIM_DEBUG_WARNING, "wgaim", "wgaim_fcntl: Unsupported command\n"); |
| 3712 | 139 | return -1; |
| 140 | }/*end switch*/ | |
| 141 | } | |
| 142 | ||
| 143 | /* sys/ioctl.h */ | |
| 144 | int wgaim_ioctl(int fd, int command, void* val) { | |
| 145 | switch( command ) { | |
| 146 | case FIONBIO: | |
| 147 | { | |
| 148 | if (ioctlsocket(fd, FIONBIO, (unsigned long *)val) == SOCKET_ERROR) { | |
| 149 | errno = WSAGetLastError(); | |
| 150 | return -1; | |
| 151 | } | |
| 152 | return 0; | |
| 153 | } | |
| 154 | default: | |
| 155 | errno = EINVAL; | |
| 156 | return -1; | |
| 157 | }/*end switch*/ | |
| 158 | } | |
| 159 | ||
| 160 | /* arpa/inet.h */ | |
| 161 | int wgaim_inet_aton(const char *name, struct in_addr *addr) { | |
| 162 | if((addr->s_addr = inet_addr(name)) == INADDR_NONE) | |
| 163 | return 0; | |
| 164 | else | |
| 165 | return 1; | |
| 166 | } | |
| 167 | ||
| 168 | /* netdb.h */ | |
| 169 | struct hostent* wgaim_gethostbyname(const char *name) { | |
| 170 | struct hostent *hp; | |
| 171 | ||
| 172 | if((hp = gethostbyname(name)) == NULL) { | |
| 173 | errno = WSAGetLastError(); | |
| 174 | return NULL; | |
| 175 | } | |
| 176 | return hp; | |
| 177 | } | |
| 178 | ||
| 179 | /* string.h */ | |
| 180 | char* wgaim_strerror( int errornum ) { | |
| 181 | if( errornum > WSABASEERR ) { | |
| 182 | sprintf( errbuf, "Windows socket error #%d", errornum ); | |
| 183 | return errbuf; | |
| 184 | } | |
| 185 | else | |
| 186 | return strerror( errornum ); | |
| 187 | } | |
| 188 | ||
|
4193
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
189 | /* From glibc 2.2.5 */ |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
190 | char* wgaim_strsep(char **stringp, const char *delim) { |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
191 | char *begin, *end; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
192 | |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
193 | begin = *stringp; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
194 | if (begin == NULL) |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
195 | return NULL; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
196 | |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
197 | /* A frequent case is when the delimiter string contains only one |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
198 | character. Here we don't need to call the expensive `strpbrk' |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
199 | function and instead work using `strchr'. */ |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
200 | if (delim[0] == '\0' || delim[1] == '\0') { |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
201 | char ch = delim[0]; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
202 | |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
203 | if (ch == '\0') |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
204 | end = NULL; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
205 | else { |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
206 | if (*begin == ch) |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
207 | end = begin; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
208 | else if (*begin == '\0') |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
209 | end = NULL; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
210 | else |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
211 | end = strchr (begin + 1, ch); |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
212 | } |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
213 | } |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
214 | else |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
215 | /* Find the end of the token. */ |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
216 | end = strpbrk (begin, delim); |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
217 | |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
218 | if (end) { |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
219 | /* Terminate the token and set *STRINGP past NUL character. */ |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
220 | *end++ = '\0'; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
221 | *stringp = end; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
222 | } |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
223 | else |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
224 | /* No more delimiters; this is the last token. */ |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
225 | *stringp = NULL; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
226 | |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
227 | return begin; |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
228 | } |
|
0f6072a0ffa4
[gaim-migrate @ 4424]
Herman Bloggs <herman@bluedigits.com>
parents:
3777
diff
changeset
|
229 | |
| 3712 | 230 | /* unistd.h */ |
| 231 | ||
| 232 | /* | |
| 233 | * We need to figure out whether fd is a file or socket handle. | |
| 234 | */ | |
| 235 | int wgaim_read(int fd, void *buf, unsigned int size) { | |
| 236 | int ret; | |
| 237 | ||
| 238 | if( wgaim_is_socket(fd) ) { | |
| 239 | if( (ret = recv(fd, buf, size, 0)) == SOCKET_ERROR ) { | |
| 240 | errno = WSAGetLastError(); | |
| 241 | return -1; | |
| 242 | } | |
| 243 | else if( ret == 0 ) { | |
| 244 | /* connection has been gracefully closed */ | |
| 245 | errno = WSAENOTCONN; | |
| 246 | return -1; | |
| 247 | } | |
| 248 | else { | |
| 249 | /* success reading socket */ | |
| 250 | return ret; | |
| 251 | } | |
| 252 | } | |
| 253 | else { | |
| 254 | /* fd is not a socket handle.. pass it off to read */ | |
| 255 | return read(fd, buf, size); | |
| 256 | } | |
| 257 | } | |
| 258 | ||
| 259 | int wgaim_write(int fd, const void *buf, unsigned int size) { | |
| 260 | int ret; | |
| 261 | ||
| 262 | if( wgaim_is_socket(fd) ) { | |
| 263 | if( (ret = send(fd, buf, size, 0)) == SOCKET_ERROR ) { | |
| 264 | errno = WSAGetLastError(); | |
| 265 | return -1; | |
| 266 | } | |
| 267 | else { | |
| 268 | /* success */ | |
| 269 | return ret; | |
| 270 | } | |
| 271 | ||
| 272 | } | |
| 273 | else | |
| 274 | return write(fd, buf, size); | |
| 275 | } | |
| 276 | ||
| 277 | int wgaim_close(int fd) { | |
| 278 | int ret; | |
| 279 | ||
| 280 | if( wgaim_is_socket(fd) ) { | |
| 281 | if( (ret = closesocket(fd)) == SOCKET_ERROR ) { | |
| 282 | errno = WSAGetLastError(); | |
| 283 | return -1; | |
| 284 | } | |
| 285 | else | |
| 286 | return 0; | |
| 287 | } | |
| 288 | else | |
| 289 | return close(fd); | |
| 290 | } | |
|
3777
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
291 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
292 | /* sys/time.h */ |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
293 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
294 | int wgaim_gettimeofday(struct timeval *p, struct timezone *z) { |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
295 | int res = 0; |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
296 | struct _timeb timebuffer; |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
297 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
298 | if (z != 0) { |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
299 | _tzset(); |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
300 | z->tz_minuteswest = _timezone/60; |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
301 | z->tz_dsttime = _daylight; |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
302 | } |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
303 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
304 | if (p != 0) { |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
305 | _ftime(&timebuffer); |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
306 | p->tv_sec = timebuffer.time; /* seconds since 1-1-1970 */ |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
307 | p->tv_usec = timebuffer.millitm*1000; /* microseconds */ |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
308 | } |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
309 | |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
310 | return res; |
|
a3f8adc4c3e0
[gaim-migrate @ 3917]
Herman Bloggs <herman@bluedigits.com>
parents:
3712
diff
changeset
|
311 | } |
|
4905
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
312 | |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
313 | /* stdio.h */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
314 | |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
315 | int wgaim_rename (const char *oldname, const char *newname) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
316 | struct _stat oldstat, newstat; |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
317 | |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
318 | if(_stat(oldname, &oldstat) == 0) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
319 | /* newname exists */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
320 | if(_stat(newname, &newstat) == 0) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
321 | /* oldname is a dir */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
322 | if(_S_ISDIR(oldstat.st_mode)) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
323 | if(!_S_ISDIR(newstat.st_mode)) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
324 | return rename(oldname, newname); |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
325 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
326 | /* newname is a dir */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
327 | else { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
328 | /* This is not quite right.. If newname is empty and |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
329 | is not a sub dir of oldname, newname should be |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
330 | deleted and oldname should be renamed. |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
331 | */ |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5913
diff
changeset
|
332 | gaim_debug(GAIM_DEBUG_WARNING, "wgaim", "wgaim_rename does not behave here as it should\n"); |
|
4905
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
333 | return rename(oldname, newname); |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
334 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
335 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
336 | /* oldname is not a dir */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
337 | else { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
338 | /* newname is a dir */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
339 | if(_S_ISDIR(newstat.st_mode)) { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
340 | errno = EISDIR; |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
341 | return -1; |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
342 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
343 | /* newname is not a dir */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
344 | else { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
345 | remove(newname); |
|
5084
21d2e7ec99ce
[gaim-migrate @ 5439]
Herman Bloggs <herman@bluedigits.com>
parents:
4905
diff
changeset
|
346 | return rename(oldname, newname); |
|
4905
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
347 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
348 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
349 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
350 | /* newname doesn't exist */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
351 | else |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
352 | return rename(oldname, newname); |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
353 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
354 | else { |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
355 | /* oldname doesn't exist */ |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
356 | errno = ENOENT; |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
357 | return -1; |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
358 | } |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
359 | |
|
1464e05926f0
[gaim-migrate @ 5239]
Herman Bloggs <herman@bluedigits.com>
parents:
4193
diff
changeset
|
360 | } |
|
5113
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
361 | |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
362 | /* time.h */ |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
363 | |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
364 | struct tm * wgaim_localtime_r (const time_t *time, struct tm *resultp) { |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
365 | struct tm* tmptm; |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
366 | |
|
5474
eec4dad3e035
[gaim-migrate @ 5868]
Herman Bloggs <herman@bluedigits.com>
parents:
5113
diff
changeset
|
367 | if(!time) |
|
5913
4d016c00bb36
[gaim-migrate @ 6345]
Herman Bloggs <herman@bluedigits.com>
parents:
5474
diff
changeset
|
368 | return NULL; |
|
5113
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
369 | tmptm = localtime(time); |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
370 | if(resultp && tmptm) |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
371 | return memcpy(resultp, tmptm, sizeof(struct tm)); |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
372 | else |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
373 | return NULL; |
|
8e545faf3d80
[gaim-migrate @ 5476]
Herman Bloggs <herman@bluedigits.com>
parents:
5084
diff
changeset
|
374 | } |