Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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 <stdio.h> |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 13 | #include <stdlib.h> |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 14 | #ifdef HAVE_GETOPT_H |
| 15 | #include <getopt.h> |
| 16 | #else |
| 17 | extern char *optarg; |
| 18 | extern int optind; |
| 19 | #endif |
| 20 | |
| 21 | #include "blkid/blkid.h" |
| 22 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 23 | const char *progname = "blkid"; |
| 24 | |
| 25 | static void print_version(FILE *out) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 26 | { |
| 27 | fprintf(stderr, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE); |
| 28 | } |
| 29 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 30 | static void usage(int error) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 31 | { |
| 32 | FILE *out = error ? stderr : stdout; |
| 33 | |
| 34 | print_version(out); |
| 35 | fprintf(out, |
| 36 | "usage:\t%s [-c <file>] [-h] " |
| 37 | "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n" |
| 38 | "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" |
| 39 | "\t-h\tprint this usage message and exit\n" |
| 40 | "\t-s\tshow specified tag(s) (default show all tags)\n" |
| 41 | "\t-t\tfind device with a specific token (NAME=value pair)\n" |
| 42 | "\t-v\tprint version and exit\n" |
| 43 | "\t-w\twrite cache to different file (/dev/null = no write)\n" |
| 44 | "\tdev\tspecify device(s) to probe (default: all devices)\n", |
| 45 | progname); |
| 46 | exit(error); |
| 47 | } |
| 48 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 49 | static void print_tags(blkid_dev dev, char *show[], int numtag) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 50 | { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 51 | blkid_tag_iterate iter; |
| 52 | const char *type, *value; |
Theodore Ts'o | ed1b33e | 2003-03-01 19:29:01 -0500 | [diff] [blame] | 53 | int i, first = 1; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 54 | |
| 55 | if (!dev) |
| 56 | return; |
| 57 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 58 | iter = blkid_tag_iterate_begin(dev); |
| 59 | while (blkid_tag_next(iter, &type, &value) == 0) { |
| 60 | if (numtag && show) { |
| 61 | for (i=0; i < numtag; i++) |
| 62 | if (!strcmp(type, show[i])) |
| 63 | break; |
| 64 | if (i >= numtag) |
| 65 | continue; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 66 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 67 | if (first) { |
Theodore Ts'o | 98999c3 | 2003-02-16 00:47:07 -0500 | [diff] [blame] | 68 | printf("%s: ", blkid_dev_devname(dev)); |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 69 | first = 0; |
| 70 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 71 | printf("%s=\"%s\" ", type, value); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 72 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 73 | blkid_tag_iterate_end(iter); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 74 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 75 | if (!first) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 76 | printf("\n"); |
| 77 | } |
| 78 | |
| 79 | int main(int argc, char **argv) |
| 80 | { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 81 | blkid_cache cache = NULL; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 82 | char *devices[128] = { NULL, }; |
| 83 | char *show[128] = { NULL, }; |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 84 | char *search_type = NULL, *search_value = NULL; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 85 | char *read = NULL; |
| 86 | char *write = NULL; |
| 87 | int numdev = 0, numtag = 0; |
| 88 | int version = 0; |
| 89 | int err = 4; |
| 90 | int i; |
| 91 | char c; |
| 92 | |
Theodore Ts'o | ed1b33e | 2003-03-01 19:29:01 -0500 | [diff] [blame] | 93 | while ((c = getopt (argc, argv, "c:f:hps:t:w:v")) != EOF) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 94 | switch (c) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 95 | case 'c': |
| 96 | if (optarg && !*optarg) |
| 97 | read = NULL; |
| 98 | else |
| 99 | read = optarg; |
| 100 | if (!write) |
| 101 | write = read; |
| 102 | break; |
| 103 | case 's': |
| 104 | if (numtag >= sizeof(show) / sizeof(*show)) { |
| 105 | fprintf(stderr, "Too many tags specified\n"); |
| 106 | usage(err); |
| 107 | } |
| 108 | show[numtag++] = optarg; |
| 109 | break; |
| 110 | case 't': |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 111 | if (search_type) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 112 | fprintf(stderr, "Can only search for " |
| 113 | "one NAME=value pair\n"); |
| 114 | usage(err); |
| 115 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 116 | if (blkid_parse_tag_string(optarg, |
| 117 | &search_type, |
| 118 | &search_value)) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 119 | fprintf(stderr, "-t needs NAME=value pair\n"); |
| 120 | usage(err); |
| 121 | } |
| 122 | break; |
| 123 | case 'v': |
| 124 | version = 1; |
| 125 | break; |
| 126 | case 'w': |
| 127 | if (optarg && !*optarg) |
| 128 | write = NULL; |
| 129 | else |
| 130 | write = optarg; |
| 131 | break; |
| 132 | case 'h': |
| 133 | err = 0; |
| 134 | default: |
| 135 | usage(err); |
| 136 | } |
| 137 | |
| 138 | while (optind < argc) |
| 139 | devices[numdev++] = argv[optind++]; |
| 140 | |
| 141 | if (version) { |
| 142 | print_version(stdout); |
| 143 | goto exit; |
| 144 | } |
| 145 | |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 146 | if (blkid_get_cache(&cache, read) < 0) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 147 | goto exit; |
| 148 | |
| 149 | err = 2; |
| 150 | /* If looking for a specific NAME=value pair, print only that */ |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 151 | if (search_type) { |
| 152 | blkid_dev dev; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 153 | |
| 154 | /* Load any additional devices not in the cache */ |
| 155 | for (i = 0; i < numdev; i++) |
Theodore Ts'o | 98999c3 | 2003-02-16 00:47:07 -0500 | [diff] [blame] | 156 | blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 157 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 158 | if ((dev = blkid_find_dev_with_tag(cache, search_type, |
| 159 | search_value))) { |
| 160 | print_tags(dev, show, numtag); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 161 | err = 0; |
| 162 | } |
| 163 | /* If we didn't specify a single device, show all available devices */ |
| 164 | } else if (!numdev) { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 165 | blkid_dev_iterate iter; |
| 166 | blkid_dev dev; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 167 | |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 168 | blkid_probe_all(cache); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 169 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 170 | iter = blkid_dev_iterate_begin(cache); |
| 171 | while (blkid_dev_next(iter, &dev) == 0) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 172 | print_tags(dev, show, numtag); |
| 173 | err = 0; |
| 174 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 175 | blkid_dev_iterate_end(iter); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 176 | /* Add all specified devices to cache (optionally display tags) */ |
| 177 | } else for (i = 0; i < numdev; i++) { |
Theodore Ts'o | 98999c3 | 2003-02-16 00:47:07 -0500 | [diff] [blame] | 178 | blkid_dev dev = blkid_get_dev(cache, devices[i], |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 179 | BLKID_DEV_NORMAL); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 180 | |
| 181 | if (dev) { |
| 182 | print_tags(dev, show, numtag); |
| 183 | err = 0; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | exit: |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 188 | if (search_type) |
| 189 | free(search_type); |
| 190 | if (search_value) |
| 191 | free(search_value); |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 192 | blkid_put_cache(cache); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 193 | return err; |
| 194 | } |