[FFmpeg-devel] [RFC] av_strlcpy instead of pstrcpy

Reimar Döffinger Reimar.Doeffinger
Sat Jun 23 17:10:26 CEST 2007


Hello,
attached incomplete patch (no uses changed) would replace pstrcpy and
pstrcat by av_strlcpy and av_strlcat which behave like the (BSD-only)
strlcpy and strlcat functions (documentation here:
http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3/strlcpy.3.html).
While I don't think we will ever really need the "return value vodoo" ;-) of these
functions it seems preferable to not have yet another different
implementation (of course that is only true if you check that my
implementations really are right).
It might also allow to use the real system functions if available like
MPlayer does (but of course in a less ugly-hackish way).

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/cutils.c
===================================================================
--- libavformat/cutils.c	(revision 9399)
+++ libavformat/cutils.c	(working copy)
@@ -63,24 +63,30 @@
  * @param buf destination buffer
  * @param buf_size size of destination buffer
  * @param str source string
+ * @return length of the string without clamping
  */
-void pstrcpy(char *buf, int buf_size, const char *str)
+size_t av_strlcpy(char *buf, const char *str, size_t buf_size)
 {
+    size_t i = 0;
     if (buf_size <= 0)
-        return;
+        goto out;
 
-    while (buf_size-- > 1 && *str)
-        *buf++ = *str++;
-    *buf = 0;
+    while (buf_size-- > 1 && *str) {
+        buf[i] = str[i];
+    }
+    buf[i] = 0;
+out:
+    while (str[i]) i++;
+    return i;
 }
 
 /* strcat and truncate. */
-char *pstrcat(char *buf, int buf_size, const char *s)
+size_t av_strlcat(char *buf, const char *s, size_t buf_size)
 {
-    int len = strlen(buf);
-    if (len < buf_size)
-        pstrcpy(buf + len, buf_size - len, s);
-    return buf;
+    size_t i = 0;
+    while (buf_size > 0 && buf[i]) { buf_size--; i++ };
+    i += strlcpy(buf + i, s, buf_size);
+    return i;
 }
 
 /* add one element to a dynamic array */



More information about the ffmpeg-devel mailing list