[hackers] [quark] Unify return values || FRIGN

From: <git_AT_suckless.org>
Date: Thu, 07 Aug 2014 16:57:34 +0200

commit 76834092d705687e13ae0c1a5f3b31608d83c651
Author: FRIGN <dev_AT_frign.de>
Date: Thu Aug 7 16:56:28 2014 +0200

    Unify return values
    
    This is the first step for unifying the return-values inside quark.
    
     1: error
     0: success
    -1: not found (non-fatal, for instance, if you run getresentry
                   on a non-essential entry)

diff --git a/quark.c b/quark.c
index 25b65f8..b15edeb 100644
--- a/quark.c
+++ b/quark.c
_AT_@ -100,11 +100,11 @@ writedata(const char *buf, size_t buf_len) {
         while(offset < buf_len) {
                 if((r = write(req.fd, buf + offset, buf_len - offset)) == -1) {
                         logerrmsg("client %s closed connection
", host);
- return -1;
+ return 1;
                 }
                 offset += r;
         }
- return offset;
+ return 0;
 }
 
 ssize_t
_AT_@ -193,8 +193,8 @@ responsefile(void) {
         r = stat(reqbuf, &st);
         if(r == -1 || (ffd = open(reqbuf, O_RDONLY)) == -1) {
                 logerrmsg("%s requests unknown path %s
", host, reqbuf);
- if(putresentry(HEADER, HttpNotFound, tstamp()) == -1
- || putresentry(CONTENTTYPE, texthtml) == -1)
+ if(putresentry(HEADER, HttpNotFound, tstamp())
+ || putresentry(CONTENTTYPE, texthtml))
                         return;
                 if(req.type == GET)
                         writetext("
<html><body>404 Not Found</body></html>
");
_AT_@ -204,7 +204,7 @@ responsefile(void) {
                 memcpy(mod, asctime(gmtime(&t)), 24);
                 mod[24] = 0;
                 if(!strcmp(reqmod, mod)) {
- if(putresentry(HEADER, HttpNotModified, tstamp()) == -1)
+ if(putresentry(HEADER, HttpNotModified, tstamp()))
                                 return;
                 } else {
                         if((p = strrchr(reqbuf, '.'))) {
_AT_@ -215,12 +215,12 @@ responsefile(void) {
                                                 break;
                                         }
                         }
- if(putresentry(HEADER, HttpOk, tstamp()) == -1
- || putresentry(MODIFIED, mod) == -1
- || putresentry(CONTENTLEN, st.st_size) == -1
- || putresentry(CONTENTTYPE, mimetype) == -1)
+ if(putresentry(HEADER, HttpOk, tstamp())
+ || putresentry(MODIFIED, mod)
+ || putresentry(CONTENTLEN, st.st_size)
+ || putresentry(CONTENTTYPE, mimetype))
                                 return;
- if(req.type == GET && writetext("
") != -1)
+ if(req.type == GET && !writetext("
"))
                                 responsefiledata(ffd, st.st_size);
                 }
                 close(ffd);
_AT_@ -231,11 +231,11 @@ void
 responsedirdata(DIR *d) {
         struct dirent *e;
 
- if(putresentry(HEADER, HttpOk, tstamp()) == -1
- || putresentry(CONTENTTYPE, texthtml) == -1)
+ if(putresentry(HEADER, HttpOk, tstamp())
+ || putresentry(CONTENTTYPE, texthtml))
                 return;
         if(req.type == GET) {
- if(writetext("
<html><body><a href='..'>..</a><br>
") == -1)
+ if(writetext("
<html><body><a href='..'>..</a><br>
"))
                         return;
                 while((e = readdir(d))) {
                         if(e->d_name[0] == '.') /* ignore hidden files, ., .. */
_AT_@ -246,7 +246,7 @@ responsedirdata(DIR *d) {
                                 logerrmsg("snprintf failed, buffer sizeof exceeded");
                                 return;
                         }
- if(writetext(resbuf) == -1)
+ if(writetext(resbuf))
                                 return;
                 }
                 writetext("</body></html>
");
_AT_@ -263,9 +263,9 @@ responsedir(void) {
                 reqbuf[len++] = '/';
                 reqbuf[len] = 0;
                 logmsg("redirecting %s to %s%s
", host, location, reqbuf);
- if(putresentry(HEADER, HttpMoved, tstamp()) == -1
- || putresentry(LOCATION, location, reqbuf) == -1
- || putresentry(CONTENTTYPE, texthtml) == -1)
+ if(putresentry(HEADER, HttpMoved, tstamp())
+ || putresentry(LOCATION, location, reqbuf)
+ || putresentry(CONTENTTYPE, texthtml))
                         return;
                 if(req.type == GET)
                         writetext("
<html><body>301 Moved Permanently</a></body></html>
");
_AT_@ -305,10 +305,10 @@ responsecgi(void) {
         if(chdir(cgi_dir) == -1)
                 logerrmsg("chdir to cgi directory %s failed: %s
", cgi_dir, strerror(errno));
         if((cgi = popen(cgi_script, "r"))) {
- if(putresentry(HEADER, HttpOk, tstamp()) == -1)
+ if(putresentry(HEADER, HttpOk, tstamp()))
                         return;
                 while((r = fread(resbuf, 1, MAXBUFLEN, cgi)) > 0) {
- if(writedata(resbuf, r) == -1) {
+ if(writedata(resbuf, r)) {
                                 pclose(cgi);
                                 return;
                         }
_AT_@ -317,8 +317,8 @@ responsecgi(void) {
         }
         else {
                 logerrmsg("%s requests %s, but cannot run cgi script %s
", host, cgi_script, reqbuf);
- if(putresentry(HEADER, HttpNotFound, tstamp()) == -1
- || putresentry(CONTENTTYPE, texthtml) == -1)
+ if(putresentry(HEADER, HttpNotFound, tstamp())
+ || putresentry(CONTENTTYPE, texthtml))
                         return;
                 if(req.type == GET)
                         writetext("
<html><body>404 Not Found</body></html>
");
_AT_@ -333,8 +333,8 @@ response(void) {
         for(p = reqbuf; *p; p++)
                 if(*p == '\' || (*p == '/' && *(p + 1) == '.')) { /* don't serve bogus or hidden files */
                         logerrmsg("%s requests bogus or hidden file %s
", host, reqbuf);
- if(putresentry(HEADER, HttpUnauthorized, tstamp()) == -1
- || putresentry(CONTENTTYPE, texthtml) == -1)
+ if(putresentry(HEADER, HttpUnauthorized, tstamp())
+ || putresentry(CONTENTTYPE, texthtml))
                                 ;
                         else
                                 return;
Received on Thu Aug 07 2014 - 16:57:34 CEST

This archive was generated by hypermail 2.3.0 : Thu Aug 07 2014 - 17:00:10 CEST