blob: 388abad018abd92a59dc19a8371b3ded3e8e3d21 [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'o3d058022008-07-12 22:06:30 -040014#include <unistd.h>
Theodore Ts'o48e6e812003-07-06 00:36:48 -040015#include <string.h>
Theodore Ts'o3d058022008-07-12 22:06:30 -040016#ifdef HAVE_TERMIOS_H
17#include <termios.h>
18#endif
19#ifdef HAVE_TERMIO_H
20#include <termio.h>
21#endif
22#ifdef HAVE_SYS_IOCTL_H
23#include <sys/ioctl.h>
24#endif
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050025#ifdef HAVE_GETOPT_H
26#include <getopt.h>
27#else
Matthias Andreea6383022006-05-30 16:27:45 +020028extern int getopt(int argc, char * const argv[], const char *optstring);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050029extern char *optarg;
30extern int optind;
31#endif
32
Theodore Ts'o89279982004-03-20 16:30:10 -050033#define OUTPUT_VALUE_ONLY 0x0001
34#define OUTPUT_DEVICE_ONLY 0x0002
Theodore Ts'o3d058022008-07-12 22:06:30 -040035#define OUTPUT_PRETTY_LIST 0x0004
Theodore Ts'o89279982004-03-20 16:30:10 -050036
Theodore Ts'o3d058022008-07-12 22:06:30 -040037#include "ext2fs/ext2fs.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050038#include "blkid/blkid.h"
39
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050040const char *progname = "blkid";
41
42static void print_version(FILE *out)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050043{
Theodore Ts'o54434922003-12-07 01:28:50 -050044 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050045}
46
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050047static void usage(int error)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050048{
49 FILE *out = error ? stderr : stdout;
50
51 print_version(out);
52 fprintf(out,
Theodore Ts'o3d058022008-07-12 22:06:30 -040053 "usage:\t%s [-c <file>] [-ghlLv] [-o format] "
54 "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050055 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
56 "\t-h\tprint this usage message and exit\n"
Theodore Ts'o46100e32007-05-18 00:16:02 -040057 "\t-g\tgarbage collect the blkid cache\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050058 "\t-s\tshow specified tag(s) (default show all tags)\n"
59 "\t-t\tfind device with a specific token (NAME=value pair)\n"
Theodore Ts'oed6acfa2005-05-07 17:06:27 -040060 "\t-l\tlookup the the first device with arguments specified by -t\n"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050061 "\t-v\tprint version and exit\n"
62 "\t-w\twrite cache to different file (/dev/null = no write)\n"
63 "\tdev\tspecify device(s) to probe (default: all devices)\n",
64 progname);
65 exit(error);
66}
67
Theodore Ts'of8efcda2007-12-16 12:26:57 -050068/*
69 * This function does "safe" printing. It will convert non-printable
70 * ASCII characters using '^' and M- notation.
71 */
72static void safe_print(const char *cp, int len)
73{
74 unsigned char ch;
75
76 if (len < 0)
77 len = strlen(cp);
78
79 while (len--) {
80 ch = *cp++;
81 if (ch > 128) {
82 fputs("M-", stdout);
83 ch -= 128;
84 }
85 if ((ch < 32) || (ch == 0x7f)) {
86 fputc('^', stdout);
87 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
88 }
89 fputc(ch, stdout);
90 }
91}
92
Theodore Ts'o3d058022008-07-12 22:06:30 -040093static int get_terminal_width(void)
94{
95#ifdef TIOCGSIZE
Theodore Ts'o36f4c402008-07-14 17:49:38 -040096 struct ttysize t_win;
Theodore Ts'o3d058022008-07-12 22:06:30 -040097#endif
98#ifdef TIOCGWINSZ
Theodore Ts'o36f4c402008-07-14 17:49:38 -040099 struct winsize w_win;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400100#endif
101 const char *cp;
102
103#ifdef TIOCGSIZE
Theodore Ts'o36f4c402008-07-14 17:49:38 -0400104 if (ioctl (0, TIOCGSIZE, &t_win) == 0)
105 return (t_win.ts_cols);
Theodore Ts'o3d058022008-07-12 22:06:30 -0400106#endif
107#ifdef TIOCGWINSZ
Theodore Ts'o36f4c402008-07-14 17:49:38 -0400108 if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
109 return (w_win.ws_col);
Theodore Ts'o3d058022008-07-12 22:06:30 -0400110#endif
111 cp = getenv("COLUMNS");
112 if (cp)
113 return strtol(cp, NULL, 10);
114 return 80;
115}
116
117static int pretty_print_word(const char *str, int max_len,
118 int left_len, int overflow_nl)
119{
120 int len = strlen(str) + left_len;
121 int ret = 0;
122
123 fputs(str, stdout);
124 if (overflow_nl && len > max_len) {
125 fputc('\n', stdout);
126 len = 0;
127 } else if (len > max_len)
128 ret = len - max_len;
129 do
130 fputc(' ', stdout);
131 while (len++ < max_len);
132 return ret;
133}
134
135static void pretty_print_line(const char *device, const char *fs_type,
136 const char *label, const char *mtpt,
137 const char *uuid)
138{
139 static int device_len = 10, fs_type_len = 7;
140 static int label_len = 8, mtpt_len = 14;
141 static int term_width = -1;
142 int len, w;
143
144 if (term_width < 0)
145 term_width = get_terminal_width();
146
147 if (term_width > 80) {
148 term_width -= 80;
149 w = term_width / 10;
150 if (w > 8)
151 w = 8;
152 term_width -= 2*w;
153 label_len += w;
154 fs_type_len += w;
155 w = term_width/2;
156 device_len += w;
157 mtpt_len +=w;
158 }
159
160 len = pretty_print_word(device, device_len, 0, 1);
161 len = pretty_print_word(fs_type, fs_type_len, len, 0);
162 len = pretty_print_word(label, label_len, len, 0);
163 len = pretty_print_word(mtpt, mtpt_len, len, 0);
164 fputs(uuid, stdout);
165 fputc('\n', stdout);
166}
167
168static void pretty_print_dev(blkid_dev dev)
169{
170 blkid_tag_iterate iter;
171 const char *type, *value, *devname;
172 const char *uuid = "", *fs_type = "", *label = "";
Theodore Ts'o3d058022008-07-12 22:06:30 -0400173 int len, mount_flags;
174 char mtpt[80];
175 errcode_t retval;
176
177 if (dev == NULL) {
178 pretty_print_line("device", "fs_type", "label",
179 "mount point", "UUID");
180 for (len=get_terminal_width()-1; len > 0; len--)
181 fputc('-', stdout);
182 fputc('\n', stdout);
183 return;
184 }
185
186 devname = blkid_dev_devname(dev);
187 if (access(devname, F_OK))
188 return;
189
190 /* Get the uuid, label, type */
191 iter = blkid_tag_iterate_begin(dev);
192 while (blkid_tag_next(iter, &type, &value) == 0) {
193 if (!strcmp(type, "UUID"))
194 uuid = value;
195 if (!strcmp(type, "TYPE"))
196 fs_type = value;
197 if (!strcmp(type, "LABEL"))
198 label = value;
199 }
200 blkid_tag_iterate_end(iter);
201
202 /* Get the mount point */
203 mtpt[0] = 0;
204 retval = ext2fs_check_mount_point(devname, &mount_flags,
205 mtpt, sizeof(mtpt));
206 if (retval == 0) {
207 if (mount_flags & EXT2_MF_MOUNTED) {
208 if (!mtpt[0])
209 strcpy(mtpt, "(mounted, mtpt unknown)");
210 } else if (mount_flags & EXT2_MF_BUSY)
211 strcpy(mtpt, "(in use)");
212 else
213 strcpy(mtpt, "(not mounted)");
214 }
215
216 pretty_print_line(devname, fs_type, label, mtpt, uuid);
217}
218
Theodore Ts'o89279982004-03-20 16:30:10 -0500219static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500220{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500221 blkid_tag_iterate iter;
222 const char *type, *value;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500223 int i, first = 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500224
225 if (!dev)
226 return;
227
Theodore Ts'o3d058022008-07-12 22:06:30 -0400228 if (output & OUTPUT_PRETTY_LIST) {
229 pretty_print_dev(dev);
230 return;
231 }
232
Theodore Ts'o89279982004-03-20 16:30:10 -0500233 if (output & OUTPUT_DEVICE_ONLY) {
234 printf("%s\n", blkid_dev_devname(dev));
235 return;
236 }
237
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500238 iter = blkid_tag_iterate_begin(dev);
239 while (blkid_tag_next(iter, &type, &value) == 0) {
240 if (numtag && show) {
241 for (i=0; i < numtag; i++)
242 if (!strcmp(type, show[i]))
243 break;
244 if (i >= numtag)
245 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500246 }
Theodore Ts'of8efcda2007-12-16 12:26:57 -0500247 if (output & OUTPUT_VALUE_ONLY) {
248 fputs(value, stdout);
249 fputc('\n', stdout);
250 } else {
251 if (first) {
252 printf("%s: ", blkid_dev_devname(dev));
253 first = 0;
254 }
255 fputs(type, stdout);
256 fputs("=\"", stdout);
257 safe_print(value, -1);
258 fputs("\" ", stdout);
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500259 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500260 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500261 blkid_tag_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500262
Theodore Ts'o89279982004-03-20 16:30:10 -0500263 if (!first && !(output & OUTPUT_VALUE_ONLY))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500264 printf("\n");
265}
266
267int main(int argc, char **argv)
268{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500269 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500270 char *devices[128] = { NULL, };
271 char *show[128] = { NULL, };
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500272 char *search_type = NULL, *search_value = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500273 char *read = NULL;
274 char *write = NULL;
Theodore Ts'o54434922003-12-07 01:28:50 -0500275 unsigned int numdev = 0, numtag = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500276 int version = 0;
277 int err = 4;
Theodore Ts'o54434922003-12-07 01:28:50 -0500278 unsigned int i;
Theodore Ts'o89279982004-03-20 16:30:10 -0500279 int output_format = 0;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400280 int lookup = 0, gc = 0;
Theodore Ts'o0af8c332005-05-06 00:10:52 -0400281 int c;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500282
Theodore Ts'o3d058022008-07-12 22:06:30 -0400283 while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500284 switch (c) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500285 case 'c':
286 if (optarg && !*optarg)
287 read = NULL;
288 else
289 read = optarg;
290 if (!write)
291 write = read;
292 break;
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400293 case 'l':
294 lookup++;
295 break;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400296 case 'L':
297 output_format = OUTPUT_PRETTY_LIST;
298 break;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400299 case 'g':
300 gc = 1;
301 break;
Theodore Ts'o89279982004-03-20 16:30:10 -0500302 case 'o':
303 if (!strcmp(optarg, "value"))
304 output_format = OUTPUT_VALUE_ONLY;
305 else if (!strcmp(optarg, "device"))
306 output_format = OUTPUT_DEVICE_ONLY;
Theodore Ts'o3d058022008-07-12 22:06:30 -0400307 else if (!strcmp(optarg, "list"))
308 output_format = OUTPUT_PRETTY_LIST;
Theodore Ts'o89279982004-03-20 16:30:10 -0500309 else if (!strcmp(optarg, "full"))
310 output_format = 0;
311 else {
Theodore Ts'o3d058022008-07-12 22:06:30 -0400312 fprintf(stderr, "Invalid output format %s. "
313 "Choose from value,\n\t"
314 "device, list, or full\n", optarg);
Theodore Ts'o89279982004-03-20 16:30:10 -0500315 exit(1);
316 }
317 break;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500318 case 's':
319 if (numtag >= sizeof(show) / sizeof(*show)) {
320 fprintf(stderr, "Too many tags specified\n");
321 usage(err);
322 }
323 show[numtag++] = optarg;
324 break;
325 case 't':
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500326 if (search_type) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500327 fprintf(stderr, "Can only search for "
328 "one NAME=value pair\n");
329 usage(err);
330 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500331 if (blkid_parse_tag_string(optarg,
332 &search_type,
333 &search_value)) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500334 fprintf(stderr, "-t needs NAME=value pair\n");
335 usage(err);
336 }
337 break;
338 case 'v':
339 version = 1;
340 break;
341 case 'w':
342 if (optarg && !*optarg)
343 write = NULL;
344 else
345 write = optarg;
346 break;
347 case 'h':
348 err = 0;
349 default:
350 usage(err);
351 }
352
353 while (optind < argc)
354 devices[numdev++] = argv[optind++];
355
356 if (version) {
357 print_version(stdout);
358 goto exit;
359 }
360
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500361 if (blkid_get_cache(&cache, read) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500362 goto exit;
363
364 err = 2;
Theodore Ts'o46100e32007-05-18 00:16:02 -0400365 if (gc) {
366 blkid_gc_cache(cache);
Theodore Ts'o3d058022008-07-12 22:06:30 -0400367 goto exit;
368 }
369 if (output_format & OUTPUT_PRETTY_LIST)
370 pretty_print_dev(NULL);
371
372 if (lookup) {
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400373 blkid_dev dev;
374
375 if (!search_type) {
376 fprintf(stderr, "The lookup option requires a "
377 "search type specified using -t\n");
378 exit(1);
379 }
380 /* Load any additional devices not in the cache */
381 for (i = 0; i < numdev; i++)
382 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
383
384 if ((dev = blkid_find_dev_with_tag(cache, search_type,
385 search_value))) {
386 print_tags(dev, show, numtag, output_format);
387 err = 0;
388 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500389 /* If we didn't specify a single device, show all available devices */
Theodore Ts'oed6acfa2005-05-07 17:06:27 -0400390 } else if (!numdev) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500391 blkid_dev_iterate iter;
392 blkid_dev dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500393
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500394 blkid_probe_all(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500395
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500396 iter = blkid_dev_iterate_begin(cache);
Theodore Ts'oc37543d2005-05-07 13:32:47 -0400397 blkid_dev_set_search(iter, search_type, search_value);
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500398 while (blkid_dev_next(iter, &dev) == 0) {
Theodore Ts'o18d12962005-01-27 19:51:47 -0500399 dev = blkid_verify(cache, dev);
400 if (!dev)
401 continue;
Theodore Ts'o89279982004-03-20 16:30:10 -0500402 print_tags(dev, show, numtag, output_format);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500403 err = 0;
404 }
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500405 blkid_dev_iterate_end(iter);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500406 /* Add all specified devices to cache (optionally display tags) */
407 } else for (i = 0; i < numdev; i++) {
Theodore Ts'o98999c32003-02-16 00:47:07 -0500408 blkid_dev dev = blkid_get_dev(cache, devices[i],
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500409 BLKID_DEV_NORMAL);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500410
411 if (dev) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400412 if (search_type &&
413 !blkid_dev_has_tag(dev, search_type,
Theodore Ts'oc37543d2005-05-07 13:32:47 -0400414 search_value))
Theodore Ts'o18d12962005-01-27 19:51:47 -0500415 continue;
Theodore Ts'o89279982004-03-20 16:30:10 -0500416 print_tags(dev, show, numtag, output_format);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500417 err = 0;
418 }
419 }
420
421exit:
Jim Meyering45e338f2009-02-23 18:07:50 +0100422 free(search_type);
423 free(search_value);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500424 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500425 return err;
426}