2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/capsicum.h>
36 #include <sys/ioctl.h>
38 #include <capsicum_helpers.h>
55 bool show_label = false;
57 typedef int (*fstyp_function)(FILE *, char *, size_t);
61 fstyp_function function;
63 char *precache_encoding;
65 { "apfs", &fstyp_apfs, true, NULL },
66 { "cd9660", &fstyp_cd9660, false, NULL },
67 { "exfat", &fstyp_exfat, false, EXFAT_ENC },
68 { "ext2fs", &fstyp_ext2fs, false, NULL },
69 { "geli", &fstyp_geli, true, NULL },
70 { "hfs+", &fstyp_hfsp, false, NULL },
71 { "msdosfs", &fstyp_msdosfs, false, NULL },
72 { "ntfs", &fstyp_ntfs, false, NTFS_ENC },
73 { "ufs", &fstyp_ufs, false, NULL },
75 { "zfs", &fstyp_zfs, true, NULL },
77 { NULL, NULL, NULL, NULL }
81 read_buf(FILE *fp, off_t off, size_t len)
87 error = fseek(fp, off, SEEK_SET);
89 warn("cannot seek to %jd", (uintmax_t)off);
95 warn("cannot malloc %zd bytes of memory", len);
99 nread = fread(buf, len, 1, fp);
111 checked_strdup(const char *s)
122 rtrim(char *label, size_t size)
126 for (i = size - 1; i >= 0; i--) {
127 if (label[i] == '\0')
129 else if (label[i] == ' ')
140 fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
145 type_check(const char *path, FILE *fp)
153 error = fstat(fd, &sb);
155 err(1, "%s: fstat", path);
157 if (S_ISREG(sb.st_mode))
160 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
162 errx(1, "%s: not a disk", path);
166 main(int argc, char **argv)
168 int ch, error, i, nbytes;
169 bool ignore_type = false, show_unmountable = false;
170 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
173 fstyp_function fstyp_f;
175 while ((ch = getopt(argc, argv, "lsu")) != -1) {
184 show_unmountable = true;
198 if (setlocale(LC_CTYPE, "") == NULL)
200 caph_cache_catpages();
202 /* Cache iconv conversion data before entering capability mode. */
204 for (i = 0; i < nitems(fstypes); i++) {
207 if (fstypes[i].precache_encoding == NULL)
209 cd = iconv_open("", fstypes[i].precache_encoding);
210 if (cd == (iconv_t)-1)
211 err(1, "%s: iconv_open %s", fstypes[i].name,
212 fstypes[i].precache_encoding);
213 /* Iconv keeps a small cache of unused encodings. */
218 fp = fopen(path, "r");
222 if (caph_enter() < 0)
225 if (ignore_type == false)
226 type_check(path, fp);
228 memset(label, '\0', sizeof(label));
231 if (show_unmountable == false && fstypes[i].unmountable == true)
233 fstyp_f = fstypes[i].function;
237 error = fstyp_f(fp, label, sizeof(label));
242 if (fstypes[i].name == NULL) {
243 warnx("%s: filesystem not recognized", path);
247 if (show_label && label[0] != '\0') {
249 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
252 nbytes = strsnvis(strvised, sizeof(strvised), label,
253 VIS_GLOB | VIS_NL, "\"'$");
257 printf("%s %s\n", fstypes[i].name, strvised);
259 printf("%s\n", fstypes[i].name);