find scanner via /proc/scsi/scsi

Oliver.Rauch@Wolfsburg.DE
Wed, 8 Apr 1998 21:39:51 +0200 (MET DST)

Hi,

this is a little tool that reads /proc/scsi/scsi
and tries to find out on which device the scanner is connected.

To compile it, just call
gcc readproc.c -o readproc

To use it call
readproc "vendor" "model"
e.g.:
readproc "UMAX " "UMAX S-12"

Please tell me if the "guessed" devicename is correct or not!

Bye
Oliver

---------------------------------------------------------------------

/* readproc.c

Copyright (C) 1998 Oliver Rauch

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

/* --------------------------------------------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

/* --------------------------------------------------------------------------------------------------------- */

char *vendorstring = "Vendor:";
char *modelstring = "Model:";
char *revisionstring = "Rev:";

/* --------------------------------------------------------------------------------------------------------- */

char *killspaces(char *string)
{
char *start = string;
while (*start == ' ') {start++;}
return (start);
}

/* --------------------------------------------------------------------------------------------------------- */

int get_scannerdevice(char *findvendor, char *findmodel)
{
FILE *proc_fd;
char *procfile = "/proc/scsi/scsi";

char line[256];

char *string;
char *vendor, *model, *revision;

int number = -1;

proc_fd = fopen(procfile,"r");
if (proc_fd == 0)
{
fprintf(stderr, "ERROR: could not open %s!\n", procfile);
return(-1);
}

while (!feof(proc_fd))
{
fgets(line, 256, proc_fd);
string = killspaces(line);
vendor=model=revision=NULL;

while (*string != '\0')
{
if (strncmp(string, vendorstring, strlen(vendorstring)) == 0)
{
number++;
string += strlen(vendorstring);
string = killspaces(string);
vendor = string;
vendor[8] = '\0';
string += 9;
string = killspaces(string);
}
else if (strncmp(string, modelstring, strlen(modelstring)) == 0)
{
string += strlen(modelstring);
string = killspaces(string);
model = string;
model[16] = '\0';
string += 17;
string = killspaces(string);
}
else if (strncmp(string, revisionstring, strlen(revisionstring)) == 0)
{
string += strlen(revisionstring);
string = killspaces(string);
revision = string;
revision[4] = '\0';
}
else
{ string++; }
}

if ( (vendor != NULL) && (model != NULL) )
{
fprintf(stderr,"device = %d : vendor = \"%s\", model = \"%s\", revision = \"%s\"\n",
number, vendor, model, revision);
if ( (strncmp(vendor, findvendor, strlen(findvendor)) == 0)
&& (strncmp(model, findmodel, strlen(findmodel)) == 0) )
{
fclose(proc_fd);
return (number);
}
}
}
fclose(proc_fd);
return (-1);
}

/* --------------------------------------------------------------------------------------------------------- */

char *get_devicename(int device)
{
int dev_fd;
int number = 0;
char name[256];
static char *device_name_list[] = { "/dev/uk", "/dev/sg", "/dev/sg", "/dev/gsc", };
static char device_parameter_list[] = "00a0";
const maxnum=4;

while (number < maxnum)
{
sprintf(name, "%s%c", device_name_list[number], device_parameter_list[number]+device);

dev_fd = open(name,O_RDWR);
if (dev_fd > 1)
{
close(dev_fd);
return ((char *)strdup(name));
}

number++;
}

return NULL;
}

/* --------------------------------------------------------------------------------------------------------- */

main(int argc,char **argv)
{
int device;
char *vendor, *model;

fprintf(stdout,"readproc reads /proc/scsi/scsi\n"
"and tries to find out on which device your scanner is connected\n");

if (argc != 3)
{
fprintf(stderr,"Usage: %s \"vendor\" \"model\"\n", argv[0]);
exit(-1);
}

vendor = argv[1];
model = argv[2];

fprintf(stdout,"looking for vendor = \"%s\", model = \"%s\"\n", vendor, model);
device = get_scannerdevice(vendor, model);
fprintf(stdout,"I guess the devicename is: %s\n",get_devicename(device));
}

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