xref: /original-bsd/old/mount_lofs/mount_lofs.c (revision f737e041)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)mount_lofs.c	8.3 (Berkeley) 02/22/94
12  */
13 
14 #include <sys/param.h>
15 #include <sys/mount.h>
16 #include <miscfs/lofs/lofs.h>
17 
18 #include <errno.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 static int subdir __P((const char *, const char *));
25 void usage __P((void));
26 
27 int
28 main(argc, argv)
29 	int argc;
30 	char *argv[];
31 {
32 	struct lofs_args args;
33 	int ch, mntflags;
34 	char target[MAXPATHLEN];
35 
36 	mntflags = 0;
37 	while ((ch = getopt(argc, argv, "F:")) != EOF)
38 		switch(ch) {
39 		case 'F':
40 			mntflags = atoi(optarg);
41 			break;
42 		case '?':
43 		default:
44 			usage();
45 		}
46 	argc -= optind;
47 	argv += optind;
48 
49 	if (argc != 2)
50 		usage();
51 
52 	if (realpath(argv[0], target) == 0) {
53 		(void)fprintf(stderr, "mount_lofs: %s: %s\n",
54 				target, strerror(errno));
55 		exit(1);
56 	}
57 
58 	if (subdir(target, argv[1]) || subdir(argv[1], target)) {
59 		(void)fprintf(stderr,
60 			"mount_lofs: %s (%s) and %s are not distinct paths\n",
61 				argv[0], target, argv[1]);
62 		exit(1);
63 	}
64 
65 	args.target = target;
66 
67 	if (mount(MOUNT_LOFS, argv[1], mntflags, &args)) {
68 		(void)fprintf(stderr, "mount_lofs: %s\n", strerror(errno));
69 		exit(1);
70 	}
71 	exit(0);
72 }
73 
74 static int
75 subdir(p, dir)
76 	const char *p;
77 	const char *dir;
78 {
79 	int l;
80 
81 	l = strlen(dir);
82 	if (l <= 1)
83 		return (1);
84 
85 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
86 		return (1);
87 
88 	return (0);
89 }
90 
91 void
92 usage()
93 {
94 	(void)fprintf(stderr,
95 	    "usage: mount_lofs [ -F fsoptions ] target_fs mount_point\n");
96 	exit(1);
97 }
98