xref: /original-bsd/sbin/mount/mount.c (revision 4f485440)
1 #ifndef lint
2 static char *sccsid = "@(#)mount.c	4.10 (Berkeley) 05/28/83";
3 #endif
4 
5 /*
6  * mount
7  */
8 #include <sys/param.h>
9 
10 #include <stdio.h>
11 #include <fstab.h>
12 #include <mtab.h>
13 
14 #define	DNMAX	(sizeof (mtab[0].m_dname) - 1)
15 #define	PNMAX	(sizeof (mtab[0].m_path) - 1)
16 
17 struct	mtab mtab[NMOUNT];
18 
19 int	all;
20 int	ro;
21 int	fake;
22 int	verbose;
23 char	*index(), *rindex();
24 
25 main(argc, argv)
26 	int argc;
27 	char **argv;
28 {
29 	register struct mtab *mp;
30 	register char *np;
31 	int mf;
32 	char *type = FSTAB_RW;
33 
34 	mf = open("/etc/mtab", 0);
35 	read(mf, (char *)mtab, sizeof (mtab));
36 	if (argc == 1) {
37 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
38 			if (mp->m_path[0] != '\0')
39 				prmtab(mp);
40 		exit(0);
41 	}
42 top:
43 	if (argc > 1) {
44 		if (!strcmp(argv[1], "-a")) {
45 			all++;
46 			argc--, argv++;
47 			goto top;
48 		}
49 		if (!strcmp(argv[1], "-r")) {
50 			type = FSTAB_RO;
51 			argc--, argv++;
52 			goto top;
53 		}
54 		if (!strcmp(argv[1], "-f")) {
55 			fake++;
56 			argc--, argv++;
57 			goto top;
58 		}
59 		if (!strcmp(argv[1], "-v")) {
60 			verbose++;
61 			argc--, argv++;
62 			goto top;
63 		}
64 	}
65 	if (all) {
66 		struct fstab *fsp;
67 
68 		if (argc > 1)
69 			goto argcnt;
70 		close(2); dup(1);
71 		if (setfsent() == 0)
72 			perror(FSTAB), exit(1);
73 		while ((fsp = getfsent()) != 0) {
74 			if (strcmp(fsp->fs_file, "/") == 0)
75 				continue;
76 			if (strcmp(fsp->fs_type, FSTAB_RO) &&
77 			    strcmp(fsp->fs_type, FSTAB_RW) &&
78 			    strcmp(fsp->fs_type, FSTAB_RQ))
79 				continue;
80 			mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type);
81 		}
82 		exit(0);
83 	}
84 	if (argc == 2) {
85 		struct fstab *fs;
86 
87 		if (setfsent() == 0)
88 			perror(FSTAB), exit(1);
89 		fs = getfsfile(argv[1]);
90 		if (fs == NULL)
91 			goto argcnt;
92 		mountfs(fs->fs_spec, fs->fs_file, type);
93 		exit(0);
94 	}
95 	if (argc != 3) {
96 argcnt:
97 		fprintf(stderr,
98     "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n");
99 		exit(1);
100 	}
101 	mountfs(argv[1], argv[2], type);
102 }
103 
104 prmtab(mp)
105 	register struct mtab *mp;
106 {
107 
108 	printf("%s on %s", mp->m_dname, mp->m_path);
109 	if (strcmp(mp->m_type, FSTAB_RO) == 0)
110 		printf("\t(read-only)");
111 	if (strcmp(mp->m_type, FSTAB_RQ) == 0)
112 		printf("\t(with quotas)");
113 	putchar('\n');
114 }
115 
116 mountfs(spec, name, type)
117 	char *spec, *name, *type;
118 {
119 	register char *np;
120 	register struct mtab *mp;
121 	int mf;
122 
123 	if (!fake) {
124 		if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) {
125 			fprintf(stderr, "%s on ", spec);
126 			perror(name);
127 			return;
128 		}
129 		/* we don't do quotas.... */
130 		if (strcmp(type, FSTAB_RQ) == 0)
131 			type = FSTAB_RW;
132 	}
133 	np = index(spec, '\0');
134 	while (*--np == '/')
135 		*np = '\0';
136 	np = rindex(spec, '/');
137 	if (np) {
138 		*np++ = '\0';
139 		spec = np;
140 	}
141 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
142 		if (strcmp(mp->m_dname, spec) == 0)
143 			goto replace;
144 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
145 		if (mp->m_path[0] == '\0')
146 			goto replace;
147 	return;
148 replace:
149 	strncpy(mp->m_dname, spec, DNMAX);
150 	mp->m_dname[DNMAX] = '\0';
151 	strncpy(mp->m_path, name, PNMAX);
152 	mp->m_path[PNMAX] = '\0';
153 	strcpy(mp->m_type, type);
154 	if (verbose)
155 		prmtab(mp);
156 	mp = mtab + NMOUNT - 1;
157 	while (mp > mtab && mp->m_path[0] == '\0')
158 		--mp;
159 	mf = creat("/etc/mtab", 0644);
160 	write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab));
161 	close(mf);
162 	return;
163 }
164