blob: 96fffae491c49418af7feb634a6b3489afe2e1f8 [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
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050013#include <stdio.h>
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050014#include <stdlib.h>
Theodore Ts'o3d058022008-07-12 22:06:30 -040015#include <unistd.h>
Theodore Ts'o48e6e812003-07-06 00:36:48 -040016#include <string.h>
Theodore Ts'o3d058022008-07-12 22:06:30 -040017#ifdef HAVE_TERMIOS_H
18#include <termios.h>
19#endif
20#ifdef HAVE_TERMIO_H
21#include <termio.h>
22#endif
23#ifdef HAVE_SYS_IOCTL_H
24#include <sys/ioctl.h>
25#endif
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050026#ifdef HAVE_GETOPT_H
27#include <getopt.h>
28#else
Matthias Andreea6383022006-05-30 16:27:45 +020029extern int getopt(int argc, char * const argv[], const char *optstring);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050030extern char *optarg;
31extern int optind;
32#endif
33
Theodore Ts'o89279982004-03-20 16:30:10 -050034#define OUTPUT_VALUE_ONLY 0x0001
35#define OUTPUT_DEVICE_ONLY 0x0002
Theodore Ts'o3d058022008-07-12 22:06:30 -040036#define OUTPUT_PRETTY_LIST 0x0004
Theodore Ts'o89279982004-03-20 16:30:10 -050037
Theodore Ts'o3d058022008-07-12 22:06:30 -040038#include "ext2fs/ext2fs.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050039#include "blkid/blkid.h"
40
Theodore Ts'o6596c992014-01-10 21:40:28 -050041static const char *progname = "blkid";
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050042
43static void print_version(FILE *out)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050044{
Theodore Ts'o54434922003-12-07 01:28:50 -050045 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050046}
47
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050048static void usage(int error)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050049{
50 FILE *out = error ? stderr : stdout;
51
52 print_version(out);
53 fprintf(out,
Theodore Ts'o3d058022008-07-12 22:06:30 -040054 "usage:\t%s [-c <file>] [-ghlLv] [-o format] "
55 "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050056 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
57 "\t-h\tprint this usage message and exit\n"
Theodore Ts'o46100e32007-05-18 00:16:02 -040058 "\t-g\tgarbage collect the blkid cache\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050059 "\t-s\tshow specified tag(s) (default show all tags)\n"
60 "\t-t\tfind device with a specific token (NAME=value pair)\n"
Theodore Ts'oed6acfa2005-05-07 17:06:27 -040061 "\t-l\tlookup the the first device with arguments specified by -t\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050062 "\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",
65 progname);
66 exit(error);
67}
68
Theodore Ts'of8efcda2007-12-16 12:26:57 -050069/*
70 * This function does "safe" printing. It will convert non-printable
71 * ASCII characters using '^' and M- notation.
72 */
73static void safe_print(const char *cp, int len)
74{
75 unsigned char ch;
76
77 if (len < 0)
78 len = strlen(cp);
79
80 while (len--) {
81 ch = *cp++;
82 if (ch > 128) {
83 fputs("M-", stdout);
84 ch -= 128;
85 }
86 if ((ch < 32) || (ch == 0x7f)) {
87 fputc('^', stdout);
88 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
89 }
90 fputc(ch, stdout);
91 }
92}
93
Theodore Ts'o3d058022008-07-12 22:06:30 -040094static int get_terminal_width(void)
95{
96#ifdef TIOCGSIZE
Theodore Ts'o36f4c402008-07-14 17:49:38 -040097 struct ttysize t_win;
Theodore Ts'o3d058022008-07-12 22:06:30 -040098#endif
99#ifdef TIOCGWINSZ
Theodore Ts'o36f4c402008-07-14 17:49:38 -0400100 struct winsize w_win;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400101#endif
102 const char *cp;
Theodore Ts'o5bb4d0c2014-01-30 16:19:01 -0500103 int width = 80;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400104
105#ifdef TIOCGSIZE
Theodore Ts'o5bb4d0c2014-01-30 16:19:01 -0500106 if (ioctl (0, TIOCGSIZE, &t_win) == 0) {
107 width = t_win.ts_cols;
108 goto got_it;
109 }
Theodore Ts'o3d058022008-07-12 22:06:30 -0400110#endif
111#ifdef TIOCGWINSZ
Theodore Ts'o5bb4d0c2014-01-30 16:19:01 -0500112 if (ioctl (0, TIOCGWINSZ, &w_win) == 0) {
113 width = w_win.ws_col;
114 goto got_it;
115 }
Theodore Ts'o3d058022008-07-12 22:06:30 -0400116#endif
117 cp = getenv("COLUMNS");
118 if (cp)
Theodore Ts'o5bb4d0c2014-01-30 16:19:01 -0500119 width = atoi(cp);
120got_it:
121 if (width > 4096)
122 return 4096; /* sanity check */
123 return width;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400124}
125
126static int pretty_print_word(const char *str, int max_len,
127 int left_len, int overflow_nl)
128{
129 int len = strlen(str) + left_len;
130 int ret = 0;
131
132 fputs(str, stdout);
133 if (overflow_nl && len > max_len) {
134 fputc('\n', stdout);
135 len = 0;
136 } else if (len > max_len)
137 ret = len - max_len;
Theodore Ts'o6596c992014-01-10 21:40:28 -0500138 do {
Theodore Ts'o3d058022008-07-12 22:06:30 -0400139 fputc(' ', stdout);
Theodore Ts'o6596c992014-01-10 21:40:28 -0500140 } while (len++ < max_len);
Theodore Ts'o3d058022008-07-12 22:06:30 -0400141 return ret;
142}
143
144static void pretty_print_line(const char *device, const char *fs_type,
145 const char *label, const char *mtpt,
146 const char *uuid)
147{
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;
151 int len, w;
152
Theodore Ts'o2fc92db2014-01-10 23:25:54 -0500153 if (term_width < 0) {
Theodore Ts'o3d058022008-07-12 22:06:30 -0400154 term_width = get_terminal_width();
155
Theodore Ts'o2fc92db2014-01-10 23:25:54 -0500156 if (term_width > 80) {
157 term_width -= 80;
158 w = term_width / 10;
159 if (w > 8)
160 w = 8;
161 term_width -= 2*w;
162 label_len += w;
163 fs_type_len += w;
164 w = term_width/2;
165 device_len += w;
166 mtpt_len +=w;
167 }
Theodore Ts'o3d058022008-07-12 22:06:30 -0400168 }
169
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);
174 fputs(uuid, stdout);
175 fputc('\n', stdout);
176}
177
178static void pretty_print_dev(blkid_dev dev)
179{
180 blkid_tag_iterate iter;
181 const char *type, *value, *devname;
182 const char *uuid = "", *fs_type = "", *label = "";
Theodore Ts'o3d058022008-07-12 22:06:30 -0400183 int len, mount_flags;
184 char mtpt[80];
185 errcode_t retval;
186
187 if (dev == NULL) {
188 pretty_print_line("device", "fs_type", "label",
189 "mount point", "UUID");
190 for (len=get_terminal_width()-1; len > 0; len--)
191 fputc('-', stdout);
192 fputc('\n', stdout);
193 return;
194 }
195
196 devname = blkid_dev_devname(dev);
197 if (access(devname, F_OK))
198 return;
199
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"))
204 uuid = value;
205 if (!strcmp(type, "TYPE"))
206 fs_type = value;
207 if (!strcmp(type, "LABEL"))
208 label = value;
209 }
210 blkid_tag_iterate_end(iter);
211
212 /* Get the mount point */
213 mtpt[0] = 0;
214 retval = ext2fs_check_mount_point(devname, &mount_flags,
215 mtpt, sizeof(mtpt));
216 if (retval == 0) {
217 if (mount_flags & EXT2_MF_MOUNTED) {
218 if (!mtpt[0])
219 strcpy(mtpt, "(mounted, mtpt unknown)");
220 } else if (mount_flags & EXT2_MF_BUSY)
221 strcpy(mtpt, "(in use)");
222 else
223 strcpy(mtpt, "(not mounted)");
224 }
225
226 pretty_print_line(devname, fs_type, label, mtpt, uuid);
227}
228
Theodore Ts'o89279982004-03-20 16:30:10 -0500229static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500230{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500231 blkid_tag_iterate iter;
232 const char *type, *value;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500233 int i, first = 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500234
235 if (!dev)
236 return;
237
Theodore Ts'o3d058022008-07-12 22:06:30 -0400238 if (output & OUTPUT_PRETTY_LIST) {
239 pretty_print_dev(dev);
240 return;
241 }
242
Theodore Ts'o89279982004-03-20 16:30:10 -0500243 if (output & OUTPUT_DEVICE_ONLY) {
244 printf("%s\n", blkid_dev_devname(dev));
245 return;
246 }
247
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500248 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]))
253 break;
254 if (i >= numtag)
255 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500256 }
Theodore Ts'of8efcda2007-12-16 12:26:57 -0500257 if (output & OUTPUT_VALUE_ONLY) {
258 fputs(value, stdout);
259 fputc('\n', stdout);
260 } else {
261 if (first) {
262 printf("%s: ", blkid_dev_devname(dev));
263 first = 0;
264 }
265 fputs(type, stdout);
266 fputs("=\"", stdout);
267 safe_print(value, -1);
268 fputs("\" ", stdout);
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500269 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500270 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500271 blkid_tag_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500272
Theodore Ts'o89279982004-03-20 16:30:10 -0500273 if (!first && !(output & OUTPUT_VALUE_ONLY))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500274 printf("\n");
275}
276
277int main(int argc, char **argv)
278{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500279 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500280 char *devices[128] = { NULL, };
281 char *show[128] = { NULL, };
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500282 char *search_type = NULL, *search_value = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500283 char *read = NULL;
284 char *write = NULL;
Theodore Ts'o54434922003-12-07 01:28:50 -0500285 unsigned int numdev = 0, numtag = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500286 int version = 0;
287 int err = 4;
Theodore Ts'o54434922003-12-07 01:28:50 -0500288 unsigned int i;
Theodore Ts'o89279982004-03-20 16:30:10 -0500289 int output_format = 0;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400290 int lookup = 0, gc = 0;
Theodore Ts'o0af8c332005-05-06 00:10:52 -0400291 int c;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500292
Theodore Ts'o3d058022008-07-12 22:06:30 -0400293 while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500294 switch (c) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500295 case 'c':
Theodore Ts'oe5397d72014-01-30 18:02:37 -0500296 read = optarg;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500297 if (!write)
298 write = read;
299 break;
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400300 case 'l':
301 lookup++;
302 break;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400303 case 'L':
304 output_format = OUTPUT_PRETTY_LIST;
305 break;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400306 case 'g':
307 gc = 1;
308 break;
Theodore Ts'o89279982004-03-20 16:30:10 -0500309 case 'o':
310 if (!strcmp(optarg, "value"))
311 output_format = OUTPUT_VALUE_ONLY;
312 else if (!strcmp(optarg, "device"))
313 output_format = OUTPUT_DEVICE_ONLY;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400314 else if (!strcmp(optarg, "list"))
315 output_format = OUTPUT_PRETTY_LIST;
Theodore Ts'o89279982004-03-20 16:30:10 -0500316 else if (!strcmp(optarg, "full"))
317 output_format = 0;
318 else {
Theodore Ts'o3d058022008-07-12 22:06:30 -0400319 fprintf(stderr, "Invalid output format %s. "
320 "Choose from value,\n\t"
321 "device, list, or full\n", optarg);
Theodore Ts'o89279982004-03-20 16:30:10 -0500322 exit(1);
323 }
324 break;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500325 case 's':
326 if (numtag >= sizeof(show) / sizeof(*show)) {
327 fprintf(stderr, "Too many tags specified\n");
328 usage(err);
329 }
330 show[numtag++] = optarg;
331 break;
332 case 't':
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500333 if (search_type) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500334 fprintf(stderr, "Can only search for "
335 "one NAME=value pair\n");
336 usage(err);
337 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500338 if (blkid_parse_tag_string(optarg,
339 &search_type,
340 &search_value)) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500341 fprintf(stderr, "-t needs NAME=value pair\n");
342 usage(err);
343 }
344 break;
345 case 'v':
346 version = 1;
347 break;
348 case 'w':
Theodore Ts'oe5397d72014-01-30 18:02:37 -0500349 write = optarg;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500350 break;
351 case 'h':
352 err = 0;
Theodore Ts'o46802ef2014-01-09 15:03:40 -0500353 /* fallthrough */
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500354 default:
355 usage(err);
356 }
357
358 while (optind < argc)
359 devices[numdev++] = argv[optind++];
360
361 if (version) {
362 print_version(stdout);
363 goto exit;
364 }
365
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500366 if (blkid_get_cache(&cache, read) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500367 goto exit;
368
369 err = 2;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400370 if (gc) {
371 blkid_gc_cache(cache);
Theodore Ts'o3d058022008-07-12 22:06:30 -0400372 goto exit;
373 }
374 if (output_format & OUTPUT_PRETTY_LIST)
375 pretty_print_dev(NULL);
376
377 if (lookup) {
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400378 blkid_dev dev;
379
380 if (!search_type) {
381 fprintf(stderr, "The lookup option requires a "
382 "search type specified using -t\n");
383 exit(1);
384 }
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);
388
389 if ((dev = blkid_find_dev_with_tag(cache, search_type,
390 search_value))) {
391 print_tags(dev, show, numtag, output_format);
392 err = 0;
393 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500394 /* If we didn't specify a single device, show all available devices */
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400395 } else if (!numdev) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500396 blkid_dev_iterate iter;
397 blkid_dev dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500398
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500399 blkid_probe_all(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500400
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500401 iter = blkid_dev_iterate_begin(cache);
Theodore Ts'oc37543d2005-05-07 13:32:47 -0400402 blkid_dev_set_search(iter, search_type, search_value);
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500403 while (blkid_dev_next(iter, &dev) == 0) {
Theodore Ts'o18d12962005-01-27 19:51:47 -0500404 dev = blkid_verify(cache, dev);
405 if (!dev)
406 continue;
Theodore Ts'o89279982004-03-20 16:30:10 -0500407 print_tags(dev, show, numtag, output_format);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500408 err = 0;
409 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500410 blkid_dev_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500411 /* Add all specified devices to cache (optionally display tags) */
412 } else for (i = 0; i < numdev; i++) {
Theodore Ts'o98999c32003-02-16 00:47:07 -0500413 blkid_dev dev = blkid_get_dev(cache, devices[i],
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500414 BLKID_DEV_NORMAL);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500415
416 if (dev) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400417 if (search_type &&
418 !blkid_dev_has_tag(dev, search_type,
Theodore Ts'oc37543d2005-05-07 13:32:47 -0400419 search_value))
Theodore Ts'o18d12962005-01-27 19:51:47 -0500420 continue;
Theodore Ts'o89279982004-03-20 16:30:10 -0500421 print_tags(dev, show, numtag, output_format);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500422 err = 0;
423 }
424 }
425
426exit:
Jim Meyering45e338f2009-02-23 18:07:50 +0100427 free(search_type);
428 free(search_value);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500429 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500430 return err;
431}