]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/blkid.c
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 */
94 static int get_terminal_width(void)
100 struct winsize w_win
;
106 if (ioctl (0, TIOCGSIZE
, &t_win
) == 0) {
107 width
= t_win
.ts_cols
;
112 if (ioctl (0, TIOCGWINSZ
, &w_win
) == 0) {
113 width
= w_win
.ws_col
;
117 cp
= getenv("COLUMNS");
122 return 4096; /* sanity check */
126 static int pretty_print_word(const char *str
, int max_len
,
127 int left_len
, int overflow_nl
)
129 int len
= strlen(str
) + left_len
;
133 if (overflow_nl
&& len
> max_len
) {
136 } else if (len
> max_len
)
140 } while (len
++ < max_len
);
144 static void pretty_print_line(const char *device
, const char *fs_type
,
145 const char *label
, const char *mtpt
,
148 static int device_len
= 10, fs_type_len
= 7;
149 static int label_len
= 8, mtpt_len
= 14;
150 static int term_width
= -1;
153 if (term_width
< 0) {
154 term_width
= get_terminal_width();
156 if (term_width
> 80) {
170 len
= pretty_print_word(device
, device_len
, 0, 1);
171 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
172 len
= pretty_print_word(label
, label_len
, len
, 0);
173 len
= pretty_print_word(mtpt
, mtpt_len
, len
, 0);
178 static void pretty_print_dev(blkid_dev dev
)
180 blkid_tag_iterate iter
;
181 const char *type
, *value
, *devname
;
182 const char *uuid
= "", *fs_type
= "", *label
= "";
183 int len
, mount_flags
;
188 pretty_print_line("device", "fs_type", "label",
189 "mount point", "UUID");
190 for (len
=get_terminal_width()-1; len
> 0; len
--)
196 devname
= blkid_dev_devname(dev
);
197 if (access(devname
, F_OK
))
200 /* Get the uuid, label, type */
201 iter
= blkid_tag_iterate_begin(dev
);
202 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
203 if (!strcmp(type
, "UUID"))
205 if (!strcmp(type
, "TYPE"))
207 if (!strcmp(type
, "LABEL"))
210 blkid_tag_iterate_end(iter
);
212 /* Get the mount point */
214 retval
= ext2fs_check_mount_point(devname
, &mount_flags
,
217 if (mount_flags
& EXT2_MF_MOUNTED
) {
219 strcpy(mtpt
, "(mounted, mtpt unknown)");
220 } else if (mount_flags
& EXT2_MF_BUSY
)
221 strcpy(mtpt
, "(in use)");
223 strcpy(mtpt
, "(not mounted)");
226 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
229 static void print_tags(blkid_dev dev
, char *show
[], int numtag
, int output
)
231 blkid_tag_iterate iter
;
232 const char *type
, *value
;
238 if (output
& OUTPUT_PRETTY_LIST
) {
239 pretty_print_dev(dev
);
243 if (output
& OUTPUT_DEVICE_ONLY
) {
244 printf("%s\n", blkid_dev_devname(dev
));
248 iter
= blkid_tag_iterate_begin(dev
);
249 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
250 if (numtag
&& show
) {
251 for (i
=0; i
< numtag
; i
++)
252 if (!strcmp(type
, show
[i
]))
257 if (output
& OUTPUT_VALUE_ONLY
) {
258 fputs(value
, stdout
);
262 printf("%s: ", blkid_dev_devname(dev
));
266 fputs("=\"", stdout
);
267 safe_print(value
, -1);
268 fputs("\" ", stdout
);
271 blkid_tag_iterate_end(iter
);
273 if (!first
&& !(output
& OUTPUT_VALUE_ONLY
))
277 int main(int argc
, char **argv
)
279 blkid_cache cache
= NULL
;
280 char *devices
[128] = { NULL
, };
281 char *show
[128] = { NULL
, };
282 char *search_type
= NULL
, *search_value
= NULL
;
285 unsigned int numdev
= 0, numtag
= 0;
289 int output_format
= 0;
290 int lookup
= 0, gc
= 0;
293 while ((c
= getopt (argc
, argv
, "c:f:ghlLo:s:t:w:v")) != EOF
)
304 output_format
= OUTPUT_PRETTY_LIST
;
310 if (!strcmp(optarg
, "value"))
311 output_format
= OUTPUT_VALUE_ONLY
;
312 else if (!strcmp(optarg
, "device"))
313 output_format
= OUTPUT_DEVICE_ONLY
;
314 else if (!strcmp(optarg
, "list"))
315 output_format
= OUTPUT_PRETTY_LIST
;
316 else if (!strcmp(optarg
, "full"))
319 fprintf(stderr
, "Invalid output format %s. "
320 "Choose from value,\n\t"
321 "device, list, or full\n", optarg
);
326 if (numtag
>= sizeof(show
) / sizeof(*show
)) {
327 fprintf(stderr
, "Too many tags specified\n");
330 show
[numtag
++] = optarg
;
334 fprintf(stderr
, "Can only search for "
335 "one NAME=value pair\n");
338 if (blkid_parse_tag_string(optarg
,
341 fprintf(stderr
, "-t needs NAME=value pair\n");
358 while (optind
< argc
)
359 devices
[numdev
++] = argv
[optind
++];
362 print_version(stdout
);
366 if (blkid_get_cache(&cache
, read
) < 0)
371 blkid_gc_cache(cache
);
374 if (output_format
& OUTPUT_PRETTY_LIST
)
375 pretty_print_dev(NULL
);
381 fprintf(stderr
, "The lookup option requires a "
382 "search type specified using -t\n");
385 /* Load any additional devices not in the cache */
386 for (i
= 0; i
< numdev
; i
++)
387 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
389 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
391 print_tags(dev
, show
, numtag
, output_format
);
394 /* If we didn't specify a single device, show all available devices */
395 } else if (!numdev
) {
396 blkid_dev_iterate iter
;
399 blkid_probe_all(cache
);
401 iter
= blkid_dev_iterate_begin(cache
);
402 blkid_dev_set_search(iter
, search_type
, search_value
);
403 while (blkid_dev_next(iter
, &dev
) == 0) {
404 dev
= blkid_verify(cache
, dev
);
407 print_tags(dev
, show
, numtag
, output_format
);
410 blkid_dev_iterate_end(iter
);
411 /* Add all specified devices to cache (optionally display tags) */
412 } else for (i
= 0; i
< numdev
; i
++) {
413 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
418 !blkid_dev_has_tag(dev
, search_type
,
421 print_tags(dev
, show
, numtag
, output_format
);
429 blkid_put_cache(cache
);