X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=ao-tools%2Flibaltos%2Flibaltos.c;h=00fb2125443bc452b9807f8f106d81b726dd30b9;hp=7d471f38f756b9bac70947a2c2d5edc413ebfa5f;hb=7877496d47ce6d25210c0e1c6500666dbfc0876c;hpb=b51497597868a40df039dd3ca11b35a6258bbbb3 diff --git a/ao-tools/libaltos/libaltos.c b/ao-tools/libaltos/libaltos.c index 7d471f38..00fb2125 100644 --- a/ao-tools/libaltos/libaltos.c +++ b/ao-tools/libaltos/libaltos.c @@ -40,6 +40,26 @@ match_dev(char *product, int serial, struct altos_device *device) return i; } +#ifdef DARWIN +/* Mac OS X don't have strndup even if _GNU_SOURCE is defined */ +static char * +altos_strndup (const char *s, size_t n) +{ + size_t len = strlen (s); + char *ret; + + if (len <= n) + return strdup (s); + ret = malloc(n + 1); + strncpy(ret, s, n); + ret[n] = '\0'; + return ret; +} + +#else +#define altos_strndup strndup +#endif + int altos_find_by_arg(char *arg, char *default_product, struct altos_device *device) { @@ -61,7 +81,7 @@ altos_find_by_arg(char *arg, char *default_product, struct altos_device *device) /* check for : */ colon = strchr(arg, ':'); if (colon) { - product = strndup(arg, colon - arg); + product = altos_strndup(arg, colon - arg); serial = strtol(colon + 1, &end, 0); if (*end != '\0') return 0; @@ -547,6 +567,14 @@ void altos_close(struct altos_file *file) { close(file->fd); + file->fd = -1; +} + +void +altos_free(struct altos_file *file) +{ + if (file->fd != -1) + close(file->fd); free(file); } @@ -572,6 +600,8 @@ altos_flush(struct altos_file *file) while (file->out_used) { int ret; + if (file->fd < 0) + return -EBADF; ret = write (file->fd, file->out_data, file->out_used); if (ret < 0) return -errno; @@ -590,6 +620,8 @@ altos_getchar(struct altos_file *file, int timeout) int ret; altos_flush(file); + if (file->fd < 0) + return -EBADF; ret = read(file->fd, file->in_data, USB_BUF_SIZE); if (ret < 0) return -errno;