Files
padavan-ng/trunk/user/httpd/aspbw.c

157 lines
2.9 KiB
C

#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/klog.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <httpd.h>
#include <shutils.h>
#define FW_CREATE 0
#define FW_APPEND 1
#define FW_NEWLINE 2
// for backup =========================================================
int f_write(const char *path, const void *buffer, int len, unsigned flags, unsigned cmode)
{
static const char nl = '\n';
int f;
int r = -1;
mode_t m;
m = umask(0);
if (cmode == 0) cmode = 0666;
if ((f = open(path, (flags & FW_APPEND) ? (O_WRONLY|O_CREAT|O_APPEND) : (O_WRONLY|O_CREAT|O_TRUNC), cmode)) >= 0) {
if ((buffer == NULL) || ((r = write(f, buffer, len)) == len)) {
if (flags & FW_NEWLINE) {
if (write(f, &nl, 1) == 1) ++r;
}
}
close(f);
}
umask(m);
return r;
}
// for bandwidth =========================================================
int f_exists(const char *path) // note: anything but a directory
{
struct stat st;
return (stat(path, &st) == 0) && (!S_ISDIR(st.st_mode));
}
static int _f_wait_exists(const char *name, int max, int invert)
{
max *= 40;
while (max-- > 0) {
if (f_exists(name) ^ invert)
return 1;
usleep(25000);
}
return 0;
}
int f_wait_exists(const char *name, int max)
{
return _f_wait_exists(name, max, 0);
}
int f_read(const char *path, void *buffer, int max)
{
int f;
int n;
if ((f = open(path, O_RDONLY)) < 0) return -1;
n = read(f, buffer, max);
close(f);
return n;
}
int f_read_string(const char *path, char *buffer, int max)
{
if (max <= 0) return -1;
int n = f_read(path, buffer, max - 1);
buffer[(n > 0) ? n : 0] = 0;
return n;
}
size_t strlcpy(char *d, const char *s, size_t bufsize)
{
size_t len = strlen(s);
size_t ret = len;
if (bufsize <= 0) return 0;
if (len >= bufsize) len = bufsize-1;
memcpy(d, s, len);
d[len] = 0;
return ret;
}
void char_to_ascii(char *output, char *input)
{
int i;
char tmp[10];
char *ptr;
ptr = output;
for ( i=0; i<strlen(input); i++ )
{
if ((input[i]>='0' && input[i] <='9')
||(input[i]>='A' && input[i]<='Z')
||(input[i] >='a' && input[i]<='z')
|| input[i] == '!' || input[i] == '*'
|| input[i] == '(' || input[i] == ')'
|| input[i] == '_' || input[i] == '-'
|| input[i] == '\'' || input[i] == '.')
{
*ptr = input[i];
ptr++;
}
else
{
sprintf(tmp, "%%%.02X", input[i]);
strcpy(ptr, tmp);
ptr+=3;
}
}
*ptr = '\0';
}
int do_f(const char *path, webs_t wp)
{
FILE *fp;
char buf[1024];
int ret = 0;
fp = fopen(path, "r");
if (fp) {
while (fgets(buf, sizeof(buf), fp))
ret += fputs(buf, wp);
fclose(fp);
} else {
ret += fputs("", wp);
}
fflush(wp);
return ret;
}