Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.32
      1  1.32      jmmv /*	$NetBSD: fsck.c,v 1.32 2004/01/05 23:23:32 jmmv Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 1980, 1989, 1993, 1994
      5   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Redistribution and use in source and binary forms, with or without
      8   1.1  christos  * modification, are permitted provided that the following conditions
      9   1.1  christos  * are met:
     10   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.30       agc  *    may be used to endorse or promote products derived from this software
     17  1.30       agc  *    without specific prior written permission.
     18  1.30       agc  *
     19  1.30       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.30       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.30       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.30       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.30       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.30       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.30       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.30       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.30       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.30       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.30       agc  * SUCH DAMAGE.
     30  1.30       agc  *
     31  1.30       agc  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
     32  1.30       agc  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
     33  1.30       agc  *
     34  1.30       agc  */
     35  1.30       agc 
     36  1.30       agc /*
     37  1.30       agc  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
     38  1.30       agc  *
     39  1.30       agc  * Redistribution and use in source and binary forms, with or without
     40  1.30       agc  * modification, are permitted provided that the following conditions
     41  1.30       agc  * are met:
     42  1.30       agc  * 1. Redistributions of source code must retain the above copyright
     43  1.30       agc  *    notice, this list of conditions and the following disclaimer.
     44  1.30       agc  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.30       agc  *    notice, this list of conditions and the following disclaimer in the
     46  1.30       agc  *    documentation and/or other materials provided with the distribution.
     47   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     48   1.1  christos  *    must display the following acknowledgement:
     49   1.1  christos  *	This product includes software developed by the University of
     50   1.1  christos  *	California, Berkeley and its contributors.
     51   1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     52   1.1  christos  *    may be used to endorse or promote products derived from this software
     53   1.1  christos  *    without specific prior written permission.
     54   1.1  christos  *
     55   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1  christos  * SUCH DAMAGE.
     66   1.1  christos  *
     67   1.1  christos  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
     68   1.1  christos  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
     69   1.1  christos  *
     70   1.1  christos  */
     71   1.1  christos 
     72  1.12     lukem #include <sys/cdefs.h>
     73  1.12     lukem #ifndef lint
     74  1.32      jmmv __RCSID("$NetBSD: fsck.c,v 1.32 2004/01/05 23:23:32 jmmv Exp $");
     75  1.12     lukem #endif /* not lint */
     76   1.1  christos 
     77   1.1  christos #include <sys/param.h>
     78   1.1  christos #include <sys/mount.h>
     79   1.1  christos #include <sys/queue.h>
     80   1.1  christos #include <sys/wait.h>
     81  1.20  christos #define FSTYPENAMES
     82  1.20  christos #define FSCKNAMES
     83   1.8  christos #include <sys/disklabel.h>
     84   1.8  christos #include <sys/ioctl.h>
     85   1.1  christos 
     86   1.1  christos #include <err.h>
     87   1.1  christos #include <errno.h>
     88   1.1  christos #include <fstab.h>
     89   1.8  christos #include <fcntl.h>
     90  1.21       abs #include <paths.h>
     91   1.1  christos #include <signal.h>
     92   1.1  christos #include <stdio.h>
     93   1.1  christos #include <stdlib.h>
     94   1.1  christos #include <string.h>
     95   1.1  christos #include <unistd.h>
     96   1.1  christos 
     97   1.1  christos #include "pathnames.h"
     98   1.4  christos #include "fsutil.h"
     99   1.1  christos 
    100   1.2  christos static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
    101   1.1  christos 
    102   1.2  christos TAILQ_HEAD(fstypelist, entry) opthead, selhead;
    103   1.1  christos 
    104   1.1  christos struct entry {
    105   1.1  christos 	char *type;
    106   1.2  christos 	char *options;
    107   1.1  christos 	TAILQ_ENTRY(entry) entries;
    108   1.1  christos };
    109   1.1  christos 
    110   1.4  christos static int maxrun = 0;
    111   1.2  christos static char *options = NULL;
    112   1.4  christos static int flags = 0;
    113   1.2  christos 
    114  1.25     lukem int main(int, char *[]);
    115   1.1  christos 
    116  1.25     lukem static int checkfs(const char *, const char *, const char *, void *, pid_t *);
    117  1.25     lukem static int selected(const char *);
    118  1.25     lukem static void addoption(char *);
    119  1.25     lukem static const char *getoptions(const char *);
    120  1.25     lukem static void addentry(struct fstypelist *, const char *, const char *);
    121  1.25     lukem static void maketypelist(char *);
    122  1.25     lukem static void catopt(char **, const char *);
    123  1.25     lukem static void mangle(char *, int *, const char ***, int *);
    124  1.25     lukem static const char *getfslab(const char *);
    125  1.25     lukem static void usage(void);
    126  1.25     lukem static void *isok(struct fstab *);
    127   1.1  christos 
    128   1.1  christos int
    129  1.25     lukem main(int argc, char *argv[])
    130   1.1  christos {
    131   1.1  christos 	struct fstab *fs;
    132   1.1  christos 	int i, rval = 0;
    133  1.17   mycroft 	const char *vfstype = NULL;
    134   1.7  christos 	char globopt[3];
    135   1.7  christos 
    136   1.7  christos 	globopt[0] = '-';
    137   1.7  christos 	globopt[2] = '\0';
    138   1.1  christos 
    139   1.2  christos 	TAILQ_INIT(&selhead);
    140   1.2  christos 	TAILQ_INIT(&opthead);
    141   1.2  christos 
    142  1.31       dsl 	while ((i = getopt(argc, argv, "dfl:npqT:t:vy")) != -1) {
    143   1.1  christos 		switch (i) {
    144   1.1  christos 		case 'd':
    145   1.4  christos 			flags |= CHECK_DEBUG;
    146  1.31       dsl 			continue;
    147  1.31       dsl 
    148  1.31       dsl 		case 'f':
    149  1.31       dsl 			flags |= CHECK_FORCE;
    150   1.1  christos 			break;
    151   1.1  christos 
    152  1.31       dsl 		case 'n':
    153   1.1  christos 			break;
    154   1.1  christos 
    155   1.7  christos 		case 'p':
    156  1.31       dsl 			flags |= CHECK_PREEN;
    157  1.31       dsl 			break;
    158  1.31       dsl 
    159  1.31       dsl 		case 'q':
    160   1.2  christos 			break;
    161   1.2  christos 
    162   1.2  christos 		case 'l':
    163   1.2  christos 			maxrun = atoi(optarg);
    164  1.31       dsl 			continue;
    165   1.1  christos 
    166   1.2  christos 		case 'T':
    167   1.1  christos 			if (*optarg)
    168   1.2  christos 				addoption(optarg);
    169  1.31       dsl 			continue;
    170   1.1  christos 
    171   1.1  christos 		case 't':
    172  1.24     lukem 			if (TAILQ_FIRST(&selhead) != NULL)
    173   1.1  christos 				errx(1, "only one -t option may be specified.");
    174   1.2  christos 
    175   1.1  christos 			maketypelist(optarg);
    176   1.1  christos 			vfstype = optarg;
    177  1.31       dsl 			continue;
    178  1.31       dsl 
    179  1.31       dsl 		case 'v':
    180  1.31       dsl 			flags |= CHECK_VERBOSE;
    181  1.31       dsl 			continue;
    182  1.31       dsl 
    183  1.31       dsl 		case 'y':
    184   1.1  christos 			break;
    185   1.1  christos 
    186   1.1  christos 		case '?':
    187   1.1  christos 		default:
    188   1.1  christos 			usage();
    189   1.1  christos 			/* NOTREACHED */
    190   1.1  christos 		}
    191   1.1  christos 
    192  1.31       dsl 		/* Pass option to fsck_xxxfs */
    193  1.31       dsl 		globopt[1] = i;
    194  1.31       dsl 		catopt(&options, globopt);
    195  1.31       dsl 	}
    196  1.31       dsl 
    197   1.1  christos 	argc -= optind;
    198   1.1  christos 	argv += optind;
    199   1.1  christos 
    200   1.2  christos 	if (argc == 0)
    201   1.4  christos 		return checkfstab(flags, maxrun, isok, checkfs);
    202   1.2  christos 
    203   1.1  christos #define	BADTYPE(type)							\
    204   1.1  christos 	(strcmp(type, FSTAB_RO) &&					\
    205   1.1  christos 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    206   1.1  christos 
    207   1.1  christos 
    208   1.1  christos 	for (; argc--; argv++) {
    209  1.21       abs 		const char *spec, *type, *cp;
    210  1.21       abs 		char	device[MAXPATHLEN];
    211   1.1  christos 
    212  1.21       abs 		spec = *argv;
    213  1.21       abs 		cp = strrchr(spec, '/');
    214  1.21       abs 		if (cp == 0) {
    215  1.21       abs 			(void)snprintf(device, sizeof(device), "%s%s",
    216  1.21       abs 				_PATH_DEV, spec);
    217  1.21       abs 			spec = device;
    218  1.21       abs 		}
    219  1.21       abs 		if ((fs = getfsfile(spec)) == NULL &&
    220  1.21       abs 		    (fs = getfsspec(spec)) == NULL) {
    221   1.1  christos 			if (vfstype == NULL)
    222  1.21       abs 				vfstype = getfslab(spec);
    223   1.1  christos 			type = vfstype;
    224   1.1  christos 		}
    225   1.1  christos 		else {
    226   1.1  christos 			spec = fs->fs_spec;
    227   1.1  christos 			type = fs->fs_vfstype;
    228   1.1  christos 			if (BADTYPE(fs->fs_type))
    229   1.1  christos 				errx(1, "%s has unknown file system type.",
    230  1.21       abs 				    spec);
    231   1.1  christos 		}
    232   1.1  christos 
    233   1.6  christos 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    234   1.1  christos 	}
    235   1.1  christos 
    236   1.1  christos 	return rval;
    237   1.1  christos }
    238   1.1  christos 
    239   1.2  christos 
    240   1.4  christos static void *
    241  1.25     lukem isok(struct fstab *fs)
    242   1.2  christos {
    243  1.25     lukem 
    244   1.2  christos 	if (fs->fs_passno == 0)
    245   1.4  christos 		return NULL;
    246   1.2  christos 
    247   1.2  christos 	if (BADTYPE(fs->fs_type))
    248   1.4  christos 		return NULL;
    249   1.2  christos 
    250   1.2  christos 	if (!selected(fs->fs_vfstype))
    251   1.4  christos 		return NULL;
    252   1.2  christos 
    253   1.4  christos 	return fs;
    254   1.2  christos }
    255   1.2  christos 
    256   1.2  christos 
    257   1.1  christos static int
    258  1.25     lukem checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
    259  1.25     lukem     pid_t *pidp)
    260   1.1  christos {
    261   1.1  christos 	/* List of directories containing fsck_xxx subcommands. */
    262   1.1  christos 	static const char *edirs[] = {
    263  1.29     lukem #ifdef _PATH_RESCUE
    264  1.29     lukem 		_PATH_RESCUE,
    265  1.29     lukem #endif
    266   1.1  christos 		_PATH_SBIN,
    267   1.1  christos 		_PATH_USRSBIN,
    268   1.1  christos 		NULL
    269   1.1  christos 	};
    270   1.7  christos 	const char **argv, **edir;
    271   1.1  christos 	pid_t pid;
    272  1.15   mycroft 	int argc, i, status, maxargc;
    273  1.15   mycroft 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
    274   1.2  christos 	const char *extra = getoptions(vfstype);
    275   1.1  christos 
    276   1.1  christos #ifdef __GNUC__
    277   1.1  christos 	/* Avoid vfork clobbering */
    278   1.1  christos 	(void) &optbuf;
    279   1.8  christos 	(void) &vfstype;
    280   1.1  christos #endif
    281   1.1  christos 
    282  1.15   mycroft 	if (!strcmp(vfstype, "ufs"))
    283   1.3       cgd 		vfstype = MOUNT_UFS;
    284   1.3       cgd 
    285  1.16   mycroft 	optbuf = NULL;
    286  1.16   mycroft 	if (options)
    287  1.16   mycroft 		catopt(&optbuf, options);
    288  1.16   mycroft 	if (extra)
    289  1.16   mycroft 		catopt(&optbuf, extra);
    290   1.2  christos 
    291  1.15   mycroft 	maxargc = 64;
    292  1.15   mycroft 	argv = emalloc(sizeof(char *) * maxargc);
    293  1.15   mycroft 
    294  1.15   mycroft 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
    295  1.15   mycroft 	argc = 0;
    296  1.15   mycroft 	argv[argc++] = execbase;
    297   1.2  christos 	if (optbuf)
    298   1.7  christos 		mangle(optbuf, &argc, &argv, &maxargc);
    299   1.2  christos 	argv[argc++] = spec;
    300   1.7  christos 	argv[argc] = NULL;
    301   1.1  christos 
    302   1.4  christos 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    303   1.8  christos 		(void)printf("start %s %swait", mntpt,
    304   1.8  christos 			pidp ? "no" : "");
    305   1.8  christos 		for (i = 0; i < argc; i++)
    306   1.1  christos 			(void)printf(" %s", argv[i]);
    307   1.1  christos 		(void)printf("\n");
    308   1.1  christos 	}
    309   1.1  christos 
    310   1.1  christos 	switch (pid = vfork()) {
    311   1.1  christos 	case -1:				/* Error. */
    312   1.1  christos 		warn("vfork");
    313   1.1  christos 		if (optbuf)
    314   1.1  christos 			free(optbuf);
    315   1.1  christos 		return (1);
    316   1.1  christos 
    317   1.1  christos 	case 0:					/* Child. */
    318  1.26     lukem 		if ((flags & CHECK_FORCE) == 0) {
    319  1.26     lukem 			struct statfs	sfs;
    320  1.26     lukem 
    321  1.26     lukem 				/*
    322  1.26     lukem 				 * if mntpt is a mountpoint of a mounted file
    323  1.26     lukem 				 * system and it's mounted read-write, skip it
    324  1.26     lukem 				 * unless -f is given.
    325  1.26     lukem 				 */
    326  1.26     lukem 			if ((statfs(mntpt, &sfs) == 0) &&
    327  1.26     lukem 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    328  1.26     lukem 			    ((sfs.f_flags & MNT_RDONLY) == 0)) {
    329  1.26     lukem 				printf(
    330  1.26     lukem 		"%s: file system is mounted read-write on %s; not checking\n",
    331  1.26     lukem 				    spec, mntpt);
    332  1.26     lukem 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    333  1.26     lukem 					_exit(0);	/* fsck -p */
    334  1.26     lukem 				else
    335  1.26     lukem 					_exit(1);	/* fsck [[-p] ...] */
    336  1.26     lukem 			}
    337  1.26     lukem 		}
    338  1.26     lukem 
    339   1.4  christos 		if (flags & CHECK_DEBUG)
    340   1.2  christos 			_exit(0);
    341   1.2  christos 
    342   1.1  christos 		/* Go find an executable. */
    343   1.1  christos 		edir = edirs;
    344   1.1  christos 		do {
    345   1.1  christos 			(void)snprintf(execname,
    346   1.8  christos 			    sizeof(execname), "%s/%s", *edir, execbase);
    347   1.1  christos 			execv(execname, (char * const *)argv);
    348  1.19      ross 			if (errno != ENOENT) {
    349   1.1  christos 				if (spec)
    350   1.1  christos 					warn("exec %s for %s", execname, spec);
    351   1.1  christos 				else
    352   1.1  christos 					warn("exec %s", execname);
    353  1.19      ross 			}
    354   1.1  christos 		} while (*++edir != NULL);
    355   1.1  christos 
    356  1.19      ross 		if (errno == ENOENT) {
    357   1.1  christos 			if (spec)
    358   1.1  christos 				warn("exec %s for %s", execname, spec);
    359   1.1  christos 			else
    360   1.1  christos 				warn("exec %s", execname);
    361  1.19      ross 		}
    362   1.8  christos 		_exit(1);
    363   1.1  christos 		/* NOTREACHED */
    364   1.1  christos 
    365   1.1  christos 	default:				/* Parent. */
    366   1.1  christos 		if (optbuf)
    367   1.1  christos 			free(optbuf);
    368   1.1  christos 
    369   1.2  christos 		if (pidp) {
    370   1.2  christos 			*pidp = pid;
    371   1.2  christos 			return 0;
    372   1.2  christos 		}
    373   1.2  christos 
    374   1.1  christos 		if (waitpid(pid, &status, 0) < 0) {
    375   1.1  christos 			warn("waitpid");
    376   1.1  christos 			return (1);
    377   1.1  christos 		}
    378   1.1  christos 
    379   1.1  christos 		if (WIFEXITED(status)) {
    380   1.1  christos 			if (WEXITSTATUS(status) != 0)
    381   1.1  christos 				return (WEXITSTATUS(status));
    382   1.1  christos 		}
    383   1.1  christos 		else if (WIFSIGNALED(status)) {
    384   1.1  christos 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    385   1.1  christos 			return (1);
    386   1.1  christos 		}
    387   1.1  christos 		break;
    388   1.1  christos 	}
    389   1.1  christos 
    390   1.1  christos 	return (0);
    391   1.1  christos }
    392   1.1  christos 
    393   1.1  christos 
    394   1.1  christos static int
    395  1.25     lukem selected(const char *type)
    396   1.1  christos {
    397   1.1  christos 	struct entry *e;
    398   1.1  christos 
    399   1.1  christos 	/* If no type specified, it's always selected. */
    400  1.24     lukem 	TAILQ_FOREACH(e, &selhead, entries)
    401   1.1  christos 		if (!strncmp(e->type, type, MFSNAMELEN))
    402   1.1  christos 			return which == IN_LIST ? 1 : 0;
    403   1.1  christos 
    404   1.1  christos 	return which == IN_LIST ? 0 : 1;
    405   1.1  christos }
    406   1.1  christos 
    407   1.1  christos 
    408   1.2  christos static const char *
    409  1.25     lukem getoptions(const char *type)
    410   1.1  christos {
    411   1.1  christos 	struct entry *e;
    412   1.1  christos 
    413  1.24     lukem 	TAILQ_FOREACH(e, &opthead, entries)
    414   1.2  christos 		if (!strncmp(e->type, type, MFSNAMELEN))
    415   1.2  christos 			return e->options;
    416   1.2  christos 	return "";
    417   1.1  christos }
    418   1.1  christos 
    419   1.1  christos 
    420   1.1  christos static void
    421  1.25     lukem addoption(char *optstr)
    422   1.1  christos {
    423   1.2  christos 	char *newoptions;
    424   1.1  christos 	struct entry *e;
    425   1.1  christos 
    426   1.2  christos 	if ((newoptions = strchr(optstr, ':')) == NULL)
    427   1.2  christos 		errx(1, "Invalid option string");
    428   1.2  christos 
    429   1.2  christos 	*newoptions++ = '\0';
    430   1.2  christos 
    431  1.24     lukem 	TAILQ_FOREACH(e, &opthead, entries)
    432   1.2  christos 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    433  1.16   mycroft 			catopt(&e->options, newoptions);
    434   1.1  christos 			return;
    435   1.1  christos 		}
    436   1.2  christos 	addentry(&opthead, optstr, newoptions);
    437   1.1  christos }
    438   1.1  christos 
    439   1.1  christos 
    440   1.1  christos static void
    441  1.25     lukem addentry(struct fstypelist *list, const char *type, const char *opts)
    442   1.1  christos {
    443   1.2  christos 	struct entry *e;
    444   1.2  christos 
    445   1.2  christos 	e = emalloc(sizeof(struct entry));
    446   1.2  christos 	e->type = estrdup(type);
    447   1.2  christos 	e->options = estrdup(opts);
    448   1.2  christos 	TAILQ_INSERT_TAIL(list, e, entries);
    449   1.1  christos }
    450   1.1  christos 
    451   1.1  christos 
    452   1.1  christos static void
    453  1.25     lukem maketypelist(char *fslist)
    454   1.1  christos {
    455   1.1  christos 	char *ptr;
    456   1.1  christos 
    457   1.1  christos 	if ((fslist == NULL) || (fslist[0] == '\0'))
    458   1.1  christos 		errx(1, "empty type list");
    459   1.1  christos 
    460   1.1  christos 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    461   1.1  christos 		fslist += 2;
    462   1.1  christos 		which = NOT_IN_LIST;
    463   1.1  christos 	}
    464   1.1  christos 	else
    465   1.1  christos 		which = IN_LIST;
    466   1.1  christos 
    467   1.1  christos 	while ((ptr = strsep(&fslist, ",")) != NULL)
    468   1.2  christos 		addentry(&selhead, ptr, "");
    469   1.1  christos 
    470   1.1  christos }
    471   1.1  christos 
    472   1.1  christos 
    473  1.16   mycroft static void
    474  1.25     lukem catopt(char **sp, const char *o)
    475   1.1  christos {
    476  1.16   mycroft 	char *s;
    477  1.16   mycroft 	size_t i, j;
    478   1.1  christos 
    479  1.16   mycroft 	s = *sp;
    480  1.16   mycroft 	if (s) {
    481  1.16   mycroft 		i = strlen(s);
    482  1.16   mycroft 		j = i + 1 + strlen(o) + 1;
    483  1.16   mycroft 		s = erealloc(s, j);
    484  1.16   mycroft 		(void)snprintf(s + i, j, ",%s", o);
    485  1.15   mycroft 	} else
    486  1.16   mycroft 		s = estrdup(o);
    487  1.16   mycroft 	*sp = s;
    488   1.1  christos }
    489   1.1  christos 
    490   1.1  christos 
    491   1.1  christos static void
    492  1.27     lukem mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    493   1.1  christos {
    494   1.1  christos 	char *p, *s;
    495  1.15   mycroft 	int argc, maxargc;
    496  1.15   mycroft 	const char **argv;
    497   1.1  christos 
    498   1.1  christos 	argc = *argcp;
    499  1.15   mycroft 	argv = *argvp;
    500   1.7  christos 	maxargc = *maxargcp;
    501   1.7  christos 
    502  1.27     lukem 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    503  1.15   mycroft 		/* Always leave space for one more argument and the NULL. */
    504   1.7  christos 		if (argc >= maxargc - 3) {
    505  1.15   mycroft 			maxargc <<= 1;
    506   1.7  christos 			argv = erealloc(argv, maxargc * sizeof(char *));
    507   1.7  christos 		}
    508  1.19      ross 		if (*p != '\0')  {
    509   1.1  christos 			if (*p == '-') {
    510   1.1  christos 				argv[argc++] = p;
    511   1.1  christos 				p = strchr(p, '=');
    512   1.1  christos 				if (p) {
    513   1.1  christos 					*p = '\0';
    514   1.1  christos 					argv[argc++] = p+1;
    515   1.1  christos 				}
    516  1.15   mycroft 			} else {
    517   1.1  christos 				argv[argc++] = "-o";
    518   1.1  christos 				argv[argc++] = p;
    519   1.1  christos 			}
    520  1.19      ross 		}
    521   1.7  christos 	}
    522   1.1  christos 
    523   1.1  christos 	*argcp = argc;
    524   1.7  christos 	*argvp = argv;
    525   1.7  christos 	*maxargcp = maxargc;
    526   1.1  christos }
    527   1.8  christos 
    528   1.8  christos 
    529  1.17   mycroft const static char *
    530  1.25     lukem getfslab(const char *str)
    531   1.8  christos {
    532   1.8  christos 	struct disklabel dl;
    533   1.8  christos 	int fd;
    534  1.17   mycroft 	char p;
    535  1.17   mycroft 	const char *vfstype;
    536   1.8  christos 	u_char t;
    537   1.8  christos 
    538  1.26     lukem 	/* deduce the file system type from the disk label */
    539   1.8  christos 	if ((fd = open(str, O_RDONLY)) == -1)
    540   1.8  christos 		err(1, "cannot open `%s'", str);
    541   1.8  christos 
    542   1.8  christos 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    543   1.8  christos 		err(1, "cannot get disklabel for `%s'", str);
    544   1.8  christos 
    545   1.8  christos 	(void) close(fd);
    546   1.8  christos 
    547   1.8  christos 	p = str[strlen(str) - 1];
    548   1.8  christos 
    549   1.8  christos 	if ((p - 'a') >= dl.d_npartitions)
    550   1.8  christos 		errx(1, "partition `%s' is not defined on disk", str);
    551   1.8  christos 
    552  1.13    bouyer 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    553   1.8  christos 		errx(1, "partition `%s' is not of a legal vfstype",
    554  1.11     mikel 		    str);
    555   1.8  christos 
    556   1.8  christos 	if ((vfstype = fscknames[t]) == NULL)
    557   1.8  christos 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    558   1.8  christos 		    fstypenames[t], str);
    559   1.8  christos 
    560   1.8  christos 	return vfstype;
    561   1.8  christos }
    562   1.1  christos 
    563   1.1  christos 
    564   1.1  christos static void
    565  1.25     lukem usage(void)
    566   1.1  christos {
    567   1.2  christos 	static const char common[] =
    568  1.31       dsl 	    "[-dfnpqvy] [-T fstype:fsoptions] [-t fstype]";
    569   1.1  christos 
    570  1.32      jmmv 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
    571  1.23       cgd 	    getprogname(), common);
    572   1.1  christos 	exit(1);
    573   1.1  christos }
    574