2 Syren -- a lightweight downloader for Linux/BSD/Win/MacOSX
3 inspired by Axel Copyright 2001-2002 Wilmer van der Gaast
4 version 0.0.6 (atomic alien)
5 coded by Ketmar // Avalon Group
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License with
18 the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
19 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 Suite 330, Boston, MA 02111-1307 USA
23 Syren os-specific functions
32 double SyGetTimeD (void) {
34 return ((double)GetTickCount())/1000;
37 gettimeofday(&time
, 0);
38 return ((double)time
.tv_sec
+(double)time
.tv_usec
/1000000);
44 /* find subs in str; case insensitive; return pointer to start or NULL */
45 char *strcasestr (const char *str
, const char *subs
) {
46 const char *t1
= subs
, *t0
= str
;
47 int l0
= strlen(t0
), l1
= strlen(t1
);
50 if (CompareString(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
, t0
, l1
, t1
, l1
) == 2) return (char *)t0
;
59 #ifdef SY_INCLUDE_FILE_IO
60 TSyResult
SyDeleteFile (const char *fname
) {
62 if (DeleteFile(fname
)) return SY_OK
;
64 if (!unlink(fname
)) return SY_OK
;
70 int SyOpenFile (const char *fname
, TSyFileMode mode
, TSyBool mustCreate
) {
72 DWORD mm
= (mode
==SY_FMODE_WRITE
)?GENERIC_WRITE
:GENERIC_READ
;
73 DWORD crea
= (mustCreate
!=SY_FALSE
)?OPEN_ALWAYS
:OPEN_EXISTING
;
76 h
= CreateFile(fname
, mm
, FILE_SHARE_READ
, NULL
, crea
, FILE_ATTRIBUTE_NORMAL
, NULL
);
80 int mm
= (mode
==SY_FMODE_WRITE
)?O_WRONLY
:O_RDONLY
;
82 if (mustCreate
!= SY_FALSE
) mm
= mm
| O_CREAT
;
83 return open(fname
, mm
, SY_FILE_DEFMODE
);
88 TSyResult
SyWriteFile (int fd
, const void *buf
, int count
) {
91 if (!WriteFile((HANDLE
)fd
, buf
, count
, &wr
, NULL
)) return SY_ERROR
;
93 int wr
= write(fd
, buf
, count
);
95 if (wr
!= count
) return SY_ERROR
;
100 TSyResult
SyReadFile (int fd
, void *buf
, int count
) {
103 if (!ReadFile((HANDLE
)fd
, buf
, count
, &rd
, NULL
)) return -1;
105 int rd
= read(fd
, buf
, count
);
107 if (rd
!= count
) return SY_ERROR
;
112 TSyResult
SySeekFile (int fd
, int64_t offset
) {
115 LONG
*ofsp
= (void *)(&offset
);
116 if (offset
< 0) res
= SetFilePointer((HANDLE
)fd
, 0, NULL
, FILE_END
);
117 else res
= SetFilePointer((HANDLE
)fd
, *ofsp
, ofsp
+1, FILE_BEGIN
);
118 if (res
== (DWORD
)0xFFFFFFFF && GetLastError() != NO_ERROR
) return SY_ERROR
;
121 offset
= offset
<0?SY_LSEEK(fd
, offset
, SEEK_END
):SY_LSEEK(fd
, offset
, SEEK_SET
);
122 if (offset
< 0) return SY_ERROR
;
128 int64_t SyFileSize (int fd
) {
131 DWORD s0
, s1
, p0
, p1
= 0;
132 p0
= SetFilePointer((HANDLE
)fd
, 0, (LONG
*)&p1
, FILE_CURRENT
);
133 if (p0
== (DWORD
)0xFFFFFFFF && GetLastError() != NO_ERROR
) return -1;
134 s0
= SetFilePointer((HANDLE
)fd
, 0, (LONG
*)&s1
, FILE_END
);
135 if (p0
== (DWORD
)0xFFFFFFFF && GetLastError() != NO_ERROR
) return -1;
136 size
= (int64_t)s0
+((int64_t)s1
<<32);
137 p0
= SetFilePointer((HANDLE
)fd
, p0
, (LONG
*)&p1
, FILE_BEGIN
);
138 if (p0
== (DWORD
)0xFFFFFFFF && GetLastError() != NO_ERROR
) return SY_ERROR
;
140 int64_t ppos
= SY_LSEEK(fd
, 0, SEEK_CUR
);
141 if (ppos
< 0) return -1;
142 size
= SY_LSEEK(fd
, 0, SEEK_END
);
143 if (SY_LSEEK(fd
, ppos
, SEEK_SET
) < 0) return -1;
150 TSyResult
SySocketInit (void) {
153 WORD wVersionRequested
= MAKEWORD(2, 0);
154 int err
= WSAStartup(wVersionRequested
, &wsaData
);
156 wVersionRequested
= MAKEWORD(1, 1);
157 err
= WSAStartup(wVersionRequested
, &wsaData
);
158 if (err
!= 0) return SY_ERROR
;
164 void SySocketShutdown (void) {