Code reformat

This commit is contained in:
Arun Prakash Jana 2021-07-16 21:19:17 +05:30
parent 26f44f0dfc
commit a911b3044a
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -1419,6 +1419,16 @@ static void appendfpath(const char *path, const size_t len)
selbufpos += xstrsncpy(pselbuf + selbufpos, path, len); selbufpos += xstrsncpy(pselbuf + selbufpos, path, len);
} }
static void selbufrealloc(const size_t alloclen)
{
if ((selbufpos + alloclen) > selbuflen) {
selbuflen = ALIGN_UP(selbufpos + alloclen, PATH_MAX);
pselbuf = xrealloc(pselbuf, selbuflen);
if (!pselbuf)
errexit();
}
}
/* Write selected file paths to fd, linefeed separated */ /* Write selected file paths to fd, linefeed separated */
static size_t seltofile(int fd, uint_t *pcount) static size_t seltofile(int fd, uint_t *pcount)
{ {
@ -1566,13 +1576,13 @@ static void invertselbuf(const int pathlen)
size_t len, endpos, shrinklen = 0, alloclen = 0; size_t len, endpos, shrinklen = 0, alloclen = 0;
char * const pbuf = g_buf + pathlen; char * const pbuf = g_buf + pathlen;
char *found; char *found;
int nmarked = 0, prev = 0; int i, nmarked = 0, prev = 0;
struct entry *dentp; struct entry *dentp;
selmark *marked = malloc(nselected * sizeof(selmark)); selmark *marked = malloc(nselected * sizeof(selmark));
bool scan = FALSE; bool scan = FALSE;
/* First pass: inversion */ /* First pass: inversion */
for (int i = 0; i < ndents; ++i) { for (i = 0; i < ndents; ++i) {
dentp = &pdents[i]; dentp = &pdents[i];
if (dentp->flags & FILE_SCANNED) { if (dentp->flags & FILE_SCANNED) {
@ -1580,7 +1590,6 @@ static void invertselbuf(const int pathlen)
dentp->flags ^= FILE_SELECTED; /* Clear selection status */ dentp->flags ^= FILE_SELECTED; /* Clear selection status */
scan = TRUE; scan = TRUE;
} else { } else {
++nselected;
dentp->flags |= FILE_SELECTED; dentp->flags |= FILE_SELECTED;
alloclen += pathlen + dentp->nlen; alloclen += pathlen + dentp->nlen;
} }
@ -1608,7 +1617,6 @@ static void invertselbuf(const int pathlen)
--nselected; --nselected;
shrinklen += len; /* buffer size adjustment */ shrinklen += len; /* buffer size adjustment */
} else { } else {
++nselected;
dentp->flags |= FILE_SELECTED; dentp->flags |= FILE_SELECTED;
alloclen += pathlen + dentp->nlen; alloclen += pathlen + dentp->nlen;
} }
@ -1625,7 +1633,7 @@ static void invertselbuf(const int pathlen)
qsort(marked, nmarked, sizeof(selmark), &markcmp); qsort(marked, nmarked, sizeof(selmark), &markcmp);
/* Some files might be adjacent. Merge them into a single entry */ /* Some files might be adjacent. Merge them into a single entry */
for (int i = 1; i < nmarked; ++i) { for (i = 1; i < nmarked; ++i) {
if (marked[i].startpos == marked[prev].startpos + marked[prev].len) if (marked[i].startpos == marked[prev].startpos + marked[prev].len)
marked[prev].len += marked[i].len; marked[prev].len += marked[i].len;
else { else {
@ -1644,7 +1652,7 @@ static void invertselbuf(const int pathlen)
nmarked = prev + 1; nmarked = prev + 1;
/* Using merged entries remove unselected chunks from selection buffer */ /* Using merged entries remove unselected chunks from selection buffer */
for (int i = 0; i < nmarked; ++i) { for (i = 0; i < nmarked; ++i) {
/* /*
* found: points to where the current block starts * found: points to where the current block starts
* variable is recycled from previous for readability * variable is recycled from previous for readability
@ -1661,26 +1669,20 @@ static void invertselbuf(const int pathlen)
memmove(found, found + len, endpos - (found + len - pselbuf)); memmove(found, found + len, endpos - (found + len - pselbuf));
} }
free(marked);
/* Buffer size adjustment */ /* Buffer size adjustment */
selbufpos -= shrinklen; selbufpos -= shrinklen;
if (alloclen > shrinklen && ((selbufpos + (alloclen - shrinklen)) > selbuflen)) { selbufrealloc(alloclen);
selbuflen = ALIGN_UP(selbufpos + (alloclen - shrinklen), PATH_MAX);
pselbuf = xrealloc(pselbuf, selbuflen);
if (!pselbuf)
errexit();
}
free(marked);
/* Second pass: append newly selected to buffer */ /* Second pass: append newly selected to buffer */
for (int i = 0; i < ndents; ++i) { for (i = 0; i < ndents; ++i) {
/* Skip unselected */ if (pdents[i].flags & FILE_SELECTED) {
if (!(pdents[i].flags & FILE_SELECTED)) len = pathlen + xstrsncpy(pbuf, pdents[i].name, NAME_MAX);
continue; appendfpath(g_buf, len);
++nselected;
len = pathlen + xstrsncpy(pbuf, pdents[i].name, NAME_MAX); }
appendfpath(g_buf, len);
} }
nselected ? writesel(pselbuf, selbufpos - 1) : clearselection(); nselected ? writesel(pselbuf, selbufpos - 1) : clearselection();
@ -1692,13 +1694,14 @@ static void invertselbuf(const int pathlen)
*/ */
static void addtoselbuf(const int pathlen, int startid, int endid) static void addtoselbuf(const int pathlen, int startid, int endid)
{ {
int i;
size_t len, alloclen = 0; size_t len, alloclen = 0;
struct entry *dentp; struct entry *dentp;
char *found; char *found;
char * const pbuf = g_buf + pathlen; char * const pbuf = g_buf + pathlen;
/* Remember current selection buffer position */ /* Remember current selection buffer position */
for (int i = startid; i <= endid; ++i) { for (i = startid; i <= endid; ++i) {
dentp = &pdents[i]; dentp = &pdents[i];
if (findselpos) { if (findselpos) {
@ -1717,14 +1720,9 @@ static void addtoselbuf(const int pathlen, int startid, int endid)
alloclen += pathlen + dentp->nlen; alloclen += pathlen + dentp->nlen;
} }
if ((selbufpos + alloclen) > selbuflen) { selbufrealloc(alloclen);
selbuflen = ALIGN_UP(selbufpos + alloclen, PATH_MAX);
pselbuf = xrealloc(pselbuf, selbuflen);
if (!pselbuf)
errexit();
}
for (int i = startid; i <= endid; ++i) { for (i = startid; i <= endid; ++i) {
if (!(pdents[i].flags & FILE_SELECTED)) { if (!(pdents[i].flags & FILE_SELECTED)) {
len = pathlen + xstrsncpy(pbuf, pdents[i].name, NAME_MAX); len = pathlen + xstrsncpy(pbuf, pdents[i].name, NAME_MAX);
appendfpath(g_buf, len); appendfpath(g_buf, len);