Minor refactor

This commit is contained in:
Arun Prakash Jana 2020-04-25 01:28:01 +05:30
parent f8a243ea91
commit 06fe7d52dd
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -1142,8 +1142,8 @@ static size_t seltofile(int fd, uint *pcount, bool tgt)
{ {
uint lastpos, count = 0; uint lastpos, count = 0;
char *pbuf = pselbuf; char *pbuf = pselbuf;
size_t pos = 0, len; size_t pos = 0;
ssize_t r, prefixlen, initlen, tgtlen; ssize_t len, prefixlen, initlen;
if (pcount) if (pcount)
*pcount = 0; *pcount = 0;
@ -1154,33 +1154,25 @@ static size_t seltofile(int fd, uint *pcount, bool tgt)
lastpos = selbufpos - 1; lastpos = selbufpos - 1;
if (tgt) { if (tgt) {
prefixlen = (ssize_t)xstrsncpy(g_buf, prefixpath, PATH_MAX) - 1; prefixlen = (ssize_t)xstrlen(prefixpath);
initlen = strlen(initpath); initlen = (ssize_t)xstrlen(initpath);
} }
while (pos <= lastpos) { while (pos <= lastpos) {
len = xstrlen(pbuf); DPRINTF_S(pbuf);
pos += len; len = (ssize_t)xstrlen(pbuf);
if (!tgt) { if (!tgt || (strncmp(initpath, pbuf, initlen) != 0)) {
r = write(fd, pbuf, len); if (write(fd, pbuf, len) != len)
if (r != (ssize_t)len)
return pos; return pos;
} else { } else {
if (!strncmp(initpath, pbuf, initlen)) { if (write(fd, prefixpath, prefixlen) != prefixlen)
tgtlen = xstrsncpy(g_buf + prefixlen, pbuf + initlen, PATH_MAX - prefixlen - 1); return pos;
tgtlen += prefixlen - 1; if (write(fd, pbuf + initlen, len - initlen) != (len - initlen))
} else return pos;
tgtlen = (ssize_t)xstrsncpy(g_buf, pbuf, PATH_MAX) - 1;
r = write(fd, g_buf, tgtlen);
if (r != tgtlen) {
DPRINTF_S(pbuf);
DPRINTF_S(g_buf);
return 0;
}
} }
pos += len;
if (pos <= lastpos) { if (pos <= lastpos) {
if (write(fd, "\n", 1) != 1) if (write(fd, "\n", 1) != 1)
return pos; return pos;
@ -5382,7 +5374,7 @@ nochange:
if (cfg.useeditor && (!sb.st_size || if (cfg.useeditor && (!sb.st_size ||
#ifdef FILE_MIME_OPTS #ifdef FILE_MIME_OPTS
(get_output(g_buf, CMD_LEN_MAX, "file", FILE_MIME_OPTS, newpath, FALSE) (get_output(g_buf, CMD_LEN_MAX, "file", FILE_MIME_OPTS, newpath, FALSE)
&& !strncmp(g_buf, "text/", 5)))) { && !(((int *)g_buf)[0] == *(int *)"text" && g_buf[4] == '/')))) {
#else #else
/* no mime option; guess from description instead */ /* no mime option; guess from description instead */
(get_output(g_buf, CMD_LEN_MAX, "file", "-b", newpath, FALSE) (get_output(g_buf, CMD_LEN_MAX, "file", "-b", newpath, FALSE)