154843Sheideman /*
261513Sbostic  * Copyright (c) 1992, 1993
361513Sbostic  *	The Regents of the University of California.  All rights reserved.
454843Sheideman  * All rights reserved.
554843Sheideman  *
654843Sheideman  * This code is derived from software donated to Berkeley by
754843Sheideman  * Jan-Simon Pendry.
854843Sheideman  *
954843Sheideman  * %sccs.include.redist.c%
1054843Sheideman  *
11*66155Spendry  *	@(#)mount_null.c	8.3 (Berkeley) 02/18/94
1254843Sheideman  */
1354843Sheideman 
1454843Sheideman #include <sys/param.h>
1554843Sheideman #include <sys/mount.h>
1655038Sbostic #include <miscfs/nullfs/null.h>
1754843Sheideman 
1854843Sheideman #include <errno.h>
1954843Sheideman #include <stdio.h>
2054843Sheideman #include <unistd.h>
2154843Sheideman #include <stdlib.h>
2254843Sheideman #include <string.h>
2354843Sheideman 
2466135Spendry static int subdir __P((char *, char *));
2554843Sheideman void usage __P((void));
2654843Sheideman 
2754843Sheideman int
2854843Sheideman main(argc, argv)
2954843Sheideman 	int argc;
3054843Sheideman 	char *argv[];
3154843Sheideman {
3254952Sheideman 	struct null_args args;
3354843Sheideman 	int ch, mntflags;
3466135Spendry 	char target[MAXPATHLEN];
3554843Sheideman 
3654843Sheideman 	mntflags = 0;
3754843Sheideman 	while ((ch = getopt(argc, argv, "F:")) != EOF)
3854843Sheideman 		switch(ch) {
3954843Sheideman 		case 'F':
4054843Sheideman 			mntflags = atoi(optarg);
4154843Sheideman 			break;
4254843Sheideman 		case '?':
4354843Sheideman 		default:
4454843Sheideman 			usage();
4554843Sheideman 		}
4654843Sheideman 	argc -= optind;
4754843Sheideman 	argv += optind;
4854843Sheideman 
4954843Sheideman 	if (argc != 2)
5054843Sheideman 		usage();
5154843Sheideman 
5266135Spendry 	if (realpath(argv[0], target) == 0) {
53*66155Spendry 		(void)fprintf(stderr, "mount_null: %s: %s\n",
5466135Spendry 				target, strerror(errno));
5566135Spendry 		exit(1);
5666135Spendry 	}
5754843Sheideman 
5866135Spendry 	if (subdir(target, argv[1])) {
5966135Spendry 		(void)fprintf(stderr,
6066135Spendry 			"mount_null: %s (%s) is a sub directory of %s\n",
6166135Spendry 				argv[0], target, argv[1]);
6266135Spendry 		exit(1);
6366135Spendry 	}
6466135Spendry 
6566135Spendry 	args.target = target;
6666135Spendry 
6754952Sheideman 	if (mount(MOUNT_NULL, argv[1], mntflags, &args)) {
6854952Sheideman 		(void)fprintf(stderr, "mount_null: %s\n", strerror(errno));
6954843Sheideman 		exit(1);
7054843Sheideman 	}
7154843Sheideman 	exit(0);
7254843Sheideman }
7354843Sheideman 
7466135Spendry static int
7566135Spendry subdir(p, dir)
7666135Spendry 	char *p;
7766135Spendry 	char *dir;
7866135Spendry {
7966135Spendry 	int l;
8066135Spendry 
8166135Spendry 	l = strlen(dir);
8266135Spendry 	if (l <= 1)
8366135Spendry 		return (1);
8466135Spendry 
8566135Spendry 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/'))
8666135Spendry 		return (1);
8766135Spendry 
8866135Spendry 	return (0);
8966135Spendry }
9066135Spendry 
9154843Sheideman void
9254843Sheideman usage()
9354843Sheideman {
9454843Sheideman 	(void)fprintf(stderr,
9554952Sheideman 	    "usage: mount_null [ -F fsoptions ] target_fs mount_point\n");
9654843Sheideman 	exit(1);
9754843Sheideman }
98