From f92f1f0c20ca6ec520d43702ec5f51ca245d7ec9 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Thu, 14 Nov 2013 17:33:53 +0100 Subject: [PATCH] Minor cosmetics, typos, and so on. --- README | 6 +++--- configure.ac | 9 ++++++--- libserialport.h | 14 +++++++------- serialport.c | 12 ++++++++++-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README b/README index 1e7f911..2a1e653 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ ----------------------------------------------------------------- +------------------------------------------------------------------------------- libserialport: cross-platform library for accessing serial ports ----------------------------------------------------------------- +------------------------------------------------------------------------------- libserialport is a minimal library written in C that is intended to take care of the OS-specific details when writing software that uses serial ports. @@ -75,7 +75,7 @@ can be obtained by calling sp_last_error_code() or sp_last_error_message(). The error code or message is that provided by the OS; libserialport does not define any error codes or messages of its own. -Functions calls that succeed return SP_OK, which is equal to zero, or where +Function calls that succeed return SP_OK, which is equal to zero, or where otherwise documented a positive value. The available functions are as follows: diff --git a/configure.ac b/configure.ac index 31e54bb..b441e50 100644 --- a/configure.ac +++ b/configure.ac @@ -58,6 +58,7 @@ AM_PROG_CC_C_O LT_INIT # Initialize pkg-config. +# We require at least 0.22, as "Requires.private" behaviour changed there. PKG_PROG_PKG_CONFIG([0.22]) # Library version for libserialport (NOT the same as the package version). @@ -76,11 +77,13 @@ AC_SUBST(SP_LIB_VERSION) AC_SUBST(SP_LIB_LDFLAGS) # Checks for header files. -# These are already checked: inttypes.h stdint.h stdlib.h string.h unistd.h. -AC_CHECK_HEADERS([sys/types.h sys/stat.h fcntl.h termios.h sys/ioctl.h errno.h]) +# These are already checked: inttypes.h dlfcn.h memory.h stdint.h stdlib.h +# string.h strings.h sys/types.h sys/stat.h unistd.h +AC_CHECK_HEADERS([errno.h fcntl.h stddef.h sys/ioctl.h termios.h]) case $target_os in *linux*) + # On Linux libudev is currently a hard requirement. PKG_CHECK_MODULES([libudev], [libudev >= 0], [CFLAGS="$CFLAGS $libudev_CFLAGS"; LIBS="$LIBS $libudev_LIBS"], [AC_MSG_ERROR([libudev.h not found])]) @@ -117,7 +120,7 @@ AC_OUTPUT echo echo "libserialport configuration summary:" echo -echo " - Package version (major.minor): $SP_PACKAGE_VERSION" +echo " - Package version (major.minor): $SP_PACKAGE_VERSION" echo " - Library version (current:revision:age): $SP_LIB_VERSION" echo " - Prefix: $prefix" echo " - Building on: $build" diff --git a/libserialport.h b/libserialport.h index 5f1f2c6..c5d3b9c 100644 --- a/libserialport.h +++ b/libserialport.h @@ -17,8 +17,8 @@ * along with this program. If not, see . */ -#ifndef SERIALPORT_H -#define SERIALPORT_H +#ifndef LIBSERIALPORT_H +#define LIBSERIALPORT_H #include #ifdef _WIN32 @@ -46,7 +46,7 @@ enum { /* A system error occured while executing the operation. */ SP_ERR_FAIL = -2, /* A memory allocation failed while executing the operation. */ - SP_ERR_MEM = -3 + SP_ERR_MEM = -3, }; /* Port access modes. */ @@ -56,7 +56,7 @@ enum { /* Open port for read access only. */ SP_MODE_RDONLY = 2, /* Open port in non-blocking mode. */ - SP_MODE_NONBLOCK = 4 + SP_MODE_NONBLOCK = 4, }; /* Parity settings. */ @@ -66,7 +66,7 @@ enum { /* Even parity. */ SP_PARITY_EVEN = 1, /* Odd parity. */ - SP_PARITY_ODD = 2 + SP_PARITY_ODD = 2, }; /* Flow control settings. */ @@ -76,7 +76,7 @@ enum { /* Hardware (RTS/CTS) flow control. */ SP_FLOW_HARDWARE = 1, /* Software (XON/XOFF) flow control. */ - SP_FLOW_SOFTWARE = 2 + SP_FLOW_SOFTWARE = 2, }; int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); @@ -95,4 +95,4 @@ int sp_last_error_code(void); char *sp_last_error_message(void); void sp_free_error_message(char *message); -#endif /* SERIALPORT_H */ +#endif /* LIBSERIALPORT_H */ diff --git a/serialport.c b/serialport.c index 838f9c7..5d8dabb 100644 --- a/serialport.c +++ b/serialport.c @@ -116,6 +116,7 @@ static struct sp_port **sp_list_append(struct sp_port **list, const char *portna { void *tmp; unsigned int count; + for (count = 0; list[count]; count++); if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2)))) goto fail; @@ -124,6 +125,7 @@ static struct sp_port **sp_list_append(struct sp_port **list, const char *portna goto fail; list[count + 1] = NULL; return list; + fail: sp_free_port_list(list); return NULL; @@ -135,7 +137,7 @@ int sp_list_ports(struct sp_port ***list_ptr) int ret = SP_OK; if (!(list = malloc(sizeof(struct sp_port **)))) - return SP_ERR_MEM;; + return SP_ERR_MEM; list[0] = NULL; @@ -343,6 +345,7 @@ out: void sp_free_port_list(struct sp_port **list) { unsigned int i; + for (i = 0; list[i]; i++) sp_free_port(list[i]); free(list); @@ -395,6 +398,7 @@ int sp_open(struct sp_port *port, int flags) return SP_ERR_FAIL; #else int flags_local = 0; + /* Map 'flags' to the OS-specific settings. */ if (flags & SP_MODE_RDWR) flags_local |= O_RDWR; @@ -452,6 +456,7 @@ int sp_write(struct sp_port *port, const void *buf, size_t count) #ifdef _WIN32 DWORD written = 0; + /* Returns non-zero upon success, 0 upon failure. */ if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) return SP_ERR_FAIL; @@ -459,10 +464,11 @@ int sp_write(struct sp_port *port, const void *buf, size_t count) #else /* Returns the number of bytes written, or -1 upon failure. */ ssize_t written = write(port->fd, buf, count); + if (written < 0) return SP_ERR_FAIL; else - return written;; + return written; #endif } @@ -475,12 +481,14 @@ int sp_read(struct sp_port *port, void *buf, size_t count) #ifdef _WIN32 DWORD bytes_read = 0; + /* Returns non-zero upon success, 0 upon failure. */ if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0) return SP_ERR_FAIL; return bytes_read; #else ssize_t bytes_read; + /* Returns the number of bytes read, or -1 upon failure. */ if ((bytes_read = read(port->fd, buf, count)) < 0) return SP_ERR_FAIL;