]> granicus.if.org Git - php/commitdiff
Add PHP_STRLCPY macro. This macro should be used in new code instead of
authorSascha Schumann <sas@php.net>
Sun, 27 Aug 2000 09:30:15 +0000 (09:30 +0000)
committerSascha Schumann <sas@php.net>
Sun, 27 Aug 2000 09:30:15 +0000 (09:30 +0000)
strlcpy/strlcat which are intended for fixing broken code.

main/php.h

index 8b6b9dd11bbc6acfed926bea59a6e1adc3fe7df5..727e789f765b876cc59366006be68763c0053a1c 100644 (file)
@@ -97,6 +97,27 @@ extern unsigned char second_arg_allow_ref[];
 #include <alloca.h>
 #endif
 
+/*
+ * This is a fast version of strlcpy which should be used, if you
+ * know the maximum size of the destination buffer and if you know
+ * the length of the source string.
+ *
+ * size is the allocated number of bytes
+ * src_size is the number of bytes excluding the NUL
+ */
+
+#define PHP_STRLCPY(dst, src, size, src_size)  \
+       {                                                                                       \
+               size_t php_str_len;                                             \
+                                                                                               \
+               if (src_size >= size)                                   \
+                       php_str_len = size - 1;                         \
+               else                                                                    \
+                       php_str_len = src_size;                         \
+               memcpy(dst, src, php_str_len);                  \
+               dst[php_str_len] = '\0';                                \
+       }
+
 #ifndef HAVE_STRLCPY
 PHPAPI size_t strlcpy(char *dst, const char *src, size_t siz);
 #endif