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