forked from avih/miniweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttppil.c
234 lines (214 loc) · 4.54 KB
/
httppil.c
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/////////////////////////////////////////////////////////////////////////////
//
// httppil.c
//
// MiniWeb Platform Independent Layer
// Copyright (c) 2005-2011 Stanley Huang <[email protected]>
//
/////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#ifndef WINCE
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#endif
#include "httppil.h"
#ifndef WIN32
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#endif
#include "win32/win_compat.h"
int InitSocket()
{
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 1), &wsaData) &&
WSAStartup(MAKEWORD(1, 1), &wsaData )) {
return 0;
}
#endif
return 1;
}
void UninitSocket()
{
#ifdef WIN32
WSACleanup();
#endif
}
#ifndef WINCE
char *GetTimeString()
{
static char buf[16];
time_t tm=time(NULL);
memcpy(buf,ctime(&tm)+4,15);
buf[15]=0;
return buf;
}
#endif
#ifdef ENABLE_THREAD
int ThreadCreate(pthread_t *pth, void* (*start_routine)(void*), void* arg)
{
#ifndef HAVE_PTHREAD
DWORD dwid;
*pth=CreateThread(0,0,(LPTHREAD_START_ROUTINE)start_routine,arg,0,&dwid);
return *pth!=NULL?0:1;
#else
pthread_attr_t attr;
pthread_attr_init(&attr);
// Change policy
pthread_attr_setschedpolicy(&attr, SCHED_RR);
int ret = pthread_create(pth, &attr, start_routine, arg);
pthread_attr_destroy(&attr);
return ret;
#endif
}
int ThreadKill(pthread_t pth)
{
#ifndef HAVE_PTHREAD
return TerminateThread(pth,0)?0:1;
#else
return pthread_cancel(pth);
#endif
}
int ThreadWait(pthread_t pth, int timeout, void** ret)
{
#ifndef HAVE_PTHREAD
if (WaitForSingleObject(pth, timeout)==WAIT_TIMEOUT)
return 1;
if (ret) GetExitCodeThread(pth,(LPDWORD)ret);
CloseHandle(pth);
return 0;
#else
return pthread_join(pth,ret);
#endif
}
void MutexCreate(pthread_mutex_t* mutex)
{
#ifndef HAVE_PTHREAD
*mutex = CreateMutex(0,FALSE,NULL);
#else
pthread_mutex_init(mutex,NULL);
#endif
}
void MutexDestroy(pthread_mutex_t* mutex)
{
#ifndef HAVE_PTHREAD
CloseHandle(*mutex);
#else
pthread_mutex_destroy(mutex);
#endif
}
void MutexLock(pthread_mutex_t* mutex)
{
#ifndef HAVE_PTHREAD
WaitForSingleObject(*mutex,INFINITE);
#else
pthread_mutex_lock(mutex);
#endif
}
void MutexUnlock(pthread_mutex_t* mutex)
{
#ifndef HAVE_PTHREAD
ReleaseMutex(*mutex);
#else
pthread_mutex_unlock(mutex);
#endif
}
#endif
int IsDir(const char* pchName)
{
struct cc_stat_s stDirInfo;
if (cc_stat( pchName, &stDirInfo) < 0) return 0;
return (stDirInfo.st_mode & S_IFDIR)?1:0;
}
int ReadDir(const char* pchDir, char* pchFileNameBuf)
{
#if defined(WIN32) || defined(WINCE)
static HANDLE hFind=NULL;
cc_WIN32_FIND_DATA finddata;
if (!pchFileNameBuf) {
if (hFind) {
FindClose(hFind);
hFind=NULL;
}
return 0;
}
if (pchDir) {
char *p;
int len = strlen(pchDir);
int has_slash = len && strstr("/\\", pchDir + len - 1);
if (!IsDir(pchDir)) return -1;
if (hFind) FindClose(hFind);
p = malloc(len + 5);
snprintf(p, len + 5, "%s%s*.*", pchDir, has_slash ? "" : "\\");
hFind=cc_FindFirstFile(p,&finddata);
free(p);
if (hFind==INVALID_HANDLE_VALUE) {
hFind=NULL;
return -1;
}
strcpy(pchFileNameBuf,finddata.cFileName);
return 0;
}
if (!hFind) return -1;
if (!cc_FindNextFile(hFind,&finddata)) {
FindClose(hFind);
hFind=NULL;
return -1;
}
strcpy(pchFileNameBuf,finddata.cFileName);
#else
static DIR *stDirIn=NULL;
struct dirent *stFiles;
if (!pchFileNameBuf) {
if (stDirIn) {
closedir(stDirIn);
stDirIn=NULL;
}
return 0;
}
if (pchDir) {
if (!IsDir(pchDir)) return -1;
if (stDirIn) closedir(stDirIn);
stDirIn = opendir( pchDir);
}
if (!stDirIn) return -1;
stFiles = readdir(stDirIn);
if (!stFiles) {
closedir(stDirIn);
stDirIn=NULL;
return -1;
}
strcpy(pchFileNameBuf, stFiles->d_name);
#endif
return 0;
}
int IsFileExist(const char* filename)
{
#ifdef WINCE
WIN32_FIND_DATA f;
HANDLE hFind = FindFirstFile(filename, &f);
if (hFind == INVALID_HANDLE_VALUE)
return 0;
FindClose(hFind);
return 1;
#else
struct cc_stat_s stat_ret;
if (cc_stat(filename, &stat_ret) != 0) return 0;
return (stat_ret.st_mode & S_IFREG) != 0;
#endif
}
#ifndef WIN32
unsigned int GetTickCount()
{
struct timeval ts;
gettimeofday(&ts,0);
return ts.tv_sec * 1000 + ts.tv_usec / 1000;
}
#endif