code-style fixes
This commit is contained in:
parent
e42bb27846
commit
192f44046c
1 changed files with 49 additions and 29 deletions
78
quark.c
78
quark.c
|
@ -102,7 +102,8 @@ static int listenfd = -1;
|
||||||
static Request req;
|
static Request req;
|
||||||
|
|
||||||
char *
|
char *
|
||||||
tstamp(time_t t) {
|
tstamp(time_t t)
|
||||||
|
{
|
||||||
static char res[30];
|
static char res[30];
|
||||||
|
|
||||||
if (!t)
|
if (!t)
|
||||||
|
@ -112,7 +113,8 @@ tstamp(time_t t) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
writedata(const char *buf, size_t buf_len) {
|
writedata(const char *buf, size_t buf_len)
|
||||||
|
{
|
||||||
ssize_t r, offset;
|
ssize_t r, offset;
|
||||||
|
|
||||||
for (offset = 0; offset < buf_len; offset += r) {
|
for (offset = 0; offset < buf_len; offset += r) {
|
||||||
|
@ -125,12 +127,14 @@ writedata(const char *buf, size_t buf_len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
writetext(const char *buf) {
|
writetext(const char *buf)
|
||||||
|
{
|
||||||
return writedata(buf, strlen(buf));
|
return writedata(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
atomiclog(int fd, const char *errstr, va_list ap) {
|
atomiclog(int fd, const char *errstr, va_list ap)
|
||||||
|
{
|
||||||
static char buf[512];
|
static char buf[512];
|
||||||
size_t n;
|
size_t n;
|
||||||
|
|
||||||
|
@ -144,7 +148,8 @@ atomiclog(int fd, const char *errstr, va_list ap) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
logmsg(const char *errstr, ...) {
|
logmsg(const char *errstr, ...)
|
||||||
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, errstr);
|
va_start(ap, errstr);
|
||||||
|
@ -153,7 +158,8 @@ logmsg(const char *errstr, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
logerrmsg(const char *errstr, ...) {
|
logerrmsg(const char *errstr, ...)
|
||||||
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, errstr);
|
va_start(ap, errstr);
|
||||||
|
@ -162,7 +168,8 @@ logerrmsg(const char *errstr, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
die(const char *errstr, ...) {
|
die(const char *errstr, ...)
|
||||||
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, errstr);
|
va_start(ap, errstr);
|
||||||
|
@ -172,7 +179,8 @@ die(const char *errstr, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
putresentry(int type, ...) {
|
putresentry(int type, ...)
|
||||||
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, type);
|
va_start(ap, type);
|
||||||
|
@ -185,12 +193,13 @@ putresentry(int type, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
responsefiledata(int fd, off_t size) {
|
responsefiledata(int fd, off_t size)
|
||||||
|
{
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
ssize_t n, m = 0, size_in;
|
ssize_t n, m = 0, size_in;
|
||||||
|
|
||||||
for (; (n = read(fd, buf, MIN(size, sizeof buf))) > 0; size -= n)
|
for (; (n = read(fd, buf, MIN(size, sizeof buf))) > 0; size -= n)
|
||||||
for(size_in = n; (m = write(req.fd, buf, size_in)) > 0; size_in -= m);
|
for (size_in = n; (m = write(req.fd, buf, size_in)) > 0; size_in -= m);
|
||||||
|
|
||||||
if (m == -1 && errno != EPIPE)
|
if (m == -1 && errno != EPIPE)
|
||||||
logerrmsg("error writing to client %s: %s\n", host, strerror(errno));
|
logerrmsg("error writing to client %s: %s\n", host, strerror(errno));
|
||||||
|
@ -199,7 +208,8 @@ responsefiledata(int fd, off_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
responsefile(void) {
|
responsefile(void)
|
||||||
|
{
|
||||||
const char *mimetype = "application/octet-stream";
|
const char *mimetype = "application/octet-stream";
|
||||||
char mod[30], *p;
|
char mod[30], *p;
|
||||||
int r, ffd;
|
int r, ffd;
|
||||||
|
@ -245,7 +255,8 @@ responsefile(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
responsedirdata(struct dirent **e, int len) {
|
responsedirdata(struct dirent **e, int len)
|
||||||
|
{
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
if (putresentry(HEADER, HttpOk, tstamp(0))
|
if (putresentry(HEADER, HttpOk, tstamp(0))
|
||||||
|
@ -255,7 +266,7 @@ responsedirdata(struct dirent **e, int len) {
|
||||||
if (req.type == GET) {
|
if (req.type == GET) {
|
||||||
if (writetext("\r\n<html><body><a href=\"..\">..</a><br/>\r\n"))
|
if (writetext("\r\n<html><body><a href=\"..\">..</a><br/>\r\n"))
|
||||||
return;
|
return;
|
||||||
for(n = 0; n < len; n++) {
|
for (n = 0; n < len; n++) {
|
||||||
if (e[n]->d_name[0] == '.') /* ignore hidden files, ., .. */
|
if (e[n]->d_name[0] == '.') /* ignore hidden files, ., .. */
|
||||||
continue;
|
continue;
|
||||||
if (snprintf(resbuf, MAXBUFLEN, "<a href=\"%s%s\">%s</a><br/>\r\n",
|
if (snprintf(resbuf, MAXBUFLEN, "<a href=\"%s%s\">%s</a><br/>\r\n",
|
||||||
|
@ -272,7 +283,8 @@ responsedirdata(struct dirent **e, int len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
responsedir(void) {
|
responsedir(void)
|
||||||
|
{
|
||||||
size_t len = strlen(reqbuf);
|
size_t len = strlen(reqbuf);
|
||||||
struct dirent **namelist = NULL;
|
struct dirent **namelist = NULL;
|
||||||
int n;
|
int n;
|
||||||
|
@ -295,7 +307,7 @@ responsedir(void) {
|
||||||
memmove(reqbuf + len, docindex, strlen(docindex) + 1);
|
memmove(reqbuf + len, docindex, strlen(docindex) + 1);
|
||||||
if (access(reqpath, R_OK) == -1) { /* directory mode */
|
if (access(reqpath, R_OK) == -1) { /* directory mode */
|
||||||
reqbuf[len] = 0; /* cut off docindex again */
|
reqbuf[len] = 0; /* cut off docindex again */
|
||||||
if(!allowdirlist) {
|
if (!allowdirlist) {
|
||||||
if (putresentry(HEADER, HttpForbidden, tstamp(0))
|
if (putresentry(HEADER, HttpForbidden, tstamp(0))
|
||||||
|| putresentry(CONTENTTYPE, texthtml))
|
|| putresentry(CONTENTTYPE, texthtml))
|
||||||
return;
|
return;
|
||||||
|
@ -317,7 +329,8 @@ responsedir(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
responsecgi(void) {
|
responsecgi(void)
|
||||||
|
{
|
||||||
FILE *cgi;
|
FILE *cgi;
|
||||||
size_t r, linesiz = 0;
|
size_t r, linesiz = 0;
|
||||||
char *q, *line = NULL, *statusline = HttpOk;
|
char *q, *line = NULL, *statusline = HttpOk;
|
||||||
|
@ -331,7 +344,7 @@ responsecgi(void) {
|
||||||
return;
|
return;
|
||||||
if (*reqhost)
|
if (*reqhost)
|
||||||
setenv("SERVER_NAME", reqhost, 1);
|
setenv("SERVER_NAME", reqhost, 1);
|
||||||
if((q = strchr(reqbuf, '?'))) {
|
if ((q = strchr(reqbuf, '?'))) {
|
||||||
setenv("QUERY_STRING", q + 1, 1);
|
setenv("QUERY_STRING", q + 1, 1);
|
||||||
*q = '\0';
|
*q = '\0';
|
||||||
setenv("PATH_INFO", reqbuf, 1);
|
setenv("PATH_INFO", reqbuf, 1);
|
||||||
|
@ -358,7 +371,7 @@ responsecgi(void) {
|
||||||
statusline = line + strlen("Status:") + 1;
|
statusline = line + strlen("Status:") + 1;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
status = strtol(statusline, NULL, 10);
|
status = strtol(statusline, NULL, 10);
|
||||||
if(errno)
|
if (errno)
|
||||||
status = 200;
|
status = 200;
|
||||||
if (putresentry(HEADER, statusline, tstamp(0)))
|
if (putresentry(HEADER, statusline, tstamp(0)))
|
||||||
return;
|
return;
|
||||||
|
@ -389,7 +402,8 @@ responsecgi(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
response(void) {
|
response(void)
|
||||||
|
{
|
||||||
char *p;
|
char *p;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int r;
|
int r;
|
||||||
|
@ -409,7 +423,7 @@ response(void) {
|
||||||
|
|
||||||
r = stat(reqpath, &st);
|
r = stat(reqpath, &st);
|
||||||
if (cgi_mode) {
|
if (cgi_mode) {
|
||||||
if(r != -1 && !S_ISDIR(st.st_mode))
|
if (r != -1 && !S_ISDIR(st.st_mode))
|
||||||
responsefile();
|
responsefile();
|
||||||
else
|
else
|
||||||
responsecgi();
|
responsecgi();
|
||||||
|
@ -422,7 +436,8 @@ response(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
getreqentry(char *name, char *target, size_t targetlen, char *breakchars) {
|
getreqentry(char *name, char *target, size_t targetlen, char *breakchars)
|
||||||
|
{
|
||||||
char *p, *res;
|
char *p, *res;
|
||||||
|
|
||||||
if ((res = strstr(reqbuf, name))) {
|
if ((res = strstr(reqbuf, name))) {
|
||||||
|
@ -442,7 +457,8 @@ getreqentry(char *name, char *target, size_t targetlen, char *breakchars) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
request(void) {
|
request(void)
|
||||||
|
{
|
||||||
char *p, *res;
|
char *p, *res;
|
||||||
ssize_t r;
|
ssize_t r;
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
@ -496,7 +512,8 @@ invalid_request:
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
serve(int fd) {
|
serve(int fd)
|
||||||
|
{
|
||||||
int result;
|
int result;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
socklen_t salen;
|
socklen_t salen;
|
||||||
|
@ -551,9 +568,10 @@ serve(int fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sighandler(int sig) {
|
sighandler(int sig)
|
||||||
|
{
|
||||||
if (sig == SIGCHLD) {
|
if (sig == SIGCHLD) {
|
||||||
while(0 < waitpid(-1, NULL, WNOHANG));
|
while (0 < waitpid(-1, NULL, WNOHANG));
|
||||||
} else {
|
} else {
|
||||||
logerrmsg("info\tsignal %s, closing down\n", strsignal(sig));
|
logerrmsg("info\tsignal %s, closing down\n", strsignal(sig));
|
||||||
close(listenfd);
|
close(listenfd);
|
||||||
|
@ -562,15 +580,17 @@ sighandler(int sig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
usage(void) {
|
usage(void)
|
||||||
|
{
|
||||||
fprintf(stderr, "usage: quark [-c] [-C chrootdir] [-d cgidir] "
|
fprintf(stderr, "usage: quark [-c] [-C chrootdir] [-d cgidir] "
|
||||||
"[-e cgiscript] [-g group] [-i index] [-l] [-p port] "
|
"[-e cgiscript] [-g group] [-i index] [-l] [-p port] "
|
||||||
"[-r docroot] [-s server] [-u user] [-v]\n");
|
"[-r docroot] [-s server] [-u user] [-v]\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[]) {
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
struct addrinfo hints, *ai = NULL;
|
struct addrinfo hints, *ai = NULL;
|
||||||
struct passwd *upwd = NULL;
|
struct passwd *upwd = NULL;
|
||||||
struct group *gpwd = NULL;
|
struct group *gpwd = NULL;
|
||||||
|
|
Loading…
Reference in a new issue