xref: /csrg-svn/sbin/mount/mount.c (revision 1057)
1*1057Sbill static char *sccsid = "@(#)mount.c	4.1 (Berkeley) 10/01/80";
2*1057Sbill #include <stdio.h>
3*1057Sbill #include <fstab.h>
4*1057Sbill 
5*1057Sbill /*
6*1057Sbill  *	Mount file systems.
7*1057Sbill  *
8*1057Sbill  *	mount -a	Mount all file systems, as determined from the
9*1057Sbill  *			file /etc/fstab.
10*1057Sbill  *	If the name entry in /etc/fstab is "/", don't mount.
11*1057Sbill  *	If the read only entry in /etc/fstab is "ro", mount read only
12*1057Sbill  *	The special file names in /etc/fstab are the block devices;
13*1057Sbill  *		this is what we want to mount.
14*1057Sbill  *	Tries to mount all of the files in /etc/fstab.
15*1057Sbill  *
16*1057Sbill  *	mount special name	Mount special on name
17*1057Sbill  *	mount special name -r	Mount special on name, read/write
18*1057Sbill  */
19*1057Sbill 
20*1057Sbill int	mountall;
21*1057Sbill #define	NMOUNT	16
22*1057Sbill #define	NAMSIZ	32
23*1057Sbill 
24*1057Sbill struct mtab {
25*1057Sbill 	char	file[NAMSIZ];
26*1057Sbill 	char	spec[NAMSIZ];
27*1057Sbill } mtab[NMOUNT];
28*1057Sbill 
29*1057Sbill main(argc, argv)
30*1057Sbill char **argv;
31*1057Sbill {
32*1057Sbill 	register int ro;
33*1057Sbill 	register struct mtab *mp;
34*1057Sbill 	register char *np;
35*1057Sbill 	int mf;
36*1057Sbill 
37*1057Sbill 	mountall = 0;
38*1057Sbill 	mf = open("/etc/mtab", 0);
39*1057Sbill 	read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
40*1057Sbill 	if (argc==1) {
41*1057Sbill 		for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
42*1057Sbill 			if (mp->file[0])
43*1057Sbill 				printf("%s on %s\n", mp->spec, mp->file);
44*1057Sbill 		exit(0);
45*1057Sbill 	}
46*1057Sbill 
47*1057Sbill 	if (argc == 2){
48*1057Sbill 		if (strcmp(argv[1], "-a") == 0)
49*1057Sbill 			mountall++;
50*1057Sbill 		else {
51*1057Sbill 			fprintf(stderr,"arg count\n");
52*1057Sbill 			exit(1);
53*1057Sbill 		}
54*1057Sbill 	}
55*1057Sbill 
56*1057Sbill 	if (!mountall){
57*1057Sbill 		ro = 0;
58*1057Sbill 		if(argc > 3)
59*1057Sbill 			ro++;
60*1057Sbill 		if (mountfs(argv[1], argv[2], ro))
61*1057Sbill 			exit(1);
62*1057Sbill 	} else {
63*1057Sbill 		FILE	*fs_file;
64*1057Sbill 		struct	fstab	fs;
65*1057Sbill 		if ( (fs_file = fopen(FSTAB, "r")) == NULL){
66*1057Sbill 			perror(FSTAB);
67*1057Sbill 			exit(1);
68*1057Sbill 		}
69*1057Sbill 		while (!feof(fs_file)){
70*1057Sbill 			fscanf(fs_file, FSTABFMT, FSTABARG(&fs));
71*1057Sbill 			if (strcmp(fs.fs_file, "/") == 0)
72*1057Sbill 				continue;
73*1057Sbill 			fprintf(stderr, "Mounting %s on %s %s",
74*1057Sbill 				fs.fs_file, fs.fs_spec,
75*1057Sbill 				FSRO(&fs) ? "(Read Only)\n" : "\n");
76*1057Sbill 			mountfs(fs.fs_spec, fs.fs_file, FSRO(&fs));
77*1057Sbill 		}
78*1057Sbill 		fclose(fs_file);
79*1057Sbill 	}
80*1057Sbill 	exit(0);
81*1057Sbill }
82*1057Sbill 
83*1057Sbill mountfs(spec, name, ro)
84*1057Sbill 	char	*spec, *name;
85*1057Sbill 	int	ro;
86*1057Sbill {
87*1057Sbill 	register	char	*np;
88*1057Sbill 	register	struct	mtab	*mp;
89*1057Sbill 	int	mf;
90*1057Sbill 
91*1057Sbill 	if(mount(spec, name, ro) < 0) {
92*1057Sbill 		perror("mount");
93*1057Sbill 		return(1);
94*1057Sbill 	}
95*1057Sbill 	np = spec;
96*1057Sbill 	while(*np++)
97*1057Sbill 		;
98*1057Sbill 	np--;
99*1057Sbill 	while(*--np == '/')
100*1057Sbill 		*np = '\0';
101*1057Sbill 	while(np > spec && *--np != '/')
102*1057Sbill 		;
103*1057Sbill 	if(*np == '/')
104*1057Sbill 		np++;
105*1057Sbill 	spec = np;
106*1057Sbill 	for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
107*1057Sbill 		if (mp->file[0] == 0) {
108*1057Sbill 			for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
109*1057Sbill 				if ((*np++ = *spec++) == 0)
110*1057Sbill 					spec--;
111*1057Sbill 			for (np = mp->file; np < &mp->file[NAMSIZ-1];)
112*1057Sbill 				if ((*np++ = *name++) == 0)
113*1057Sbill 					name--;
114*1057Sbill 			mp = &mtab[NMOUNT];
115*1057Sbill 			while ((--mp)->file[0] == 0);
116*1057Sbill 			mf = creat("/etc/mtab", 0644);
117*1057Sbill 			write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
118*1057Sbill 		}
119*1057Sbill 	}
120*1057Sbill 	return(0);
121*1057Sbill }
122