1 /*****************************************************************************/
3 * usbmisc.c -- Misc USB routines
5 * Copyright (C) 2003 Aurelien Jarno (aurelien@aurel32.net)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /*****************************************************************************/
35 /* ---------------------------------------------------------------------- */
37 static const char *devbususb = "/dev/bus/usb";
39 /* ---------------------------------------------------------------------- */
41 static int readlink_recursive(const char *path, char *buf, size_t bufsize)
43 char temp[PATH_MAX + 1];
47 ret = readlink(path, buf, bufsize);
53 strncpy(temp, path, sizeof(temp));
54 ptemp = temp + strlen(temp);
55 while (*ptemp != '/' && ptemp != temp) ptemp--;
57 strncpy(ptemp, buf, bufsize + temp - ptemp);
60 strncpy(temp, buf, sizeof(temp));
61 return readlink_recursive(temp, buf, bufsize);
64 strncpy(buf, path, bufsize);
69 static char *get_absolute_path(const char *path, char *result, size_t result_size)
71 const char *ppath; /* pointer on the input string */
72 char *presult; /* pointer on the output string */
82 result = getcwd(result, result_size);
83 presult += strlen(result);
84 result_size -= strlen(result);
90 while (*ppath != 0 && result_size > 1) {
92 do ppath++; while (*ppath == '/');
96 else if (*ppath == '.' && *(ppath + 1) == '.' && *(ppath + 2) == '/' && *(presult - 1) == '/') {
97 if ((presult - 1) != result)
99 /* go one directory upper */
103 } while (*(presult - 1) != '/');
107 else if (*ppath == '.' && *(ppath + 1) == '/' && *(presult - 1) == '/') {
111 *presult++ = *ppath++;
115 /* Don't forget to mark the end of the string! */
121 struct usb_device *get_usb_device(const char *path)
124 struct usb_device *dev;
125 char device_path[PATH_MAX + 1];
126 char absolute_path[PATH_MAX + 1];
128 readlink_recursive(path, device_path, sizeof(device_path));
129 get_absolute_path(device_path, absolute_path, sizeof(absolute_path));
131 for (bus = usb_busses; bus; bus = bus->next) {
132 for (dev = bus->devices; dev; dev = dev->next) {
133 snprintf(device_path, sizeof(device_path), "%s/%s/%s", devbususb, bus->dirname, dev->filename);
134 if (!strcmp(device_path, absolute_path))