X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=extras%2Fusb_id%2Fusb_id.c;h=b7d67958c9a1bd6d4edec4902da1af95b3d30344;hb=9bd72b9b6b2645ea3c36ef0be9a070e8ff67904f;hp=1eb45b911412ca28b0f27a3a7f132bc01dae2d55;hpb=46f01c6dc44ba516e42d5c8c1ada98a743f916f2;p=elogind.git diff --git a/extras/usb_id/usb_id.c b/extras/usb_id/usb_id.c index 1eb45b911..b7d67958c 100644 --- a/extras/usb_id/usb_id.c +++ b/extras/usb_id/usb_id.c @@ -4,23 +4,13 @@ * Identify an USB (block) device * * Copyright (c) 2005 SUSE Linux Products GmbH, Germany + * * Author: * Hannes Reinecke * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of the - * License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * 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 version 2 of the License. */ #include @@ -76,14 +66,13 @@ static int use_num_info; static int export; static int debug; -static void set_str(char *to, const unsigned char *from, int count) +static void set_str(char *to, const char *from, size_t count) { - int i, j; - int len; + size_t i, j, len; /* strip trailing whitespace */ len = strnlen(from, count); - while (isspace(from[len-1])) + while (len && isspace(from[len-1])) len--; /* strip leading whitespace */ @@ -120,25 +109,26 @@ static void set_str(char *to, const unsigned char *from, int count) * * Set the type based on the USB interface class */ -static void set_usb_iftype(char *to, const char *from, int count) +static void set_usb_iftype(char *to, const char *from, size_t len) { int type_num; char *eptr; + char *type = "generic"; type_num = strtoul(from, &eptr, 0); if (eptr != from) { switch (type_num) { case 1: - sprintf(to, "audio"); + type = "audio"; break; case 3: - sprintf(to, "hid"); + type = "hid"; break; case 7: - sprintf(to, "printer"); + type = "printer"; break; case 8: - sprintf(to, "disk"); + type = "disk"; break; case 2: /* CDC-Control */ case 5: /* Physical */ @@ -153,12 +143,11 @@ static void set_usb_iftype(char *to, const char *from, int count) case 0xf2: /* Application-specific */ case 0xff: /* Vendor-specific */ default: - sprintf(to, "generic"); break; } - } else { - sprintf(to, "generic"); } + strncpy(to, type, len); + to[len-1] = '\0'; } /* @@ -167,32 +156,35 @@ static void set_usb_iftype(char *to, const char *from, int count) * Set the type base on the interfaceSubClass. * Valid for Mass-Storage devices (type 8) only. */ -static int set_usb_ifsubtype(char *to, const char *from, int count) +static int set_usb_ifsubtype(char *to, const char *from, size_t len) { int type_num = 0; char *eptr; + char *type = "generic"; type_num = strtoul(from, &eptr, 0); if (eptr != from) { switch (type_num) { case 2: - sprintf(to, "cd"); + type = "cd"; break; case 3: - sprintf(to, "tape"); + type = "tape"; break; case 4: /* UFI */ case 5: /* SFF-8070i */ - sprintf(to, "floppy"); + type = "floppy"; break; case 1: /* RBC devices */ case 6: /* Transparent SPC-2 devices */ - sprintf(to, "disk"); + type = "disk"; + break; + default: break; } - } else { - sprintf(to, "generic"); } + strncpy(to, type, len); + to[len-1] = '\0'; return type_num; } @@ -316,11 +308,11 @@ static int usb_id(const char *target_path) /* usb device directory */ usb_dev = sysfs_get_device_parent(interface_dev); - if (!strcmp(interface_dev->bus, "bus") != 0) { + if (strcmp(interface_dev->bus, "usb") != 0) { info("%s is not an usb device", target_path); return 1; } - + if_class = sysfs_get_device_attr(interface_dev, "bInterfaceClass"); if (!if_class) { info("%s: cannot get bInterfaceClass attribute", interface_dev->name);