Permalink
Browse files

util: add uv_strlcpy() and uv_strlcat() functions

  • Loading branch information...
1 parent 0834e73 commit b52b8c7128ba7c99ae7774f9815384aabaa07bed @bnoordhuis bnoordhuis committed Nov 23, 2011
Showing with 155 additions and 22 deletions.
  1. +15 −0 include/uv.h
  2. +0 −18 src/unix/core.c
  3. +0 −2 src/unix/internal.h
  4. +2 −2 src/unix/pipe.c
  5. +35 −0 src/uv-common.c
  6. +5 −0 test/test-list.h
  7. +97 −0 test/test-util.c
  8. +1 −0 uv.gyp
View
@@ -371,6 +371,21 @@ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
UV_EXTERN uv_buf_t uv_buf_init(char* base, size_t len);
+/*
+ * Utility function. Copies up to `size` characters from `src` to `dst`
+ * and ensures that `dst` is properly NUL terminated unless `size` is zero.
+ */
+UV_EXTERN size_t uv_strlcpy(char* dst, const char* src, size_t size);
+
+/*
+ * Utility function. Appends `src` to `dst` and ensures that `dst` is
+ * properly NUL terminated unless `size` is zero or `dst` does not
+ * contain a NUL byte. `size` is the total length of `dst` so at most
+ * `size - strlen(dst) - 1` characters will be copied from `src`.
+ */
+UV_EXTERN size_t uv_strlcat(char* dst, const char* src, size_t size);
+
+
#define UV_STREAM_FIELDS \
/* number of bytes queued for writing */ \
size_t write_queue_size; \
View
@@ -827,21 +827,3 @@ int uv__cloexec(int fd, int set) {
return 0;
#endif
}
-
-
-/* TODO move to uv-common.c? */
-size_t uv__strlcpy(char* dst, const char* src, size_t size) {
- const char *org;
-
- if (size == 0) {
- return 0;
- }
-
- org = src;
- while (--size && *src) {
- *dst++ = *src++;
- }
- *dst = '\0';
-
- return src - org;
-}
View
@@ -138,8 +138,6 @@ enum {
UV_TCP_KEEPALIVE = 0x100 /* Turn on keep-alive. */
};
-size_t uv__strlcpy(char* dst, const char* src, size_t size);
-
int uv__close(int fd);
void uv__req_init(uv_req_t*);
void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle, uv_handle_type type);
View
@@ -74,7 +74,7 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
}
memset(&saddr, 0, sizeof saddr);
- uv__strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
+ uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;
if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
@@ -197,7 +197,7 @@ void uv_pipe_connect(uv_connect_t* req,
}
memset(&saddr, 0, sizeof saddr);
- uv__strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
+ uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;
/* We don't check for EINPROGRESS. Think about it: the socket
View
@@ -41,6 +41,41 @@ uv_counters_t* uv_counters() {
}
+size_t uv_strlcpy(char* dst, const char* src, size_t size) {
+ size_t n;
+
+ if (size == 0)
+ return 0;
+
+ for (n = 0; n < (size - 1) && *src != '\0'; n++)
+ *dst++ = *src++;
+
+ *dst = '\0';
+
+ return n;
+}
+
+
+size_t uv_strlcat(char* dst, const char* src, size_t size) {
+ size_t n;
+
+ if (size == 0)
+ return 0;
+
+ for (n = 0; n < size && *dst != '\0'; n++, dst++);
+
+ if (n == size)
+ return n;
+
+ while (n < (size - 1) && *src != '\0')
+ n++, *dst++ = *src++;
+
+ *dst = '\0';
+
+ return n;
+}
+
+
uv_buf_t uv_buf_init(char* base, size_t len) {
uv_buf_t buf;
buf.base = base;
View
@@ -118,6 +118,9 @@ TEST_DECLARE (threadpool_queue_work_simple)
TEST_DECLARE (thread_mutex)
TEST_DECLARE (thread_rwlock)
TEST_DECLARE (thread_create)
+TEST_DECLARE (strlcpy)
+TEST_DECLARE (strlcat)
+
#ifdef _WIN32
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping)
@@ -273,6 +276,8 @@ TASK_LIST_START
TEST_ENTRY (thread_mutex)
TEST_ENTRY (thread_rwlock)
TEST_ENTRY (thread_create)
+ TEST_ENTRY (strlcpy)
+ TEST_ENTRY (strlcat)
#if 0
/* These are for testing the test runner. */
View
@@ -0,0 +1,97 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "task.h"
+
+#include <string.h>
+
+#define memeq(a, b, c) (memcmp((a), (b), (c)) == 0)
+
+
+TEST_IMPL(strlcpy) {
+ size_t r;
+
+ {
+ char dst[2] = "A";
+ r = uv_strlcpy(dst, "", 0);
+ ASSERT(r == 0);
+ ASSERT(memeq(dst, "A", 1));
+ }
+
+ {
+ char dst[2] = "A";
+ r = uv_strlcpy(dst, "B", 1);
+ ASSERT(r == 0);
+ ASSERT(memeq(dst, "", 1));
+ }
+
+ {
+ char dst[2] = "A";
+ r = uv_strlcpy(dst, "B", 2);
+ ASSERT(r == 1);
+ ASSERT(memeq(dst, "B", 2));
+ }
+
+ {
+ char dst[3] = "AB";
+ r = uv_strlcpy(dst, "CD", 3);
+ ASSERT(r == 2);
+ ASSERT(memeq(dst, "CD", 3));
+ }
+
+ return 0;
+}
+
+
+TEST_IMPL(strlcat) {
+ size_t r;
+
+ {
+ char dst[2] = "A";
+ r = uv_strlcat(dst, "B", 1);
+ ASSERT(r == 1);
+ ASSERT(memeq(dst, "A", 2));
+ }
+
+ {
+ char dst[2] = "A";
+ r = uv_strlcat(dst, "B", 2);
+ ASSERT(r == 1);
+ ASSERT(memeq(dst, "A", 2));
+ }
+
+ {
+ char dst[3] = "A";
+ r = uv_strlcat(dst, "B", 3);
+ ASSERT(r == 2);
+ ASSERT(memeq(dst, "AB", 3));
+ }
+
+ {
+ char dst[5] = "AB";
+ r = uv_strlcat(dst, "CD", 5);
+ ASSERT(r == 4);
+ ASSERT(memeq(dst, "ABCD", 5));
+ }
+
+ return 0;
+}
View
1 uv.gyp
@@ -278,6 +278,7 @@
'test/runner.h',
'test/test-get-loadavg.c',
'test/task.h',
+ 'test/test-util.c',
'test/test-async.c',
'test/test-error.c',
'test/test-callback-stack.c',

0 comments on commit b52b8c7

Please sign in to comment.