blob: 84ae3dd5715e744460bcd8f979ea601645242ae3 [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
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>
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050013#include <stdlib.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050014#ifdef HAVE_GETOPT_H
15#include <getopt.h>
16#else
17extern char *optarg;
18extern int optind;
19#endif
20
21#include "blkid/blkid.h"
22
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050023const char *progname = "blkid";
24
25static void print_version(FILE *out)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050026{
27 fprintf(stderr, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
28}
29
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050030static void usage(int error)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050031{
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
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050049static void print_tags(blkid_dev dev, char *show[], int numtag)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050050{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050051 blkid_tag_iterate iter;
52 const char *type, *value;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050053 int i, first = 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050054
55 if (!dev)
56 return;
57
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050058 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;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050066 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050067 if (first) {
Theodore Ts'o98999c32003-02-16 00:47:07 -050068 printf("%s: ", blkid_dev_devname(dev));
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050069 first = 0;
70 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050071 printf("%s=\"%s\" ", type, value);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050072 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050073 blkid_tag_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050074
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050075 if (!first)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050076 printf("\n");
77}
78
79int main(int argc, char **argv)
80{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050081 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050082 char *devices[128] = { NULL, };
83 char *show[128] = { NULL, };
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050084 char *search_type = NULL, *search_value = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050085 char *read = NULL;
86 char *write = NULL;
87 int numdev = 0, numtag = 0;
88 int version = 0;
89 int err = 4;
90 int i;
91 char c;
92
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050093 while ((c = getopt (argc, argv, "c:f:hps:t:w:v")) != EOF)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050094 switch (c) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050095 case 'c':
96 if (optarg && !*optarg)
97 read = NULL;
98 else
99 read = optarg;
100 if (!write)
101 write = read;
102 break;
103 case 's':
104 if (numtag >= sizeof(show) / sizeof(*show)) {
105 fprintf(stderr, "Too many tags specified\n");
106 usage(err);
107 }
108 show[numtag++] = optarg;
109 break;
110 case 't':
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500111 if (search_type) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500112 fprintf(stderr, "Can only search for "
113 "one NAME=value pair\n");
114 usage(err);
115 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500116 if (blkid_parse_tag_string(optarg,
117 &search_type,
118 &search_value)) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500119 fprintf(stderr, "-t needs NAME=value pair\n");
120 usage(err);
121 }
122 break;
123 case 'v':
124 version = 1;
125 break;
126 case 'w':
127 if (optarg && !*optarg)
128 write = NULL;
129 else
130 write = optarg;
131 break;
132 case 'h':
133 err = 0;
134 default:
135 usage(err);
136 }
137
138 while (optind < argc)
139 devices[numdev++] = argv[optind++];
140
141 if (version) {
142 print_version(stdout);
143 goto exit;
144 }
145
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500146 if (blkid_get_cache(&cache, read) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500147 goto exit;
148
149 err = 2;
150 /* If looking for a specific NAME=value pair, print only that */
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500151 if (search_type) {
152 blkid_dev dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500153
154 /* Load any additional devices not in the cache */
155 for (i = 0; i < numdev; i++)
Theodore Ts'o98999c32003-02-16 00:47:07 -0500156 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500157
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500158 if ((dev = blkid_find_dev_with_tag(cache, search_type,
159 search_value))) {
160 print_tags(dev, show, numtag);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500161 err = 0;
162 }
163 /* If we didn't specify a single device, show all available devices */
164 } else if (!numdev) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500165 blkid_dev_iterate iter;
166 blkid_dev dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500167
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500168 blkid_probe_all(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500169
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500170 iter = blkid_dev_iterate_begin(cache);
171 while (blkid_dev_next(iter, &dev) == 0) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500172 print_tags(dev, show, numtag);
173 err = 0;
174 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500175 blkid_dev_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500176 /* Add all specified devices to cache (optionally display tags) */
177 } else for (i = 0; i < numdev; i++) {
Theodore Ts'o98999c32003-02-16 00:47:07 -0500178 blkid_dev dev = blkid_get_dev(cache, devices[i],
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500179 BLKID_DEV_NORMAL);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500180
181 if (dev) {
182 print_tags(dev, show, numtag);
183 err = 0;
184 }
185 }
186
187exit:
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500188 if (search_type)
189 free(search_type);
190 if (search_value)
191 free(search_value);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500192 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500193 return err;
194}