mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 11:51:27 +00:00
Remove redundant stuff
1. Have a faster copy of strlcpy() 2. Remove redundant legacy files 3. Fix an issue with NULL check on array name
This commit is contained in:
parent
4bc2ce0fbc
commit
87e0891b8d
25
Makefile
25
Makefile
|
@ -5,26 +5,25 @@ MANPREFIX = $(PREFIX)/man
|
||||||
|
|
||||||
#CPPFLAGS = -DDEBUG
|
#CPPFLAGS = -DDEBUG
|
||||||
#CFLAGS = -g
|
#CFLAGS = -g
|
||||||
CFLAGS = -O3 -march=native -s
|
CFLAGS = -O3 -march=native
|
||||||
LDLIBS = -lcurses
|
LDLIBS = -lcurses
|
||||||
|
|
||||||
DISTFILES = nnn.c strlcat.c strlcpy.c util.h config.def.h \
|
DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE
|
||||||
nnn.1 Makefile README.md LICENSE
|
LOCALCONFIG = config.h
|
||||||
OBJ = nnn.o strlcat.o strlcpy.o
|
SRC = nnn.c
|
||||||
BIN = nnn
|
BIN = nnn
|
||||||
|
|
||||||
all: $(BIN)
|
all: $(BIN)
|
||||||
|
|
||||||
$(BIN): $(OBJ)
|
$(LOCALCONFIG): config.def.h
|
||||||
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS)
|
|
||||||
|
|
||||||
nnn.o: util.h config.h
|
|
||||||
strlcat.o: util.h
|
|
||||||
strlcpy.o: util.h
|
|
||||||
|
|
||||||
config.h: config.def.h
|
|
||||||
cp config.def.h $@
|
cp config.def.h $@
|
||||||
|
|
||||||
|
$(SRC): $(LOCALCONFIG)
|
||||||
|
|
||||||
|
$(BIN): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $(SRC) $(LDFLAGS) $(LDLIBS)
|
||||||
|
strip $(BIN)
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
||||||
cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
|
cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
|
||||||
|
@ -43,4 +42,4 @@ dist:
|
||||||
rm -rf nnn-$(VERSION)
|
rm -rf nnn-$(VERSION)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(BIN) $(OBJ) nnn-$(VERSION).tar.gz
|
rm -f $(BIN) nnn-$(VERSION).tar.gz
|
||||||
|
|
63
nnn.c
63
nnn.c
|
@ -18,8 +18,6 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define DEBUG_FD 8
|
#define DEBUG_FD 8
|
||||||
#define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
|
#define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
|
||||||
|
@ -142,6 +140,29 @@ xrealloc(void *p, size_t size)
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
xstrlcpy(char *dest, const char *src, size_t n)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < n && *src; i++)
|
||||||
|
*dest++ = *src++;
|
||||||
|
|
||||||
|
if (n) {
|
||||||
|
*dest = '\0';
|
||||||
|
|
||||||
|
#ifdef CHECK_XSTRLCPY_RET
|
||||||
|
/* Compiling this out as we are not checking
|
||||||
|
the return value anywhere (controlled case).
|
||||||
|
Just returning the number of bytes copied. */
|
||||||
|
while(*src++)
|
||||||
|
i++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The poor man's implementation of memrchr().
|
* The poor man's implementation of memrchr().
|
||||||
* We are only looking for '/' in this program.
|
* We are only looking for '/' in this program.
|
||||||
|
@ -173,11 +194,11 @@ xdirname(const char *path)
|
||||||
static char out[PATH_MAX];
|
static char out[PATH_MAX];
|
||||||
char tmp[PATH_MAX], *p;
|
char tmp[PATH_MAX], *p;
|
||||||
|
|
||||||
strlcpy(tmp, path, sizeof(tmp));
|
xstrlcpy(tmp, path, sizeof(tmp));
|
||||||
p = dirname(tmp);
|
p = dirname(tmp);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
printerr(1, "dirname");
|
printerr(1, "dirname");
|
||||||
strlcpy(out, p, sizeof(out));
|
xstrlcpy(out, p, sizeof(out));
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -194,10 +215,10 @@ xdirname(const char *path)
|
||||||
static char name[PATH_MAX];
|
static char name[PATH_MAX];
|
||||||
char *last_slash;
|
char *last_slash;
|
||||||
|
|
||||||
strlcpy(name, path, PATH_MAX);
|
xstrlcpy(name, path, PATH_MAX);
|
||||||
|
|
||||||
/* Find last '/'. */
|
/* Find last '/'. */
|
||||||
last_slash = name != NULL ? strrchr(name, '/') : NULL;
|
last_slash = strrchr(name, '/');
|
||||||
|
|
||||||
if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
|
if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
|
||||||
/* Determine whether all remaining characters are slashes. */
|
/* Determine whether all remaining characters are slashes. */
|
||||||
|
@ -523,7 +544,7 @@ mkpath(char *dir, char *name, char *out, size_t n)
|
||||||
{
|
{
|
||||||
/* Handle absolute path */
|
/* Handle absolute path */
|
||||||
if (name[0] == '/')
|
if (name[0] == '/')
|
||||||
strlcpy(out, name, n);
|
xstrlcpy(out, name, n);
|
||||||
else {
|
else {
|
||||||
/* Handle root case */
|
/* Handle root case */
|
||||||
if (strcmp(dir, "/") == 0)
|
if (strcmp(dir, "/") == 0)
|
||||||
|
@ -632,7 +653,7 @@ dentfill(char *path, struct entry **dents,
|
||||||
if (filter(re, dp->d_name) == 0)
|
if (filter(re, dp->d_name) == 0)
|
||||||
continue;
|
continue;
|
||||||
*dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
|
*dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
|
||||||
strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
|
xstrlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
|
||||||
/* Get mode flags */
|
/* Get mode flags */
|
||||||
mkpath(path, dp->d_name, newpath, sizeof(newpath));
|
mkpath(path, dp->d_name, newpath, sizeof(newpath));
|
||||||
r = lstat(newpath, &sb);
|
r = lstat(newpath, &sb);
|
||||||
|
@ -797,8 +818,8 @@ browse(char *ipath, char *ifilter)
|
||||||
regex_t re;
|
regex_t re;
|
||||||
int r, fd;
|
int r, fd;
|
||||||
|
|
||||||
strlcpy(path, ipath, sizeof(path));
|
xstrlcpy(path, ipath, sizeof(path));
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
oldpath[0] = '\0';
|
oldpath[0] = '\0';
|
||||||
newpath[0] = '\0';
|
newpath[0] = '\0';
|
||||||
begin:
|
begin:
|
||||||
|
@ -829,10 +850,10 @@ nochange:
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
/* Save history */
|
/* Save history */
|
||||||
strlcpy(oldpath, path, sizeof(oldpath));
|
xstrlcpy(oldpath, path, sizeof(oldpath));
|
||||||
strlcpy(path, dir, sizeof(path));
|
xstrlcpy(path, dir, sizeof(path));
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
goto begin;
|
goto begin;
|
||||||
case SEL_GOIN:
|
case SEL_GOIN:
|
||||||
/* Cannot descend in empty directories */
|
/* Cannot descend in empty directories */
|
||||||
|
@ -863,9 +884,9 @@ nochange:
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
strlcpy(path, newpath, sizeof(path));
|
xstrlcpy(path, newpath, sizeof(path));
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
goto begin;
|
goto begin;
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
{
|
{
|
||||||
|
@ -935,7 +956,7 @@ nochange:
|
||||||
r = setfilter(&re, tmp);
|
r = setfilter(&re, tmp);
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
goto nochange;
|
goto nochange;
|
||||||
strlcpy(fltr, tmp, sizeof(fltr));
|
xstrlcpy(fltr, tmp, sizeof(fltr));
|
||||||
DPRINTF_S(fltr);
|
DPRINTF_S(fltr);
|
||||||
/* Save current */
|
/* Save current */
|
||||||
if (ndents > 0)
|
if (ndents > 0)
|
||||||
|
@ -982,9 +1003,9 @@ nochange:
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
strlcpy(path, newpath, sizeof(path));
|
xstrlcpy(path, newpath, sizeof(path));
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
DPRINTF_S(path);
|
DPRINTF_S(path);
|
||||||
goto begin;
|
goto begin;
|
||||||
case SEL_CDHOME:
|
case SEL_CDHOME:
|
||||||
|
@ -997,15 +1018,15 @@ nochange:
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
strlcpy(path, tmp, sizeof(path));
|
xstrlcpy(path, tmp, sizeof(path));
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
DPRINTF_S(path);
|
DPRINTF_S(path);
|
||||||
goto begin;
|
goto begin;
|
||||||
case SEL_TOGGLEDOT:
|
case SEL_TOGGLEDOT:
|
||||||
showhidden ^= 1;
|
showhidden ^= 1;
|
||||||
initfilter(showhidden, &ifilter);
|
initfilter(showhidden, &ifilter);
|
||||||
strlcpy(fltr, ifilter, sizeof(fltr));
|
xstrlcpy(fltr, ifilter, sizeof(fltr));
|
||||||
goto begin;
|
goto begin;
|
||||||
case SEL_DETAIL:
|
case SEL_DETAIL:
|
||||||
showdetail = !showdetail;
|
showdetail = !showdetail;
|
||||||
|
|
55
strlcat.c
55
strlcat.c
|
@ -1,55 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Appends src to string dst of size dsize (unlike strncat, dsize is the
|
|
||||||
* full size of dst, not space left). At most dsize-1 characters
|
|
||||||
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
|
|
||||||
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
|
|
||||||
* If retval >= dsize, truncation occurred.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
strlcat(char *dst, const char *src, size_t dsize)
|
|
||||||
{
|
|
||||||
const char *odst = dst;
|
|
||||||
const char *osrc = src;
|
|
||||||
size_t n = dsize;
|
|
||||||
size_t dlen;
|
|
||||||
|
|
||||||
/* Find the end of dst and adjust bytes left but don't go past end. */
|
|
||||||
while (n-- != 0 && *dst != '\0')
|
|
||||||
dst++;
|
|
||||||
dlen = dst - odst;
|
|
||||||
n = dsize - dlen;
|
|
||||||
|
|
||||||
if (n-- == 0)
|
|
||||||
return(dlen + strlen(src));
|
|
||||||
while (*src != '\0') {
|
|
||||||
if (n != 0) {
|
|
||||||
*dst++ = *src;
|
|
||||||
n--;
|
|
||||||
}
|
|
||||||
src++;
|
|
||||||
}
|
|
||||||
*dst = '\0';
|
|
||||||
|
|
||||||
return(dlen + (src - osrc)); /* count does not include NUL */
|
|
||||||
}
|
|
50
strlcpy.c
50
strlcpy.c
|
@ -1,50 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copy string src to buffer dst of size dsize. At most dsize-1
|
|
||||||
* chars will be copied. Always NUL terminates (unless dsize == 0).
|
|
||||||
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
strlcpy(char *dst, const char *src, size_t dsize)
|
|
||||||
{
|
|
||||||
const char *osrc = src;
|
|
||||||
size_t nleft = dsize;
|
|
||||||
|
|
||||||
/* Copy as many bytes as will fit. */
|
|
||||||
if (nleft != 0) {
|
|
||||||
while (--nleft != 0) {
|
|
||||||
if ((*dst++ = *src++) == '\0')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Not enough room in dst, add NUL and traverse rest of src. */
|
|
||||||
if (nleft == 0) {
|
|
||||||
if (dsize != 0)
|
|
||||||
*dst = '\0'; /* NUL-terminate dst */
|
|
||||||
while (*src++)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
return(src - osrc - 1); /* count does not include NUL */
|
|
||||||
}
|
|
Loading…
Reference in a new issue