xref: /csrg-svn/sbin/mount/mount.c (revision 4460)
1*4460Sroot static char *sccsid = "@(#)mount.c	4.5 (Berkeley) 10/03/81";
21057Sbill #include <stdio.h>
31057Sbill #include <fstab.h>
41057Sbill 
51057Sbill /*
61414Sbill  * mount
71057Sbill  */
81057Sbill 
91057Sbill #define	NMOUNT	16
101057Sbill #define	NAMSIZ	32
111057Sbill 
121057Sbill struct mtab {
131057Sbill 	char	file[NAMSIZ];
141057Sbill 	char	spec[NAMSIZ];
151057Sbill } mtab[NMOUNT];
161057Sbill 
17*4460Sroot int	all;
181442Sbill int	ro;
194010Sroot int	fake;
20*4460Sroot int	verbose;
211057Sbill main(argc, argv)
221057Sbill char **argv;
231057Sbill {
241057Sbill 	register struct mtab *mp;
251057Sbill 	register char *np;
261057Sbill 	int mf;
271057Sbill 
281057Sbill 	mf = open("/etc/mtab", 0);
291057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
301057Sbill 	if (argc==1) {
311057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
321057Sbill 			if (mp->file[0])
331057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
341057Sbill 		exit(0);
351057Sbill 	}
36*4460Sroot top:
37*4460Sroot 	if (argc > 1) {
38*4460Sroot 		if (strcmp(argv[1], "-a") == 0) {
39*4460Sroot 			all++;
40*4460Sroot 			argc--, argv++;
41*4460Sroot 			goto top;
421057Sbill 		}
43*4460Sroot 		if (!strcmp(argv[1], "-f")) {
44*4460Sroot 			fake++;
45*4460Sroot 			argc--, argv++;
46*4460Sroot 			goto top;
47*4460Sroot 		}
48*4460Sroot 		if (!strcmp(argv[1], "-v")) {
49*4460Sroot 			verbose++;
50*4460Sroot 			argc--, argv++;
51*4460Sroot 			goto top;
52*4460Sroot 		}
531057Sbill 	}
54*4460Sroot 	if (all) {
551442Sbill 		struct	fstab	*fsp;
56*4460Sroot 		if (argc > 1)
57*4460Sroot 			goto argcnt;
581442Sbill 		close(2); dup(1);
591442Sbill 		if (setfsent() == 0)
601442Sbill 			perror(FSTAB), exit(1);
61*4460Sroot 		while ( (fsp = getfsent()) != 0) {
621442Sbill 			if (strcmp(fsp->fs_file, "/") == 0)
631057Sbill 				continue;
641442Sbill 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
651442Sbill 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
661414Sbill 				continue;
67*4460Sroot 			mountfs(fsp->fs_spec, fsp->fs_file, ro);
681057Sbill 		}
69*4460Sroot 		exit(0);
701057Sbill 	}
71*4460Sroot 	if (argc < 2 || argc > 3) {
72*4460Sroot argcnt:
73*4460Sroot 		printf("arg count\n");
74*4460Sroot 		exit(1);
75*4460Sroot 	}
76*4460Sroot 	ro = 0;
77*4460Sroot 	if(argc > 3)
78*4460Sroot 		ro++;
79*4460Sroot 	mountfs(argv[1], argv[2], ro);
801057Sbill }
811057Sbill 
821057Sbill mountfs(spec, name, ro)
83*4460Sroot 	char *spec, *name;
841057Sbill {
85*4460Sroot 	register char *np;
86*4460Sroot 	register struct mtab *mp;
87*4460Sroot 	int mf;
881057Sbill 
89*4460Sroot 	if (fake==0) {
90*4460Sroot 		if (mount(spec, name, ro) < 0) {
91*4460Sroot 			fprintf(stderr, "%s on ", spec);
92*4460Sroot 			perror(name);
93*4460Sroot 			return;
94*4460Sroot 		}
95*4460Sroot 	}
96*4460Sroot 	if (verbose)
97*4460Sroot 		fprintf(stderr, "%s on %s%s\n", spec, name,
98*4460Sroot 		    ro ? " read only" : "");
991057Sbill 	np = spec;
100*4460Sroot 	while (*np++)
1011057Sbill 		;
1021057Sbill 	np--;
103*4460Sroot 	while (*--np == '/')
1041057Sbill 		*np = '\0';
105*4460Sroot 	while (np > spec && *--np != '/')
1061057Sbill 		;
107*4460Sroot 	if (*np == '/')
1081057Sbill 		np++;
1091057Sbill 	spec = np;
110*4460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
111*4460Sroot 		if (!strcmp(mp->spec, spec))
112*4460Sroot 			goto replace;
113*4460Sroot 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
114*4460Sroot 		if (mp->file[0] == 0)
115*4460Sroot 			goto replace;
116*4460Sroot 	return;
117*4460Sroot replace:
118*4460Sroot 	for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
119*4460Sroot 		if ((*np++ = *spec++) == 0)
120*4460Sroot 			spec--;
121*4460Sroot 	for (np = mp->file; np < &mp->file[NAMSIZ-1];)
122*4460Sroot 		if ((*np++ = *name++) == 0)
123*4460Sroot 			name--;
124*4460Sroot 	mp = &mtab[NMOUNT];
125*4460Sroot 	while ((--mp)->file[0] == 0);
126*4460Sroot 	mf = creat("/etc/mtab", 0644);
127*4460Sroot 	write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
128*4460Sroot 	close(mf);
129*4460Sroot 	return;
1301057Sbill }
131