154698Sbostic /*
2*66472Sbostic * Copyright (c) 1992, 1993, 1994
361509Sbostic * The Regents of the University of California. All rights reserved.
454698Sbostic *
554698Sbostic * This code is derived from software donated to Berkeley by
654698Sbostic * Jan-Simon Pendry.
754698Sbostic *
854698Sbostic * %sccs.include.redist.c%
954698Sbostic */
1054698Sbostic
11*66472Sbostic #ifndef lint
12*66472Sbostic char copyright[] =
13*66472Sbostic "@(#) Copyright (c) 1992, 1993, 1994\n\
14*66472Sbostic The Regents of the University of California. All rights reserved.\n";
15*66472Sbostic #endif /* not lint */
16*66472Sbostic
17*66472Sbostic #ifndef lint
18*66472Sbostic static char sccsid[] = "@(#)mount_lofs.c 8.4 (Berkeley) 03/27/94";
19*66472Sbostic #endif /* not lint */
20*66472Sbostic
2154698Sbostic #include <sys/param.h>
2254698Sbostic #include <sys/mount.h>
23*66472Sbostic
2455026Sbostic #include <miscfs/lofs/lofs.h>
2554698Sbostic
26*66472Sbostic #include <err.h>
2754699Sbostic #include <stdio.h>
2854699Sbostic #include <unistd.h>
2954699Sbostic #include <stdlib.h>
3054699Sbostic #include <string.h>
3154699Sbostic
32*66472Sbostic #include "mntopts.h"
3354699Sbostic
34*66472Sbostic struct mntopt mopts[] = {
35*66472Sbostic MOPT_STDOPTS,
36*66472Sbostic { NULL }
37*66472Sbostic };
38*66472Sbostic
39*66472Sbostic int subdir __P((const char *, const char *));
40*66472Sbostic void usage __P((void));
41*66472Sbostic
4254699Sbostic int
main(argc,argv)4354699Sbostic main(argc, argv)
4454699Sbostic int argc;
4554699Sbostic char *argv[];
4654698Sbostic {
4754698Sbostic struct lofs_args args;
4854699Sbostic int ch, mntflags;
4966134Spendry char target[MAXPATHLEN];
5054698Sbostic
5154699Sbostic mntflags = 0;
52*66472Sbostic while ((ch = getopt(argc, argv, "o:")) != EOF)
53*66472Sbostic switch (ch) {
54*66472Sbostic case 'o':
55*66472Sbostic getmntopts(optarg, mopts, &mntflags);
5654699Sbostic break;
5754699Sbostic case '?':
5854699Sbostic default:
5954699Sbostic usage();
6054699Sbostic }
6154699Sbostic argc -= optind;
6254699Sbostic argv += optind;
6354698Sbostic
6454699Sbostic if (argc != 2)
6554699Sbostic usage();
6654698Sbostic
67*66472Sbostic if (realpath(argv[0], target) == 0)
68*66472Sbostic err(1, "%s", target);
6954698Sbostic
70*66472Sbostic if (subdir(target, argv[1]) || subdir(argv[1], target))
71*66472Sbostic errx(1, "%s (%s) and %s are not distinct paths",
72*66472Sbostic argv[0], target, argv[1]);
7366134Spendry
7466134Spendry args.target = target;
7566134Spendry
76*66472Sbostic if (mount(MOUNT_LOFS, argv[1], mntflags, &args))
77*66472Sbostic err(1, NULL);
7854698Sbostic exit(0);
7954698Sbostic }
8054699Sbostic
81*66472Sbostic int
subdir(p,dir)8266134Spendry subdir(p, dir)
8366223Spendry const char *p;
8466223Spendry const char *dir;
8566134Spendry {
8666134Spendry int l;
8766134Spendry
8866134Spendry l = strlen(dir);
8966134Spendry if (l <= 1)
9066134Spendry return (1);
9166134Spendry
9266223Spendry if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
9366134Spendry return (1);
9466134Spendry
9566134Spendry return (0);
9666134Spendry }
9766134Spendry
9854699Sbostic void
usage()9954699Sbostic usage()
10054699Sbostic {
10154699Sbostic (void)fprintf(stderr,
102*66472Sbostic "usage: mount_lofs [-o options] target_fs mount_point\n");
10354699Sbostic exit(1);
10454699Sbostic }
105