# HG changeset patch # User Jeandet Alexis # Date 2014-03-24 15:29:18 # Node ID def3deb8bd342ad5cba9f7984f02f49f8d1df63e # Parent 81254c2cf7635e1647872a68845512c4e87ea149 Removed ugly bug in serial port enumeration on linux. diff --git a/src/RS232.h b/src/RS232.h --- a/src/RS232.h +++ b/src/RS232.h @@ -1,4 +1,3 @@ -// SOPSUYSI_RS232.h #ifndef RS232_H #define RS232_H diff --git a/src/RS232_unix.c b/src/RS232_unix.c --- a/src/RS232_unix.c +++ b/src/RS232_unix.c @@ -58,18 +58,16 @@ rs232portslist_t* rs232getportlist() DIR *dir = opendir(path); rs232portslist_t* firstitem=NULL; rs232portslist_t* previtem=NULL; - char linkname[1024]; + char* linkname; + char* devName; int i=0; if(dir!=NULL) { while ((dp=readdir(dir)) != NULL) { - char* name = (char*)malloc(1024); - for(i=0;i<1024;i++) - { - name[i]='\0'; - linkname[i]='\0'; - } + int len = strlen(path)+strlen(dp->d_name)+8;//+8 => let's put some margin + linkname = (char*)malloc(len); + memset(linkname,'\0',len); struct stat statbuf; strcpy(linkname,path); strcat(linkname,"/"); @@ -77,32 +75,18 @@ rs232portslist_t* rs232getportlist() lstat(linkname, &statbuf); if(S_ISLNK(statbuf.st_mode)) { - if(-1!=readlink(linkname,name,1024)) + devName=realpath(linkname, NULL); + if(devName!=NULL) { - for(i=0;i<1024;i++) - { - linkname[i]='\0'; - } - strcpy(linkname,path); - strcat(linkname,"/"); - strcat(linkname,name); - for(i=0;i<1024;i++) - { - name[i]='\0'; - } - if(NULL!=realpath(linkname, name)) - { - rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); - item->name = name; - item->next = NULL; - if(NULL!=previtem)previtem->next = item; - previtem = item; - if(NULL==firstitem)firstitem = item; - } - + rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); + item->name = devName; + item->next = NULL; + if(NULL!=previtem)previtem->next = item; + previtem = item; + if(NULL==firstitem)firstitem = item; } } - + free(linkname); } } return firstitem;