Wed Nov 21 10:25:17 2018 UTC ()
two fixes reported by mouse:
- don't check contents of 'st' if stat(2) failed.
- round up instead of truncate.  now 10000 byte files say 10kB not 9kB.


(mrg)
diff -r1.26 -r1.27 src/libexec/httpd/dir-index-bozo.c

cvs diff -r1.26 -r1.27 src/libexec/httpd/dir-index-bozo.c (expand / switch to unified diff)

--- src/libexec/httpd/dir-index-bozo.c 2018/11/20 01:06:46 1.26
+++ src/libexec/httpd/dir-index-bozo.c 2018/11/21 10:25:17 1.27
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: dir-index-bozo.c,v 1.26 2018/11/20 01:06:46 mrg Exp $ */ 1/* $NetBSD: dir-index-bozo.c,v 1.27 2018/11/21 10:25:17 mrg Exp $ */
2 2
3/* $eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $ */ 3/* $eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $ */
4 4
5/* 5/*
6 * Copyright (c) 1997-2018 Matthew R. Green 6 * Copyright (c) 1997-2018 Matthew R. Green
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -147,27 +147,27 @@ bozo_dir_index(bozo_httpreq_t *request,  @@ -147,27 +147,27 @@ bozo_dir_index(bozo_httpreq_t *request,
147 snprintf(buf, sizeof buf, "%s/%s", dirpath, name); 147 snprintf(buf, sizeof buf, "%s/%s", dirpath, name);
148 if (stat(buf, &sb)) 148 if (stat(buf, &sb))
149 nostat = 1; 149 nostat = 1;
150 150
151 l = 0; 151 l = 0;
152 152
153 urlname = bozo_escape_rfc3986(httpd, name, 0); 153 urlname = bozo_escape_rfc3986(httpd, name, 0);
154 htmlname = bozo_escape_html(httpd, name); 154 htmlname = bozo_escape_html(httpd, name);
155 if (htmlname == NULL) 155 if (htmlname == NULL)
156 htmlname = name; 156 htmlname = name;
157 if (strcmp(name, "..") == 0) { 157 if (strcmp(name, "..") == 0) {
158 bozo_printf(httpd, "<a href=\"../\">"); 158 bozo_printf(httpd, "<a href=\"../\">");
159 l += bozo_printf(httpd, "Parent Directory"); 159 l += bozo_printf(httpd, "Parent Directory");
160 } else if (S_ISDIR(sb.st_mode)) { 160 } else if (!nostat && S_ISDIR(sb.st_mode)) {
161 bozo_printf(httpd, "<a href=\"%s/\">", urlname); 161 bozo_printf(httpd, "<a href=\"%s/\">", urlname);
162 l += bozo_printf(httpd, "%s/", htmlname); 162 l += bozo_printf(httpd, "%s/", htmlname);
163 } else if (strchr(name, ':') != NULL) { 163 } else if (strchr(name, ':') != NULL) {
164 /* RFC 3986 4.2 */ 164 /* RFC 3986 4.2 */
165 bozo_printf(httpd, "<a href=\"./%s\">", urlname); 165 bozo_printf(httpd, "<a href=\"./%s\">", urlname);
166 l += bozo_printf(httpd, "%s", htmlname); 166 l += bozo_printf(httpd, "%s", htmlname);
167 } else { 167 } else {
168 bozo_printf(httpd, "<a href=\"%s\">", urlname); 168 bozo_printf(httpd, "<a href=\"%s\">", urlname);
169 l += bozo_printf(httpd, "%s", htmlname); 169 l += bozo_printf(httpd, "%s", htmlname);
170 } 170 }
171 if (htmlname != name) 171 if (htmlname != name)
172 free(htmlname); 172 free(htmlname);
173 bozo_printf(httpd, "</a>"); 173 bozo_printf(httpd, "</a>");
@@ -175,42 +175,45 @@ bozo_dir_index(bozo_httpreq_t *request,  @@ -175,42 +175,45 @@ bozo_dir_index(bozo_httpreq_t *request,
175 /* NAMELEN spaces */ 175 /* NAMELEN spaces */
176 /*LINTED*/ 176 /*LINTED*/
177 assert(/*CONSTCOND*/sizeof(spacebuf) > NAMELEN); 177 assert(/*CONSTCOND*/sizeof(spacebuf) > NAMELEN);
178 i = (l < NAMELEN) ? (NAMELEN - l) : 0; 178 i = (l < NAMELEN) ? (NAMELEN - l) : 0;
179 i++; 179 i++;
180 memset(spacebuf, ' ', (size_t)i); 180 memset(spacebuf, ' ', (size_t)i);
181 spacebuf[i] = '\0'; 181 spacebuf[i] = '\0';
182 bozo_printf(httpd, "%s", spacebuf); 182 bozo_printf(httpd, "%s", spacebuf);
183 l += i; 183 l += i;
184 184
185 if (nostat) 185 if (nostat)
186 bozo_printf(httpd, "? ?"); 186 bozo_printf(httpd, "? ?");
187 else { 187 else {
 188 unsigned long long len;
 189
 190 len = ((unsigned long long)sb.st_size + 1023) / 1024;
 191
188 tm = gmtime(&sb.st_mtime); 192 tm = gmtime(&sb.st_mtime);
189 strftime(buf, sizeof buf, "%d-%b-%Y %R", tm); 193 strftime(buf, sizeof buf, "%d-%b-%Y %R", tm);
190 l += bozo_printf(httpd, "%s", buf); 194 l += bozo_printf(httpd, "%s", buf);
191 195
192 /* LMODLEN spaces */ 196 /* LMODLEN spaces */
193 /*LINTED*/ 197 /*LINTED*/
194 assert(/*CONSTCOND*/sizeof(spacebuf) > LMODLEN); 198 assert(/*CONSTCOND*/sizeof(spacebuf) > LMODLEN);
195 i = (l < (LMODLEN+NAMELEN+1)) ? 199 i = (l < (LMODLEN+NAMELEN+1)) ?
196 ((LMODLEN+NAMELEN+1) - l) : 0; 200 ((LMODLEN+NAMELEN+1) - l) : 0;
197 i++; 201 i++;
198 memset(spacebuf, ' ', (size_t)i); 202 memset(spacebuf, ' ', (size_t)i);
199 spacebuf[i] = '\0'; 203 spacebuf[i] = '\0';
200 bozo_printf(httpd, "%s", spacebuf); 204 bozo_printf(httpd, "%s", spacebuf);
201 205
202 bozo_printf(httpd, "%12llukB", 206 bozo_printf(httpd, "%12llukB", len);
203 (unsigned long long)sb.st_size >> 10); 
204 } 207 }
205 bozo_printf(httpd, "\r\n"); 208 bozo_printf(httpd, "\r\n");
206 } 209 }
207 210
208 closedir(dp); 211 closedir(dp);
209 while (k--) 212 while (k--)
210 free(deo[k]); 213 free(deo[k]);
211 free(deo); 214 free(deo);
212 bozo_printf(httpd, "</pre>"); 215 bozo_printf(httpd, "</pre>");
213 directory_hr(httpd); 216 directory_hr(httpd);
214 bozo_printf(httpd, "</body></html>\r\n\r\n"); 217 bozo_printf(httpd, "</body></html>\r\n\r\n");
215 bozo_flush(httpd, stdout); 218 bozo_flush(httpd, stdout);
216 219