From 9b5a6a354b8e9dded8fe0f3de187ff8c70b59047 Mon Sep 17 00:00:00 2001 From: kraxel Date: Wed, 23 May 2007 09:19:54 +0000 Subject: ls.c fixes --- httpd.h | 2 +- ls.c | 83 +++++++++++++++++++++++++++++++------------------------------- response.c | 5 +++- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/httpd.h b/httpd.h index a7265d9..072dbae 100644 --- a/httpd.h +++ b/httpd.h @@ -220,7 +220,7 @@ void write_request(struct REQUEST *req); /* --- ls.c ----------------------------------------------------- */ void init_quote(void); -char* quote(unsigned char *path, int maxlength); +char* quote(unsigned char *path, int maxlength, char *buf, int bufsize); struct DIRCACHE *get_dir(struct REQUEST *req, char *filename); void free_dir(struct DIRCACHE *dir); diff --git a/ls.c b/ls.c index 2ce7887..7645966 100644 --- a/ls.c +++ b/ls.c @@ -140,15 +140,14 @@ init_quote(void) } char* -quote(unsigned char *path, int maxlength) +quote(unsigned char *path, int maxlength, char *buf, int bufsize) { - static unsigned char buf[2048]; /* FIXME: threads break this... */ int i,j,n=strlen(path); if (n > maxlength) n = maxlength; - for (i=0, j=0; in,file->d_name); - sprintf(line,"%s/%s",filename,file->d_name); + snprintf(line,sizeof(line),"%s/%s",filename,file->d_name); if (-1 == stat(line,&files[count]->s)) { free(files[count]); count--; @@ -264,11 +263,11 @@ ls(time_t now, char *hostname, char *filename, char *path, int *length) goto oom; len = 0; - len += sprintf(buf+len, - "%s:%d%s\n" - "\n" - "

listing: \n", - hostname,tcp_port,path); + len += snprintf(buf+len, size-len, + "%s:%d%s\n" + "\n" + "

listing: \n", + hostname,tcp_port,path); h1 = path, h2 = path+1; for (;;) { @@ -281,11 +280,11 @@ ls(time_t now, char *hostname, char *filename, char *path, int *length) goto oom; buf = re2; } - len += sprintf(buf+len,"%*.*s", - quote(path,h2-path), - (int)(h2-h1), - (int)(h2-h1), - h1); + len += snprintf(buf+len, size-len, "%*.*s", + quote(path, h2-path, line, sizeof(line)), + (int)(h2-h1), + (int)(h2-h1), + h1); h1 = h2; h2 = strchr(h2,'/'); if (NULL == h2) @@ -293,10 +292,10 @@ ls(time_t now, char *hostname, char *filename, char *path, int *length) h2++; } - len += sprintf(buf+len, - "


\n"
-		   "access      user      group     date             "
-		   "size  name\n\n");
+    len += snprintf(buf+len, size-len,
+		    "
\n"
+		    "access      user      group     date             "
+		    "size  name\n\n");
 
     for (i = 0; i < count; i++) {
 	if (len > size)
@@ -318,63 +317,63 @@ ls(time_t now, char *hostname, char *filename, char *path, int *length)
 	/* user */
 	pw = xgetpwuid(files[i]->s.st_uid);
 	if (NULL != pw)
-	    len += sprintf(buf+len,"%-8.8s  ",pw);
+	    len += snprintf(buf+len, size-len, "%-8.8s  ",pw);
 	else
-	    len += sprintf(buf+len,"%8d  ",(int)files[i]->s.st_uid);
+	    len += snprintf(buf+len, size-len, "%8d  ",(int)files[i]->s.st_uid);
 		
 	/* group */
 	gr = xgetgrgid(files[i]->s.st_gid);
 	if (NULL != gr)
-	    len += sprintf(buf+len,"%-8.8s  ",gr);
+	    len += snprintf(buf+len, size-len, "%-8.8s  ",gr);
 	else
-	    len += sprintf(buf+len,"%8d  ",(int)files[i]->s.st_gid);
+	    len += snprintf(buf+len, size-len, "%8d  ",(int)files[i]->s.st_gid);
 	
 	/* mtime */
 	if (now - files[i]->s.st_mtime > 60*60*24*30*6)
-	    len += strftime(buf+len,255,"%b %d  %Y  ",
+	    len += strftime(buf+len, size-len, "%b %d  %Y  ",
 			    gmtime(&files[i]->s.st_mtime));
 	else
-	    len += strftime(buf+len,255,"%b %d %H:%M  ",
+	    len += strftime(buf+len, size-len, "%b %d %H:%M  ",
 			    gmtime(&files[i]->s.st_mtime));
 	
 	/* size */
 	if (S_ISDIR(files[i]->s.st_mode)) {
-	    len += sprintf(buf+len,"  <DIR>  ");
+	    len += snprintf(buf+len, size-len, "  <DIR>  ");
 	} else if (!S_ISREG(files[i]->s.st_mode)) {
-	    len += sprintf(buf+len,"     --  ");
+	    len += snprintf(buf+len, size-len, "     --  ");
 	} else if (files[i]->s.st_size < 1024*9) {
-	    len += sprintf(buf+len,"%4d  B  ",
+	    len += snprintf(buf+len, size-len, "%4d  B  ",
 			   (int)files[i]->s.st_size);
 	} else if (files[i]->s.st_size < 1024*1024*9) {
-	    len += sprintf(buf+len,"%4d kB  ",
+	    len += snprintf(buf+len, size-len, "%4d kB  ",
 			   (int)(files[i]->s.st_size>>10));
 	} else if ((int64_t)(files[i]->s.st_size) < (int64_t)1024*1024*1024*9) {
-	    len += sprintf(buf+len,"%4d MB  ",
+	    len += snprintf(buf+len, size-len, "%4d MB  ",
 			   (int)(files[i]->s.st_size>>20));
 	} else if ((int64_t)(files[i]->s.st_size) < (int64_t)1024*1024*1024*1024*9) {
-	    len += sprintf(buf+len,"%4d GB  ",
+	    len += snprintf(buf+len, size-len, "%4d GB  ",
 			   (int)(files[i]->s.st_size>>30));
 	} else {
-	    len += sprintf(buf+len,"%4d TB  ",
+	    len += snprintf(buf+len, size-len, "%4d TB  ",
 			   (int)(files[i]->s.st_size>>40));
 	}
 	
 	/* filename */
 	if (files[i]->r) {
-	    len += sprintf(buf+len,"%s\n",
-			   quote(files[i]->n,9999),
-			   S_ISDIR(files[i]->s.st_mode) ? "/" : "",
-			   files[i]->n);
+	    len += snprintf(buf+len, size-len, "%s\n",
+			    quote(files[i]->n, 9999, line, sizeof(line)),
+			    S_ISDIR(files[i]->s.st_mode) ? "/" : "",
+			    files[i]->n);
 	} else {
-	    len += sprintf(buf+len,"%s\n",files[i]->n);
+	    len += snprintf(buf+len, size-len, "%s\n", files[i]->n);
 	}
     }
     strftime(line,32,"%d/%b/%Y %H:%M:%S GMT",gmtime(&now));
-    len += sprintf(buf+len,
-		   "

\n" - "%s   %s\n" - "\n", - HOMEPAGE,server_name,line); + len += snprintf(buf+len, size-len, + "

\n" + "%s   %s\n" + "\n", + HOMEPAGE,server_name,line); for (i = 0; i < count; i++) free(files[i]); if (count) diff --git a/response.c b/response.c index 8c61a9b..7599985 100644 --- a/response.c +++ b/response.c @@ -209,6 +209,8 @@ mkerror(struct REQUEST *req, int status, int ka) void mkredirect(struct REQUEST *req) { + char buf[MAX_PATH]; + req->status = 302; req->body = req->path; req->lbody = strlen(req->body); @@ -219,7 +221,8 @@ mkredirect(struct REQUEST *req) "Content-Length: %" PRId64 "\r\n", "302 Redirect",server_name, req->keep_alive ? "Keep-Alive" : "Close", - req->hostname,tcp_port,quote(req->path,9999), + req->hostname, tcp_port, + quote(req->path,9999,buf,sizeof(buf)), (int64_t)req->lbody); req->lres += strftime(req->hres+req->lres,80, "Date: " RFC1123 "\r\n\r\n", -- cgit