Patch: dll cleanup and bugfix

Petter Reinholdtsen (pere@hungry.com)
Wed, 28 Apr 1999 01:06:44 +0200

This is a new version of my earlier DLL cleanup patch.

diff -ur sane-1.0.1/ChangeLog sane-1.0.1-pere/ChangeLog
--- sane-1.0.1/ChangeLog Mon Apr 19 18:21:33 1999
+++ sane-1.0.1-pere/ChangeLog Wed Apr 28 00:52:19 1999
@@ -1,3 +1,11 @@
+1999-04-28 Petter Reinholdtsen <pere@td.org.uit.no>
+
+ * backend/dll.c: Cleanup. Use calloc() instead of
+ malloc/memset(0). Only check the correct environment path
+ variables. Make ifdef'ed code smaller and clearer. Include
+ bugfix reported by Ingo Wilken: use dlerror() instead of
+ strerror(errno).
+
1999-04-19 David Mosberger-Tang <David.Mosberger@acm.org>

* Version 1.0.1 released.
diff -ur sane-1.0.1/backend/dll.c sane-1.0.1-pere/backend/dll.c
--- sane-1.0.1/backend/dll.c Sun Feb 28 00:51:37 1999
+++ sane-1.0.1-pere/backend/dll.c Wed Apr 28 01:01:58 1999
@@ -68,12 +68,22 @@
# ifndef RTLD_LAZY
# define RTLD_LAZY 1
# endif
+# if defined(_AIX)
+# define DLL_PATH_ENV "LIBPATH"
+# else
+# define DLL_PATH_ENV "LD_LIBRARY_PATH"
+# endif
+# define DLL_PATH_SEPARATOR ":"
+# define DLL_SLASH "/"
+# define DLL_NAME "libsane-%s.so." STRINGIFY(V_MAJOR)
# define HAVE_DLL
-#endif
-
+#elif defined (HAVE_SHL_LOAD) && defined(HAVE_DL_H)
/* HP/UX DLL support */
-#if defined (HAVE_SHL_LOAD) && defined(HAVE_DL_H)
# include <dl.h>
+# define DLL_PATH_ENV "SHLIB_PATH"
+# define DLL_PATH_SEPARATOR ":"
+# define DLL_SLASH "/"
+# define DLL_NAME "libsane-%s.sl." STRINGIFY(V_MAJOR)
# define HAVE_DLL
#endif

@@ -211,11 +221,10 @@
return SANE_STATUS_GOOD;
}

- be = malloc (sizeof (*be));
+ be = calloc (1, sizeof (*be));
if (!be)
return SANE_STATUS_NO_MEM;

- memset (be, 0, sizeof (*be));
be->name = strdup (name);
if (!be->name)
return SANE_STATUS_NO_MEM;
@@ -230,24 +239,12 @@
load (struct backend *be)
{
#ifdef HAVE_DLL
- int mode = 0;
+ int funcnamesize = 0;
char *funcname, *src, *dir, *path = 0;
char libname[PATH_MAX];
int i;
FILE *fp = 0;

-#if defined(HAVE_DLOPEN)
-# define PREFIX "libsane-"
-# define POSTFIX ".so.%u"
- mode = getenv ("LD_BIND_NOW") ? RTLD_NOW : RTLD_LAZY;
-#elif defined(HAVE_SHL_LOAD)
-# define PREFIX "libsane-"
-# define POSTFIX ".sl.%u"
- mode = BIND_DEFERRED;
-#else
-# error "Tried to compile unsupported DLL."
-#endif /* HAVE_DLOPEN */
-
DBG(1, "loading backend %s\n", be->name);

/* initialize all ops to "unsupported" so we can "use" the backend
@@ -260,28 +257,23 @@
dir = STRINGIFY(LIBDIR);
while (dir)
{
- snprintf (libname, sizeof (libname), "%s/"PREFIX"%s"POSTFIX,
- dir, be->name, V_MAJOR);
+ /* Make string like "<libdir>/sane-<backend>.so.<version>" */
+ snprintf (libname, sizeof (libname), "%s" DLL_SLASH DLL_NAME,
+ dir, be->name);
fp = fopen (libname, "r");
if (fp)
break;

if (!path)
{
- path = getenv ("LD_LIBRARY_PATH");
- if (!path)
- {
- path = getenv ("SHLIB_PATH"); /* for HP-UX */
- if (!path)
- path = getenv ("LIBPATH"); /* for AIX */
- }
+ path = getenv (DLL_PATH_ENV);
if (!path)
break;

path = strdup (path);
src = path;
}
- dir = strsep (&src, ":");
+ dir = strsep (&src, DLL_PATH_SEPARATOR);
}
if (path)
free (path);
@@ -294,57 +286,52 @@
DBG(2, "dlopen()ing `%s'\n", libname);

#ifdef HAVE_DLOPEN
- be->handle = dlopen (libname, mode);
+ be->handle = dlopen (libname, getenv ("LD_BIND_NOW") ? RTLD_NOW : RTLD_LAZY);
#elif defined(HAVE_SHL_LOAD)
- be->handle = (shl_t)shl_load (libname, mode, 0L);
+ be->handle = (shl_t)shl_load (libname, BIND_DEFERRED, 0L);
#else
# error "Tried to compile unsupported DLL."
#endif /* HAVE_DLOPEN */
if (!be->handle)
{
+#ifdef HAVE_DLOPEN
+ DBG(2, "dlopen() failed (%s)\n", dlerror());
+#else
DBG(2, "dlopen() failed (%s)\n", strerror (errno));
+#endif
return SANE_STATUS_INVAL;
}

/* all is dandy---lookup and fill in backend ops: */
- funcname = alloca (strlen (be->name) + 64);
+ funcnamesize = strlen (be->name) + 64;
+ funcname = alloca (funcnamesize);
for (i = 0; i < NUM_OPS; ++i)
{
void *(*op) ();
+ void *(*op_) ();

- sprintf (funcname, "_sane_%s_%s", be->name, op_name[i]);
+ snprintf (funcname, funcnamesize, "_sane_%s_%s", be->name, op_name[i]);

/* First try looking up the symbol without a leading underscore. */
+ /* Then try again, with an underscore prepended. */
#ifdef HAVE_DLOPEN
op = (void *(*)()) dlsym (be->handle, funcname + 1);
+ op_ = (void *(*)()) dlsym (be->handle, funcname);
#elif defined(HAVE_SHL_LOAD)
shl_findsym ((shl_t*)&(be->handle), funcname + 1, TYPE_UNDEFINED, &op);
+ shl_findsym (be->handle, funcname, TYPE_UNDEFINED, &op_);
#else
# error "Tried to compile unsupported DLL."
#endif /* HAVE_DLOPEN */
- if (op)
- be->op[i] = op;
+
+ if (NULL != op || NULL != op_)
+ be->op[i] = (NULL != op) ? op : op_;
else
- {
- /* Try again, with an underscore prepended. */
-#ifdef HAVE_DLOPEN
- op = (void *(*)()) dlsym (be->handle, funcname);
-#elif defined(HAVE_SHL_LOAD)
- shl_findsym (be->handle, funcname, TYPE_UNDEFINED, &op);
-#else
-# error "Tried to compile unsupported DLL."
-#endif /* HAVE_DLOPEN */
- if (op)
- be->op[i] = op;
- }
- if (NULL == op)
DBG(2, "unable to find %s\n", funcname);
}

return SANE_STATUS_GOOD;

-# undef PREFIX
-# undef POSTFIX
#else /* HAVE_DLL */
DBG(1, "load: ignoring attempt to load `%s'; compiled without dl support\n",
be->name);
@@ -441,15 +428,16 @@
(*be->op[OP_EXIT]) ();
#ifdef HAVE_DLL

-#ifdef HAVE_DLOPEN
if (be->handle)
- dlclose (be->handle);
+ {
+#ifdef HAVE_DLOPEN
+ dlclose (be->handle);
#elif defined(HAVE_SHL_LOAD)
- if (be->handle)
- shl_unload(be->handle);
+ shl_unload(be->handle);
#else
# error "Tried to compile unsupported DLL."
#endif /* HAVE_DLOPEN */
+ }

#endif /* HAVE_DLL */
}
@@ -607,11 +595,10 @@
if (status != SANE_STATUS_GOOD)
return status;

- s = malloc (sizeof (*s));
+ s = calloc (1, sizeof (*s));
if (!s)
return SANE_STATUS_NO_MEM;

- memset (s, 0, sizeof (*s));
s->be = be;
s->handle = handle;
*meta_handle = s;

-- 
##>  Petter Reinholdtsen <##    | pere@td.org.uit.no
http://www.hungry.com/~pere/ O- | Go Mozilla, go! Go!

--
Source code, list archive, and docs: http://www.mostang.com/sane/
To unsubscribe: echo unsubscribe sane-devel | mail majordomo@mostang.com