Eliminate newline from selection buffer

This commit is contained in:
Arun Prakash Jana 2018-11-27 21:06:43 +05:30
parent 28eb6a6312
commit ab61bb03d0
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -683,17 +683,46 @@ static bool appendfpath(const char *path, const size_t len)
} }
} }
if (copybufpos) /* Enabling the following will miss files with newlines */
pcopybuf[copybufpos - 1] = '\n'; /* if (copybufpos)
pcopybuf[copybufpos - 1] = '\n'; */
copybufpos += xstrlcpy(pcopybuf + copybufpos, path, len); copybufpos += xstrlcpy(pcopybuf + copybufpos, path, len);
return TRUE; return TRUE;
} }
/* Write selected file paths to fd, linefeed separated */
static ssize_t selectiontofd(int fd)
{
char *pbuf = pcopybuf;
ssize_t pos = 0, len, r, lastpos = copybufpos - 1;
while (pos < copybufpos) {
while(pcopybuf[pos])
++pos;
len = strlen(pbuf);
r = write(fd, pbuf, len);
if (r != len)
return pos;
if (pos != lastpos) {
if (write(fd, "\n", 1) != 1)
return pos;
pbuf = pbuf + len + 1;
}
++pos;
}
return pos;
}
static bool showcplist() static bool showcplist()
{ {
ssize_t len; int fd;
ssize_t pos;
if (!copybufpos) if (!copybufpos)
return FALSE; return FALSE;
@ -705,15 +734,15 @@ static bool showcplist()
return -1; return -1;
} }
int fd = mkstemp(g_tmpfpath); fd = mkstemp(g_tmpfpath);
if (fd == -1) if (fd == -1)
return FALSE; return FALSE;
len = write(fd, pcopybuf, copybufpos - 1); pos = selectiontofd(fd);
close(fd);
close(fd);
exitcurses(); exitcurses();
if (len == copybufpos - 1) if (pos && pos == copybufpos)
get_output(NULL, 0, "cat", g_tmpfpath, NULL, 1); get_output(NULL, 0, "cat", g_tmpfpath, NULL, 1);
unlink(g_tmpfpath); unlink(g_tmpfpath);
refresh(); refresh();
@ -3056,11 +3085,11 @@ nochange:
} }
if (sel == SEL_CP) if (sel == SEL_CP)
snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -d \'\n\' -a %s cp -ir --preserve=all -t .", g_cppath); snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -a %s cp -ir --preserve=all -t .", g_cppath);
else if (sel == SEL_MV) else if (sel == SEL_MV)
snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -d \'\n\' -a %s mv -i -t .", g_cppath); snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -a %s mv -i -t .", g_cppath);
else /* SEL_RMMUL */ else /* SEL_RMMUL */
snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -d \'\n\' -a %s rm -Ir", g_cppath); snprintf(g_buf, MAX_CMD_LEN, "xargs -0 -a %s rm -Ir", g_cppath);
spawn("sh", "-c", g_buf, path, F_NORMAL | F_SIGINT); spawn("sh", "-c", g_buf, path, F_NORMAL | F_SIGINT);
@ -3570,8 +3599,8 @@ int main(int argc, char *argv[])
if (cfg.pickraw) { if (cfg.pickraw) {
if (copybufpos) { if (copybufpos) {
opt = write(1, pcopybuf, copybufpos - 1); opt = selectiontofd(1);
if (opt != (int)(copybufpos - 1)) if (opt != (int)(copybufpos))
fprintf(stderr, "%s\n", strerror(errno)); fprintf(stderr, "%s\n", strerror(errno));
} }
} else if (!cfg.picker && g_cppath[0]) } else if (!cfg.picker && g_cppath[0])