DLL backend cleanup patch

Petter Reinholdtsen (pere@hungry.com)
Fri, 9 Apr 1999 23:06:41 +0200

Here is a small patch with some cleanup in the dll backend. No
bugfixes, but made the code smaller. It also is structured like my
earlier Win32 patch, so it should be easier to add the Win32 part of
the patch later.

It is relative to v1.01 pre4.

Index: ChangeLog
===================================================================
RCS file: /cvsroot/external/sane/ChangeLog,v
retrieving revision 1.3
diff -u -r1.3 ChangeLog
--- ChangeLog 1999/04/07 21:04:34 1.3
+++ ChangeLog 1999/04/09 21:02:19
@@ -1,3 +1,9 @@
+1999-04-09 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.
+
1999-04-03 David Mosberger-Tang <David.Mosberger@acm.org>

* include/sane/sanei_debug.h: Define sanei_debug_BACKEND_NAME only
Index: backend/dll.c
===================================================================
RCS file: /cvsroot/external/sane/backend/dll.c,v
retrieving revision 1.1.1.3
diff -u -r1.1.1.3 dll.c
--- dll.c 1999/01/13 11:07:04 1.1.1.3
+++ dll.c 1999/04/09 21:02:19
@@ -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,9 +286,9 @@
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 */
@@ -307,44 +299,35 @@
}

/* 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 +424,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 +591,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
 O-  <SCRIPT Language="Javascript">window.close()</SCRIPT>
http://www.hungry.com/~pere/    | 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