1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

Port name string length now no longer needs to be passed around.

This commit is contained in:
Martin Ling 2013-11-03 21:30:43 +00:00 committed by Uwe Hermann
parent 8b532d9c1e
commit 5919c9134a

View File

@ -45,13 +45,16 @@
#include "serialport.h" #include "serialport.h"
static struct sp_port *sp_port_new(char *portname, size_t len) static struct sp_port *sp_port_new(const char *portname)
{ {
struct sp_port *port; struct sp_port *port;
int len;
if (!(port = malloc(sizeof(struct sp_port)))) if (!(port = malloc(sizeof(struct sp_port))))
return NULL; return NULL;
len = strlen(portname) + 1;
if (!(port->name = malloc(len))) if (!(port->name = malloc(len)))
{ {
free(port); free(port);
@ -63,7 +66,7 @@ static struct sp_port *sp_port_new(char *portname, size_t len)
return port; return port;
} }
static struct sp_port **sp_list_append(struct sp_port **list, char *portname, size_t len) static struct sp_port **sp_list_append(struct sp_port **list, const char *portname)
{ {
void *tmp; void *tmp;
unsigned int count; unsigned int count;
@ -71,7 +74,7 @@ static struct sp_port **sp_list_append(struct sp_port **list, char *portname, si
if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2)))) if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
goto fail; goto fail;
list = tmp; list = tmp;
if (!(list[count] = sp_port_new(portname, len))) if (!(list[count] = sp_port_new(portname)))
goto fail; goto fail;
list[count + 1] = NULL; list[count + 1] = NULL;
return list; return list;
@ -135,7 +138,7 @@ struct sp_port **sp_list_ports(void)
strcpy(name, data); strcpy(name, data);
#endif #endif
if (type == REG_SZ) if (type == REG_SZ)
if (!(list = sp_list_append(list, name, name_len))) if (!(list = sp_list_append(list, name)))
goto out; goto out;
index++; index++;
} }
@ -179,7 +182,7 @@ out_close:
path, PATH_MAX, kCFStringEncodingASCII); path, PATH_MAX, kCFStringEncodingASCII);
CFRelease(cf_path); CFRelease(cf_path);
if (result) if (result)
if (!(list = sp_list_append(list, path, strlen(path) + 1))) if (!(list = sp_list_append(list, path)))
{ {
IOObjectRelease(port); IOObjectRelease(port);
goto out; goto out;
@ -238,7 +241,7 @@ out_release:
if (serial_info.type == PORT_UNKNOWN) if (serial_info.type == PORT_UNKNOWN)
goto skip; goto skip;
} }
list = sp_list_append(list, (void *)name, strlen(name) + 1); list = sp_list_append(list, name);
skip: skip:
udev_device_unref(ud_dev); udev_device_unref(ud_dev);
if (!list) if (!list)