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