#include <iostream.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include "Scsi.h"

void
hexdump(const u_char *buf, int len)
{
   int lines = (len + 15) / 16;

   for (int line = 0; line < lines; line++)
   {
      cout.form("%08x ", line << 4);
      for (int byte = 0; byte < 16; byte++)
      {
	 if ((line << 4) + byte >= len) {
	   int i;
	   for (i = byte; i < 16; i++)
	     cout << "   ";
	   break;
	 } else
	   cout.form(" %02x", (int)buf[(line << 4) + byte]);
      }
      cout.form("  ");
      for (int byte = 0; byte < 16; byte++)
      {
	 u_char ch = buf[(line << 4) + byte];
	 if ((line << 4) + byte >= len)
	   break;
	 else
	   cout.form("%c", isprint(ch) ? ch : '.');
      }
      cout.form("\n");
   }
}

int main(int argc, char **argv)
{
  ScsiDevice dev;

  if (argc < 2) {
    cerr << "usage: " << argv[0] << " <device>" << endl;
    return 1;
  }
  char *devname = argv[1];
  if (dev.open(devname) != 0) {
    cerr << "Failed to open scsi device: " << dev.strError() << endl;
    return 1;
  }
  setuid(getuid());
  setgid(getgid());
  const u_char *inq = dev.inquiry();
  if (!inq) {
    cerr << "Inquiry failed: " << dev.strError() << endl;
    return 1;
  }

  cout << "Inquiry info:" << endl;
  hexdump(inq, 40);
  return 0;
}
