test-suite-pull-tools-directly-in-20020114
[openafs.git] / src / tests / afsdump_dirlist.c
1 /*
2  * CMUCS AFStools
3  * dumpscan - routines for scanning and manipulating AFS volume dumps
4  *
5  * Copyright (c) 1998 Carnegie Mellon University
6  * All Rights Reserved.
7  * 
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28
29 /* afsdump_dirlist.c - List an AFS directory file */
30
31 #include <sys/fcntl.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35
36 #include "dumpscan.h"
37
38 extern int optind;
39 extern char *optarg;
40
41 char *argv0;
42 static char *input_path;
43 static int quiet, verbose, error_count;
44
45 static path_hashinfo phi;
46 static dump_parser dp;
47
48
49 /* Print a usage message and exit */
50 static void usage(int status, char *msg)
51 {
52   if (msg) fprintf(stderr, "%s: %s\n", argv0, msg);
53   fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
54   fprintf(stderr, "  -h     Print this help message\n");
55   fprintf(stderr, "  -q     Quiet mode (don't print errors)\n");
56   fprintf(stderr, "  -v     Verbose mode\n");
57   exit(status);
58 }
59
60
61 /* Parse the command-line options */
62 static void parse_options(int argc, char **argv)
63 {
64   int c;
65
66   /* Set the program name */
67   if (argv0 = strrchr(argv[0], '/')) argv0++;
68   else argv0 = argv[0];
69
70   /* Initialize options */
71   input_path = 0;
72   quiet = verbose = 0;
73
74   /* Initialize other stuff */
75   error_count = 0;
76
77   /* Parse the options */
78   while ((c = getopt(argc, argv, "hqv")) != EOF) {
79     switch (c) {
80       case 'q': quiet        = 1;                         continue;
81       case 'v': verbose      = 1;                         continue;
82       case 'h': usage(0, 0);
83       default:  usage(1, "Invalid option!");
84     }
85   }
86
87   if (quiet && verbose) usage(1, "Can't specify both -q and -v");
88
89   /* Parse non-option arguments */
90   if (argc - optind > 1) usage(1, "Too many arguments!");
91   input_path = (argc == optind) ? "-" : argv[optind];
92 }
93
94
95 /* A callback to count and print errors */
96 static afs_uint32 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
97 {
98   va_list alist;
99
100   error_count++;
101   if (!quiet) {
102     va_start(alist, msg);
103     com_err_va(argv0, code, msg, alist);
104     va_end(alist);
105   }
106 }
107
108
109 /* Main program */
110 void main(int argc, char **argv)
111 {
112   XFILE input_file;
113   afs_uint32 r;
114
115   parse_options(argc, argv);
116   initialize_acfg_error_table();
117   initialize_AVds_error_table();
118   initialize_rxk_error_table();
119   initialize_u_error_table();
120   initialize_vl_error_table();
121   initialize_vols_error_table();
122   initialize_xFil_error_table();
123   r = xfopen(&input_file, O_RDONLY, input_path);
124   if (r) {
125     com_err(argv0, r, "opening %s", input_path);
126     exit(2);
127   }
128
129   memset(&dp, 0, sizeof(dp));
130   dp.cb_error     = my_error_cb;
131   dp.print_flags  = DSPRINT_DIR;
132   if (input_file.is_seekable) dp.flags |= DSFLAG_SEEK;
133
134   r = ParseDirectory(&input_file, &dp, 0, 1);
135   xfclose(&input_file);
136
137   if (verbose && error_count) fprintf(stderr, "*** %d errors\n", error_count);
138   if (r && !quiet) fprintf(stderr, "*** FAILED: %s\n", error_message(r));
139   exit(0);
140 }