mirror of https://github.com/jarun/nnn.git
Add function to count newlines in file
This commit is contained in:
parent
ff562983e1
commit
9469479c0a
68
src/nnn.c
68
src/nnn.c
|
@ -900,6 +900,14 @@ static void endselection(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void clearselection(void)
|
||||||
|
{
|
||||||
|
nselected = 0;
|
||||||
|
selbufpos = 0;
|
||||||
|
cfg.selmode = 0;
|
||||||
|
writesel(NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
static bool seledit(void)
|
static bool seledit(void)
|
||||||
{
|
{
|
||||||
bool ret = FALSE;
|
bool ret = FALSE;
|
||||||
|
@ -974,10 +982,7 @@ static bool seledit(void)
|
||||||
|
|
||||||
emptyedit:
|
emptyedit:
|
||||||
resetselind();
|
resetselind();
|
||||||
nselected = 0;
|
clearselection();
|
||||||
selbufpos = 0;
|
|
||||||
cfg.selmode = 0;
|
|
||||||
writesel(NULL, 0);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1283,9 +1288,22 @@ static void xrm(char *path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint lines_in_file(int fd, char *buf, size_t buflen)
|
||||||
|
{
|
||||||
|
ssize_t len;
|
||||||
|
uint count = 0;
|
||||||
|
|
||||||
|
while ((len = read(fd, buf, buflen)) > 0)
|
||||||
|
while (len)
|
||||||
|
count += (buf[--len] == '\n');
|
||||||
|
|
||||||
|
/* For all use cases 0 linecount is considered as error */
|
||||||
|
return ((len < 0) ? 0 : count);
|
||||||
|
}
|
||||||
|
|
||||||
static bool cpmv_rename(const char *path, const char *cmd)
|
static bool cpmv_rename(const char *path, const char *cmd)
|
||||||
{
|
{
|
||||||
int fd, i;
|
int fd;
|
||||||
uint count = 0, lines = 0;
|
uint count = 0, lines = 0;
|
||||||
bool ret = FALSE;
|
bool ret = FALSE;
|
||||||
const char formatcmd[] = "sed -i 's|^\\(\\(.*/\\)\\(.*\\)$\\)|#\\1\\n\\3|' %s";
|
const char formatcmd[] = "sed -i 's|^\\(\\(.*/\\)\\(.*\\)$\\)|#\\1\\n\\3|' %s";
|
||||||
|
@ -1301,15 +1319,11 @@ static bool cpmv_rename(const char *path, const char *cmd)
|
||||||
snprintf(buf, sizeof(buf), "cat %s | tr '\\0' '\\n' > %s", g_selpath, g_tmpfpath);
|
snprintf(buf, sizeof(buf), "cat %s | tr '\\0' '\\n' > %s", g_selpath, g_tmpfpath);
|
||||||
spawn("sh", "-c", buf, NULL, F_NORMAL | F_CMD);
|
spawn("sh", "-c", buf, NULL, F_NORMAL | F_CMD);
|
||||||
|
|
||||||
while ((i = read(fd, buf, sizeof(buf))) > 0)
|
count = lines_in_file(fd, buf, sizeof(buf));
|
||||||
while(i)
|
|
||||||
count += (buf[--i] == '\n');
|
|
||||||
|
|
||||||
if (!count)
|
if (!count)
|
||||||
goto finish;
|
goto finish;
|
||||||
} else {
|
} else
|
||||||
seltofile(fd, &count);
|
seltofile(fd, &count);
|
||||||
}
|
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
@ -1321,15 +1335,11 @@ static bool cpmv_rename(const char *path, const char *cmd)
|
||||||
if ((fd = open(g_tmpfpath, O_RDONLY)) == -1)
|
if ((fd = open(g_tmpfpath, O_RDONLY)) == -1)
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
while ((i = read(fd, buf, sizeof(buf))) > 0)
|
lines = lines_in_file(fd, buf, sizeof(buf));
|
||||||
while (i)
|
|
||||||
lines += (buf[--i] == '\n');
|
|
||||||
|
|
||||||
if (i < 0)
|
|
||||||
goto finish;
|
|
||||||
|
|
||||||
DPRINTF_U(count);
|
DPRINTF_U(count);
|
||||||
DPRINTF_U(lines);
|
DPRINTF_U(lines);
|
||||||
|
if (!lines)
|
||||||
|
goto finish;
|
||||||
|
|
||||||
if (2 * count != lines) {
|
if (2 * count != lines) {
|
||||||
DPRINTF_S("cannot delete files");
|
DPRINTF_S("cannot delete files");
|
||||||
|
@ -1396,15 +1406,11 @@ static bool batch_rename(const char *path)
|
||||||
if ((fd2 = open(g_tmpfpath, O_RDONLY)) == -1)
|
if ((fd2 = open(g_tmpfpath, O_RDONLY)) == -1)
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
while ((i = read(fd2, buf, sizeof(buf))) > 0)
|
lines = lines_in_file(fd2, buf, sizeof(buf));
|
||||||
while (i)
|
|
||||||
lines += (buf[--i] == '\n');
|
|
||||||
|
|
||||||
if (i < 0)
|
|
||||||
goto finish;
|
|
||||||
|
|
||||||
DPRINTF_U(count);
|
DPRINTF_U(count);
|
||||||
DPRINTF_U(lines);
|
DPRINTF_U(lines);
|
||||||
|
if (!lines)
|
||||||
|
goto finish;
|
||||||
|
|
||||||
if (count != lines) {
|
if (count != lines) {
|
||||||
DPRINTF_S("cannot delete files");
|
DPRINTF_S("cannot delete files");
|
||||||
|
@ -4341,10 +4347,7 @@ nochange:
|
||||||
/* Clear selection on repeat on same file */
|
/* Clear selection on repeat on same file */
|
||||||
if (selstartid == selendid) {
|
if (selstartid == selendid) {
|
||||||
resetselind();
|
resetselind();
|
||||||
nselected = 0;
|
clearselection();
|
||||||
selbufpos = 0;
|
|
||||||
cfg.selmode = 0;
|
|
||||||
writesel(NULL, 0);
|
|
||||||
break;
|
break;
|
||||||
} // fallthrough
|
} // fallthrough
|
||||||
case SEL_SELALL:
|
case SEL_SELALL:
|
||||||
|
@ -4432,11 +4435,8 @@ nochange:
|
||||||
spawn("sh", "-c", g_buf, path, F_NORMAL);
|
spawn("sh", "-c", g_buf, path, F_NORMAL);
|
||||||
|
|
||||||
/* Clear selection on move or delete */
|
/* Clear selection on move or delete */
|
||||||
if (sel == SEL_MV || sel == SEL_MVAS || sel == SEL_RMMUL) {
|
if (sel == SEL_MV || sel == SEL_MVAS || sel == SEL_RMMUL)
|
||||||
nselected = 0;
|
clearselection();
|
||||||
selbufpos = 0;
|
|
||||||
writesel(NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ndents)
|
if (ndents)
|
||||||
copycurname();
|
copycurname();
|
||||||
|
|
Loading…
Reference in New Issue