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