]> Dogcows Code - chaz/tar/commitdiff
(options, parse_opt): Allow for optional argument to
authorSergey Poznyakoff <gray@gnu.org.ua>
Mon, 12 Jun 2006 13:20:10 +0000 (13:20 +0000)
committerSergey Poznyakoff <gray@gnu.org.ua>
Mon, 12 Jun 2006 13:20:10 +0000 (13:20 +0000)
the --totals option, which specifies a signal upon delivery of which
the statistics must be output.
(main): Call print_total_stats if total_option is set.

src/tar.c

index 0375c41bbe3946a4547200a2918aefd2cf2898e5..88aaaff007cf0f73c1baf29100cfd9d24329124d 100644 (file)
--- a/src/tar.c
+++ b/src/tar.c
@@ -657,8 +657,11 @@ static struct argp_option options[] = {
    GRID+1 },
   {"check-links", 'l', 0, 0,
    N_("print a message if not all links are dumped"), GRID+1 },
-  {"totals", TOTALS_OPTION, 0, 0,
-   N_("print total bytes written while creating archive"), GRID+1 },
+  {"totals", TOTALS_OPTION, N_("SIGNAL"), OPTION_ARG_OPTIONAL,
+   N_("print total bytes after processing the archive; "
+      "with an argument - print total bytes when this SIGNAL is delivered; "
+      "Allowed signals are: SIGHUP, SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; "
+      "the names without SIG prefix are also accepted"), GRID+1 },
   {"utc", UTC_OPTION, 0, 0,
    N_("print file modification dates in UTC"), GRID+1 },
   {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
@@ -814,7 +817,62 @@ set_use_compress_program_option (const char *string)
 
   use_compress_program_option = string;
 }
+\f
+static RETSIGTYPE
+sigstat (int signo)
+{
+  compute_duration ();
+  print_total_stats ();
+#ifndef HAVE_SIGACTION
+  signal (signo, sigstat);
+#endif
+}
+
+static void
+stat_on_signal (int signo)
+{
+#ifdef HAVE_SIGACTION
+  struct sigaction act;
+  act.sa_handler = sigstat;
+  sigemptyset (&act.sa_mask);
+  act.sa_flags = 0;
+  sigaction (signo, &act, NULL);
+#else
+  signal (signo, sigstat);
+#endif
+}
+
+void
+set_stat_signal (const char *name)
+{
+  static struct sigtab
+  {
+    char *name;
+    int signo;
+  } sigtab[] = {
+    { "SIGUSR1", SIGUSR1 },
+    { "USR1", SIGUSR1 },
+    { "SIGUSR2", SIGUSR2 },
+    { "USR2", SIGUSR2 },
+    { "SIGHUP", SIGHUP },
+    { "HUP", SIGHUP },
+    { "SIGINT", SIGINT },
+    { "INT", SIGINT },
+    { "SIGQUIT", SIGQUIT },
+    { "QUIT", SIGQUIT }
+  };
+  struct sigtab *p;
+  
+  for (p = sigtab; p < sigtab + sizeof (sigtab) / sizeof (sigtab[0]); p++)
+    if (strcmp (p->name, name) == 0)
+      {
+       stat_on_signal (p->signo);
+       return;
+      }
+  FATAL_ERROR ((0, 0, _("Unknown signal name: %s"), name));
+}
 
+\f
 static volatile int _argp_hang;
 
 enum read_file_list_state  /* Result of reading file name from the list file */
@@ -1576,7 +1634,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
 
     case TOTALS_OPTION:
-      totals_option = true;
+      if (arg)
+       set_stat_signal (arg);
+      else
+       totals_option = true;
       break;
 
     case TRANSFORM_OPTION:
@@ -2150,8 +2211,6 @@ main (int argc, char **argv)
 
     case CREATE_SUBCOMMAND:
       create_archive ();
-      if (totals_option)
-       print_total_written ();
       break;
 
     case EXTRACT_SUBCOMMAND:
@@ -2174,6 +2233,9 @@ main (int argc, char **argv)
       break;
     }
 
+  if (totals_option)
+    print_total_stats ();
+
   if (check_links_option)
     check_links ();
 
This page took 0.02661 seconds and 4 git commands to generate.