kernel: Remove two final \ in macro definitions.
[dragonfly.git] / usr.sbin / fstyp / fstyp.c
blobcf44d260b1fa5060b3d1647cd0d82b6918e5a3b1
1 /*-
2 * Copyright (c) 2016 The DragonFly Project
3 * Copyright (c) 2014 The FreeBSD Foundation
4 * All rights reserved.
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
32 #include <sys/diskslice.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <vis.h>
45 #include "fstyp.h"
47 #define LABEL_LEN 256
49 typedef int (*fstyp_function)(FILE *, char *, size_t);
50 typedef int (*fsvtyp_function)(const char *, char *, size_t);
52 static struct {
53 const char *name;
54 fstyp_function function;
55 bool unmountable;
56 } fstypes[] = {
57 { "cd9660", &fstyp_cd9660, false },
58 { "ext2fs", &fstyp_ext2fs, false },
59 { "msdosfs", &fstyp_msdosfs, false },
60 { "ntfs", &fstyp_ntfs, false },
61 { "ufs", &fstyp_ufs, false },
62 { "hammer", &fstyp_hammer, false },
63 { "hammer2", &fstyp_hammer2, false },
64 { NULL, NULL, NULL }
67 static struct {
68 const char *name;
69 fsvtyp_function function;
70 bool unmountable;
71 } fsvtypes[] = {
72 { "hammer", &fsvtyp_hammer, false }, /* Must be before partial */
73 { "hammer(partial)", &fsvtyp_hammer_partial, true },
74 // XXX hammer2 not supported yet
75 { NULL, NULL, NULL }
78 void *
79 read_buf(FILE *fp, off_t off, size_t len)
81 int error;
82 size_t nread;
83 void *buf;
85 error = fseek(fp, off, SEEK_SET);
86 if (error != 0) {
87 warn("cannot seek to %jd", (uintmax_t)off);
88 return (NULL);
91 buf = malloc(len);
92 if (buf == NULL) {
93 warn("cannot malloc %zd bytes of memory", len);
94 return (NULL);
97 nread = fread(buf, len, 1, fp);
98 if (nread != 1) {
99 free(buf);
100 if (feof(fp) == 0)
101 warn("fread");
102 return (NULL);
105 return (buf);
108 char *
109 checked_strdup(const char *s)
111 char *c;
113 c = strdup(s);
114 if (c == NULL)
115 err(1, "strdup");
116 return (c);
119 void
120 rtrim(char *label, size_t size)
122 ptrdiff_t i;
124 for (i = size - 1; i >= 0; i--) {
125 if (label[i] == '\0')
126 continue;
127 else if (label[i] == ' ')
128 label[i] = '\0';
129 else
130 break;
134 static void
135 usage(void)
138 fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n");
139 exit(1);
142 static void
143 type_check(const char *path, FILE *fp)
145 int error, fd;
146 struct stat sb;
147 struct partinfo pinfo;
149 fd = fileno(fp);
151 error = fstat(fd, &sb);
152 if (error != 0)
153 err(1, "%s: fstat", path);
155 if (S_ISREG(sb.st_mode))
156 return;
158 error = ioctl(fd, DIOCGPART, &pinfo);
159 if (error != 0)
160 errx(1, "%s: not a disk", path);
164 main(int argc, char **argv)
166 int ch, error, i, nbytes;
167 bool ignore_type = false, show_label = false, show_unmountable = false;
168 char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
169 char *path;
170 const char *name = NULL;
171 FILE *fp;
172 fstyp_function fstyp_f;
173 fsvtyp_function fsvtyp_f;
175 while ((ch = getopt(argc, argv, "lsu")) != -1) {
176 switch (ch) {
177 case 'l':
178 show_label = true;
179 break;
180 case 's':
181 ignore_type = true;
182 break;
183 case 'u':
184 show_unmountable = true;
185 break;
186 default:
187 usage();
191 argc -= optind;
192 argv += optind;
193 if (argc != 1)
194 usage();
196 path = argv[0];
198 fp = fopen(path, "r");
199 if (fp == NULL)
200 goto fsvtyp; /* DragonFly */
202 if (ignore_type == false)
203 type_check(path, fp);
205 memset(label, '\0', sizeof(label));
207 for (i = 0;; i++) {
208 if (show_unmountable == false && fstypes[i].unmountable == true)
209 continue;
210 fstyp_f = fstypes[i].function;
211 if (fstyp_f == NULL)
212 break;
214 error = fstyp_f(fp, label, sizeof(label));
215 if (error == 0) {
216 name = fstypes[i].name;
217 goto done;
220 fsvtyp:
221 for (i = 0;; i++) {
222 if (show_unmountable == false && fsvtypes[i].unmountable == true)
223 continue;
224 fsvtyp_f = fsvtypes[i].function;
225 if (fsvtyp_f == NULL)
226 break;
228 error = fsvtyp_f(path, label, sizeof(label));
229 if (error == 0) {
230 name = fsvtypes[i].name;
231 goto done;
235 warnx("%s: filesystem not recognized", path);
236 return (1);
237 done:
238 if (show_label && label[0] != '\0') {
240 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
241 * encodes spaces.
242 * XXX: DragonFly doesn't have strsnvis().
244 nbytes = strnvis(strvised, label, sizeof(strvised),
245 VIS_GLOB | VIS_NL);
246 if (nbytes == -1)
247 err(1, "strnvis");
249 printf("%s %s\n", name, strvised);
250 } else {
251 printf("%s\n", name);
254 return (0);