fstyp.c revision 286193
1/*-
2 * Copyright (c) 2014 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/usr.sbin/fstyp/fstyp.c 286193 2015-08-02 10:08:57Z trasz $");
33
34#include <sys/capability.h>
35#include <sys/disk.h>
36#include <sys/ioctl.h>
37#include <sys/stat.h>
38#include <err.h>
39#include <errno.h>
40#include <stdbool.h>
41#include <stddef.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <vis.h>
48
49#include "fstyp.h"
50
51#define	LABEL_LEN	256
52
53typedef int (*fstyp_function)(FILE *, char *, size_t);
54
55static struct {
56	const char	*name;
57	fstyp_function	function;
58} fstypes[] = {
59	{ "cd9660", &fstyp_cd9660 },
60	{ "ext2fs", &fstyp_ext2fs },
61	{ "msdosfs", &fstyp_msdosfs },
62	{ "ntfs", &fstyp_ntfs },
63	{ "ufs", &fstyp_ufs },
64	{ NULL, NULL }
65};
66
67void *
68read_buf(FILE *fp, off_t off, size_t len)
69{
70	int error;
71	size_t nread;
72	void *buf;
73
74	error = fseek(fp, off, SEEK_SET);
75	if (error != 0) {
76		warn("cannot seek to %jd", (uintmax_t)off);
77		return (NULL);
78	}
79
80	buf = malloc(len);
81	if (buf == 0) {
82		warn("cannot malloc %zd bytes of memory", len);
83		return (NULL);
84	}
85
86	nread = fread(buf, len, 1, fp);
87	if (nread != 1) {
88		free(buf);
89		if (feof(fp) == 0)
90			warn("fread");
91		return (NULL);
92	}
93
94	return (buf);
95}
96
97char *
98checked_strdup(const char *s)
99{
100	char *c;
101
102	c = strdup(s);
103	if (c == NULL)
104		err(1, "strdup");
105	return (c);
106}
107
108void
109rtrim(char *label, size_t size)
110{
111	ptrdiff_t i;
112
113	for (i = size - 1; i >= 0; i--) {
114		if (label[i] == '\0')
115			continue;
116		else if (label[i] == ' ')
117			label[i] = '\0';
118		else
119			break;
120	}
121}
122
123static void
124usage(void)
125{
126
127	fprintf(stderr, "usage: fstyp [-l][-s] special\n");
128	exit(1);
129}
130
131static void
132type_check(const char *path, FILE *fp)
133{
134	int error, fd;
135	off_t mediasize;
136	struct stat sb;
137
138	fd = fileno(fp);
139
140	error = fstat(fd, &sb);
141	if (error != 0)
142		err(1, "%s: fstat", path);
143
144	if (S_ISREG(sb.st_mode))
145		return;
146
147	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
148	if (error != 0)
149		errx(1, "%s: not a disk", path);
150}
151
152int
153main(int argc, char **argv)
154{
155	int ch, error, i, nbytes;
156	bool ignore_type = false, show_label = false;
157	char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1];
158	char *path;
159	FILE *fp;
160	fstyp_function fstyp_f;
161
162	while ((ch = getopt(argc, argv, "ls")) != -1) {
163		switch (ch) {
164		case 'l':
165			show_label = true;
166			break;
167		case 's':
168			ignore_type = true;
169			break;
170		default:
171			usage();
172		}
173	}
174
175	argc -= optind;
176	argv += optind;
177	if (argc != 1)
178		usage();
179
180	path = argv[0];
181
182	fp = fopen(path, "r");
183	if (fp == NULL)
184		err(1, "%s", path);
185
186	error = cap_enter();
187	if (error != 0 && errno != ENOSYS)
188		err(1, "cap_enter");
189
190	if (ignore_type == false)
191		type_check(path, fp);
192
193	memset(label, '\0', sizeof(label));
194
195	for (i = 0;; i++) {
196		fstyp_f = fstypes[i].function;
197		if (fstyp_f == NULL)
198			break;
199
200		error = fstyp_f(fp, label, sizeof(label));
201		if (error == 0)
202			break;
203	}
204
205	if (fstypes[i].name == NULL) {
206		warnx("%s: filesystem not recognized", path);
207		return (1);
208	}
209
210	if (show_label && label[0] != '\0') {
211		/*
212		 * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally
213		 *      encodes spaces.
214		 */
215		nbytes = strsnvis(strvised, sizeof(strvised), label,
216		    VIS_GLOB | VIS_NL, "\"'$");
217		if (nbytes == -1)
218			err(1, "strsnvis");
219
220		printf("%s %s\n", fstypes[i].name, strvised);
221	} else {
222		printf("%s\n", fstypes[i].name);
223	}
224
225	return (0);
226}
227