Introduce function nullTrailingSpace() that nulls blank characters from the end of a string. Use this function in the ATA, USB MSC, and bootorder code. --- src/ata.c | 6 +----- src/boot.c | 1 + src/usb-msc.c | 14 ++++++++------ src/util.c | 10 ++++++++++ src/util.h | 1 + 5 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/src/ata.c b/src/ata.c index e9a2aef..397e402 100644 --- a/src/ata.c +++ b/src/ata.c @@ -727,11 +727,7 @@ ata_extract_model(char *model, u32 size, u16 *buffer) for (i=0; i<size/2; i++) *(u16*)&model[i*2] = ntohs(buffer[27+i]); model[size] = 0x00; - - // Trim trailing spaces from model name. - for (i=size-1; i>0 && model[i] == 0x20; i--) - model[i] = 0x00; - + nullTrailingSpace(model); return model; }
diff --git a/src/boot.c b/src/boot.c index e5475a2..b59794e 100644 --- a/src/boot.c +++ b/src/boot.c @@ -51,6 +51,7 @@ loadBootOrder(void) f = strchr(f, '\n'); if (f) *(f++) = '\0'; + nullTrailingSpace(Bootorder[i]); dprintf(3, "%d: %s\n", i+1, Bootorder[i]); i++; } while (f); diff --git a/src/usb-msc.c b/src/usb-msc.c index 58a5d14..1aa57d1 100644 --- a/src/usb-msc.c +++ b/src/usb-msc.c @@ -228,14 +228,16 @@ usb_msc_init(struct usb_pipe *pipe goto fail; char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1]; char rev[sizeof(data.rev)+1]; + strtcpy(vendor, data.vendor, sizeof(vendor)); + nullTrailingSpace(vendor); + strtcpy(product, data.product, sizeof(product)); + nullTrailingSpace(product); + strtcpy(rev, data.rev, sizeof(rev)); + nullTrailingSpace(rev); int pdt = data.pdt & 0x1f; int removable = !!(data.removable & 0x80); - dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s'" - " type=%d removable=%d\n" - , strtcpy(vendor, data.vendor, sizeof(vendor)) - , strtcpy(product, data.product, sizeof(product)) - , strtcpy(rev, data.rev, sizeof(rev)) - , pdt, removable); + dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s' type=%d removable=%d\n" + , vendor, product, rev, pdt, removable); udrive_g->drive.removable = removable;
if (pdt == USB_MSC_TYPE_CDROM) { diff --git a/src/util.c b/src/util.c index 579a044..ed73d63 100644 --- a/src/util.c +++ b/src/util.c @@ -272,6 +272,16 @@ strchr(const char *s, int c) return NULL; }
+// Remove any trailing blank characters (spaces, new lines, carriage returns) +void +nullTrailingSpace(char *buf) +{ + int len = strlen(buf); + char *end = &buf[len-1]; + while (end >= buf && *end <= ' ') + *(end--) = '\0'; +} + /**************************************************************** * Keyboard calls ****************************************************************/ diff --git a/src/util.h b/src/util.h index 7102c27..17fc54a 100644 --- a/src/util.h +++ b/src/util.h @@ -210,6 +210,7 @@ void iomemcpy(void *d, const void *s, u32 len); void *memmove(void *d, const void *s, size_t len); char *strtcpy(char *dest, const char *src, size_t len); char *strchr(const char *s, int c); +void nullTrailingSpace(char *buf); int get_keystroke(int msec);
// stacks.c