[libbluray-devel] Replace non-C99 strdup() with own implementation

hpi1 git at videolan.org
Wed Aug 29 14:02:42 CEST 2012


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Fri Aug 17 22:13:55 2012 +0300| [374039b47521fe7671f2b14359811a84a221a64e] | committer: hpi1

Replace non-C99 strdup() with own implementation

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=374039b47521fe7671f2b14359811a84a221a64e
---

 src/examples/mpls_dump.c         |    2 +-
 src/libbluray/bdnav/navigation.c |    4 ++--
 src/libbluray/bluray.c           |    6 +++---
 src/util/strutl.c                |    6 ++++++
 src/util/strutl.h                |    1 +
 5 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/examples/mpls_dump.c b/src/examples/mpls_dump.c
index 6d0ed51..9b0a873 100644
--- a/src/examples/mpls_dump.c
+++ b/src/examples/mpls_dump.c
@@ -644,7 +644,7 @@ main(int argc, char *argv[])
             int jj = 0;
             for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
                 if (ent->d_name != NULL) {
-                    dirlist[jj++] = strdup(ent->d_name);
+                    dirlist[jj++] = str_dup(ent->d_name);
                 }
             }
             qsort(dirlist, jj, sizeof(char*), _qsort_str_cmp);
diff --git a/src/libbluray/bdnav/navigation.c b/src/libbluray/bdnav/navigation.c
index 46e47a7..f6ca0d8 100644
--- a/src/libbluray/bdnav/navigation.c
+++ b/src/libbluray/bdnav/navigation.c
@@ -277,7 +277,7 @@ char* nav_find_main_title(const char *root)
         mpls_free(pl_list[ii]);
     }
     if (count > 0) {
-        return strdup(longest);
+        return str_dup(longest);
     } else {
         return NULL;
     }
@@ -464,7 +464,7 @@ NAV_TITLE* nav_title_open(const char *root, const char *playlist, unsigned angle
     if (title == NULL) {
         return NULL;
     }
-    title->root = strdup(root);
+    title->root = str_dup(root);
     strncpy(title->name, playlist, 11);
     title->name[10] = '\0';
     path = str_printf("%s" DIR_SEP "BDMV" DIR_SEP "PLAYLIST" DIR_SEP "%s",
diff --git a/src/libbluray/bluray.c b/src/libbluray/bluray.c
index bcd3bc2..7e66f58 100644
--- a/src/libbluray/bluray.c
+++ b/src/libbluray/bluray.c
@@ -1003,7 +1003,7 @@ static void get_mount_point(BLURAY *bd)
 #endif
         if (!strcmp (m->mnt_fsname, bd->device_path)) {
             free(bd->device_path);
-            bd->device_path = strdup (m->mnt_dir);
+            bd->device_path = str_dup (m->mnt_dir);
             break;
         }
     }
@@ -1033,7 +1033,7 @@ static void get_mount_point(BLURAY *bd)
     for ( int i = 0; i < fs_count; ++i) {
         if (!strcmp (mbuf[i].f_mntfromname, bd->device_path)) {
             free(bd->device_path);
-            bd->device_path = strdup (mbuf[i].f_mntonname);
+            bd->device_path = str_dup (mbuf[i].f_mntonname);
         }
     }
 }
@@ -1059,7 +1059,7 @@ BLURAY *bd_open(const char* device_path, const char* keyfile_path)
         return NULL;
     }
 
-    bd->device_path = strdup(device_path);
+    bd->device_path = str_dup(device_path);
 
 #if (defined HAVE_MNTENT_H || defined __APPLE__)
     get_mount_point(bd);
diff --git a/src/util/strutl.c b/src/util/strutl.c
index 8330280..9831ebc 100644
--- a/src/util/strutl.c
+++ b/src/util/strutl.c
@@ -23,6 +23,12 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <string.h>
+
+char * str_dup(const char *str)
+{
+  return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
+}
 
 char * str_printf(const char *fmt, ...)
 {
diff --git a/src/util/strutl.h b/src/util/strutl.h
index 4e87b4f..1e59cd6 100644
--- a/src/util/strutl.h
+++ b/src/util/strutl.h
@@ -24,6 +24,7 @@
 
 #include <stdint.h>
 
+BD_PRIVATE char * str_dup(const char *str) BD_ATTR_MALLOC;
 BD_PRIVATE char * str_printf(const char *fmt, ...) BD_ATTR_FORMAT_PRINTF(1,2) BD_ATTR_MALLOC;
 
 BD_PRIVATE uint32_t str_to_uint32(const char *s, int n);



More information about the libbluray-devel mailing list