]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Fully remove snprintf requirement from the library.
authorNick Bowler <nbowler@draconx.ca>
Sat, 18 Nov 2023 21:41:44 +0000 (16:41 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sat, 18 Nov 2023 21:41:44 +0000 (16:41 -0500)
There is only one remaining unconditional snprintf usage in the library,
which implements the cdecl__strlcpy function.  Replacing this with an
alternate implementation means we no longer need snprintf at all except
when NLS is enabled (in that case, we assume snprintf is available --
possibly provided by GNU libintl).

So we can drop quite a lot of gnulib code potentially going into the
library.  The usual gnulib behaviour is to substitute complete re-
implementations for almost any possible problem, even ones that are
totally irrelevant to the usage at hand.

Bumping dxcommon removes snprintf from the applications too so we can
drop all of this nonsense.  On systems that used the snprintf fallback,
this reduces the overall library size by about 30%.

bootstrap
common
m4/gnulib-cache.m4
src/output.c

index 5ec2dc408e5a7e12b836435c7d6604b3d9d52e26..ef46efa59b22d522ea2995412cfe485e268e37c2 100755 (executable)
--- a/bootstrap
+++ b/bootstrap
@@ -33,7 +33,7 @@ if test -x $GNULIB/gnulib-tool; then
   exec 3>lib/symfiles.tmp 4<lib/symfiles.tmp
   rm -f lib/symfiles.tmp
 
-  shared_modules='lock tls snprintf'
+  shared_modules='lock tls'
   set x --extract-recursive-dependencies $shared_modules; shift
   (set -x; $GNULIB/gnulib-tool "$@" >&3 ) || die "gnulib-tool failed"
   shared_modules=`LC_ALL=C sort -u <&4`
diff --git a/common b/common
index 4081782832b7dc8b7426cedcec9a4495229dda6c..11dfda46d8834955846372602441bbcec6a9047f 160000 (submodule)
--- a/common
+++ b/common
@@ -1 +1 @@
-Subproject commit 4081782832b7dc8b7426cedcec9a4495229dda6c
+Subproject commit 11dfda46d8834955846372602441bbcec6a9047f
index c9703057da93505b034b7d51d6ed5ef969960bf8..a246eaba2653d557bb924420f189eda817a317dc 100644 (file)
@@ -49,7 +49,6 @@
 #  lock \
 #  mbswidth \
 #  readline \
-#  snprintf \
 #  tls
 
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
@@ -63,7 +62,6 @@ gl_MODULES([
   lock
   mbswidth
   readline
-  snprintf
   tls
 ])
 gl_AVOID([gperf std-gnu11])
index fb80f99e7128e17606498287a4b936b4b1d311dc..c0438e602753fb0b37c4bdaeaba0be457481b5da 100644 (file)
@@ -38,9 +38,15 @@ size_t cdecl__advance(struct output_state *dst, size_t amount)
 
 size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen)
 {
-       if (dst)
-               snprintf(dst, dstlen, "%s", src);
-       return strlen(src);
+       size_t srclen = strlen(src);
+
+       if (dstlen > 0) {
+               memcpy(dst, src, MIN(dstlen, srclen+1));
+               if (dstlen <= srclen)
+                       dst[dstlen-1] = 0;
+       }
+
+       return srclen;
 }
 
 size_t cdecl__emit(struct output_state *dst, const char *src)