-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
323 lines (284 loc) · 7.31 KB
/
util.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Hippo Memory Cached
* Copyright (C) 2012 heaven. All rights reserved.
*
* Hippo is a mini open-source cache daemon, mainly used in dynamic data cache.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* To learn more open-source code, visit: http://code.google.com/p/heavenmvc/
* Email: wangwei([email protected])
*/
#include "util.h"
// Die alert message
void die(char *mess){
perror(mess);
write_file_lock(mess);
exit(1);
}
//link to die
void err_dump(char *mess){
die(mess);
}
// debug information
void debug_log(short level, char *msg){
char log_level[10];
bzero(log_level, 10);
switch(level){
case LOG_LEVEL_DEBUG: strcpy(log_level, "DEBUG"); break;
case LOG_LEVEL_TRACE: strcpy(log_level, "TRACE"); break;
case LOG_LEVEL_NOTICE: strcpy(log_level, "NOTICE"); break;
case LOG_LEVEL_WARNING: strcpy(log_level, "WARNING"); break;
case LOG_LEVEL_FATAL: strcpy(log_level, "FATAL"); break;
default: strcpy(log_level, "NOTICE");
}
printf("[%s] %s\n", log_level, msg);
}
// substr - Sub string from pos to length
char *substr( const char *s, int start_pos, int length, char *ret ){
char buf[length+1];
int i, j, end_pos;
int str_len = strlen(s);
if (str_len <= 0 || length < 0){
return "";
}
if (length == 0){
length = str_len - start_pos;
}
start_pos = ( start_pos < 0 ? (str_len + start_pos) : ( start_pos==0 ? start_pos : start_pos-- ) );
end_pos = start_pos + length;
for(i=start_pos, j=0; i<end_pos && j<=length; i++, j++){
buf[j] = s[i];
}
buf[length] = '\0';
strcpy(ret, buf);
return(ret);
}
/**
* explode - separate string by separator
*
* @param string from - need separator
* @param char delim - separator
* @param pointarray to - save return separate result
* @param int item_num - return sub string total
*
* @include stdlib.h
* @include string.h
*
* @example
* char *s, **r;
* int num;
* explode(s, '\n', &r, &num);
* for(i=0; i<num; i++){
* printf("%s\n", r[i]);
* }
*
*/
void explode(char *from, char delim, char ***to, int *item_num){
int i, j, k, n, temp_len;
int max_len = strlen(from) + 1;
char buf[max_len], **ret;
for(i=0, n=1; from[i]!='\0'; i++){
if (from[i] == delim) n++;
}
ret = (char **)malloc(n*sizeof(char *));
for (i=0, k=0; k<n; k++){
memset(buf, 0, max_len);
for(j=0; from[i]!='\0' && from[i]!=delim; i++, j++) buf[j] = from[i];
i++;
temp_len = strlen(buf)+1;
ret[k] = malloc(temp_len);
memcpy(ret[k], buf, temp_len);
}
*to = ret;
*item_num = n;
}
// strtolower - string to lowner
char *strtolower( char *s ){
int i, len = sizeof(s);
for( i = 0; i < len; i++ ){
s[i] = ( s[i] >= 'A' && s[i] <= 'Z' ? s[i] + 'a' - 'A' : s[i] );
}
return(s);
}
// strtoupper - string to upper
char *strtoupper( char *s ){
int i, len = sizeof(s);
for( i = 0; i < len; i++ ){
s[i] = ( s[i] >= 'a' && s[i] <= 'z' ? s[i] + 'A' - 'a' : s[i] );
}
return(s);
}
// strpos - find char at string position
int strpos (const char *s, char c){
int i, len;
if (!s || !c) return -1;
len = strlen(s);
for (i=0; i<len; i++){
if (s[i] == c) return i;
}
return -1;
}
// strrpos - find char at string last position
int strrpos (const char *s, char c){
int i, len;
if (!s || !c) return -1;
len = strlen(s);
for (i=len; i>=0; i--){
if (s[i] == c) return i;
}
return -1;
}
// str_pad Pad a string to a certain length with another string
int str_pad(char *s, int len, char c, char *to){
if (!s || !c || !to) return -1;
memset(to, c, len);
memcpy(to, s, strlen(s));
return 0;
}
// str_repeat Repeat a string
int str_repeat(char input, int len, char *to){
if (!input || !to) return -1;
memset(to, input, len);
return 0;
}
// trim - strip left&right space char
char *trim( char *s ){
int l;
for( l=strlen(s); l>0 && isspace((u_char)s[l-1]); l-- ){
s[l-1] = '\0';
}
return(s);
}
// ltrim - strip left space char
char *ltrim( char *s ){
char *p;
for(p=s; isspace((u_char)*p); p++ );
if( p!=s ) strcpy(s, p);
return(s);
}
// rtrim - strip right space char
char * rtrim(char *str){
int p;
p = strlen(str) - 1;
while ( isspace(str[p]) )
p--;
str[p + 1] = '\0';
return str;
}
// is_numeric - Check string is number
int is_numeric( const char *s ){
int i = 0;
size_t len = strlen(s);
for ( ; isdigit(s[i]); i++ ) ;
return ( i==len ? 1 : 0 );
}
// Fetch current date tme
void getdate( char *s ){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
sprintf(s, "%d-%d-%d %d:%d:%d",(1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
}
// Set socket nonblock
int socket_set_nonblock( int sockfd ){
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
return -1;
}
return 0;
}
// File lock reister
int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len) {
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
return ( fcntl(fd, cmd, &lock) );
}
// check file exists
int file_exists (const char *path) {
FILE *fp;
fp = fopen(path, "r");
if (fp == NULL){
return -1;
}
return 0;
}
// filesize - get file size
long filesize(const char *filename){
struct stat buf;
if (!stat(filename, &buf)){
return buf.st_size;
}
return 0;
}
// file_get_contents - read file contents
int file_get_contents( const char *filename, size_t filesize, char *ret, off_t length ){
if ( !file_exists(filename) || access(filename, R_OK)!=0 ) return -1;
int fd;
char buf[filesize];
if ( (fd = open(filename, O_RDONLY)) == -1) return -1;
length = ( length > 0 ? length : filesize);
read(fd, buf, length);
strcpy(ret, buf);
close(fd);
return 0;
}
// is_dir - check file is directory
int is_dir(const char *filename){
struct stat buf;
if ( stat(filename, &buf) < 0 ){
return -1;
}
if (S_ISDIR(buf.st_mode)){
return 1;
}
return 0;
}
// is_file - check file is regular file
int is_file(const char *filename){
struct stat buf;
if ( stat(filename, &buf) < 0 ){
return -1;
}
if (S_ISREG(buf.st_mode)){
return 1;
}
return 0;
}
// daemon
void init_daemon() {
int pid;
int i;
if(pid = fork()) exit(0);
else if(pid < 0) exit(1);
setsid();
if(pid = fork()) exit(0);
else if(pid < 0) exit(1);
for(i = 0; i < NOFILE; i++){
close(i);
}
chdir("/tmp");
umask(0);
return;
}
// Send error messgae to client
void send_error_to_client(int client_sock, short error_type, char *msg){
char buf[BUFFER_SIZE];
memset(buf, 0, BUFFER_SIZE);
switch(error_type){
case E_GENERAL:
sprintf(buf, "ERROR\r\n");break;
case E_CLIENT:
sprintf(buf, "CLIENT_ERROR %s\r\n", msg);break;
case E_SERVER:
sprintf(buf, "SERVER_ERROR %s\r\n", msg);break;
default:
sprintf(buf, "ERROR\r\n");
}
send_info_to_client(client_sock, buf, 0);
}