new build system (taken from Trigger Rally -- tnx, boys!)
[syren.git] / src / syren_os.c
blob3a86631ad56ae40e33daba46acf35eecb1a786e8
1 /*
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
25 #ifndef _SYREN_OS_C
26 #define _SYREN_OS_C
28 #include "syren_os.h"
32 double SyGetTimeD (void) {
33 #ifdef WINDOZE
34 return ((double)GetTickCount())/1000;
35 #else
36 struct timeval time;
37 gettimeofday(&time, 0);
38 return ((double)time.tv_sec+(double)time.tv_usec/1000000);
39 #endif
43 #ifdef WINDOZE
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);
49 while (l0 >= l1) {
50 if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, t0, l1, t1, l1) == 2) return (char *)t0;
51 t0++; l0--;
53 return NULL;
55 #endif
59 #ifdef SY_INCLUDE_FILE_IO
60 TSyResult SyDeleteFile (const char *fname) {
61 #ifdef WINDOZE
62 if (DeleteFile(fname)) return SY_OK;
63 #else
64 if (!unlink(fname)) return SY_OK;
65 #endif
66 return SY_ERROR;
70 int SyOpenFile (const char *fname, TSyFileMode mode, TSyBool mustCreate) {
71 #ifdef WINDOZE
72 DWORD mm = (mode==SY_FMODE_WRITE)?GENERIC_WRITE:GENERIC_READ;
73 DWORD crea = (mustCreate!=SY_FALSE)?OPEN_ALWAYS:OPEN_EXISTING;
74 HANDLE h;
76 h = CreateFile(fname, mm, FILE_SHARE_READ, NULL, crea, FILE_ATTRIBUTE_NORMAL, NULL);
77 if (h) return (int)h;
78 return -1;
79 #else
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);
84 #endif
88 TSyResult SyWriteFile (int fd, const void *buf, int count) {
89 #ifdef WINDOZE
90 DWORD wr;
91 if (!WriteFile((HANDLE)fd, buf, count, &wr, NULL)) return SY_ERROR;
92 #else
93 int wr = write(fd, buf, count);
94 #endif
95 if (wr != count) return SY_ERROR;
96 return SY_OK;
100 TSyResult SyReadFile (int fd, void *buf, int count) {
101 #ifdef WINDOZE
102 DWORD rd;
103 if (!ReadFile((HANDLE)fd, buf, count, &rd, NULL)) return -1;
104 #else
105 int rd = read(fd, buf, count);
106 #endif
107 if (rd != count) return SY_ERROR;
108 return SY_OK;
112 TSyResult SySeekFile (int fd, int64_t offset) {
113 #ifdef WINDOZE
114 DWORD res;
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;
119 #else
120 //!!
121 offset = offset<0?SY_LSEEK(fd, offset, SEEK_END):SY_LSEEK(fd, offset, SEEK_SET);
122 if (offset < 0) return SY_ERROR;
123 #endif
124 return SY_OK;
128 int64_t SyFileSize (int fd) {
129 int64_t size;
130 #ifdef WINDOZE
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;
139 #else
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;
144 #endif
145 return size;
147 #endif
150 TSyResult SySocketInit (void) {
151 #ifdef WINDOZE
152 WSADATA wsaData;
153 WORD wVersionRequested = MAKEWORD(2, 0);
154 int err = WSAStartup(wVersionRequested, &wsaData);
155 if (err != 0) {
156 wVersionRequested = MAKEWORD(1, 1);
157 err = WSAStartup(wVersionRequested, &wsaData);
158 if (err != 0) return SY_ERROR;
160 #endif
161 return SY_OK;
164 void SySocketShutdown (void) {
165 #ifdef WINDOZE
166 WSACleanup();
167 #endif
171 #endif