summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJeff Layton <jlayton@redhat.com>2010-02-07 16:23:23 -0500
committerJeff Layton <jlayton@redhat.com>2010-02-07 16:23:23 -0500
commit8e14979db50d380f7c614154d81365649b392657 (patch)
treed49b52b5a4f9d1d8426c2e891a37b8a991993f04 /util.c
parent502ba1666b2cf73c479bb041fcd5ad38236b322c (diff)
downloadcifs-utils-8e14979db50d380f7c614154d81365649b392657.tar.gz
cifs-utils-8e14979db50d380f7c614154d81365649b392657.tar.bz2
cifs-utils-8e14979db50d380f7c614154d81365649b392657.zip
util.c: move strlcat and strlcpy into a separate "util.c" object
Signed-off-by: Jeff Layton <jlayton@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..00e0cc5
--- /dev/null
+++ b/util.c
@@ -0,0 +1,47 @@
+#include <sys/types.h>
+#include <string.h>
+
+/* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
+ * don't link to libreplace so need them here. */
+
+/* like strncpy but does not 0 fill the buffer and always null
+ * terminates. bufsize is the size of the destination buffer */
+
+#ifndef HAVE_STRLCPY
+size_t strlcpy(char *d, const char *s, size_t bufsize)
+{
+ size_t len = strlen(s);
+ size_t ret = len;
+ if (bufsize <= 0) return 0;
+ if (len >= bufsize) len = bufsize-1;
+ memcpy(d, s, len);
+ d[len] = 0;
+ return ret;
+}
+#endif
+
+/* like strncat but does not 0 fill the buffer and always null
+ * terminates. bufsize is the length of the buffer, which should
+ * be one more than the maximum resulting string length */
+
+#ifndef HAVE_STRLCAT
+size_t strlcat(char *d, const char *s, size_t bufsize)
+{
+ size_t len1 = strlen(d);
+ size_t len2 = strlen(s);
+ size_t ret = len1 + len2;
+
+ if (len1+len2 >= bufsize) {
+ if (bufsize < (len1+1)) {
+ return ret;
+ }
+ len2 = bufsize - (len1+1);
+ }
+ if (len2 > 0) {
+ memcpy(d+len1, s, len2);
+ d[len1+len2] = 0;
+ }
+ return ret;
+}
+#endif
+