OSDN Git Service

Merge branch 'maint' into next
[android-x86/external-e2fsprogs.git] / misc / blkid.c
1 /*
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 "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #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
26 #ifdef HAVE_GETOPT_H
27 #include <getopt.h>
28 #else
29 extern int getopt(int argc, char * const argv[], const char *optstring);
30 extern char *optarg;
31 extern int optind;
32 #endif
33
34 #define OUTPUT_VALUE_ONLY       0x0001
35 #define OUTPUT_DEVICE_ONLY      0x0002
36 #define OUTPUT_PRETTY_LIST      0x0004
37
38 #include "ext2fs/ext2fs.h"
39 #include "blkid/blkid.h"
40
41 static const char *progname = "blkid";
42
43 static void print_version(FILE *out)
44 {
45         fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
46 }
47
48 static void usage(int error)
49 {
50         FILE *out = error ? stderr : stdout;
51
52         print_version(out);
53         fprintf(out,
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",
65                 progname);
66         exit(error);
67 }
68
69 /*
70  * This function does "safe" printing.  It will convert non-printable
71  * ASCII characters using '^' and M- notation.
72  */
73 static 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
94 static int get_terminal_width(void)
95 {
96 #ifdef TIOCGSIZE
97         struct ttysize  t_win;
98 #endif
99 #ifdef TIOCGWINSZ
100         struct winsize  w_win;
101 #endif
102         const char      *cp;
103
104 #ifdef TIOCGSIZE
105         if (ioctl (0, TIOCGSIZE, &t_win) == 0)
106                 return (t_win.ts_cols);
107 #endif
108 #ifdef TIOCGWINSZ
109         if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
110                 return (w_win.ws_col);
111 #endif
112         cp = getenv("COLUMNS");
113         if (cp)
114                 return strtol(cp, NULL, 10);
115         return 80;
116 }
117
118 static int pretty_print_word(const char *str, int max_len,
119                              int left_len, int overflow_nl)
120 {
121         int len = strlen(str) + left_len;
122         int ret = 0;
123
124         fputs(str, stdout);
125         if (overflow_nl && len > max_len) {
126                 fputc('\n', stdout);
127                 len = 0;
128         } else if (len > max_len)
129                 ret = len - max_len;
130         do {
131                 fputc(' ', stdout);
132         } while (len++ < max_len);
133         return ret;
134 }
135
136 static void pretty_print_line(const char *device, const char *fs_type,
137                               const char *label, const char *mtpt,
138                               const char *uuid)
139 {
140         static int device_len = 10, fs_type_len = 7;
141         static int label_len = 8, mtpt_len = 14;
142         static int term_width = -1;
143         int len, w;
144
145         if (term_width < 0) {
146                 term_width = get_terminal_width();
147
148                 if (term_width > 80) {
149                         term_width -= 80;
150                         w = term_width / 10;
151                         if (w > 8)
152                                 w = 8;
153                         term_width -= 2*w;
154                         label_len += w;
155                         fs_type_len += w;
156                         w = term_width/2;
157                         device_len += w;
158                         mtpt_len +=w;
159                 }
160         }
161
162         len = pretty_print_word(device, device_len, 0, 1);
163         len = pretty_print_word(fs_type, fs_type_len, len, 0);
164         len = pretty_print_word(label, label_len, len, 0);
165         len = pretty_print_word(mtpt, mtpt_len, len, 0);
166         fputs(uuid, stdout);
167         fputc('\n', stdout);
168 }
169
170 static void pretty_print_dev(blkid_dev dev)
171 {
172         blkid_tag_iterate       iter;
173         const char              *type, *value, *devname;
174         const char              *uuid = "", *fs_type = "", *label = "";
175         int                     len, mount_flags;
176         char                    mtpt[80];
177         errcode_t               retval;
178
179         if (dev == NULL) {
180                 pretty_print_line("device", "fs_type", "label",
181                                   "mount point", "UUID");
182                 for (len=get_terminal_width()-1; len > 0; len--)
183                         fputc('-', stdout);
184                 fputc('\n', stdout);
185                 return;
186         }
187
188         devname = blkid_dev_devname(dev);
189         if (access(devname, F_OK))
190                 return;
191
192         /* Get the uuid, label, type */
193         iter = blkid_tag_iterate_begin(dev);
194         while (blkid_tag_next(iter, &type, &value) == 0) {
195                 if (!strcmp(type, "UUID"))
196                         uuid = value;
197                 if (!strcmp(type, "TYPE"))
198                         fs_type = value;
199                 if (!strcmp(type, "LABEL"))
200                         label = value;
201         }
202         blkid_tag_iterate_end(iter);
203
204         /* Get the mount point */
205         mtpt[0] = 0;
206         retval = ext2fs_check_mount_point(devname, &mount_flags,
207                                           mtpt, sizeof(mtpt));
208         if (retval == 0) {
209                 if (mount_flags & EXT2_MF_MOUNTED) {
210                         if (!mtpt[0])
211                                 strcpy(mtpt, "(mounted, mtpt unknown)");
212                 } else if (mount_flags & EXT2_MF_BUSY)
213                         strcpy(mtpt, "(in use)");
214                 else
215                         strcpy(mtpt, "(not mounted)");
216         }
217
218         pretty_print_line(devname, fs_type, label, mtpt, uuid);
219 }
220
221 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
222 {
223         blkid_tag_iterate       iter;
224         const char              *type, *value;
225         int                     i, first = 1;
226
227         if (!dev)
228                 return;
229
230         if (output & OUTPUT_PRETTY_LIST) {
231                 pretty_print_dev(dev);
232                 return;
233         }
234
235         if (output & OUTPUT_DEVICE_ONLY) {
236                 printf("%s\n", blkid_dev_devname(dev));
237                 return;
238         }
239
240         iter = blkid_tag_iterate_begin(dev);
241         while (blkid_tag_next(iter, &type, &value) == 0) {
242                 if (numtag && show) {
243                         for (i=0; i < numtag; i++)
244                                 if (!strcmp(type, show[i]))
245                                         break;
246                         if (i >= numtag)
247                                 continue;
248                 }
249                 if (output & OUTPUT_VALUE_ONLY) {
250                         fputs(value, stdout);
251                         fputc('\n', stdout);
252                 } else {
253                         if (first) {
254                                 printf("%s: ", blkid_dev_devname(dev));
255                                 first = 0;
256                         }
257                         fputs(type, stdout);
258                         fputs("=\"", stdout);
259                         safe_print(value, -1);
260                         fputs("\" ", stdout);
261                 }
262         }
263         blkid_tag_iterate_end(iter);
264
265         if (!first && !(output & OUTPUT_VALUE_ONLY))
266                 printf("\n");
267 }
268
269 int main(int argc, char **argv)
270 {
271         blkid_cache cache = NULL;
272         char *devices[128] = { NULL, };
273         char *show[128] = { NULL, };
274         char *search_type = NULL, *search_value = NULL;
275         char *read = NULL;
276         char *write = NULL;
277         unsigned int numdev = 0, numtag = 0;
278         int version = 0;
279         int err = 4;
280         unsigned int i;
281         int output_format = 0;
282         int lookup = 0, gc = 0;
283         int c;
284
285         while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
286                 switch (c) {
287                 case 'c':
288                         if (optarg && !*optarg)
289                                 read = NULL;
290                         else
291                                 read = optarg;
292                         if (!write)
293                                 write = read;
294                         break;
295                 case 'l':
296                         lookup++;
297                         break;
298                 case 'L':
299                         output_format = OUTPUT_PRETTY_LIST;
300                         break;
301                 case 'g':
302                         gc = 1;
303                         break;
304                 case 'o':
305                         if (!strcmp(optarg, "value"))
306                                 output_format = OUTPUT_VALUE_ONLY;
307                         else if (!strcmp(optarg, "device"))
308                                 output_format = OUTPUT_DEVICE_ONLY;
309                         else if (!strcmp(optarg, "list"))
310                                 output_format = OUTPUT_PRETTY_LIST;
311                         else if (!strcmp(optarg, "full"))
312                                 output_format = 0;
313                         else {
314                                 fprintf(stderr, "Invalid output format %s. "
315                                         "Choose from value,\n\t"
316                                         "device, list, or full\n", optarg);
317                                 exit(1);
318                         }
319                         break;
320                 case 's':
321                         if (numtag >= sizeof(show) / sizeof(*show)) {
322                                 fprintf(stderr, "Too many tags specified\n");
323                                 usage(err);
324                         }
325                         show[numtag++] = optarg;
326                         break;
327                 case 't':
328                         if (search_type) {
329                                 fprintf(stderr, "Can only search for "
330                                                 "one NAME=value pair\n");
331                                 usage(err);
332                         }
333                         if (blkid_parse_tag_string(optarg,
334                                                    &search_type,
335                                                    &search_value)) {
336                                 fprintf(stderr, "-t needs NAME=value pair\n");
337                                 usage(err);
338                         }
339                         break;
340                 case 'v':
341                         version = 1;
342                         break;
343                 case 'w':
344                         if (optarg && !*optarg)
345                                 write = NULL;
346                         else
347                                 write = optarg;
348                         break;
349                 case 'h':
350                         err = 0;
351                         /* fallthrough */
352                 default:
353                         usage(err);
354                 }
355
356         while (optind < argc)
357                 devices[numdev++] = argv[optind++];
358
359         if (version) {
360                 print_version(stdout);
361                 goto exit;
362         }
363
364         if (blkid_get_cache(&cache, read) < 0)
365                 goto exit;
366
367         err = 2;
368         if (gc) {
369                 blkid_gc_cache(cache);
370                 goto exit;
371         }
372         if (output_format & OUTPUT_PRETTY_LIST)
373                 pretty_print_dev(NULL);
374
375         if (lookup) {
376                 blkid_dev dev;
377
378                 if (!search_type) {
379                         fprintf(stderr, "The lookup option requires a "
380                                 "search type specified using -t\n");
381                         exit(1);
382                 }
383                 /* Load any additional devices not in the cache */
384                 for (i = 0; i < numdev; i++)
385                         blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
386
387                 if ((dev = blkid_find_dev_with_tag(cache, search_type,
388                                                    search_value))) {
389                         print_tags(dev, show, numtag, output_format);
390                         err = 0;
391                 }
392         /* If we didn't specify a single device, show all available devices */
393         } else if (!numdev) {
394                 blkid_dev_iterate       iter;
395                 blkid_dev               dev;
396
397                 blkid_probe_all(cache);
398
399                 iter = blkid_dev_iterate_begin(cache);
400                 blkid_dev_set_search(iter, search_type, search_value);
401                 while (blkid_dev_next(iter, &dev) == 0) {
402                         dev = blkid_verify(cache, dev);
403                         if (!dev)
404                                 continue;
405                         print_tags(dev, show, numtag, output_format);
406                         err = 0;
407                 }
408                 blkid_dev_iterate_end(iter);
409         /* Add all specified devices to cache (optionally display tags) */
410         } else for (i = 0; i < numdev; i++) {
411                 blkid_dev dev = blkid_get_dev(cache, devices[i],
412                                                   BLKID_DEV_NORMAL);
413
414                 if (dev) {
415                         if (search_type &&
416                             !blkid_dev_has_tag(dev, search_type,
417                                                search_value))
418                                 continue;
419                         print_tags(dev, show, numtag, output_format);
420                         err = 0;
421                 }
422         }
423
424 exit:
425         free(search_type);
426         free(search_value);
427         blkid_put_cache(cache);
428         return err;
429 }