]> git.cworth.org Git - tar/blob - tests/genfile.c
Imported Upstream version 1.20
[tar] / tests / genfile.c
1 /* Generate a file containing some preset patterns.
2    Print statistics for existing files.
3
4    Copyright (C) 1995, 1996, 1997, 2001, 2003, 2004, 2005, 2006, 2007,
5    2008 Free Software Foundation, Inc.
6
7    François Pinard <pinard@iro.umontreal.ca>, 1995.
8    Sergey Poznyakoff <gray@mirddin.farlep.net>, 2004, 2005, 2006, 2007, 2008.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3, or (at your option)
13    any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software Foundation,
22    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 #include <system.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <argmatch.h>
29 #include <argp.h>
30 #include <argcv.h>
31 #include <getdate.h>
32 #include <utimens.h>
33 #include <inttostr.h>
34 #include <fcntl.h>
35 #include <sys/stat.h>
36 #define obstack_chunk_alloc malloc
37 #define obstack_chunk_free free
38 #include <obstack.h>
39
40 #ifndef EXIT_SUCCESS
41 # define EXIT_SUCCESS 0
42 #endif
43 #ifndef EXIT_FAILURE
44 # define EXIT_FAILURE 1
45 #endif
46
47 #if ! defined SIGCHLD && defined SIGCLD
48 # define SIGCHLD SIGCLD
49 #endif
50
51 enum pattern
52 {
53   DEFAULT_PATTERN,
54   ZEROS_PATTERN
55 };
56
57 /* The name this program was run with. */
58 const char *program_name;
59
60 /* Name of file to generate */
61 static char *file_name;
62
63 /* Name of the file-list file: */
64 static char *files_from;
65 static char filename_terminator = '\n';
66
67 /* Length of file to generate.  */
68 static off_t file_length = 0;
69 static off_t seek_offset = 0;
70
71 /* Pattern to generate.  */
72 static enum pattern pattern = DEFAULT_PATTERN;
73
74 /* Next checkpoint number */
75 size_t checkpoint;
76
77 enum genfile_mode
78   {
79     mode_generate,
80     mode_sparse,
81     mode_stat,
82     mode_exec
83   };
84
85 enum genfile_mode mode = mode_generate;
86
87 #define DEFAULT_STAT_FORMAT \
88   "name,dev,ino,mode,nlink,uid,gid,size,blksize,blocks,atime,mtime,ctime"
89
90 /* Format for --stat option */
91 static char *stat_format = DEFAULT_STAT_FORMAT;
92
93 /* Size of a block for sparse file */
94 size_t block_size = 512;
95
96 /* Block buffer for sparse file */
97 char *buffer;
98
99 /* Number of arguments and argument vector for mode == mode_exec */
100 int exec_argc;
101 char **exec_argv;
102
103 /* Time for --touch option */
104 struct timespec touch_time;
105
106 /* Verbose mode */
107 int verbose;
108
109 const char *argp_program_version = "genfile (" PACKAGE ") " VERSION;
110 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
111 static char doc[] = N_("genfile manipulates data files for GNU paxutils test suite.\n"
112 "OPTIONS are:\n");
113
114 #define OPT_CHECKPOINT 256
115 #define OPT_TOUCH      257
116 #define OPT_APPEND     258
117 #define OPT_TRUNCATE   259
118 #define OPT_EXEC       260
119 #define OPT_DATE       261
120 #define OPT_VERBOSE    262
121 #define OPT_SEEK       263
122
123 static struct argp_option options[] = {
124 #define GRP 0
125   {NULL, 0, NULL, 0,
126    N_("File creation options:"), GRP},
127   {"length", 'l', N_("SIZE"), 0,
128    N_("Create file of the given SIZE"), GRP+1 },
129   {"file", 'f', N_("NAME"), 0,
130    N_("Write to file NAME, instead of standard output"), GRP+1},
131   {"files-from", 'T', N_("FILE"), 0,
132    N_("Read file names from FILE"), GRP+1},
133   {"null", '0', NULL, 0,
134    N_("-T reads null-terminated names"), GRP+1},
135   {"pattern", 'p', N_("PATTERN"), 0,
136    N_("Fill the file with the given PATTERN. PATTERN is 'default' or 'zeros'"),
137    GRP+1 },
138   {"block-size", 'b', N_("SIZE"), 0,
139    N_("Size of a block for sparse file"), GRP+1},
140   {"sparse", 's', NULL, 0,
141    N_("Generate sparse file. Rest of the command line gives the file map."),
142    GRP+1 },
143   {"seek", OPT_SEEK, N_("OFFSET"), 0,
144    N_("Seek to the given offset before writing data"),
145    GRP+1 },
146
147 #undef GRP
148 #define GRP 10
149   {NULL, 0, NULL, 0,
150    N_("File statistics options:"), GRP},
151
152   {"stat", 'S', N_("FORMAT"), OPTION_ARG_OPTIONAL,
153    N_("Print contents of struct stat for each given file. Default FORMAT is: ")
154    DEFAULT_STAT_FORMAT,
155    GRP+1 },
156
157 #undef GRP
158 #define GRP 20
159   {NULL, 0, NULL, 0,
160    N_("Synchronous execution options:"), GRP},
161
162   {"run", 'r', N_("COMMAND"), 0,
163    N_("Execute given COMMAND. Useful with --checkpoint and one of --cut, --append, --touch"),
164    GRP+1 },
165   {"checkpoint", OPT_CHECKPOINT, N_("NUMBER"), 0,
166    N_("Perform given action (see below) upon reaching checkpoint NUMBER"),
167    GRP+1 },
168   {"date", OPT_DATE, N_("STRING"), 0,
169    N_("Set date for next --touch option"),
170    GRP+1 },
171   {"verbose", OPT_VERBOSE, NULL, 0,
172    N_("Display executed checkpoints and exit status of COMMAND"),
173    GRP+1 },
174 #undef GRP
175 #define GRP 30
176   {NULL, 0, NULL, 0,
177    N_("Synchronous execution actions. These are executed when checkpoint number given by --checkpoint option is reached."), GRP},
178
179   {"cut", OPT_TRUNCATE, N_("FILE"), 0,
180    N_("Truncate FILE to the size specified by previous --length option (or 0, if it is not given)"),
181    GRP+1 },
182   {"truncate", 0, NULL, OPTION_ALIAS, NULL, GRP+1 },
183   {"append", OPT_APPEND, N_("FILE"), 0,
184    N_("Append SIZE bytes to FILE. SIZE is given by previous --length option."),
185    GRP+1 },
186   {"touch", OPT_TOUCH, N_("FILE"), 0,
187    N_("Update the access and modification times of FILE"),
188    GRP+1 },
189   {"exec", OPT_EXEC, N_("COMMAND"), 0,
190    N_("Execute COMMAND"),
191    GRP+1 },
192 #undef GRP
193   { NULL, }
194 };
195
196 static char const * const pattern_args[] = { "default", "zeros", 0 };
197 static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
198
199 static int
200 xlat_suffix (off_t *vp, const char *p)
201 {
202   off_t val = *vp;
203
204   if (p[1])
205     return 1;
206   switch (p[0])
207     {
208     case 'g':
209     case 'G':
210       *vp *= 1024;
211
212     case 'm':
213     case 'M':
214       *vp *= 1024;
215
216     case 'k':
217     case 'K':
218       *vp *= 1024;
219       break;
220
221     default:
222       return 1;
223     }
224   return *vp <= val;
225 }
226
227 static off_t
228 get_size (const char *str, int allow_zero)
229 {
230   const char *p;
231   off_t v = 0;
232
233   for (p = str; *p; p++)
234     {
235       int digit = *p - '0';
236       off_t x = v * 10;
237       if (9 < (unsigned) digit)
238         {
239           if (xlat_suffix (&v, p))
240             error (EXIT_FAILURE, 0, _("Invalid size: %s"), str);
241           else
242             break;
243         }
244       else if (x / 10 != v)
245         error (EXIT_FAILURE, 0, _("Number out of allowed range: %s"), str);
246       v = x + digit;
247       if (v < 0)
248         error (EXIT_FAILURE, 0, _("Negative size: %s"), str);
249     }
250   return v;
251 }
252
253 void
254 verify_file (char *file_name)
255 {
256   if (file_name)
257     {
258       struct stat st;
259
260       if (stat (file_name, &st))
261         error (0, errno, _("stat(%s) failed"), file_name);
262
263       if (st.st_size != file_length + seek_offset)
264         {
265           printf ("%lu %lu\n", (unsigned long)st.st_size , (unsigned long)file_length);
266           exit (1);
267         }
268
269       if (mode == mode_sparse && !ST_IS_SPARSE (st))
270         exit (1);
271     }
272 }
273
274 struct action
275 {
276   struct action *next;
277   size_t checkpoint;
278   int action;
279   char *name;
280   off_t size;
281   enum pattern pattern;
282   struct timespec ts;
283 };
284
285 static struct action *action_list;
286
287 void
288 reg_action (int action, char *arg)
289 {
290   struct action *act = xmalloc (sizeof (*act));
291   act->checkpoint = checkpoint;
292   act->action = action;
293   act->pattern = pattern;
294   act->ts = touch_time;
295   act->size = file_length;
296   act->name = arg;
297   act->next = action_list;
298   action_list = act;
299 }
300
301 static error_t
302 parse_opt (int key, char *arg, struct argp_state *state)
303 {
304   switch (key)
305     {
306     case '0':
307       filename_terminator = 0;
308       break;
309
310     case 'f':
311       file_name = arg;
312       break;
313
314     case 'l':
315       file_length = get_size (arg, 1);
316       break;
317
318     case 'p':
319       pattern = XARGMATCH ("--pattern", arg, pattern_args, pattern_types);
320       break;
321
322     case 'b':
323       block_size = get_size (arg, 0);
324       break;
325
326     case 's':
327       mode = mode_sparse;
328       break;
329
330     case 'S':
331       mode = mode_stat;
332       if (arg)
333         stat_format = arg;
334       break;
335
336     case 'r':
337       mode = mode_exec;
338       argcv_get (arg, "", NULL, &exec_argc, &exec_argv);
339       break;
340
341     case 'T':
342       files_from = arg;
343       break;
344
345     case OPT_SEEK:
346       seek_offset = get_size (arg, 0);
347       break;
348
349     case OPT_CHECKPOINT:
350       {
351         char *p;
352
353         checkpoint = strtoul (arg, &p, 0);
354         if (*p)
355           argp_error (state, _("Error parsing number near `%s'"), p);
356       }
357       break;
358
359     case OPT_DATE:
360       if (!get_date (&touch_time, arg, NULL))
361         argp_error (state, _("Unknown date format"));
362       break;
363
364     case OPT_APPEND:
365     case OPT_TRUNCATE:
366     case OPT_TOUCH:
367     case OPT_EXEC:
368       reg_action (key, arg);
369       break;
370
371     case OPT_VERBOSE:
372       verbose++;
373       break;
374
375     default:
376       return ARGP_ERR_UNKNOWN;
377     }
378   return 0;
379 }
380
381 static struct argp argp = {
382   options,
383   parse_opt,
384   N_("[ARGS...]"),
385   doc,
386   NULL,
387   NULL,
388   NULL
389 };
390
391 \f
392 void
393 fill (FILE *fp, off_t length, enum pattern pattern)
394 {
395   off_t i;
396
397   switch (pattern)
398     {
399     case DEFAULT_PATTERN:
400       for (i = 0; i < length; i++)
401         fputc (i & 255, fp);
402       break;
403
404     case ZEROS_PATTERN:
405       for (i = 0; i < length; i++)
406         fputc (0, fp);
407       break;
408     }
409 }
410
411 /* Generate Mode: usual files */
412 static void
413 generate_simple_file (char *filename)
414 {
415   FILE *fp;
416
417   if (filename)
418     {
419       fp = fopen (filename, seek_offset ? "rb+" : "wb");
420       if (!fp)
421         error (EXIT_FAILURE, errno, _("cannot open `%s'"), filename);
422     }
423   else
424     fp = stdout;
425
426   if (fseeko (fp, seek_offset, 0))
427     error (EXIT_FAILURE, errno, "%s", _("cannot seek"));
428
429   fill (fp, file_length, pattern);
430
431   fclose (fp);
432 }
433
434 /* A simplified version of the same function from tar */
435 int
436 read_name_from_file (FILE *fp, struct obstack *stk)
437 {
438   int c;
439   size_t counter = 0;
440
441   for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
442     {
443       if (c == 0)
444         error (EXIT_FAILURE, 0, _("file name contains null character"));
445       obstack_1grow (stk, c);
446       counter++;
447     }
448
449   obstack_1grow (stk, 0);
450
451   return (counter == 0 && c == EOF);
452 }
453
454 void
455 generate_files_from_list ()
456 {
457   FILE *fp = strcmp (files_from, "-") ? fopen (files_from, "rb") : stdin;
458   struct obstack stk;
459
460   if (!fp)
461     error (EXIT_FAILURE, errno, _("cannot open `%s'"), files_from);
462
463   obstack_init (&stk);
464   while (!read_name_from_file (fp, &stk))
465     {
466       char *name = obstack_finish (&stk);
467       generate_simple_file (name);
468       verify_file (name);
469       obstack_free (&stk, name);
470     }
471   fclose (fp);
472   obstack_free (&stk, NULL);
473 }
474
475 \f
476 /* Generate Mode: sparse files */
477
478 static void
479 mkhole (int fd, off_t displ)
480 {
481   if (lseek (fd, displ, SEEK_CUR) == -1)
482     error (EXIT_FAILURE, errno, "lseek");
483   ftruncate (fd, lseek (fd, 0, SEEK_CUR));
484 }
485
486 static void
487 mksparse (int fd, off_t displ, char *marks)
488 {
489   if (lseek (fd, displ, SEEK_CUR) == -1)
490     error (EXIT_FAILURE, errno, "lseek");
491
492   for (; *marks; marks++)
493     {
494       memset (buffer, *marks, block_size);
495       if (write (fd, buffer, block_size) != block_size)
496         error (EXIT_FAILURE, errno, "write");
497     }
498 }
499
500 static void
501 generate_sparse_file (int argc, char **argv)
502 {
503   int i;
504   int fd;
505   int flags = O_CREAT | O_RDWR | O_BINARY;
506
507   if (!file_name)
508     error (EXIT_FAILURE, 0,
509            _("cannot generate sparse files on standard output, use --file option"));
510   if (!seek_offset)
511     flags |= O_TRUNC;
512   fd = open (file_name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
513   if (fd < 0)
514     error (EXIT_FAILURE, errno, _("cannot open `%s'"), file_name);
515
516   buffer = xmalloc (block_size);
517
518   file_length = 0;
519
520   for (i = 0; i < argc; i += 2)
521     {
522       off_t displ = get_size (argv[i], 1);
523       file_length += displ;
524
525       if (i == argc-1)
526         {
527           mkhole (fd, displ);
528           break;
529         }
530       else
531         {
532           file_length += block_size * strlen (argv[i+1]);
533           mksparse (fd, displ, argv[i+1]);
534         }
535     }
536
537   close (fd);
538 }
539
540 \f
541 /* Status Mode */
542
543 void
544 print_time (time_t t)
545 {
546   char buf[20]; /* ccyy-mm-dd HH:MM:SS\0 */
547   strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", gmtime (&t));
548   printf ("%s ", buf);
549 }
550
551 void
552 print_stat (const char *name)
553 {
554   char *fmt, *p;
555   struct stat st;
556   char buf[UINTMAX_STRSIZE_BOUND];
557
558   if (stat (name, &st))
559     {
560       error (0, errno, _("stat(%s) failed"), name);
561       return;
562     }
563
564   fmt = strdup (stat_format);
565   for (p = strtok (fmt, ","); p; )
566     {
567       if (memcmp (p, "st_", 3) == 0)
568         p += 3;
569       if (strcmp (p, "name") == 0)
570         printf ("%s", name);
571       else if (strcmp (p, "dev") == 0)
572         printf ("%lu", (unsigned long) st.st_dev);
573       else if (strcmp (p, "ino") == 0)
574         printf ("%lu", (unsigned long) st.st_ino);
575       else if (strncmp (p, "mode", 4) == 0)
576         {
577           mode_t mask = ~0;
578
579           if (ispunct (p[4]))
580             {
581               char *q;
582
583               mask = strtoul (p + 5, &q, 8);
584               if (*q)
585                 {
586                   printf ("\n");
587                   error (EXIT_FAILURE, 0, _("incorrect mask (near `%s')"), q);
588                 }
589             }
590           else if (p[4])
591             {
592               printf ("\n");
593               error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
594             }
595           printf ("%0o", st.st_mode & mask);
596         }
597       else if (strcmp (p, "nlink") == 0)
598         printf ("%lu", (unsigned long) st.st_nlink);
599       else if (strcmp (p, "uid") == 0)
600         printf ("%ld", (long unsigned) st.st_uid);
601       else if (strcmp (p, "gid") == 0)
602         printf ("%lu", (unsigned long) st.st_gid);
603       else if (strcmp (p, "size") == 0)
604         printf ("%s", umaxtostr (st.st_size, buf));
605       else if (strcmp (p, "blksize") == 0)
606         printf ("%s", umaxtostr (st.st_blksize, buf));
607       else if (strcmp (p, "blocks") == 0)
608         printf ("%s", umaxtostr (st.st_blocks, buf));
609       else if (strcmp (p, "atime") == 0)
610         printf ("%lu", (unsigned long) st.st_atime);
611       else if (strcmp (p, "atimeH") == 0)
612         print_time (st.st_atime);
613       else if (strcmp (p, "mtime") == 0)
614         printf ("%lu", (unsigned long) st.st_mtime);
615       else if (strcmp (p, "mtimeH") == 0)
616         print_time (st.st_mtime);
617       else if (strcmp (p, "ctime") == 0)
618         printf ("%lu", (unsigned long) st.st_ctime);
619       else if (strcmp (p, "ctimeH") == 0)
620         print_time (st.st_ctime);
621       else if (strcmp (p, "sparse") == 0)
622         printf ("%d", ST_IS_SPARSE (st));
623       else
624         {
625           printf ("\n");
626           error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
627         }
628       p = strtok (NULL, ",");
629       if (p)
630         printf (" ");
631     }
632   printf ("\n");
633   free (fmt);
634 }
635
636 \f
637 /* Exec Mode */
638
639 void
640 exec_checkpoint (struct action *p)
641 {
642   if (verbose)
643     printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint);
644   switch (p->action)
645     {
646     case OPT_TOUCH:
647       {
648         struct timespec ts[2];
649
650         ts[0] = ts[1] = p->ts;
651         if (utimens (p->name, ts) != 0)
652           {
653             error (0, errno, _("cannot set time on `%s'"), p->name);
654             break;
655           }
656       }
657       break;
658
659     case OPT_APPEND:
660       {
661         FILE *fp = fopen (p->name, "ab");
662         if (!fp)
663           {
664             error (0, errno, _("cannot open `%s'"), p->name);
665             break;
666           }
667
668         fill (fp, p->size, p->pattern);
669         fclose (fp);
670       }
671       break;
672
673     case OPT_TRUNCATE:
674       {
675         int fd = open (p->name, O_RDWR | O_BINARY);
676         if (fd == -1)
677           {
678             error (0, errno, _("cannot open `%s'"), p->name);
679             break;
680           }
681         ftruncate (fd, p->size);
682         close (fd);
683       }
684       break;
685
686     case OPT_EXEC:
687       system (p->name);
688       break;
689
690     default:
691       abort ();
692     }
693 }
694
695 void
696 process_checkpoint (size_t n)
697 {
698   struct action *p, *prev = NULL;
699
700   for (p = action_list; p; )
701     {
702       struct action *next = p->next;
703
704       if (p->checkpoint <= n)
705         {
706           exec_checkpoint (p);
707           /* Remove the item from the list */
708           if (prev)
709             prev->next = next;
710           else
711             action_list = next;
712           free (p);
713         }
714       else
715         prev = p;
716
717       p = next;
718     }
719 }
720
721 #define CHECKPOINT_TEXT "Write checkpoint"
722
723 void
724 exec_command (void)
725 {
726   int status;
727   pid_t pid;
728   int fd[2];
729   char *p;
730   FILE *fp;
731   char buf[128];
732
733   /* Insert --checkpoint option.
734      FIXME: This assumes that exec_argv does not use traditional tar options
735      (without dash) */
736   exec_argc++;
737   exec_argv = xrealloc (exec_argv, (exec_argc + 1) * sizeof (*exec_argv));
738   memmove (exec_argv+2, exec_argv+1, (exec_argc - 1) * sizeof (*exec_argv));
739   exec_argv[1] = "--checkpoint";
740
741 #ifdef SIGCHLD
742   /* System V fork+wait does not work if SIGCHLD is ignored.  */
743   signal (SIGCHLD, SIG_DFL);
744 #endif
745
746   pipe (fd);
747
748   pid = fork ();
749   if (pid == -1)
750     error (EXIT_FAILURE, errno, "fork");
751
752   if (pid == 0)
753     {
754       /* Child */
755
756       /* Pipe stderr */
757       if (fd[1] != 2)
758         dup2 (fd[1], 2);
759       close (fd[0]);
760
761       /* Make sure POSIX locale is used */
762       setenv ("LC_ALL", "POSIX", 1);
763
764       execvp (exec_argv[0], exec_argv);
765       error (EXIT_FAILURE, errno, "execvp");
766     }
767
768   /* Master */
769   close (fd[1]);
770   fp = fdopen (fd[0], "rb");
771   if (fp == NULL)
772     error (EXIT_FAILURE, errno, "fdopen");
773
774   while ((p = fgets (buf, sizeof buf, fp)))
775     {
776       while (*p && !isspace (*p) && *p != ':')
777         p++;
778
779       if (*p == ':')
780         {
781           for (p++; *p && isspace (*p); p++)
782             ;
783
784           if (*p
785               && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0)
786             {
787               char *end;
788               size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10);
789               if (!(*end && !isspace (*end)))
790                 {
791                   process_checkpoint (n);
792                   continue;
793                 }
794             }
795         }
796       fprintf (stderr, "%s", buf);
797     }
798
799   /* Collect exit status */
800   waitpid (pid, &status, 0);
801
802   if (verbose)
803     {
804       if (WIFEXITED (status))
805         {
806           if (WEXITSTATUS (status) == 0)
807             printf (_("Command exited successfully\n"));
808           else
809             printf (_("Command failed with status %d\n"),
810                     WEXITSTATUS (status));
811         }
812       else if (WIFSIGNALED (status))
813         printf (_("Command terminated on signal %d\n"), WTERMSIG (status));
814       else if (WIFSTOPPED (status))
815         printf (_("Command stopped on signal %d\n"), WSTOPSIG (status));
816 #ifdef WCOREDUMP
817       else if (WCOREDUMP (status))
818         printf (_("Command dumped core\n"));
819 #endif
820       else
821         printf(_("Command terminated\n"));
822     }
823
824   if (WIFEXITED (status))
825     exit (WEXITSTATUS (status));
826   exit (EXIT_FAILURE);
827 }
828
829 int
830 main (int argc, char **argv)
831 {
832   int index;
833
834   program_name = argv[0];
835   setlocale (LC_ALL, "");
836   bindtextdomain (PACKAGE, LOCALEDIR);
837   textdomain (PACKAGE);
838
839   get_date (&touch_time, "now", NULL);
840
841   /* Decode command options.  */
842
843   if (argp_parse (&argp, argc, argv, 0, &index, NULL))
844     exit (EXIT_FAILURE);
845
846   argc -= index;
847   argv += index;
848
849   switch (mode)
850     {
851     case mode_stat:
852       if (argc == 0)
853         error (EXIT_FAILURE, 0, _("--stat requires file names"));
854
855       while (argc--)
856         print_stat (*argv++);
857       break;
858
859     case mode_sparse:
860       generate_sparse_file (argc, argv);
861       verify_file (file_name);
862       break;
863
864     case mode_generate:
865       if (argc)
866         error (EXIT_FAILURE, 0, _("too many arguments"));
867       if (files_from)
868         generate_files_from_list ();
869       else
870         {
871           generate_simple_file (file_name);
872           verify_file (file_name);
873         }
874       break;
875
876     case mode_exec:
877       exec_command ();
878       break;
879
880     default:
881       /* Just in case */
882       abort ();
883     }
884   exit (EXIT_SUCCESS);
885 }