154843Sheideman /*
2*66473Sbostic  * Copyright (c) 1992, 1993, 1994
361513Sbostic  *	The Regents of the University of California.  All rights reserved.
454843Sheideman  *
554843Sheideman  * This code is derived from software donated to Berkeley by
654843Sheideman  * Jan-Simon Pendry.
754843Sheideman  *
854843Sheideman  * %sccs.include.redist.c%
954843Sheideman  */
1054843Sheideman 
11*66473Sbostic #ifndef lint
12*66473Sbostic char copyright[] =
13*66473Sbostic "@(#) Copyright (c) 1992, 1993, 1994\n\
14*66473Sbostic 	The Regents of the University of California.  All rights reserved.\n";
15*66473Sbostic #endif /* not lint */
16*66473Sbostic 
17*66473Sbostic #ifndef lint
18*66473Sbostic static char sccsid[] = "@(#)mount_null.c	8.5 (Berkeley) 03/27/94";
19*66473Sbostic #endif /* not lint */
20*66473Sbostic 
2154843Sheideman #include <sys/param.h>
2254843Sheideman #include <sys/mount.h>
2355038Sbostic #include <miscfs/nullfs/null.h>
2454843Sheideman 
25*66473Sbostic #include <err.h>
2654843Sheideman #include <stdio.h>
2754843Sheideman #include <unistd.h>
2854843Sheideman #include <stdlib.h>
2954843Sheideman #include <string.h>
3054843Sheideman 
31*66473Sbostic #include "mntopts.h"
3254843Sheideman 
33*66473Sbostic struct mntopt mopts[] = {
34*66473Sbostic 	MOPT_STDOPTS,
35*66473Sbostic 	{ NULL }
36*66473Sbostic };
37*66473Sbostic 
38*66473Sbostic int	subdir __P((const char *, const char *));
39*66473Sbostic void	usage __P((void));
40*66473Sbostic 
4154843Sheideman int
4254843Sheideman main(argc, argv)
4354843Sheideman 	int argc;
4454843Sheideman 	char *argv[];
4554843Sheideman {
4654952Sheideman 	struct null_args args;
4754843Sheideman 	int ch, mntflags;
4866135Spendry 	char target[MAXPATHLEN];
4954843Sheideman 
5054843Sheideman 	mntflags = 0;
51*66473Sbostic 	while ((ch = getopt(argc, argv, "o:")) != EOF)
5254843Sheideman 		switch(ch) {
53*66473Sbostic 		case 'o':
54*66473Sbostic 			getmntopts(optarg, mopts, &mntflags);
5554843Sheideman 			break;
5654843Sheideman 		case '?':
5754843Sheideman 		default:
5854843Sheideman 			usage();
5954843Sheideman 		}
6054843Sheideman 	argc -= optind;
6154843Sheideman 	argv += optind;
6254843Sheideman 
6354843Sheideman 	if (argc != 2)
6454843Sheideman 		usage();
6554843Sheideman 
66*66473Sbostic 	if (realpath(argv[0], target) == 0)
67*66473Sbostic 		err(1, "%s", target);
6854843Sheideman 
69*66473Sbostic 	if (subdir(target, argv[1]) || subdir(argv[1], target))
70*66473Sbostic 		errx(1, "%s (%s) and %s are not distinct paths",
71*66473Sbostic 		    argv[0], target, argv[1]);
7266135Spendry 
7366135Spendry 	args.target = target;
7466135Spendry 
75*66473Sbostic 	if (mount(MOUNT_NULL, argv[1], mntflags, &args))
76*66473Sbostic 		err(1, NULL);
7754843Sheideman 	exit(0);
7854843Sheideman }
7954843Sheideman 
80*66473Sbostic int
8166135Spendry subdir(p, dir)
8266223Spendry 	const char *p;
8366223Spendry 	const char *dir;
8466135Spendry {
8566135Spendry 	int l;
8666135Spendry 
8766135Spendry 	l = strlen(dir);
8866135Spendry 	if (l <= 1)
8966135Spendry 		return (1);
9066135Spendry 
9166223Spendry 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
9266135Spendry 		return (1);
9366135Spendry 
9466135Spendry 	return (0);
9566135Spendry }
9666135Spendry 
9754843Sheideman void
9854843Sheideman usage()
9954843Sheideman {
10054843Sheideman 	(void)fprintf(stderr,
101*66473Sbostic 		"usage: mount_null [-o options] target_fs mount_point\n");
10254843Sheideman 	exit(1);
10354843Sheideman }
104