xref: /original-bsd/sbin/mount/mount.c (revision 0b685140)
1 static char *sccsid = "@(#)mount.c	4.6 (Berkeley) 11/25/81";
2 #include <stdio.h>
3 #include <fstab.h>
4 
5 /*
6  * mount
7  */
8 
9 #define	NMOUNT	16
10 #define	NAMSIZ	32
11 
12 struct mtab {
13 	char	file[NAMSIZ];
14 	char	spec[NAMSIZ];
15 } mtab[NMOUNT];
16 
17 int	all;
18 int	ro;
19 int	fake;
20 int	verbose;
21 
22 main(argc, argv)
23 	int argc;
24 	char **argv;
25 {
26 	register struct mtab *mp;
27 	register char *np;
28 	int mf;
29 
30 	mf = open("/etc/mtab", 0);
31 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
32 	if (argc==1) {
33 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
34 			if (mp->file[0])
35 				printf("%s on %s\n", mp->spec, mp->file);
36 		exit(0);
37 	}
38 top:
39 	if (argc > 1) {
40 		if (!strcmp(argv[1], "-a")) {
41 			all++;
42 			argc--, argv++;
43 			goto top;
44 		}
45 		if (!strcmp(argv[1], "-r")) {
46 			ro++;
47 			argc--, argv++;
48 			goto top;
49 		}
50 		if (!strcmp(argv[1], "-f")) {
51 			fake++;
52 			argc--, argv++;
53 			goto top;
54 		}
55 		if (!strcmp(argv[1], "-v")) {
56 			verbose++;
57 			argc--, argv++;
58 			goto top;
59 		}
60 	}
61 	if (all) {
62 		struct fstab *fsp;
63 
64 		if (argc > 1)
65 			goto argcnt;
66 		close(2); dup(1);
67 		if (setfsent() == 0)
68 			perror(FSTAB), exit(1);
69 		while ( (fsp = getfsent()) != 0) {
70 			if (strcmp(fsp->fs_file, "/") == 0)
71 				continue;
72 			ro = !strcmp(fsp->fs_type, FSTAB_RO);
73 			if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
74 				continue;
75 			mountfs(fsp->fs_spec, fsp->fs_file, ro);
76 		}
77 		exit(0);
78 	}
79 	if (argc < 2 || argc > 3) {
80 argcnt:
81 		printf("arg count\n");
82 		exit(1);
83 	}
84 	ro = 0;
85 	if(argc > 3)
86 		ro++;
87 	mountfs(argv[1], argv[2], ro);
88 }
89 
90 mountfs(spec, name, ro)
91 	char *spec, *name;
92 {
93 	register char *np;
94 	register struct mtab *mp;
95 	int mf;
96 
97 	if (fake==0) {
98 		if (mount(spec, name, ro) < 0) {
99 			fprintf(stderr, "%s on ", spec);
100 			perror(name);
101 			return;
102 		}
103 	}
104 	if (verbose)
105 		fprintf(stderr, "%s on %s%s\n", spec, name,
106 		    ro ? " read only" : "");
107 	np = spec;
108 	while (*np++)
109 		;
110 	np--;
111 	while (*--np == '/')
112 		*np = '\0';
113 	while (np > spec && *--np != '/')
114 		;
115 	if (*np == '/')
116 		np++;
117 	spec = np;
118 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
119 		if (!strcmp(mp->spec, spec))
120 			goto replace;
121 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
122 		if (mp->file[0] == 0)
123 			goto replace;
124 	return;
125 replace:
126 	for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
127 		if ((*np++ = *spec++) == 0)
128 			spec--;
129 	for (np = mp->file; np < &mp->file[NAMSIZ-1];)
130 		if ((*np++ = *name++) == 0)
131 			name--;
132 	mp = &mtab[NMOUNT];
133 	while ((--mp)->file[0] == 0);
134 	mf = creat("/etc/mtab", 0644);
135 	write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
136 	close(mf);
137 	return;
138 }
139