]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
df: add --output=file to directly output specified arguments
authorPádraig Brady <P@draigBrady.com>
Fri, 22 Nov 2013 02:12:34 +0000 (02:12 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 27 Nov 2013 01:39:11 +0000 (01:39 +0000)
* src/df.c (usage): Document the new 'file' --output field.
(get_dev): Add a new parameter to pass the specified
argument from the command line through.  Use '-' if a
command line parameter is not being used.
* doc/coreutils.texi (df invocation): Describe the new 'file' field.
* tests/df/df-output.sh: Adjust all fields test, and
add a specific test for --output=file.
* NEWS: Mention the new feature.

NEWS
doc/coreutils.texi
src/df.c
tests/df/df-output.sh

diff --git a/NEWS b/NEWS
index b6dab624c1ac11b2ce9e9a5648574b672869a9b2..5ea592c550739d42d3eb1f540f72da35903ad672 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -49,6 +49,9 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** New features
 
+  df --output now accepts a 'file' field, to propagate a specified
+  command line argument through to the output.
+
   du accepts a new option: --inodes to show the number of inodes instead
   of the blocks used.
 
index 67feb125ac3b64741c6d28b7772a084f570ef630..c7258ff5deb961602cbfee03af3393d32c808917 100644 (file)
@@ -11213,6 +11213,8 @@ Number of available blocks.
 @item pcent
 Percentage of @var{used} divided by @var{size}.
 
+@item file
+The file name if specified on the command line.
 @item target
 The mount point.
 @end table
index 949fe2fe3be4d39f6e139addf0f5d0ab8631ddad..07a468bc82fa3dfdee3bb2500232f94fbd4fa442 100644 (file)
--- a/src/df.c
+++ b/src/df.c
@@ -143,7 +143,8 @@ typedef enum
   IUSED_FIELD,  /* inodes used */
   IAVAIL_FIELD, /* inodes available */
   IPCENT_FIELD, /* inodes used in percent */
-  TARGET_FIELD  /* mount point */
+  TARGET_FIELD, /* mount point */
+  FILE_FIELD    /* specified file name */
 } display_field_t;
 
 /* Flag if a field contains a block, an inode or another value.  */
@@ -199,11 +200,15 @@ static struct field_data_t field_data[] = {
     "ipcent", INODE_FLD, N_("IUse%"),       4, MBS_ALIGN_RIGHT, false },
 
   [TARGET_FIELD] = { TARGET_FIELD,
-    "target", OTHER_FLD, N_("Mounted on"),  0, MBS_ALIGN_LEFT,  false }
+    "target", OTHER_FLD, N_("Mounted on"),  0, MBS_ALIGN_LEFT,  false },
+
+  [FILE_FIELD] = { FILE_FIELD,
+    "file",   OTHER_FLD, N_("File"),        0, MBS_ALIGN_LEFT,  false }
 };
 
 static char const *all_args_string =
-  "source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,target";
+  "source,fstype,itotal,iused,iavail,ipcent,size,"
+  "used,avail,pcent,file,target";
 
 /* Storage for the definition of output columns.  */
 static struct field_data_t **columns;
@@ -403,6 +408,7 @@ decode_output_arg (char const *arg)
         case IAVAIL_FIELD:
         case IPCENT_FIELD:
         case TARGET_FIELD:
+        case FILE_FIELD:
           alloc_field (field, NULL);
           break;
 
@@ -837,7 +843,7 @@ add_to_grand_total (struct field_values_t *bv, struct field_values_t *iv)
    when df is invoked with no non-option argument.  See below for details.  */
 
 static void
-get_dev (char const *disk, char const *mount_point,
+get_dev (char const *disk, char const *mount_point, char const* file,
          char const *stat_file, char const *fstype,
          bool me_dummy, bool me_remote,
          const struct fs_usage *force_fsu,
@@ -880,6 +886,9 @@ get_dev (char const *disk, char const *mount_point,
   if (! disk)
     disk = "-";                        /* unknown */
 
+  if (! file)
+    file = "-";                        /* unspecified */
+
   char *dev_name = xstrdup (disk);
   char *resolved_dev;
 
@@ -1011,6 +1020,10 @@ get_dev (char const *disk, char const *mount_point,
             break;
           }
 
+        case FILE_FIELD:
+          cell = xstrdup (file);
+          break;
+
         case TARGET_FIELD:
 #ifdef HIDE_AUTOMOUNT_PREFIX
           /* Don't print the first directory name in MOUNT_POINT if it's an
@@ -1054,7 +1067,7 @@ get_disk (char const *disk)
 
   if (best_match)
     {
-      get_dev (best_match->me_devname, best_match->me_mountdir, NULL,
+      get_dev (best_match->me_devname, best_match->me_mountdir, disk, NULL,
                best_match->me_type, best_match->me_dummy,
                best_match->me_remote, NULL, false);
       return true;
@@ -1142,7 +1155,7 @@ get_point (const char *point, const struct stat *statp)
       }
 
   if (best_match)
-    get_dev (best_match->me_devname, best_match->me_mountdir, point,
+    get_dev (best_match->me_devname, best_match->me_mountdir, point, point,
              best_match->me_type, best_match->me_dummy, best_match->me_remote,
              NULL, false);
   else
@@ -1155,7 +1168,7 @@ get_point (const char *point, const struct stat *statp)
       char *mp = find_mount_point (point, statp);
       if (mp)
         {
-          get_dev (NULL, mp, NULL, NULL, false, false, NULL, false);
+          get_dev (NULL, mp, point, NULL, NULL, false, false, NULL, false);
           free (mp);
         }
     }
@@ -1186,7 +1199,7 @@ get_all_entries (void)
     filter_mount_list ();
 
   for (me = mount_list; me; me = me->me_next)
-    get_dev (me->me_devname, me->me_mountdir, NULL, me->me_type,
+    get_dev (me->me_devname, me->me_mountdir, NULL, NULL, me->me_type,
              me->me_dummy, me->me_remote, NULL, true);
 }
 
@@ -1265,7 +1278,7 @@ or all file systems by default.\n\
       fputs (_("\n\
 FIELD_LIST is a comma-separated list of columns to be included.  Valid\n\
 field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',\n\
-'size', 'used', 'avail', 'pcent' and 'target' (see info page).\n\
+'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).\n\
 "), stdout);
       emit_ancillary_info ();
     }
@@ -1544,7 +1557,7 @@ main (int argc, char **argv)
       if (print_grand_total)
         get_dev ("total",
                  (field_data[SOURCE_FIELD].used ? "-" : "total"),
-                 NULL, NULL, false, false, &grand_fsu, false);
+                 NULL, NULL, NULL, false, false, &grand_fsu, false);
 
       print_table ();
     }
index a10f2706be2d08c526ff478f5e9b6e2528b0da11..83cdc93ee8626107416ae22dbba03838401b1665 100755 (executable)
@@ -67,11 +67,11 @@ compare exp out || fail=1
 # that --o (without argument) is identical to the full list.
 
 cat <<\EOF > exp || framework_failure_
-Filesystem Type Inodes IUsed IFree IUse% Size Used Avail Use% Mounted on
+Filesystem Type Inodes IUsed IFree IUse% Size Used Avail Use% File Mounted on
 EOF
 
 df -h --o=source,fstype,itotal,iused,iavail,ipcent \
- --o=size,used,avail,pcent,target '.' >out || fail=1
+ --o=size,used,avail,pcent,file,target '.' >out || fail=1
 sed -e '1 {
           s/ [ ]*/ /g
           q
@@ -127,4 +127,14 @@ compare exp out2 || fail=1
 df --help > out || fail=1
 grep ' --output' out >/dev/null || { fail=1; cat out; }
 
+# Ensure that the FILE field contains the argument.
+cat <<\EOF > exp || framework_failure_
+.
+exp
+EOF
+
+df --output=file '.' exp >out || fail=1
+sed '1d' out > out2
+compare exp out2 || fail=1
+
 Exit $fail