Minor optimization, refactor

This commit is contained in:
Arun Prakash Jana 2017-12-04 19:52:35 +05:30
parent 092cdf3f82
commit 6ccb8ee94b
No known key found for this signature in database
GPG key ID: A75979F35C080412

12
nnn.c
View file

@ -188,11 +188,11 @@ typedef unsigned char uchar;
/* Directory entry */ /* Directory entry */
typedef struct entry { typedef struct entry {
char name[NAME_MAX]; char name[NAME_MAX + 1];
mode_t mode;
time_t t; time_t t;
off_t size; off_t size;
blkcnt_t blocks; /* number of 512B blocks allocated */ blkcnt_t blocks; /* number of 512B blocks allocated */
mode_t mode;
} *pEntry; } *pEntry;
/* Bookmark */ /* Bookmark */
@ -348,7 +348,7 @@ xstrlen(const char *s)
static size_t static size_t
xstrlcpy(char *dest, const char *src, size_t n) xstrlcpy(char *dest, const char *src, size_t n)
{ {
static size_t len, blocks; static size_t len, blocks, lsize = sizeof(ulong);
static const uint _WSHIFT = (sizeof(ulong) == 8) ? 3 : 2; static const uint _WSHIFT = (sizeof(ulong) == 8) ? 3 : 2;
if (!src || !dest) if (!src || !dest)
@ -361,8 +361,11 @@ xstrlcpy(char *dest, const char *src, size_t n)
/* Save total number of bytes to copy in len */ /* Save total number of bytes to copy in len */
len = n; len = n;
if (n >= lsize) {
blocks = n >> _WSHIFT; blocks = n >> _WSHIFT;
n -= (blocks << _WSHIFT); n -= (blocks << _WSHIFT);
} else
blocks = 0;
if (blocks) { if (blocks) {
static ulong *s, *d; static ulong *s, *d;
@ -872,12 +875,11 @@ static void
fill(struct entry **dents, int (*filter)(regex_t *, char *), regex_t *re) fill(struct entry **dents, int (*filter)(regex_t *, char *), regex_t *re)
{ {
static int count; static int count;
static struct entry _dent, *dentp1, *dentp2;
for (count = 0; count < ndents; ++count) { for (count = 0; count < ndents; ++count) {
if (filter(re, (*dents)[count].name) == 0) { if (filter(re, (*dents)[count].name) == 0) {
if (count != --ndents) { if (count != --ndents) {
static struct entry _dent, *dentp1, *dentp2;
dentp1 = &(*dents)[count]; dentp1 = &(*dents)[count];
dentp2 = &(*dents)[ndents]; dentp2 = &(*dents)[ndents];