mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 20:01:27 +00:00
Refactor file list processing
This commit is contained in:
parent
04d9b667ed
commit
47a2f93e02
55
src/nnn.c
55
src/nnn.c
|
@ -6132,9 +6132,8 @@ static char *load_input()
|
||||||
{
|
{
|
||||||
/* 512 KiB chunk size */
|
/* 512 KiB chunk size */
|
||||||
ssize_t i, chunk_count = 1, chunk = 512 * 1024, entries = 0;
|
ssize_t i, chunk_count = 1, chunk = 512 * 1024, entries = 0;
|
||||||
char *input = malloc(sizeof(char) * chunk), *tmpdir;
|
char *input = malloc(sizeof(char) * chunk), *tmpdir = NULL;
|
||||||
char cwd[PATH_MAX], *next, *prev, *tmp;
|
char cwd[PATH_MAX], *next, *tmp;
|
||||||
size_t offsets[1 << 16];
|
|
||||||
char *paths[1 << 16];
|
char *paths[1 << 16];
|
||||||
ssize_t input_read, total_read = 0, off = 0;
|
ssize_t input_read, total_read = 0, off = 0;
|
||||||
|
|
||||||
|
@ -6143,6 +6142,11 @@ static char *load_input()
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!getcwd(cwd, PATH_MAX)) {
|
||||||
|
free(input);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
while (chunk_count < 512) {
|
while (chunk_count < 512) {
|
||||||
input_read = read(STDIN_FILENO, input, chunk);
|
input_read = read(STDIN_FILENO, input, chunk);
|
||||||
if (input_read < 0) {
|
if (input_read < 0) {
|
||||||
|
@ -6166,7 +6170,7 @@ static char *load_input()
|
||||||
if (entries == (1 << 16))
|
if (entries == (1 << 16))
|
||||||
goto malloc_1;
|
goto malloc_1;
|
||||||
|
|
||||||
offsets[entries++] = off;
|
paths[entries++] = input + off;
|
||||||
off = next - input;
|
off = next - input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6174,14 +6178,14 @@ static char *load_input()
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (chunk_count == 512 || !(input = xrealloc(input, (chunk_count + 1) * chunk)))
|
if (chunk_count == 512 || !(input = xrealloc(input, (chunk_count + 1) * chunk)))
|
||||||
goto malloc_1;
|
goto malloc_2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (off != total_read) {
|
if (off != total_read) {
|
||||||
if (entries == (1 << 16))
|
if (entries == (1 << 16))
|
||||||
goto malloc_1;
|
goto malloc_2;
|
||||||
|
|
||||||
offsets[entries++] = off;
|
paths[entries++] = input + off;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entries)
|
if (!entries)
|
||||||
|
@ -6189,31 +6193,18 @@ static char *load_input()
|
||||||
|
|
||||||
input[total_read] = '\0';
|
input[total_read] = '\0';
|
||||||
|
|
||||||
for (i = 0; i < entries; ++i)
|
|
||||||
paths[i] = input + offsets[i];
|
|
||||||
|
|
||||||
/* prev used as tmp variable */
|
|
||||||
prev = getcwd(cwd, PATH_MAX);
|
|
||||||
if (!prev)
|
|
||||||
goto malloc_1;
|
|
||||||
|
|
||||||
for (i = 0; i < entries; ++i) {
|
|
||||||
if (!(paths[i] = xrealpath(paths[i], cwd))) {
|
|
||||||
for (--i; i >= 0; --i)
|
|
||||||
free(paths[i]);
|
|
||||||
goto malloc_1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g_prefixpath = malloc(sizeof(char) * PATH_MAX);
|
g_prefixpath = malloc(sizeof(char) * PATH_MAX);
|
||||||
if (!g_prefixpath)
|
if (!g_prefixpath)
|
||||||
goto malloc_2;
|
goto malloc_2;
|
||||||
|
|
||||||
|
if (!(paths[0] = xrealpath(paths[0], cwd)))
|
||||||
|
goto malloc_2; // free all entries
|
||||||
|
|
||||||
xstrlcpy(g_prefixpath, paths[0], strlen(paths[0]) + 1);
|
xstrlcpy(g_prefixpath, paths[0], strlen(paths[0]) + 1);
|
||||||
for (i = 1; i < entries; ++i) {
|
|
||||||
if (!common_prefix(paths[i], g_prefixpath))
|
for (i = 1; i < entries; ++i)
|
||||||
goto malloc_2;
|
if (!(paths[i] = xrealpath(paths[i], cwd)) || !common_prefix(paths[i], g_prefixpath))
|
||||||
}
|
goto malloc_2; // free all entries
|
||||||
|
|
||||||
if (entries == 1) {
|
if (entries == 1) {
|
||||||
tmp = xmemrchr((uchar *)g_prefixpath, '/', strlen(g_prefixpath));
|
tmp = xmemrchr((uchar *)g_prefixpath, '/', strlen(g_prefixpath));
|
||||||
|
@ -6225,20 +6216,12 @@ static char *load_input()
|
||||||
|
|
||||||
tmpdir = make_tmp_tree(paths, entries, g_prefixpath);
|
tmpdir = make_tmp_tree(paths, entries, g_prefixpath);
|
||||||
|
|
||||||
if (tmpdir) {
|
|
||||||
for (i = entries - 1; i >= 0; --i)
|
|
||||||
free(paths[i]);
|
|
||||||
free(input);
|
|
||||||
|
|
||||||
return tmpdir;
|
|
||||||
}
|
|
||||||
|
|
||||||
malloc_2:
|
malloc_2:
|
||||||
for (i = entries - 1; i >= 0; --i)
|
for (i = entries - 1; i >= 0; --i)
|
||||||
free(paths[i]);
|
free(paths[i]);
|
||||||
malloc_1:
|
malloc_1:
|
||||||
free(input);
|
free(input);
|
||||||
return NULL;
|
return tmpdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check_key_collision(void)
|
static void check_key_collision(void)
|
||||||
|
|
Loading…
Reference in a new issue