2 * blkid.c - User command-line interface for libblkid
4 * Copyright (C) 2001 Andreas Dilger
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
23 #ifdef HAVE_SYS_IOCTL_H
24 #include <sys/ioctl.h>
29 extern int getopt(int argc, char * const argv[], const char *optstring);
34 #define OUTPUT_VALUE_ONLY 0x0001
35 #define OUTPUT_DEVICE_ONLY 0x0002
36 #define OUTPUT_PRETTY_LIST 0x0004
38 #include "ext2fs/ext2fs.h"
39 #include "blkid/blkid.h"
41 static const char *progname = "blkid";
43 static void print_version(FILE *out)
45 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
48 static void usage(int error)
50 FILE *out = error ? stderr : stdout;
54 "usage:\t%s [-c <file>] [-ghlLv] [-o format] "
55 "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n"
56 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
57 "\t-h\tprint this usage message and exit\n"
58 "\t-g\tgarbage collect the blkid cache\n"
59 "\t-s\tshow specified tag(s) (default show all tags)\n"
60 "\t-t\tfind device with a specific token (NAME=value pair)\n"
61 "\t-l\tlookup the the first device with arguments specified by -t\n"
62 "\t-v\tprint version and exit\n"
63 "\t-w\twrite cache to different file (/dev/null = no write)\n"
64 "\tdev\tspecify device(s) to probe (default: all devices)\n",
70 * This function does "safe" printing. It will convert non-printable
71 * ASCII characters using '^' and M- notation.
73 static void safe_print(const char *cp, int len)
86 if ((ch < 32) || (ch == 0x7f)) {
88 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
96 static int get_terminal_width(void)
102 struct winsize w_win;
108 if (ioctl (0, TIOCGSIZE, &t_win) == 0) {
109 width = t_win.ts_cols;
114 if (ioctl (0, TIOCGWINSZ, &w_win) == 0) {
115 width = w_win.ws_col;
119 cp = getenv("COLUMNS");
124 return 4096; /* sanity check */
128 static int pretty_print_word(const char *str, int max_len,
129 int left_len, int overflow_nl)
131 int len = strlen(str) + left_len;
135 if (overflow_nl && len > max_len) {
138 } else if (len > max_len)
142 } while (len++ < max_len);
146 static void pretty_print_line(const char *device, const char *fs_type,
147 const char *label, const char *mtpt,
150 static int device_len = 10, fs_type_len = 7;
151 static int label_len = 8, mtpt_len = 14;
152 static int term_width = -1;
155 if (term_width < 0) {
156 term_width = get_terminal_width();
158 if (term_width > 80) {
172 len = pretty_print_word(device, device_len, 0, 1);
173 len = pretty_print_word(fs_type, fs_type_len, len, 0);
174 len = pretty_print_word(label, label_len, len, 0);
175 len = pretty_print_word(mtpt, mtpt_len, len, 0);
180 static void pretty_print_dev(blkid_dev dev)
182 blkid_tag_iterate iter;
183 const char *type, *value, *devname;
184 const char *uuid = "", *fs_type = "", *label = "";
185 int len, mount_flags;
190 pretty_print_line("device", "fs_type", "label",
191 "mount point", "UUID");
192 for (len=get_terminal_width()-1; len > 0; len--)
198 devname = blkid_dev_devname(dev);
199 if (access(devname, F_OK))
202 /* Get the uuid, label, type */
203 iter = blkid_tag_iterate_begin(dev);
204 while (blkid_tag_next(iter, &type, &value) == 0) {
205 if (!strcmp(type, "UUID"))
207 if (!strcmp(type, "TYPE"))
209 if (!strcmp(type, "LABEL"))
212 blkid_tag_iterate_end(iter);
214 /* Get the mount point */
216 retval = ext2fs_check_mount_point(devname, &mount_flags,
219 if (mount_flags & EXT2_MF_MOUNTED) {
221 strcpy(mtpt, "(mounted, mtpt unknown)");
222 } else if (mount_flags & EXT2_MF_BUSY)
223 strcpy(mtpt, "(in use)");
225 strcpy(mtpt, "(not mounted)");
228 pretty_print_line(devname, fs_type, label, mtpt, uuid);
231 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
233 blkid_tag_iterate iter;
234 const char *type, *value;
240 if (output & OUTPUT_PRETTY_LIST) {
241 pretty_print_dev(dev);
245 if (output & OUTPUT_DEVICE_ONLY) {
246 printf("%s\n", blkid_dev_devname(dev));
250 iter = blkid_tag_iterate_begin(dev);
251 while (blkid_tag_next(iter, &type, &value) == 0) {
252 if (numtag && show) {
253 for (i=0; i < numtag; i++)
254 if (!strcmp(type, show[i]))
259 if (output & OUTPUT_VALUE_ONLY) {
260 fputs(value, stdout);
264 printf("%s: ", blkid_dev_devname(dev));
268 fputs("=\"", stdout);
269 safe_print(value, -1);
270 fputs("\" ", stdout);
273 blkid_tag_iterate_end(iter);
275 if (!first && !(output & OUTPUT_VALUE_ONLY))
279 int main(int argc, char **argv)
281 blkid_cache cache = NULL;
282 char *devices[128] = { NULL, };
283 char *show[128] = { NULL, };
284 char *search_type = NULL, *search_value = NULL;
287 unsigned int numdev = 0, numtag = 0;
291 int output_format = 0;
292 int lookup = 0, gc = 0;
295 while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
306 output_format = OUTPUT_PRETTY_LIST;
312 if (!strcmp(optarg, "value"))
313 output_format = OUTPUT_VALUE_ONLY;
314 else if (!strcmp(optarg, "device"))
315 output_format = OUTPUT_DEVICE_ONLY;
316 else if (!strcmp(optarg, "list"))
317 output_format = OUTPUT_PRETTY_LIST;
318 else if (!strcmp(optarg, "full"))
321 fprintf(stderr, "Invalid output format %s. "
322 "Choose from value,\n\t"
323 "device, list, or full\n", optarg);
328 if (numtag >= sizeof(show) / sizeof(*show)) {
329 fprintf(stderr, "Too many tags specified\n");
332 show[numtag++] = optarg;
336 fprintf(stderr, "Can only search for "
337 "one NAME=value pair\n");
340 if (blkid_parse_tag_string(optarg,
343 fprintf(stderr, "-t needs NAME=value pair\n");
360 while (optind < argc)
361 devices[numdev++] = argv[optind++];
364 print_version(stdout);
368 if (blkid_get_cache(&cache, read) < 0)
373 blkid_gc_cache(cache);
376 if (output_format & OUTPUT_PRETTY_LIST)
377 pretty_print_dev(NULL);
383 fprintf(stderr, "The lookup option requires a "
384 "search type specified using -t\n");
387 /* Load any additional devices not in the cache */
388 for (i = 0; i < numdev; i++)
389 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
391 if ((dev = blkid_find_dev_with_tag(cache, search_type,
393 print_tags(dev, show, numtag, output_format);
396 /* If we didn't specify a single device, show all available devices */
397 } else if (!numdev) {
398 blkid_dev_iterate iter;
401 blkid_probe_all(cache);
403 iter = blkid_dev_iterate_begin(cache);
404 blkid_dev_set_search(iter, search_type, search_value);
405 while (blkid_dev_next(iter, &dev) == 0) {
406 dev = blkid_verify(cache, dev);
409 print_tags(dev, show, numtag, output_format);
412 blkid_dev_iterate_end(iter);
413 /* Add all specified devices to cache (optionally display tags) */
414 } else for (i = 0; i < numdev; i++) {
415 blkid_dev dev = blkid_get_dev(cache, devices[i],
420 !blkid_dev_has_tag(dev, search_type,
423 print_tags(dev, show, numtag, output_format);
431 blkid_put_cache(cache);