$OpenBSD: patch-dvdread_dvd_input_c,v 1.3 2005/05/01 09:31:20 hshoexer Exp $
--- dvdread/dvd_input.c.orig	Thu Feb 13 22:48:24 2003
+++ dvdread/dvd_input.c	Fri Mar 25 12:20:54 2005
@@ -36,30 +36,30 @@ int         (*dvdinput_read)  (dvd_input
 char *      (*dvdinput_error) (dvd_input_t);
 
 #ifdef HAVE_DVDCSS_DVDCSS_H
-/* linking to libdvdcss */
-#include <dvdcss/dvdcss.h>
-#define DVDcss_open(a) dvdcss_open((char*)(a))
-#define DVDcss_close   dvdcss_close
-#define DVDcss_seek    dvdcss_seek
-#define DVDcss_title   dvdcss_title
-#define DVDcss_read    dvdcss_read
-#define DVDcss_error   dvdcss_error
+/* linking to dvdcss */
+#include <libdvd/libdvd.h>
+#define DVD_open(a) libdvd_open((char*)(a))
+#define DVD_close   libdvd_close
+#define DVD_seek    libdvd_seek
+#define DVD_title   libdvd_title
+#define DVD_read    libdvd_read
+#define DVD_error   libdvd_error
 #else
-/* dlopening libdvdcss */
+/* dlopening dvdcss */
 #include <dlfcn.h>
-typedef struct dvdcss_s *dvdcss_handle;
-static dvdcss_handle (*DVDcss_open)  (const char *);
-static int           (*DVDcss_close) (dvdcss_handle);
-static int           (*DVDcss_seek)  (dvdcss_handle, int, int);
-static int           (*DVDcss_title) (dvdcss_handle, int); 
-static int           (*DVDcss_read)  (dvdcss_handle, void *, int, int);
-static char *        (*DVDcss_error) (dvdcss_handle);
+typedef struct libdvd_s *libdvd_handle;
+static libdvd_handle (*DVD_open)  (const char *);
+static int           (*DVD_close) (libdvs_handle);
+static int           (*DVD_seek)  (libdvd_handle, int, int);
+static int           (*DVD_title) (libdvd_handle, int); 
+static int           (*DVD_read)  (libdvd_handle, void *, int, int);
+static char *        (*DVD_error) (libdvd_handle);
 #endif
 
 /* The DVDinput handle, add stuff here for new input methods. */
 struct dvd_input_s {
-  /* libdvdcss handle */
-  dvdcss_handle dvdcss;
+  /* dvdcss handle */
+  libdvd_handle libdvd;
   
   /* dummy file input */
   int fd;
@@ -69,7 +69,7 @@ struct dvd_input_s {
 /**
  * initialize and open a DVD device or file.
  */
-static dvd_input_t css_open(const char *target)
+static dvd_input_t libdvd_open(const char *target)
 {
   dvd_input_t dev;
     
@@ -80,10 +80,10 @@ static dvd_input_t css_open(const char *
     return NULL;
   }
   
-  /* Really open it with libdvdcss */
-  dev->dvdcss = DVDcss_open(target);
-  if(dev->dvdcss == 0) {
-    fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target);
+  /* Really open it with dvdcss */
+  dev->libdvd = DVD_open(target);
+  if(dev->libdvd == 0) {
+    fprintf(stderr, "libdvdread: Could not open %s with libdvd.\n", target);
     free(dev);
     return NULL;
   }
@@ -94,44 +94,44 @@ static dvd_input_t css_open(const char *
 /**
  * return the last error message
  */
-static char *css_error(dvd_input_t dev)
+static char *libdvd_error(dvd_input_t dev)
 {
-  return DVDcss_error(dev->dvdcss);
+  return DVD_error(dev->libdvd);
 }
 
 /**
  * seek into the device.
  */
-static int css_seek(dvd_input_t dev, int blocks)
+static int libdvd_seek(dvd_input_t dev, int blocks)
 {
   /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */
-  return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS);
+  return DVD_seek(dev->libdvd, blocks, DVDINPUT_NOFLAGS);
 }
 
 /**
  * set the block for the begining of a new title (key).
  */
-static int css_title(dvd_input_t dev, int block)
+static int libdvd_title(dvd_input_t dev, int block)
 {
-  return DVDcss_title(dev->dvdcss, block);
+  return DVD_title(dev->libdvd, block);
 }
 
 /**
  * read data from the device.
  */
-static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags)
+static int libdvd_read(dvd_input_t dev, void *buffer, int blocks, int flags)
 {
-  return DVDcss_read(dev->dvdcss, buffer, blocks, flags);
+  return DVD_read(dev->libdvd, buffer, blocks, flags);
 }
 
 /**
  * close the DVD device and clean up the library.
  */
-static int css_close(dvd_input_t dev)
+static int libdvd_close(dvd_input_t dev)
 {
   int ret;
 
-  ret = DVDcss_close(dev->dvdcss);
+  ret = DVD_close(dev->libdvd);
 
   if(ret < 0)
     return ret;
@@ -257,85 +257,115 @@ static int file_close(dvd_input_t dev)
   return 0;
 }
 
+#ifdef __OpenBSD__
+#include <dirent.h>
+#include <string.h>
+static void *findlibrary(char *base)
+{
+	DIR *d;
+	struct dirent *de;
+	size_t len;
+	void *lib = NULL;
 
+	len = strlen(base);
+
+	d = opendir(".");
+	if (!d)
+		return lib;
+	while ((de = readdir(d)) != NULL) {
+	    if (strncmp(de->d_name, base, len) != 0)
+		continue;
+	    lib = dlopen(de->d_name, RTLD_LAZY);
+	    if (lib)
+	    	break;
+	}
+	closedir(d);
+	return lib;
+}
+#endif
+
 /**
- * Setup read functions with either libdvdcss or minimal DVD access.
+ * Setup read functions with either libdvd or minimal DVD access.
  */
 int dvdinput_setup(void)
 {
-  void *dvdcss_library = NULL;
-  char **dvdcss_version = NULL;
+  void *libdvd_library = NULL;
+  char **libdvd_version = NULL;
 
 #ifdef HAVE_DVDCSS_DVDCSS_H
-  /* linking to libdvdcss */
-  dvdcss_library = &dvdcss_library;  /* Give it some value != NULL */
-  /* the DVDcss_* functions have been #defined at the top */
-  dvdcss_version = &dvdcss_interface_2;
+  /* linking to dvdcss */
+  libdvd_library = &libdvd_library;  /* Give it some value != NULL */
+  /* the DVD_* functions have been #defined at the top */
+  libdvd_version = &libdvd_interface_2;
 
 #else
-  /* dlopening libdvdcss */
-  dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY);
+  /* dlopening dvdcss */
+  libdvd_library = dlopen("libdvd.so.0", RTLD_LAZY);
+#if defined(__OpenBSD__)
+  if (!libdvd_library)
+  	libdvd_library = findlibrary("libdvd.so.0.");
+#endif
   
-  if(dvdcss_library != NULL) {
+  if(libdvd_library != NULL) {
 #if defined(__OpenBSD__) && !defined(__ELF__)
 #define U_S "_"
 #else
 #define U_S
 #endif
-    DVDcss_open = (dvdcss_handle (*)(const char*))
-      dlsym(dvdcss_library, U_S "dvdcss_open");
-    DVDcss_close = (int (*)(dvdcss_handle))
-      dlsym(dvdcss_library, U_S "dvdcss_close");
-    DVDcss_title = (int (*)(dvdcss_handle, int))
-      dlsym(dvdcss_library, U_S "dvdcss_title");
-    DVDcss_seek = (int (*)(dvdcss_handle, int, int))
-      dlsym(dvdcss_library, U_S "dvdcss_seek");
-    DVDcss_read = (int (*)(dvdcss_handle, void*, int, int))
-      dlsym(dvdcss_library, U_S "dvdcss_read");
-    DVDcss_error = (char* (*)(dvdcss_handle))
-      dlsym(dvdcss_library, U_S "dvdcss_error");
+    DVD_open = (libdvd_handle (*)(const char*))
+      dlsym(libdvd_library, U_S "libdvd_open");
+    DVD_close = (int (*)(libdvd_handle))
+      dlsym(libdvd_library, U_S "libdvd_close");
+    DVD_title = (int (*)(libdvd_handle, int))
+      dlsym(libdvd_library, U_S "libdvd_title");
+    DVD_seek = (int (*)(libdvd_handle, int, int))
+      dlsym(libdvd_library, U_S "libdvd_seek");
+    DVD_read = (int (*)(libdvd_handle, void*, int, int))
+      dlsym(libdvd_library, U_S "libdvd_read");
+    DVD_error = (char* (*)(libdvd_handle))
+      dlsym(libdvd_library, U_S "libdvd_error");
     
-    dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2");
+    libdvd_version = (char **)dlsym(libdvd_library, U_S "libdvd_interface_2");
 
-    if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
+    if(dlsym(libdvd_library, U_S "libdvd_crack")) {
       fprintf(stderr, 
-	      "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n"
+	      "libdvdread: Old (pre-0.0.2) version of libdvd found.\n"
 	      "libdvdread: You should get the latest version from "
 	      "http://www.videolan.org/\n" );
-      dlclose(dvdcss_library);
-      dvdcss_library = NULL;
-    } else if(!DVDcss_open  || !DVDcss_close || !DVDcss_title || !DVDcss_seek
-	      || !DVDcss_read || !DVDcss_error || !dvdcss_version) {
-      fprintf(stderr,  "libdvdread: Missing symbols in libdvdcss.so.2, "
+      dlclose(libdvd_library);
+      libdvd_library = NULL;
+    } else if(!DVD_open  || !DVD_close || !DVD_title || !DVD_seek
+	      || !DVD_read || !DVD_error || !libdvd_version) {
+      fprintf(stderr,  "libdvdread: Missing symbols in libdvd.so.0, "
 	      "this shouldn't happen !\n");
-      dlclose(dvdcss_library);
+      dlclose(libdvd_library);
     }
   }
 #endif /* HAVE_DVDCSS_DVDCSS_H */
   
-  if(dvdcss_library != NULL) {
+  if(libdvd_library != NULL) {
     /*
     char *psz_method = getenv( "DVDCSS_METHOD" );
     char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
     fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method);
     fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose);
     */
-    fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n",
-	    *dvdcss_version);
+    fprintf(stderr, "libdvdread: Using libdvd version %s for DVD access\n",
+	    *libdvd_version);
     
-    /* libdvdcss wrapper functions */
-    dvdinput_open  = css_open;
-    dvdinput_close = css_close;
-    dvdinput_seek  = css_seek;
-    dvdinput_title = css_title;
-    dvdinput_read  = css_read;
-    dvdinput_error = css_error;
+    /* dvdcss wrapper functions */
+    dvdinput_open  = libdvd_open;
+    dvdinput_close = libdvd_close;
+    dvdinput_seek  = libdvd_seek;
+    dvdinput_title = libdvd_title;
+    dvdinput_read  = libdvd_read;
+    dvdinput_error = libdvd_error;
     return 1;
     
   } else {
     fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n");
 
-    /* libdvdcss replacement functions */
+    /* dvdcss replacement functions */
     dvdinput_open  = file_open;
     dvdinput_close = file_close;
     dvdinput_seek  = file_seek;
