154843Sheideman /*
266473Sbostic  * 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 
1166473Sbostic #ifndef lint
1266473Sbostic char copyright[] =
1366473Sbostic "@(#) Copyright (c) 1992, 1993, 1994\n\
1466473Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1566473Sbostic #endif /* not lint */
1666473Sbostic 
1766473Sbostic #ifndef lint
18*68901Smckusick static char sccsid[] = "@(#)mount_null.c	8.6 (Berkeley) 04/26/95";
1966473Sbostic #endif /* not lint */
2066473Sbostic 
2154843Sheideman #include <sys/param.h>
2254843Sheideman #include <sys/mount.h>
2355038Sbostic #include <miscfs/nullfs/null.h>
2454843Sheideman 
2566473Sbostic #include <err.h>
2654843Sheideman #include <stdio.h>
2754843Sheideman #include <unistd.h>
2854843Sheideman #include <stdlib.h>
2954843Sheideman #include <string.h>
3054843Sheideman 
3166473Sbostic #include "mntopts.h"
3254843Sheideman 
3366473Sbostic struct mntopt mopts[] = {
3466473Sbostic 	MOPT_STDOPTS,
3566473Sbostic 	{ NULL }
3666473Sbostic };
3766473Sbostic 
3866473Sbostic int	subdir __P((const char *, const char *));
3966473Sbostic void	usage __P((void));
4066473Sbostic 
4154843Sheideman int
main(argc,argv)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;
5166473Sbostic 	while ((ch = getopt(argc, argv, "o:")) != EOF)
5254843Sheideman 		switch(ch) {
5366473Sbostic 		case 'o':
54*68901Smckusick 			getmntopts(optarg, mopts, &mntflags, 0);
5554843Sheideman 			break;
5654843Sheideman 		case '?':
5754843Sheideman 		default:
5854843Sheideman 			usage();
5954843Sheideman 		}
6054843Sheideman 	argc -= optind;
6154843Sheideman 	argv += optind;
6254843Sheideman 
6354843Sheideman 	if (argc != 2)
6454843Sheideman 		usage();
6554843Sheideman 
6666473Sbostic 	if (realpath(argv[0], target) == 0)
6766473Sbostic 		err(1, "%s", target);
6854843Sheideman 
6966473Sbostic 	if (subdir(target, argv[1]) || subdir(argv[1], target))
7066473Sbostic 		errx(1, "%s (%s) and %s are not distinct paths",
7166473Sbostic 		    argv[0], target, argv[1]);
7266135Spendry 
7366135Spendry 	args.target = target;
7466135Spendry 
75*68901Smckusick 	if (mount("loopback", argv[1], mntflags, &args))
7666473Sbostic 		err(1, NULL);
7754843Sheideman 	exit(0);
7854843Sheideman }
7954843Sheideman 
8066473Sbostic int
subdir(p,dir)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
usage()9854843Sheideman usage()
9954843Sheideman {
10054843Sheideman 	(void)fprintf(stderr,
10166473Sbostic 		"usage: mount_null [-o options] target_fs mount_point\n");
10254843Sheideman 	exit(1);
10354843Sheideman }
104