1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
5 * Copyright (C) 2001-2008, Eduardo Silva P.
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 Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include <sys/types.h>
38 #include <sys/socket.h>
43 #include <sys/sendfile.h>
56 int SendFile(int socket
, struct request
*sr
)
60 nbytes
= sendfile(socket
, sr
->fd_file
, &sr
->bytes_offset
,
64 fprintf(stderr
, "error from sendfile: %s\n", strerror(errno
));
69 sr
->bytes_to_send
-=nbytes
;
71 return sr
->bytes_to_send
;
74 /* It's a valid directory ? */
75 int CheckDir(char *pathfile
)
79 if(stat(pathfile
,&path
)==-1)
82 if(!(path
.st_mode
& S_IFDIR
))
88 int CheckFile(char *pathfile
)
92 if(stat(pathfile
,&path
)==-1)
95 if(!(path
.st_mode
& S_IFREG
))
101 /* Devuelve la fecha para enviarla
103 mk_pointer
PutDate_string(time_t date
)
109 mk_pointer_reset(&date_gmt
);
112 if ( (date
= time(NULL
)) == -1 ){
117 date_gmt
.data
= mk_mem_malloc(size
);
118 gmt_tm
= (struct tm
*) gmtime(&date
);
119 n
= strftime(date_gmt
.data
, size
-1, GMT_DATEFORMAT
, gmt_tm
);
120 date_gmt
.data
[n
] = '\0';
126 time_t PutDate_unix(char *date
)
128 time_t new_unix_time
;
131 if(!strptime(date
, GMT_DATEFORMAT
, (struct tm
*) &t_data
)){
135 new_unix_time
= mktime((struct tm
*) &t_data
);
137 return (new_unix_time
);
140 int mk_buffer_cat(mk_pointer
*p
, char *buf1
, char *buf2
){
148 p
->data
= (char *) mk_mem_malloc(len1
+len2
+1);
151 memcpy(p
->data
, buf1
, len1
);
152 memcpy(p
->data
+len1
, buf2
, len2
);
153 p
->data
[len1
+len2
]='\0';
161 char *m_build_buffer(char **buffer
, unsigned long *len
, const char *format
, ...)
166 static size_t _mem_alloc
= 64;
169 /* *buffer *must* be an empty/NULL buffer */
171 *buffer
= (char *) mk_mem_malloc(_mem_alloc
);
178 va_start(ap
, format
);
179 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
181 if(length
>= alloc
) {
182 ptr
= realloc(*buffer
, length
+ 1);
189 length
= vsnprintf(*buffer
, alloc
, format
, ap
);
204 /* Run current process in background mode (daemon, evil Monkey >:) */
205 int mk_utils_set_daemon()
210 case -1: exit(1); break; /* Error */
211 default: exit(0); /* Success */
214 setsid(); /* Create new session */
215 fclose(stdin
); /* close screen outputs */
223 char *get_real_string(mk_pointer uri
){
225 int i
, hex_result
, aux_char
;
230 if((i
= mk_string_char_search(uri
.data
, '%', uri
.len
))<0)
235 buf
= mk_mem_malloc_z(uri
.len
);
239 strncpy(buf
, uri
.data
, i
);
245 if(uri
.data
[i
]=='%' && i
+2<uri
.len
){
246 memset(hex
, '\0', sizeof(hex
));
247 strncpy(hex
, uri
.data
+i
+1,2);
250 if((hex_result
=hex2int(hex
))<=127){
251 buf
[buf_idx
]=toascii(hex_result
);
254 if((aux_char
=get_char(hex_result
))!=-1){
255 buf
[buf_idx
]=aux_char
;
265 buf
[buf_idx
] = uri
.data
[i
];
275 void mk_utils_toupper(char *string
)
279 len
= strlen(string
);
282 string
[i
] = toupper(string
[i
]);
286 mk_pointer
mk_utils_int2mkp(int n
)
293 buf
= mk_mem_malloc(size
);
294 len
= snprintf(buf
, 32, "%i", n
);