Whamcloud - gitweb
Fixed up minor documentation issues (man page for badblocks and usage
[tools/e2fsprogs.git] / misc / blkid.c
1 /*
2  * blkid.c - User command-line interface for libblkid
3  *
4  * Copyright (C) 2001 Andreas Dilger
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the
8  * GNU Lesser General Public License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #ifdef HAVE_GETOPT_H
15 #include <getopt.h>
16 #else
17 extern char *optarg;
18 extern int optind;
19 #endif
20
21 #include "blkid/blkid.h"
22
23 const char *progname = "blkid";
24
25 static void print_version(FILE *out)
26 {
27         fprintf(stderr, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
28 }
29
30 static void usage(int error)
31 {
32         FILE *out = error ? stderr : stdout;
33
34         print_version(out);
35         fprintf(out,
36                 "usage:\t%s [-c <file>] [-h] "
37                 "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n"
38                 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
39                 "\t-h\tprint this usage message and exit\n"
40                 "\t-s\tshow specified tag(s) (default show all tags)\n"
41                 "\t-t\tfind device with a specific token (NAME=value pair)\n"
42                 "\t-v\tprint version and exit\n"
43                 "\t-w\twrite cache to different file (/dev/null = no write)\n"
44                 "\tdev\tspecify device(s) to probe (default: all devices)\n",
45                 progname);
46         exit(error);
47 }
48
49 static void print_tags(blkid_dev dev, char *show[], int numtag)
50 {
51         blkid_tag_iterate       iter;
52         const char              *type, *value;
53         int                     i, first = 1, printed_type = 0;
54
55         if (!dev)
56                 return;
57
58         iter = blkid_tag_iterate_begin(dev);
59         while (blkid_tag_next(iter, &type, &value) == 0) {
60                 if (numtag && show) {
61                         for (i=0; i < numtag; i++)
62                                 if (!strcmp(type, show[i]))
63                                         break;
64                         if (i >= numtag)
65                                 continue;
66                 }
67                 if (first) {
68                         printf("%s: ", blkid_devname_name(dev));
69                         first = 0;
70                 }
71                 if (!strcmp(type, "TYPE")) {
72                         if (printed_type)
73                                 return;
74                         printed_type = 1;
75                 }
76                 printf("%s=\"%s\" ", type, value);
77         }
78         blkid_tag_iterate_end(iter);
79
80         if (!first)
81                 printf("\n");
82 }
83
84 int main(int argc, char **argv)
85 {
86         blkid_cache cache = NULL;
87         char *devices[128] = { NULL, };
88         char *show[128] = { NULL, };
89         char *search_type = NULL, *search_value = NULL;
90         char *read = NULL;
91         char *write = NULL;
92         int numdev = 0, numtag = 0;
93         int version = 0;
94         int err = 4;
95         int i;
96         char c;
97
98         while ((c = getopt (argc, argv, "c:d:f:hps:t:w:v")) != EOF)
99                 switch (c) {
100                 case 'd':       /* deprecated */
101                         if (numdev >= sizeof(devices) / sizeof(*devices)) {
102                                 fprintf(stderr,
103                                         "Too many devices specified\n");
104                                 usage(err);
105                         }
106                         devices[numdev++] = optarg;
107                         break;
108                 case 'c':
109                         if (optarg && !*optarg)
110                                 read = NULL;
111                         else
112                                 read = optarg;
113                         if (!write)
114                                 write = read;
115                         break;
116                 case 's':
117                         if (numtag >= sizeof(show) / sizeof(*show)) {
118                                 fprintf(stderr, "Too many tags specified\n");
119                                 usage(err);
120                         }
121                         show[numtag++] = optarg;
122                         break;
123                 case 't':
124                         if (search_type) {
125                                 fprintf(stderr, "Can only search for "
126                                                 "one NAME=value pair\n");
127                                 usage(err);
128                         }
129                         if (blkid_parse_tag_string(optarg,
130                                                    &search_type,
131                                                    &search_value)) {
132                                 fprintf(stderr, "-t needs NAME=value pair\n");
133                                 usage(err);
134                         }
135                         break;
136                 case 'v':
137                         version = 1;
138                         break;
139                 case 'w':
140                         if (optarg && !*optarg)
141                                 write = NULL;
142                         else
143                                 write = optarg;
144                         break;
145                 case 'h':
146                         err = 0;
147                 default:
148                         usage(err);
149                 }
150
151         while (optind < argc)
152                 devices[numdev++] = argv[optind++];
153
154         if (version) {
155                 print_version(stdout);
156                 goto exit;
157         }
158
159         if (blkid_read_cache(&cache, read) < 0)
160                 goto exit;
161
162         err = 2;
163         /* If looking for a specific NAME=value pair, print only that */
164         if (search_type) {
165                 blkid_dev dev;
166
167                 /* Load any additional devices not in the cache */
168                 for (i = 0; i < numdev; i++)
169                         blkid_get_devname(cache, devices[i]);
170
171                 if ((dev = blkid_find_dev_with_tag(cache, search_type,
172                                                    search_value))) {
173                         print_tags(dev, show, numtag);
174                         err = 0;
175                 }
176         /* If we didn't specify a single device, show all available devices */
177         } else if (!numdev) {
178                 blkid_dev_iterate       iter;
179                 blkid_dev               dev;
180
181                 blkid_probe_all(&cache);
182
183                 iter = blkid_dev_iterate_begin(cache);
184                 while (blkid_dev_next(iter, &dev) == 0) {
185                         print_tags(dev, show, numtag);
186                         err = 0;
187                 }
188                 blkid_dev_iterate_end(iter);
189         /* Add all specified devices to cache (optionally display tags) */
190         } else for (i = 0; i < numdev; i++) {
191                 blkid_dev dev = blkid_get_devname(cache, devices[i]);
192
193                 if (dev) {
194                         print_tags(dev, show, numtag);
195                         err = 0;
196                 }
197         }
198
199 exit:
200         if (search_type)
201                 free(search_type);
202         if (search_value)
203                 free(search_value);
204         blkid_save_cache(cache, write);
205         blkid_free_cache(cache);
206         return err;
207 }