]> git.sesse.net Git - vlc/blobdiff - src/extras/libc.c
OpenBSDish strlcpy()
[vlc] / src / extras / libc.c
index 6d48b12cad13b9152b2c48dee6c888e49fde1aba..90604f56898d7bd4036d8218e3eb888cc511b537 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * libc.c: Extra libc function for some systems.
  *****************************************************************************
- * Copyright (C) 2002 the VideoLAN team
+ * Copyright (C) 2002-2006 the VideoLAN team
  * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
@@ -9,6 +9,7 @@
  *          Gildas Bazin <gbazin@videolan.org>
  *          Derk-Jan Hartman <hartman at videolan dot org>
  *          Christophe Massiot <massiot@via.ecp.fr>
+ *          Rémi Denis-Courmont <rem à videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -356,6 +357,36 @@ lldiv_t vlc_lldiv( long long numer, long long denom )
 }
 #endif
 
+
+/**
+ * Copy a string to a sized buffer. The result is always nul-terminated
+ * (contrary to strncpy()).
+ *
+ * @param dest destination buffer
+ * @param src string to be copied
+ * @param len maximum number of characters to be copied plus one for the
+ * terminating nul.
+ *
+ * @return strlen(src)
+ */
+#ifndef HAVE_STRLCPY
+extern size_t vlc_strlcpy (char *tgt, const char *src, size_t bufsize)
+{
+    size_t length;
+
+    for (length = 1; (length < bufsize) && *src; length++)
+        *tgt++ = *src++;
+
+    if (bufsize)
+        *tgt = '\0';
+
+    while (*src++)
+        length++;
+
+    return length - 1;
+}
+#endif
+
 /*****************************************************************************
  * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
  * when called with an empty argument or just '\'