Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/sysinst Simplify name trimming following a suggesti...



details:   https://anonhg.NetBSD.org/src/rev/9cf3a54548e9
branches:  trunk
changeset: 369758:9cf3a54548e9
user:      martin <martin%NetBSD.org@localhost>
date:      Tue Aug 30 15:27:37 2022 +0000

description:
Simplify name trimming following a suggestion from rillig.

diffstat:

 usr.sbin/sysinst/disks.c |  33 +++++++++++----------------------
 1 files changed, 11 insertions(+), 22 deletions(-)

diffs (69 lines):

diff -r f749f4801f4d -r 9cf3a54548e9 usr.sbin/sysinst/disks.c
--- a/usr.sbin/sysinst/disks.c  Tue Aug 30 13:40:37 2022 +0000
+++ b/usr.sbin/sysinst/disks.c  Tue Aug 30 15:27:37 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: disks.c,v 1.89 2022/08/30 11:45:28 martin Exp $ */
+/*     $NetBSD: disks.c,v 1.90 2022/08/30 15:27:37 martin Exp $ */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -143,26 +143,16 @@
 }
 
 /*
- * like strncpy, but drop trailing whitespace
+ * Find length of string but ignore trailing whitespace
  */
-static void
-trim_name(char *name, size_t len, const char *src)
+static int
+trimmed_len(const char *s)
 {
-       size_t i, last = ~0;
+       size_t len = strlen(s);
 
-       for (i = 0; i < len && src[i]; i++) {
-               if (isspace((unsigned char)src[i]))
-                       last = i;
-               else
-                       last = ~0;
-               name[i] = src[i];
-       }
-       if (i >= len)
-               i = len-1;
-       if (last < i)
-               name[last] = 0;
-       else
-               name[i] = 0;
+       while (len > 0 && isspace((unsigned char)s[len - 1]))
+               len--;
+       return len;
 }
 
 /* from src/sbin/atactl/atactl.c
@@ -358,7 +348,7 @@
        int8_t perr;
        int error, fd;
        bool rv;
-       char size[5], name[sizeof(dd->dd_descr)];
+       char size[5];
        const char *model;
 
        fd = open("/dev/drvctl", O_RDONLY);
@@ -400,14 +390,13 @@
                prop_object_release(results_dict);
                return 0;
        }
-       trim_name(name, sizeof name, model);
 
        humanize_number(size, sizeof(size),
            (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
            "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
 
-       snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
-           dd->dd_name, size, name);
+       snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %.*s)",
+           dd->dd_name, size, trimmed_len(model), model);
 
        prop_object_release(results_dict);
 



Home | Main Index | Thread Index | Old Index