#include "serialport.h"
-static char **sp_list_append(char **list, void *data, size_t len)
+static struct sp_port *sp_port_new(const char *portname)
+{
+ struct sp_port *port;
+ int len;
+
+ if (!(port = malloc(sizeof(struct sp_port))))
+ return NULL;
+
+ len = strlen(portname) + 1;
+
+ if (!(port->name = malloc(len)))
+ {
+ free(port);
+ return NULL;
+ }
+
+ memcpy(port->name, portname, len);
+
+ return port;
+}
+
+static struct sp_port **sp_list_append(struct sp_port **list, const char *portname)
{
void *tmp;
unsigned int count;
for (count = 0; list[count]; count++);
- if (!(tmp = realloc(list, sizeof(char *) * (count + 2))))
+ if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
goto fail;
list = tmp;
- if (!(list[count] = malloc(len)))
+ if (!(list[count] = sp_port_new(portname)))
goto fail;
- memcpy(list[count], data, len);
list[count + 1] = NULL;
return list;
fail:
*
* @return A null-terminated array of port name strings.
*/
-char **sp_list_ports(void)
+struct sp_port **sp_list_ports(void)
{
- char **list;
+ struct sp_port **list;
- if (!(list = malloc(sizeof(char *))))
+ if (!(list = malloc(sizeof(struct sp_port **))))
return NULL;
list[0] = NULL;
DWORD max_value_len, max_data_size, max_data_len;
DWORD value_len, data_size, data_len;
DWORD type, index = 0;
+ char *name;
+ int name_len;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
{
data_len = data_size / sizeof(TCHAR);
data[data_len] = '\0';
+#ifdef UNICODE
+ name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
+#else
+ name_len = data_len + 1;
+#endif
+ if (!(name = malloc(name_len)))
+ goto out;
+#ifdef UNICODE
+ WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
+#else
+ strcpy(name, data);
+#endif
if (type == REG_SZ)
- if (!(list = sp_list_append(list,
- data, (data_len + 1) * sizeof(TCHAR))))
+ if (!(list = sp_list_append(list, name)))
goto out;
index++;
}
path, PATH_MAX, kCFStringEncodingASCII);
CFRelease(cf_path);
if (result)
- if (!(list = sp_list_append(list, path, strlen(path) + 1)))
+ if (!(list = sp_list_append(list, path)))
{
IOObjectRelease(port);
goto out;
if (serial_info.type == PORT_UNKNOWN)
goto skip;
}
- list = sp_list_append(list, (void *)name, strlen(name) + 1);
+ list = sp_list_append(list, name);
skip:
udev_device_unref(ud_dev);
if (!list)
/**
* Free a port list returned by sp_list_ports.
*/
-void sp_free_port_list(char **list)
+void sp_free_port_list(struct sp_port **list)
{
unsigned int i;
for (i = 0; list[i]; i++)
* @return SP_OK on success, SP_ERR_FAIL on failure,
* or SP_ERR_ARG if an invalid port or name is passed.
*/
-int sp_open(struct sp_port *port, char *portname, int flags)
+int sp_open(struct sp_port *port, int flags)
{
if (!port)
return SP_ERR_ARG;
- if (!portname)
- return SP_ERR_ARG;
-
- port->name = portname;
-
#ifdef _WIN32
DWORD desired_access = 0, flags_and_attributes = 0;
/* Map 'flags' to the OS-specific settings. */