aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2017-04-02 21:15:31 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2017-04-02 21:45:10 +0530
commit87e0891b8d8b9ac2779d2785ac6fd6e946fdcc5b (patch)
tree735286b4f89d0f297483d9be768e028569f194db
parent4bc2ce0fbc27180ea5108121fec93eebf3112de1 (diff)
downloadnnn-87e0891b8d8b9ac2779d2785ac6fd6e946fdcc5b.tar.gz
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
-rw-r--r--Makefile23
-rw-r--r--nnn.c63
-rw-r--r--strlcat.c55
-rw-r--r--strlcpy.c50
-rw-r--r--util.h5
5 files changed, 53 insertions, 143 deletions
diff --git a/Makefile b/Makefile
index e971355..02f4661 100644
--- a/Makefile
+++ b/Makefile
@@ -5,25 +5,24 @@ MANPREFIX = $(PREFIX)/man
#CPPFLAGS = -DDEBUG
#CFLAGS = -g
-CFLAGS = -O3 -march=native -s
+CFLAGS = -O3 -march=native
LDLIBS = -lcurses
-DISTFILES = nnn.c strlcat.c strlcpy.c util.h config.def.h \
- nnn.1 Makefile README.md LICENSE
-OBJ = nnn.o strlcat.o strlcpy.o
+DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE
+LOCALCONFIG = config.h
+SRC = nnn.c
BIN = nnn
all: $(BIN)
-$(BIN): $(OBJ)
- $(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(LDLIBS)
+$(LOCALCONFIG): config.def.h
+ cp config.def.h $@
-nnn.o: util.h config.h
-strlcat.o: util.h
-strlcpy.o: util.h
+$(SRC): $(LOCALCONFIG)
-config.h: config.def.h
- cp config.def.h $@
+$(BIN): $(SRC)
+ $(CC) $(CFLAGS) -o $@ $(SRC) $(LDFLAGS) $(LDLIBS)
+ strip $(BIN)
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
@@ -43,4 +42,4 @@ dist:
rm -rf nnn-$(VERSION)
clean:
- rm -f $(BIN) $(OBJ) nnn-$(VERSION).tar.gz
+ rm -f $(BIN) nnn-$(VERSION).tar.gz
diff --git a/nnn.c b/nnn.c
index 8005b28..6641a11 100644
--- a/nnn.c
+++ b/nnn.c
@@ -18,8 +18,6 @@
#include <unistd.h>
#include <time.h>
-#include "util.h"
-
#ifdef DEBUG
#define DEBUG_FD 8
#define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
@@ -142,6 +140,29 @@ xrealloc(void *p, size_t size)
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().
* We are only looking for '/' in this program.
@@ -173,11 +194,11 @@ xdirname(const char *path)
static char out[PATH_MAX];
char tmp[PATH_MAX], *p;
- strlcpy(tmp, path, sizeof(tmp));
+ xstrlcpy(tmp, path, sizeof(tmp));
p = dirname(tmp);
if (p == NULL)
printerr(1, "dirname");
- strlcpy(out, p, sizeof(out));
+ xstrlcpy(out, p, sizeof(out));
return out;
}
#endif
@@ -194,10 +215,10 @@ xdirname(const char *path)
static char name[PATH_MAX];
char *last_slash;
- strlcpy(name, path, PATH_MAX);
+ xstrlcpy(name, path, PATH_MAX);
/* Find last '/'. */
- last_slash = name != NULL ? strrchr(name, '/') : NULL;
+ last_slash = strrchr(name, '/');
if (last_slash != NULL && last_slash != name && last_slash[1] == '\0') {
/* Determine whether all remaining characters are slashes. */
@@ -523,7 +544,7 @@ mkpath(char *dir, char *name, char *out, size_t n)
{
/* Handle absolute path */
if (name[0] == '/')
- strlcpy(out, name, n);
+ xstrlcpy(out, name, n);
else {
/* Handle root case */
if (strcmp(dir, "/") == 0)
@@ -632,7 +653,7 @@ dentfill(char *path, struct entry **dents,
if (filter(re, dp->d_name) == 0)
continue;
*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 */
mkpath(path, dp->d_name, newpath, sizeof(newpath));
r = lstat(newpath, &sb);
@@ -797,8 +818,8 @@ browse(char *ipath, char *ifilter)
regex_t re;
int r, fd;
- strlcpy(path, ipath, sizeof(path));
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(path, ipath, sizeof(path));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
oldpath[0] = '\0';
newpath[0] = '\0';
begin:
@@ -829,10 +850,10 @@ nochange:
goto nochange;
}
/* Save history */
- strlcpy(oldpath, path, sizeof(oldpath));
- strlcpy(path, dir, sizeof(path));
+ xstrlcpy(oldpath, path, sizeof(oldpath));
+ xstrlcpy(path, dir, sizeof(path));
/* Reset filter */
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
goto begin;
case SEL_GOIN:
/* Cannot descend in empty directories */
@@ -863,9 +884,9 @@ nochange:
printwarn();
goto nochange;
}
- strlcpy(path, newpath, sizeof(path));
+ xstrlcpy(path, newpath, sizeof(path));
/* Reset filter */
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
goto begin;
case S_IFREG:
{
@@ -935,7 +956,7 @@ nochange:
r = setfilter(&re, tmp);
if (r != 0)
goto nochange;
- strlcpy(fltr, tmp, sizeof(fltr));
+ xstrlcpy(fltr, tmp, sizeof(fltr));
DPRINTF_S(fltr);
/* Save current */
if (ndents > 0)
@@ -982,9 +1003,9 @@ nochange:
printwarn();
goto nochange;
}
- strlcpy(path, newpath, sizeof(path));
+ xstrlcpy(path, newpath, sizeof(path));
/* Reset filter */
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
DPRINTF_S(path);
goto begin;
case SEL_CDHOME:
@@ -997,15 +1018,15 @@ nochange:
printwarn();
goto nochange;
}
- strlcpy(path, tmp, sizeof(path));
+ xstrlcpy(path, tmp, sizeof(path));
/* Reset filter */
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
DPRINTF_S(path);
goto begin;
case SEL_TOGGLEDOT:
showhidden ^= 1;
initfilter(showhidden, &ifilter);
- strlcpy(fltr, ifilter, sizeof(fltr));
+ xstrlcpy(fltr, ifilter, sizeof(fltr));
goto begin;
case SEL_DETAIL:
showdetail = !showdetail;
diff --git a/strlcat.c b/strlcat.c
deleted file mode 100644
index bc523fb..0000000
--- a/strlcat.c
+++ /dev/null
@@ -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 */
-}
diff --git a/strlcpy.c b/strlcpy.c
deleted file mode 100644
index 0ec2b78..0000000
--- a/strlcpy.c
+++ /dev/null
@@ -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 */
-}
diff --git a/util.h b/util.h
deleted file mode 100644
index c4d1904..0000000
--- a/util.h
+++ /dev/null
@@ -1,5 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#undef strlcat
-size_t strlcat(char *, const char *, size_t);
-#undef strlcpy
-size_t strlcpy(char *, const char *, size_t);