Commit 3dd26868 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Sort directories alphabetically in the http servers

parent c3a5c415
......@@ -630,8 +630,8 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
{
unsigned char *outdata = malloc(1000);
size_t pos = 0;
DIR *dir;
struct dirent *dirent;
struct dirent **namelist;
int n, i;
unsigned char *h = outdata;
char *server = "console_httpd libwebsockets";
char *type = "text/html";
......@@ -655,29 +655,26 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
outdata = NULL;
pos = 0;
dir = opendir(resource_path);
printf("dir: %p\n", dir);
while (dir) {
n = scandir(resource_path, &namelist, NULL, alphasort);
for (i = 0; i < n; i++) {
errno = 0;
if ((dirent = readdir(dir)) != NULL) {
if (dirent->d_type != DT_REG) {
printf("%s is not a regular file\n", dirent->d_name);
continue;
}
char *line;
asprintf(&line, "<a href='%s'>%s</a><br>\n", dirent->d_name, dirent->d_name);
outdata = realloc(outdata, pos + strlen(line) + 1);
strcpy((char*)outdata + pos, line);
pos += strlen(line);
free(line);
} else {
closedir(dir);
dir = NULL;
if (namelist[i]->d_type != DT_REG) {
printf("%s is not a regular file\n", namelist[i]->d_name);
continue;
}
char *line;
asprintf(&line, "<a href='%s'>%s</a><br>\n",
namelist[i]->d_name, namelist[i]->d_name);
outdata = realloc(outdata, pos + strlen(line) + 1);
strcpy((char*)outdata + pos, line);
pos += strlen(line);
free(line);
}
if (n >= 0)
free(namelist);
if (!lws_send_pipe_choked(wsi))
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment