coreutils
[Top][All Lists]
Advanced

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

[PATCH] df: add --without-header option


From: Bernhard Voelker
Subject: [PATCH] df: add --without-header option
Date: Sat, 08 Dec 2012 21:18:19 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0

I wondered if it would be useful in some cases
to suppress the header line in df.

Looking at the already-used options and how they start,
I took "--without-header" which can be abbreviated by
"--w". WDYT?

Have a nice day,
Berny


>From da7f50ad55ca73c0e336b42216847132e963ed42 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <address@hidden>
Date: Sat, 8 Dec 2012 21:13:25 +0100
Subject: [PATCH] df: add --without-header option

* src/df.c (WITHOUT_HEADER_OPTION): Add new enum.
(long_options): Add "without-header" array element.
(usage): Add description of --without-header option.
(main): Define boolean without_header, and set it to
true when the --without-header option is specified.
Call get_header() only if without_header is false.
* tests/df/without-header.sh: Add new test to exercise
the behaviour of the new option.
* tests/local.mk: Add the above test.
* doc/coreutils.texi (df invocation): Document the new option.
* NEWS (New features): Mention it.
---
 NEWS                       |    2 ++
 doc/coreutils.texi         |    5 +++++
 src/df.c                   |   13 ++++++++++++-
 tests/df/without-header.sh |   36 ++++++++++++++++++++++++++++++++++++
 tests/local.mk             |    1 +
 5 files changed, 56 insertions(+), 1 deletions(-)
 create mode 100755 tests/df/without-header.sh

diff --git a/NEWS b/NEWS
index 0e1414c..173119f 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,8 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   to include in the output, or all available columns if the FIELD_LIST is
   omitted.  Note this enables df to output both block and inode fields 
together.

+  df now accepts the --without-header option to omit the header line.
+
 ** Bug fixes

   cut no longer accepts the invalid range 0-, which made it print empty lines.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 21400ad..97371d8 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -10846,6 +10846,11 @@ An MS-DOS file system, usually on a diskette.

 @end table

+@item --without-header
+@opindex --without-header
+Omit the header line. This can be useful for scripts to parse @command{df}'s
+output.
+
 @item -x @var{fstype}
 @itemx --exclude-type=@var{fstype}
 @opindex -x
diff --git a/src/df.c b/src/df.c
index 63c8b31..9dbce4e 100644
--- a/src/df.c
+++ b/src/df.c
@@ -246,6 +246,7 @@ enum
   SYNC_OPTION,
   TOTAL_OPTION,
   OUTPUT_OPTION,
+  WITHOUT_HEADER_OPTION,
   MEGABYTES_OPTION  /* FIXME: remove long opt in Aug 2013 */
 };

@@ -265,6 +266,7 @@ static struct option const long_options[] =
   {"no-sync", no_argument, NULL, NO_SYNC_OPTION},
   {"total", no_argument, NULL, TOTAL_OPTION},
   {"type", required_argument, NULL, 't'},
+  {"without-header", no_argument, NULL, WITHOUT_HEADER_OPTION},
   {"exclude-type", required_argument, NULL, 'x'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
@@ -1204,6 +1206,7 @@ Mandatory arguments to long options are mandatory for 
short options too.\n\
       --sync            invoke sync before getting usage info\n\
   -t, --type=TYPE       limit listing to file systems of type TYPE\n\
   -T, --print-type      print file system type\n\
+      --without-header  omit header line\n\
   -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE\n\
   -v                    (ignored)\n\
 "), stdout);
@@ -1248,6 +1251,9 @@ main (int argc, char **argv)
   /* If true, use the POSIX output format.  */
   bool posix_format = false;

+  /* If true, do not print the header line.  */
+  bool without_header = false;
+
   const char *msg_mut_excl = _("options %s and %s are mutually exclusive");

   while (true)
@@ -1338,6 +1344,9 @@ main (int argc, char **argv)
         case 'v':              /* For SysV compatibility.  */
           /* ignore */
           break;
+        case WITHOUT_HEADER_OPTION:
+          without_header = true;
+          break;
         case 'x':
           add_excluded_fs_type (optarg);
           break;
@@ -1473,7 +1482,9 @@ main (int argc, char **argv)
     sync ();

   get_field_list ();
-  get_header ();
+
+  if (!without_header)
+    get_header ();

   if (optind < argc)
     {
diff --git a/tests/df/without-header.sh b/tests/df/without-header.sh
new file mode 100755
index 0000000..9fcfb88
--- /dev/null
+++ b/tests/df/without-header.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Test behavior of df --without-header.
+
+# Copyright (C) 2012 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ df
+
+# With header.
+df --output=fstype '.' >out || fail=1
+grep '^Type' out || fail=1
+sed '1d' out > exp
+
+# Without header.
+df --without-header --output=fstype '.' >out2 || fail=1
+grep '^Type' out2 && fail=1
+compare exp out2 || { fail=1; cat out2; }
+
+# Ensure that --without-header is mentioned in the usage.
+df --help > out || fail=1
+grep ' --without-header ' out || { fail=1; cat out; }
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index d5bb6f7..5168dac 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -459,6 +459,7 @@ all_tests =                                 \
   tests/df/no-mtab-status.sh                   \
   tests/df/skip-duplicates.sh                  \
   tests/df/skip-rootfs.sh                      \
+  tests/df/without-header.sh                   \
   tests/dd/direct.sh                           \
   tests/dd/misc.sh                             \
   tests/dd/nocache.sh                          \
-- 
1.7.7




reply via email to

[Prev in Thread] Current Thread [Next in Thread]