[sane-devel] [patch] sane-find-scanner

Frank Zago fzago at greshamstorage.com
Sun Apr 28 08:39:00 BST 2002


This patch fixes a bug where the return of sanei_scsi_cmd() wasn't checked.
It also displays the inquiry buffer if -v is enabled:

$ ./sane-find-scanner -v
...
sane-find-scanner: checking /dev/scanner... failed to open
sane-find-scanner: checking /dev/sg0... open ok
  Inquiry for device:
    000: 06 31 14 01 1f 00 00 00 41 43 52 4f 53 53 20 20    .1......ACROSS
    016: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
    032: 31 2e 31 36                                        1.16
sane-find-scanner: found SCSI scanner "ACROSS  1.16" at device /dev/sg0
sane-find-scanner: checking /dev/sg1... failed to open
sane-find-scanner: checking /dev/sg2... failed to open
...

That might be usefull for debug purposes. Should I add to cvs?

Frank.
-------------- next part --------------
Index: sane-find-scanner.c
===================================================================
RCS file: /cvsroot/external/sane/sane-backends/tools/sane-find-scanner.c,v
retrieving revision 1.8
diff -u -B -b -r1.8 sane-find-scanner.c
--- sane-find-scanner.c	2001/12/25 16:07:35	1.8
+++ sane-find-scanner.c	2002/04/28 07:49:07
@@ -107,23 +107,87 @@
   return 1; /* Give up, and assume yes to avoid false negatives */
 }
 
+/* Display a buffer in the log. Display by lines of 16 bytes. */
 static void 
+hexdump (const char *comment, unsigned char *buf, const int length)
+{
+  int i;
+  char line[128];
+  char *ptr;
+  char asc_buf[17];
+  char *asc_ptr;
+
+  printf ("  %s\n", comment);
+
+  i = 0;
+  goto start;
+
+  do
+    {
+      if (i < length)
+	{
+	  ptr += sprintf (ptr, " %2.2x", *buf);
+
+	  if (*buf >= 32 && *buf <= 127)
+	    {
+	      asc_ptr += sprintf (asc_ptr, "%c", *buf);
+	    }
+	  else
+	    {
+	      asc_ptr += sprintf (asc_ptr, ".");
+	    }
+	}
+      else
+	{
+	  /* After the length; do nothing. */
+	  ptr += sprintf (ptr, "   ");
+	}
+
+      i++;
+      buf++;
+
+      if ((i % 16) == 0)
+	{
+	  /* It's a new line */
+	  printf ("  %s    %s\n", line, asc_buf);
+
+	start:
+	  ptr = line;
+	  *ptr = '\0';
+	  asc_ptr = asc_buf;
+	  *asc_ptr = '\0';
+
+	  ptr += sprintf (ptr, "  %3.3d:", i);
+	}
+
+    }
+  while (i < ((length + 15) & ~15));
+}
+
+static SANE_Status
 scanner_do_inquiry (unsigned char *buffer, int sfd)
 {
   size_t size;
+  SANE_Status status;
 
   DBG (5, "do_inquiry\n");
   memset (buffer, '\0', 256);	/* clear buffer */
 
   size = 5; /* first get only 5 bytes to get size of inquiry_return_block */
   set_inquiry_return_size (inquiry.cmd, size);
-  sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+  status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
 
+  if (status != SANE_STATUS_GOOD) {
+	  return(status);
+  }
+
   size = get_inquiry_additional_length (buffer) + 5;
 
   /* then get inquiry with actual size */
   set_inquiry_return_size (inquiry.cmd, size);
-  sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+  status = sanei_scsi_cmd (sfd, inquiry.cmd, inquiry.size, buffer, &size);
+
+  return(status);
 }
 
 static void 
@@ -140,8 +204,17 @@
       "CD-drive", "scanner", "optical-drive", "jukebox",
       "communicator"
     };
+
+  if (scanner_do_inquiry (buffer, sfd) != SANE_STATUS_GOOD)
+    {
+      return;
+    }
+
+  if (verbose) {
+	  hexdump ("Inquiry for device:", buffer,
+			   get_inquiry_additional_length (buffer) + 5);
+  }
 
-  scanner_do_inquiry (buffer, sfd);	/* get inquiry */
   devtype = get_inquiry_periph_devtype (buffer);
   if (!verbose
       && devtype != IN_periph_devtype_scanner


More information about the sane-devel mailing list