blkid.c (3029B)
1 /* see LICENSE file for copyright and license details */ 2 #include "diskutil.h" 3 #include "util.h" 4 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <strings.h> 10 #include <unistd.h> 11 12 static char *oflag = "full"; 13 static char *sflag = NULL; 14 static int Uflag = 0; 15 static int Lflag = 0; 16 17 static void 18 usage(void) 19 { 20 eprintf("usage: %s [-o format] [-s tag] [-U] [-L] [device ...]\n", argv0); 21 } 22 23 static void 24 print_tag(const char *devname, const char *tag, const char *value) 25 { 26 (void)devname; 27 if (sflag && strcasecmp(sflag, tag) != 0) 28 return; 29 30 if (strcmp(oflag, "value") == 0) { 31 printf("%s\n", value); 32 } else if (strcmp(oflag, "export") == 0) { 33 printf("%s=%s\n", tag, value); 34 } else { 35 printf(" %s=\"%s\"", tag, value); 36 } 37 } 38 39 static int 40 do_blkid(const char *path) 41 { 42 struct BlockDev dev; 43 char type[64] = {0}; 44 char label[256] = {0}; 45 char uuid[256] = {0}; 46 int res; 47 48 if (blockdev_open(&dev, path, 0) < 0) 49 return -1; 50 51 res = blockdev_detect_fs(&dev, type, sizeof(type), label, sizeof(label), uuid, sizeof(uuid)); 52 if (res == 0) { 53 if (Uflag || Lflag) { 54 if (Uflag) { 55 printf("%s\n", uuid); 56 } else { 57 printf("%s\n", label); 58 } 59 } else { 60 if (strcmp(oflag, "export") == 0) { 61 printf("DEVNAME=%s\n", path); 62 } else if (strcmp(oflag, "value") != 0) { 63 printf("%s:", path); 64 } 65 66 if (label[0]) 67 print_tag(path, "LABEL", label); 68 if (uuid[0]) 69 print_tag(path, "UUID", uuid); 70 print_tag(path, "TYPE", type); 71 72 if (strcmp(oflag, "full") == 0) 73 printf("\n"); 74 } 75 } 76 77 blockdev_close(&dev); 78 return res == 0 ? 0 : -2; 79 } 80 81 // ?man blkid: print block device attributes 82 // ?man arguments: [device ...] 83 // ?man blkid locates and prints attributes (such as uuid, volume label, and filesystem type) 84 // ?man of block devices or partition images 85 int 86 main(int argc, char *argv[]) 87 { 88 int ret = 0; 89 90 ARGBEGIN 91 { 92 // ?man -o:specify output format (full, value, export) 93 case 'o': 94 oflag = EARGF(usage()); 95 break; 96 // ?man -s:only show specified tag (e.g. UUID, LABEL, TYPE) 97 case 's': 98 sflag = EARGF(usage()); 99 break; 100 // ?man -U:print UUID only 101 case 'U': 102 Uflag = 1; 103 break; 104 // ?man -L:print volume label only 105 case 'L': 106 Lflag = 1; 107 break; 108 default: 109 usage(); 110 } 111 ARGEND 112 113 if (argc > 0) { 114 for (; *argv; argv++) { 115 if (do_blkid(*argv) < 0) 116 ret = 1; 117 } 118 } else { 119 struct BlockDevInfo *list = blockdev_get_list(); 120 struct BlockDevInfo *curr; 121 if (!list) { 122 return 1; 123 } 124 for (curr = list; curr; curr = curr->next) { 125 do_blkid(curr->path); 126 struct BlockDevInfo *part; 127 for (part = curr->parts; part; part = part->next) { 128 do_blkid(part->path); 129 } 130 } 131 blockdev_free_list(list); 132 } 133 134 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 135 ret = 2; 136 137 return ret; 138 }