[PATCH] adjust "parted -s dev print" not to emit trailing spaces

Jim Meyering meyering at redhat.com
Thu Mar 5 14:18:02 UTC 2009


* parted/table.c (table_render_row): Don't append trailing spaces
or delimiter after final field, and trim any other trailing spaces.
* tests/t5000-tags.sh: Remove trailing spaces from expected output.
---
 parted/table.c      |   18 ++++++++++++++----
 tests/t5000-tags.sh |    6 +++---
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/parted/table.c b/parted/table.c
index 83f9cb5..6bc7f9c 100644
--- a/parted/table.c
+++ b/parted/table.c
@@ -3,7 +3,7 @@
  */
 /*
     parted - a frontend to libparted
-    Copyright (C) 2006-2008 Free Software Foundation, Inc.
+    Copyright (C) 2006-2009 Free Software Foundation, Inc.

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -173,7 +173,7 @@ void table_add_row_from_strlist (Table* t, StrList* list)
 static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)
 {
         wchar_t** row = t->rows[rownum];
-        int len = 1, i;
+        size_t len = 1, i;
         size_t newsize;

         assert(t);
@@ -189,6 +189,10 @@ static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)

         for (i = 0; i < ncols; ++i)
         {
+                wcscat (*s, row[i]);
+                if (ncols <= i + 1)
+                        break;
+
                 int j;
                 int nspaces = max(t->widths[i] - wcswidth(row[i], MAX_WIDTH),
                                   0);
@@ -199,15 +203,21 @@ static void table_render_row (Table* t, int rownum, int ncols, wchar_t** s)

                 pad[nspaces] = L_('\0');

-                wcscat (*s, row[i]);
                 wcscat (*s, pad);
                 if (i + 1 < ncols) 
                         wcscat (*s, DELIMITER);

                 free (pad);
-                pad = NULL;
         }

+        /* Remove any trailing blanks.  */
+        wchar_t *p = *s;
+        size_t k = wcslen (p);
+        while (k && p[k-1] == L_(' '))
+                --k;
+        p[k] = L_('\0');
+
+
         wcscat (*s, COLSUFFIX);
 }

diff --git a/tests/t5000-tags.sh b/tests/t5000-tags.sh
index bb4c6d3..6a89729 100755
--- a/tests/t5000-tags.sh
+++ b/tests/t5000-tags.sh
@@ -1,6 +1,6 @@
 #!/bin/sh

-# Copyright (C) 2007,2008 Free Software Foundation, Inc.
+# Copyright (C) 2007-2009 Free Software Foundation, Inc.

 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -59,14 +59,14 @@ Sector size (logical/physical): 512B/512B
 Partition Table: gpt

 Number  Start   End     Size    File system  Name     Flags
- 1      17.4kB  48.6kB  31.2kB               primary       
+ 1      17.4kB  48.6kB  31.2kB               primary

 Model:  (file)
 Disk .../$dev: 65.5kB
 Sector size (logical/physical): 512B/512B
 Partition Table: gpt

-Number  Start   End     Size    File system  Name     Flags    
+Number  Start   End     Size    File system  Name     Flags
  1      17.4kB  48.6kB  31.2kB               primary  bios_grub

 EOF
-- 
1.6.2.rc1.285.gc5f54




More information about the parted-devel mailing list