@@ -1791,40 +1791,31 @@ disable_queue (const char *printer_uri, void *context)
17911791 ippDelete (answer );
17921792 httpClose (cups );
17931793}
1794- static int
1795- compare_usb_uri (char * uri_store , char * uri_uevent )
1794+
1795+ static int
1796+ device_exists (char * device_path )
17961797{
1797- int index = 0 ;
1798- int subset = 1 ;
1799- int exist = -1 ;
1800- char * full_path = NULL ;
1798+ char full_path [100 ];
18011799 struct stat buffer ;
1802-
1803- if (strlen (uri_uevent ) < strlen (uri_store ))
1804- return -1 ;
1805-
1806- // check whether it's a substring
1807- full_path = (char * ) malloc (strlen (uri_store ) + 4 );
1808- memset (full_path , 0 , sizeof (full_path ));
1809- strcpy (full_path , "/sys" );
1810- strcat (full_path , uri_store );
1811- while (uri_store [index ] != '\0' ){
1812- if (uri_store [index ] != uri_uevent [index ]){
1813- subset = -1 ;
1814- break ;
1815- }
1816- index ++ ;
1817- }
1800+ int exist = -1 ;
1801+ int path_length = strlen (device_path );
1802+ // 5 is the length of "/sys" + null
1803+ snprintf (full_path , path_length + 5 , "%s%s" , "/sys" , device_path );
18181804
18191805 // check the exist of this device
1820- if (subset == 1 && stat (full_path , & buffer ) == 0 ){
1821- exist = 1 ;
1806+ if (stat (full_path , & buffer ) == 0 ){
1807+ exist = 1 ;
18221808 }
18231809
1824- free (full_path );
18251810 return exist ;
18261811}
18271812
1813+ static char *
1814+ compare_usb_uri (char * uri_store , char * uri_uevent )
1815+ {
1816+ return strstr (uri_uevent , uri_store );
1817+ }
1818+
18281819static int
18291820do_remove (const char * devaddr )
18301821{
@@ -1864,7 +1855,7 @@ do_remove (const char *devaddr)
18641855 prev = & map -> entries ;
18651856 for (entry = map -> entries ; entry ; entry = entry -> next )
18661857 {
1867- if (compare_usb_uri (entry -> devpath , devpath ))
1858+ if (compare_usb_uri (entry -> devpath , devpath ) != NULL && device_exists ( entry -> devpath ) == -1 )
18681859 {
18691860 uris = & entry -> uris ;
18701861 break ;
0 commit comments