Optimized xstrlen(), xstrdup()

This commit is contained in:
Arun Prakash Jana 2020-04-15 15:00:40 +05:30
parent 30a6caf8bc
commit 588b413832
No known key found for this signature in database
GPG key ID: A75979F35C080412

108
src/nnn.c
View file

@ -927,13 +927,32 @@ static size_t xstrsncpy(char *restrict dst, const char *restrict src, size_t n)
return end - dst; return end - dst;
} }
static inline size_t xstrlen(const char *s)
{
#if !defined(__GLIBC__)
return strlen(s);
#else
return (char *)rawmemchr(s, '\0') - s;
#endif
}
static char *xstrdup(const char* s)
{
size_t len = xstrlen(s) + 1;
char *ptr = malloc(len);
if (ptr)
xstrsncpy(ptr, s, len);
return ptr;
}
static bool is_suffix(const char *str, const char *suffix) static bool is_suffix(const char *str, const char *suffix)
{ {
if (!str || !suffix) if (!str || !suffix)
return FALSE; return FALSE;
size_t lenstr = strlen(str); size_t lenstr = xstrlen(str);
size_t lensuffix = strlen(suffix); size_t lensuffix = xstrlen(suffix);
if (lensuffix > lenstr) if (lensuffix > lenstr)
return FALSE; return FALSE;
@ -945,7 +964,7 @@ static bool is_suffix(const char *str, const char *suffix)
* The poor man's implementation of memrchr(3). * The poor man's implementation of memrchr(3).
* We are only looking for '/' in this program. * We are only looking for '/' in this program.
* And we are NOT expecting a '/' at the end. * And we are NOT expecting a '/' at the end.
* Ideally 0 < n <= strlen(s). * Ideally 0 < n <= xstrlen(s).
*/ */
static void *xmemrchr(uchar *s, uchar ch, size_t n) static void *xmemrchr(uchar *s, uchar ch, size_t n)
{ {
@ -1014,7 +1033,7 @@ static char *abspath(const char *path, const char *cwd)
if (!path || !cwd) if (!path || !cwd)
return NULL; return NULL;
size_t dst_size = 0, src_size = strlen(path), cwd_size = strlen(cwd); size_t dst_size = 0, src_size = xstrlen(path), cwd_size = xstrlen(cwd);
const char *src; const char *src;
char *dst; char *dst;
char *resolved_path = malloc(src_size + (*path == '/' ? 0 : cwd_size) + 1); char *resolved_path = malloc(src_size + (*path == '/' ? 0 : cwd_size) + 1);
@ -1061,7 +1080,7 @@ static char *abspath(const char *path, const char *cwd)
/* A very simplified implementation, changes path */ /* A very simplified implementation, changes path */
static char *xdirname(char *path) static char *xdirname(char *path)
{ {
char *base = xmemrchr((uchar *)path, '/', strlen(path)); char *base = xmemrchr((uchar *)path, '/', xstrlen(path));
if (base == path) if (base == path)
path[1] = '\0'; path[1] = '\0';
@ -1073,7 +1092,7 @@ static char *xdirname(char *path)
static char *xbasename(char *path) static char *xbasename(char *path)
{ {
char *base = xmemrchr((uchar *)path, '/', strlen(path)); // NOLINT char *base = xmemrchr((uchar *)path, '/', xstrlen(path)); // NOLINT
return base ? base + 1 : path; return base ? base + 1 : path;
} }
@ -1136,7 +1155,7 @@ static size_t seltofile(int fd, uint *pcount)
lastpos = selbufpos - 1; lastpos = selbufpos - 1;
while (pos <= lastpos) { while (pos <= lastpos) {
len = strlen(pbuf); len = xstrlen(pbuf);
pos += len; pos += len;
r = write(fd, pbuf, len); r = write(fd, pbuf, len);
@ -1612,7 +1631,7 @@ static int spawn(char *file, char *arg1, char *arg2, const char *dir, uchar flag
} }
if (flag & F_MULTI) { if (flag & F_MULTI) {
size_t len = strlen(file) + 1; size_t len = xstrlen(file) + 1;
cmd = (char *)malloc(len); cmd = (char *)malloc(len);
if (!cmd) { if (!cmd) {
@ -1870,7 +1889,7 @@ static bool batch_rename(const char *path)
if (fd1 == -1) if (fd1 == -1)
return ret; return ret;
xstrsncpy(foriginal, g_tmpfpath, strlen(g_tmpfpath) + 1); xstrsncpy(foriginal, g_tmpfpath, xstrlen(g_tmpfpath) + 1);
fd2 = create_tmp_file(); fd2 = create_tmp_file();
if (fd2 == -1) { if (fd2 == -1) {
@ -1941,8 +1960,8 @@ static void get_archive_cmd(char *cmd, const char *archive)
static void archive_selection(const char *cmd, const char *archive, const char *curpath) static void archive_selection(const char *cmd, const char *archive, const char *curpath)
{ {
/* The 70 comes from the string below */ /* The 70 comes from the string below */
char *buf = (char *)malloc((70 + strlen(cmd) + strlen(archive) char *buf = (char *)malloc((70 + xstrlen(cmd) + xstrlen(archive)
+ strlen(curpath) + strlen(selpath)) * sizeof(char)); + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char));
if (!buf) { if (!buf) {
DPRINTF_S(strerror(errno)); DPRINTF_S(strerror(errno));
printwarn(NULL); printwarn(NULL);
@ -1963,7 +1982,7 @@ static void archive_selection(const char *cmd, const char *archive, const char *
static bool write_lastdir(const char *curpath) static bool write_lastdir(const char *curpath)
{ {
bool ret = TRUE; bool ret = TRUE;
size_t len = strlen(cfgdir); size_t len = xstrlen(cfgdir);
xstrsncpy(cfgdir + len, "/.lastd", 8); xstrsncpy(cfgdir + len, "/.lastd", 8);
DPRINTF_S(cfgdir); DPRINTF_S(cfgdir);
@ -2150,7 +2169,7 @@ static int setfilter(regex_t *regex, const char *filter)
static int visible_re(const fltrexp_t *fltrexp, const char *fname) static int visible_re(const fltrexp_t *fltrexp, const char *fname)
{ {
#ifdef PCRE #ifdef PCRE
return pcre_exec(fltrexp->pcrex, NULL, fname, strlen(fname), 0, 0, NULL, 0) == 0; return pcre_exec(fltrexp->pcrex, NULL, fname, xstrlen(fname), 0, 0, NULL, 0) == 0;
#else #else
return regexec(fltrexp->regex, fname, 0, NULL, 0) == 0; return regexec(fltrexp->regex, fname, 0, NULL, 0) == 0;
#endif #endif
@ -2360,7 +2379,7 @@ static void showfilterinfo(void)
((fnstrstr == &strcasestr) ? "ic" : "noic")); ((fnstrstr == &strcasestr) ? "ic" : "noic"));
clearinfoln(); clearinfoln();
mvaddstr(xlines - 2, xcols - strlen(info), info); mvaddstr(xlines - 2, xcols - xstrlen(info), info);
} }
static void showfilter(char *str) static void showfilter(char *str)
@ -2890,7 +2909,7 @@ static int xlink(char *prefix, char *path, char *curfname, char *buf, int *prese
} }
while (pos < selbufpos) { while (pos < selbufpos) {
len = strlen(psel); len = xstrlen(psel);
fname = xbasename(psel); fname = xbasename(psel);
r = xstrsncpy(buf, prefix, NAME_MAX + 1); /* Copy prefix */ r = xstrsncpy(buf, prefix, NAME_MAX + 1); /* Copy prefix */
@ -2918,11 +2937,9 @@ static bool parsekvpair(kv **arr, char **envcpy, const uchar id, uchar *items)
return TRUE; return TRUE;
nextkey = ptr; nextkey = ptr;
while (*nextkey) { while (*nextkey)
if (*nextkey == ':') if (*nextkey++ == ':')
++maxitems; ++maxitems;
++nextkey;
}
if (!maxitems || maxitems > 100) if (!maxitems || maxitems > 100)
return FALSE; return FALSE;
@ -2935,16 +2952,15 @@ static bool parsekvpair(kv **arr, char **envcpy, const uchar id, uchar *items)
kvarr = *arr; kvarr = *arr;
*envcpy = strdup(ptr); *envcpy = xstrdup(ptr);
if (!*envcpy) { if (!*envcpy) {
xerror(); xerror();
return FALSE; return FALSE;
} }
if (nextkey - ptr > 1) /* Clear trailing ;s */
/* Clear trailing ; or / */ if (*--nextkey == ';')
if (*--nextkey == ';') *(*envcpy + (nextkey - ptr)) = '\0';
*(*envcpy + (nextkey - ptr)) = '\0';
ptr = *envcpy; ptr = *envcpy;
nextkey = ptr; nextkey = ptr;
@ -2977,7 +2993,7 @@ static bool parsekvpair(kv **arr, char **envcpy, const uchar id, uchar *items)
/* Redundant check so far, all paths will get evaluated and fail */ /* Redundant check so far, all paths will get evaluated and fail */
//for (i = 0; i < maxitems && kvarr[i].key; ++i) //for (i = 0; i < maxitems && kvarr[i].key; ++i)
// if (strlen(kvarr[i].val) >= PATH_MAX) // if (xstrlen(kvarr[i].val) >= PATH_MAX)
// return FALSE; // return FALSE;
*items = maxitems; *items = maxitems;
@ -3002,8 +3018,8 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar max, bool bookmark)
return kvarr[r].val; return kvarr[r].val;
if (kvarr[r].val[0] == '~') { if (kvarr[r].val[0] == '~') {
ssize_t len = strlen(home); ssize_t len = xstrlen(home);
ssize_t loclen = strlen(kvarr[r].val); ssize_t loclen = xstrlen(kvarr[r].val);
xstrsncpy(g_buf, home, len + 1); xstrsncpy(g_buf, home, len + 1);
xstrsncpy(g_buf + len, kvarr[r].val + 1, loclen); xstrsncpy(g_buf + len, kvarr[r].val + 1, loclen);
@ -3129,7 +3145,7 @@ static char *coolsize(off_t size)
char *frac = xitoa(rem); char *frac = xitoa(rem);
size_t toprint = i > 3 ? 3 : i; size_t toprint = i > 3 ? 3 : i;
size_t len = strlen(frac); size_t len = xstrlen(frac);
if (len < toprint) { if (len < toprint) {
size_buf[ret] = size_buf[ret + 1] = size_buf[ret + 2] = '0'; size_buf[ret] = size_buf[ret + 1] = size_buf[ret + 2] = '0';
@ -3273,7 +3289,7 @@ static void printent_long(const struct entry *ent, uint namecols, bool sel)
} }
size = coolsize(cfg.blkorder ? ent->blocks << blk_shift : ent->size); size = coolsize(cfg.blkorder ? ent->blocks << blk_shift : ent->size);
len = 10 - (uint)strlen(size); len = 10 - (uint)xstrlen(size);
while (--len) while (--len)
addch(' '); addch(' ');
addstr(size); addstr(size);
@ -3786,21 +3802,19 @@ static bool archive_mount(char *path, char *newpath)
{ {
char *dir, *cmd = utils[UTIL_ARCHIVEMOUNT]; char *dir, *cmd = utils[UTIL_ARCHIVEMOUNT];
char *name = dents[cur].name; char *name = dents[cur].name;
size_t len; size_t len = dents[cur].nlen;
if (!getutil(cmd)) { if (!getutil(cmd)) {
printmsg(messages[MSG_UTIL_MISSING]); printmsg(messages[MSG_UTIL_MISSING]);
return FALSE; return FALSE;
} }
dir = strdup(name); dir = xstrdup(name);
if (!dir) { if (!dir) {
printmsg(messages[MSG_FAILED]); printmsg(messages[MSG_FAILED]);
return FALSE; return FALSE;
} }
len = strlen(dir);
while (len > 1) while (len > 1)
if (dir[--len] == '.') { if (dir[--len] == '.') {
dir[len] = '\0'; dir[len] = '\0';
@ -3882,7 +3896,7 @@ static bool remote_mount(char *newpath, char *currentpath)
} }
/* Convert "Host" to "Host:" */ /* Convert "Host" to "Host:" */
size_t len = strlen(tmp); size_t len = xstrlen(tmp);
if (tmp[len - 1] != ':') { /* Append ':' if missing */ if (tmp[len - 1] != ':') { /* Append ':' if missing */
tmp[len] = ':'; tmp[len] = ':';
@ -4154,7 +4168,7 @@ static bool run_cmd_as_plugin(const char *path, const char *file, char *runfile)
xstrsncpy(g_buf, file, PATH_MAX); xstrsncpy(g_buf, file, PATH_MAX);
len = strlen(g_buf); len = xstrlen(g_buf);
if (len > 1 && g_buf[len - 1] == '*') { if (len > 1 && g_buf[len - 1] == '*') {
flags &= ~F_CONFIRM; /* Skip user confirmation */ flags &= ~F_CONFIRM; /* Skip user confirmation */
g_buf[len - 1] = '\0'; /* Get rid of trailing no confirmation symbol */ g_buf[len - 1] = '\0'; /* Get rid of trailing no confirmation symbol */
@ -4340,7 +4354,7 @@ static int dentfill(char *path, struct entry **dents)
if (cfg.blkorder) { if (cfg.blkorder) {
num_files = 0; num_files = 0;
dir_blocks = 0; dir_blocks = 0;
buf = (char *)alloca(strlen(path) + NAME_MAX + 2); buf = (char *)alloca(xstrlen(path) + NAME_MAX + 2);
if (!buf) if (!buf)
return 0; return 0;
@ -4967,7 +4981,7 @@ static void redraw(char *path)
attron(A_UNDERLINE); attron(A_UNDERLINE);
/* Print path */ /* Print path */
i = (int)strlen(path); i = (int)xstrlen(path);
if ((i + MIN_DISPLAY_COLS) <= ncols) if ((i + MIN_DISPLAY_COLS) <= ncols)
addnstr(path, ncols - MIN_DISPLAY_COLS); addnstr(path, ncols - MIN_DISPLAY_COLS);
else { else {
@ -5386,7 +5400,7 @@ nochange:
#ifdef PCRE #ifdef PCRE
if (!pcre_exec(archive_pcre, NULL, dents[cur].name, if (!pcre_exec(archive_pcre, NULL, dents[cur].name,
strlen(dents[cur].name), 0, 0, NULL, 0)) { xstrlen(dents[cur].name), 0, 0, NULL, 0)) {
#else #else
if (!regexec(&archive_re, dents[cur].name, 0, NULL, 0)) { if (!regexec(&archive_re, dents[cur].name, 0, NULL, 0)) {
#endif #endif
@ -5525,7 +5539,7 @@ nochange:
goto begin; goto begin;
case SEL_PIN: case SEL_PIN:
free(mark); free(mark);
mark = strdup(path); mark = xstrdup(path);
printwait(mark, &presel); printwait(mark, &presel);
goto nochange; goto nochange;
case SEL_FLTR: case SEL_FLTR:
@ -6276,7 +6290,7 @@ static char *make_tmp_tree(char **paths, ssize_t entries, const char *prefix)
int err, ignore = 0; int err, ignore = 0;
struct stat sb; struct stat sb;
char *slash, *tmp; char *slash, *tmp;
ssize_t len = strlen(prefix); ssize_t len = xstrlen(prefix);
char *tmpdir = malloc(sizeof(char) * (PATH_MAX + TMP_LEN_MAX)); char *tmpdir = malloc(sizeof(char) * (PATH_MAX + TMP_LEN_MAX));
if (!tmpdir) { if (!tmpdir) {
@ -6311,10 +6325,10 @@ static char *make_tmp_tree(char **paths, ssize_t entries, const char *prefix)
} }
/* Don't copy the common prefix */ /* Don't copy the common prefix */
xstrsncpy(tmp, paths[i] + len, strlen(paths[i]) - len + 1); xstrsncpy(tmp, paths[i] + len, xstrlen(paths[i]) - len + 1);
/* Get the dir containing the path */ /* Get the dir containing the path */
slash = xmemrchr((uchar *)tmp, '/', strlen(paths[i]) - len); slash = xmemrchr((uchar *)tmp, '/', xstrlen(paths[i]) - len);
if (slash) if (slash)
*slash = '\0'; *slash = '\0';
@ -6466,7 +6480,7 @@ static char *load_input()
if (prefixpath[0]) { if (prefixpath[0]) {
if (entries == 1) { if (entries == 1) {
tmp = xmemrchr((uchar *)prefixpath, '/', strlen(prefixpath)); tmp = xmemrchr((uchar *)prefixpath, '/', xstrlen(prefixpath));
if (!tmp) if (!tmp)
goto malloc_2; goto malloc_2;
@ -6558,12 +6572,12 @@ static bool setup_config(void)
return FALSE; return FALSE;
} }
len = strlen(xdgcfg) + 1 + 13; /* add length of "/nnn/sessions" */ len = xstrlen(xdgcfg) + 1 + 13; /* add length of "/nnn/sessions" */
xdg = TRUE; xdg = TRUE;
} }
if (!xdg) if (!xdg)
len = strlen(home) + 1 + 21; /* add length of "/.config/nnn/sessions" */ len = xstrlen(home) + 1 + 21; /* add length of "/.config/nnn/sessions" */
cfgdir = (char *)malloc(len); cfgdir = (char *)malloc(len);
plugindir = (char *)malloc(len); plugindir = (char *)malloc(len);
@ -6675,7 +6689,7 @@ int main(int argc, char *argv[])
middle_click_key = (uchar)middle_click_env[0]; middle_click_key = (uchar)middle_click_env[0];
#endif #endif
const char* const env_opts = xgetenv(env_cfg[NNN_OPTS], NULL); const char* const env_opts = xgetenv(env_cfg[NNN_OPTS], NULL);
int env_opts_id = env_opts ? (int)strlen(env_opts) : -1; int env_opts_id = env_opts ? (int)xstrlen(env_opts) : -1;
#ifndef NORL #ifndef NORL
bool rlhist = FALSE; bool rlhist = FALSE;
#endif #endif
@ -6861,7 +6875,7 @@ int main(int argc, char *argv[])
} else { } else {
arg = argv[optind]; arg = argv[optind];
DPRINTF_S(arg); DPRINTF_S(arg);
if (strlen(arg) > 7 && !strncmp(arg, "file://", 7)) if (xstrlen(arg) > 7 && !strncmp(arg, "file://", 7))
arg = arg + 7; arg = arg + 7;
initpath = realpath(arg, NULL); initpath = realpath(arg, NULL);
DPRINTF_S(initpath); DPRINTF_S(initpath);