new readproc

Oliver.Rauch@Wolfsburg.DE
Tue, 14 Apr 1998 22:50:15 +0200 (MET DST)

Hi,

this is a new version of readproc, you can specifiy
vendor and device or bus, channel, id and lun

compile with:
gcc readproc.c -o readproc

Please tell if it works or not, especally on non-linux-systems!

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 *busstring = "scsi";
char *channelstring = "Channel:";
char *idstring = "Id:";
char *lunstring = "Lun:";

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

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

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

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

char line[256];

char *string;
char *vendor, *model, *revision;
int bus, cha, id, lun;

int number = -1;

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

bus = cha = id = lun = -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)
{
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';
string += 4;
string = killspaces(string);
}
else if (strncmp(string, busstring, strlen(busstring)) == 0)
{
number++;
string += strlen(busstring);
string = killspaces(string);
bus = atoi(string);
string += 1;
string = killspaces(string);
}
else if (strncmp(string, channelstring, strlen(channelstring)) == 0)
{
string += strlen(channelstring);
string = killspaces(string);
cha = atoi(string);
string += 2;
string = killspaces(string);
}
else if (strncmp(string, idstring, strlen(idstring)) == 0)
{
string += strlen(idstring);
string = killspaces(string);
id = atoi(string);
string += 2;
string = killspaces(string);
}
else if (strncmp(string, lunstring, strlen(lunstring)) == 0)
{
string += strlen(lunstring);
string = killspaces(string);
lun = atoi(string);
string += 2;
string = killspaces(string);
}
else
{ string++; }
}

if ( (vendor != NULL) && (model != NULL) )
{
fprintf(stderr,"device = %d : vendor = \"%s\", model = \"%s\", revision = \"%s\", "
"bus = %d, channel = %d, id = %d, lun = %d\n",
number, vendor, model, revision,
bus, cha, id, lun);

if ( (findvendor != NULL) && (findmodel != NULL) )
if ( (strncmp(vendor, findvendor, strlen(findvendor)) == 0)
&& (strncmp(model, findmodel, strlen(findmodel)) == 0) )

{
fclose(proc_fd);
return (number);
}

if ( (findbus == bus) && (findcha == cha) && (findid == id) && (findlun == lun) )
{
fclose(proc_fd);
return (number);
}
bus = cha = id = lun = -1;
}
}
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;

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

if (argc == 3)
{
char *vendor, *model;

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

fprintf(stdout,"looking for vendor = \"%s\", model = \"%s\"\n", vendor, model);
device = get_scannerdevice(vendor, model, 99,99,99,99);
fprintf(stdout,"I guess the devicename is: %s\n",get_devicename(device));
}
else if (argc == 5)
{
int bus, cha, id, lun;

bus = atoi(argv[1]);
cha = atoi(argv[2]);
id = atoi(argv[3]);
lun = atoi(argv[4]);
fprintf(stdout,"looking for bus = %d, channel = %d, id = %d, lun = %d\n", bus, cha, id, lun);
device = get_scannerdevice(NULL, NULL, bus, cha, id, lun);
fprintf(stdout,"I guess the devicename is: %s\n",get_devicename(device));
}
else
{
fprintf(stderr,"Usage:\n"
" %s \"vendor\" \"model\"\n"
"or \n"
" %s bus channel id lun\n", argv[0], argv[0]);
exit(-1);
}

}

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