diff options
Diffstat (limited to 'com32/lib')
| -rw-r--r-- | com32/lib/Makefile | 3 | ||||
| -rw-r--r-- | com32/lib/bufprintf.c | 41 | ||||
| -rw-r--r-- | com32/lib/chrreplace.c | 11 | ||||
| -rw-r--r-- | com32/lib/inet.c | 39 | ||||
| -rw-r--r-- | com32/lib/pci/scan.c | 11 |
5 files changed, 95 insertions, 10 deletions
diff --git a/com32/lib/Makefile b/com32/lib/Makefile index 48a166dd..bf0da998 100644 --- a/com32/lib/Makefile +++ b/com32/lib/Makefile @@ -28,6 +28,9 @@ LIBOBJS = \ asprintf.o vasprintf.o strlcpy.o strlcat.o \ vsscanf.o zalloc.o \ skipspace.o \ + chrreplace.o \ + bufprintf.o \ + inet.o \ \ lmalloc.o lstrdup.o \ \ diff --git a/com32/lib/bufprintf.c b/com32/lib/bufprintf.c new file mode 100644 index 00000000..939bcec3 --- /dev/null +++ b/com32/lib/bufprintf.c @@ -0,0 +1,41 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <bufprintf.h> + +int vbufprintf(struct print_buf *buf, const char *format, va_list ap) +{ + va_list ap2; + int rv; + + va_copy(ap2, ap); + rv = vsnprintf(NULL, 0, format, ap); + + /* >= to make sure we have space for terminating null */ + if (rv + buf->len >= buf->size) { + size_t newsize = rv + buf->len + BUFPAD; + char *newbuf; + + newbuf = realloc(buf->buf, newsize); + if (!newbuf) + return -1; + + buf->buf = newbuf; + buf->size = newsize; + } + + rv = vsnprintf(buf->buf + buf->len, buf->size - buf->len, format, ap2); + buf->len += rv; + return rv; +} + +int bufprintf(struct print_buf *buf, const char *format, ...) +{ + va_list ap; + int rv; + + va_start(ap, format); + rv = vbufprintf(buf, format, ap); + va_end(ap); + return rv; +} diff --git a/com32/lib/chrreplace.c b/com32/lib/chrreplace.c new file mode 100644 index 00000000..65786f94 --- /dev/null +++ b/com32/lib/chrreplace.c @@ -0,0 +1,11 @@ +#include <ctype.h> + +/* Replace char 'old' by char 'new' in source */ +void chrreplace(char *source, char old, char new) +{ + while (*source) { + source++; + if (source[0] == old) source[0]=new; + } +} + diff --git a/com32/lib/inet.c b/com32/lib/inet.c new file mode 100644 index 00000000..133645ed --- /dev/null +++ b/com32/lib/inet.c @@ -0,0 +1,39 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2011 Erwan Velu - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- + */ + +#include <stdio.h> +#include <netinet/in.h> + +char *inet_ntoa(struct in_addr addr) +{ + static char buf[16]; + const uint8_t *bytes = (const uint8_t *)&addr.s_addr; + + sprintf(buf, "%u.%u.%u.%u", bytes[0], bytes[1], bytes[2], bytes[3]); + return buf; +} diff --git a/com32/lib/pci/scan.c b/com32/lib/pci/scan.c index c8334b11..4e5635f6 100644 --- a/com32/lib/pci/scan.c +++ b/com32/lib/pci/scan.c @@ -66,15 +66,6 @@ static int hex_to_int(char *hexa) return strtoul(hexa, NULL, 16); } -/* Replace char 'old' by char 'new' in source */ -void chr_replace(char *source, char old, char new) -{ - while (*source) { - source++; - if (source[0] == old) source[0]=new; - } -} - /* Try to match any pci device to the appropriate kernel module */ /* it uses the modules.pcimap from the boot device */ int get_module_name_from_pcimap(struct pci_domain *domain, @@ -135,7 +126,7 @@ int get_module_name_from_pcimap(struct pci_domain *domain, * in the module name whereas modules.alias is only using '_'. * To avoid kernel modules duplication, let's rename all '-' in '_' * to match what modules.alias provides */ - case 0:chr_replace(result,'-','_');strcpy(module_name,result); break; + case 0:chrreplace(result,'-','_');strcpy(module_name,result); break; case 1:strcpy(vendor_id,result); break; case 2:strcpy(product_id,result); break; case 3:strcpy(sub_vendor_id,result); break; |
