Commit | Line | Data |
---|---|---|
a3c5c1bf | 1 | #ifndef lint |
78b57350 | 2 | static char *sccsid = "@(#)mount.c 4.11 (Berkeley) %G%"; |
a3c5c1bf | 3 | #endif |
9dc9fb3d BJ |
4 | |
5 | /* | |
943c4275 | 6 | * mount |
9dc9fb3d | 7 | */ |
7a6d69cb SL |
8 | #include <sys/param.h> |
9 | ||
a3c5c1bf SL |
10 | #include <stdio.h> |
11 | #include <fstab.h> | |
7a6d69cb | 12 | #include <mtab.h> |
78b57350 | 13 | #include <errno.h> |
9dc9fb3d | 14 | |
7a6d69cb SL |
15 | #define DNMAX (sizeof (mtab[0].m_dname) - 1) |
16 | #define PNMAX (sizeof (mtab[0].m_path) - 1) | |
9dc9fb3d | 17 | |
7a6d69cb | 18 | struct mtab mtab[NMOUNT]; |
9dc9fb3d | 19 | |
df611cb9 | 20 | int all; |
abab6b8f | 21 | int ro; |
adf52a13 | 22 | int fake; |
df611cb9 | 23 | int verbose; |
7a6d69cb | 24 | char *index(), *rindex(); |
62c2bd36 | 25 | |
9dc9fb3d | 26 | main(argc, argv) |
62c2bd36 BJ |
27 | int argc; |
28 | char **argv; | |
9dc9fb3d | 29 | { |
9dc9fb3d BJ |
30 | register struct mtab *mp; |
31 | register char *np; | |
32 | int mf; | |
7a6d69cb | 33 | char *type = FSTAB_RW; |
9dc9fb3d | 34 | |
9dc9fb3d | 35 | mf = open("/etc/mtab", 0); |
7a6d69cb SL |
36 | read(mf, (char *)mtab, sizeof (mtab)); |
37 | if (argc == 1) { | |
9dc9fb3d | 38 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) |
7a6d69cb SL |
39 | if (mp->m_path[0] != '\0') |
40 | prmtab(mp); | |
9dc9fb3d BJ |
41 | exit(0); |
42 | } | |
df611cb9 BJ |
43 | top: |
44 | if (argc > 1) { | |
62c2bd36 | 45 | if (!strcmp(argv[1], "-a")) { |
df611cb9 BJ |
46 | all++; |
47 | argc--, argv++; | |
48 | goto top; | |
9dc9fb3d | 49 | } |
62c2bd36 | 50 | if (!strcmp(argv[1], "-r")) { |
7a6d69cb | 51 | type = FSTAB_RO; |
62c2bd36 BJ |
52 | argc--, argv++; |
53 | goto top; | |
54 | } | |
df611cb9 BJ |
55 | if (!strcmp(argv[1], "-f")) { |
56 | fake++; | |
57 | argc--, argv++; | |
58 | goto top; | |
9dc9fb3d | 59 | } |
df611cb9 BJ |
60 | if (!strcmp(argv[1], "-v")) { |
61 | verbose++; | |
62 | argc--, argv++; | |
63 | goto top; | |
64 | } | |
65 | } | |
66 | if (all) { | |
62c2bd36 BJ |
67 | struct fstab *fsp; |
68 | ||
df611cb9 BJ |
69 | if (argc > 1) |
70 | goto argcnt; | |
abab6b8f BJ |
71 | close(2); dup(1); |
72 | if (setfsent() == 0) | |
73 | perror(FSTAB), exit(1); | |
7a6d69cb | 74 | while ((fsp = getfsent()) != 0) { |
abab6b8f | 75 | if (strcmp(fsp->fs_file, "/") == 0) |
9dc9fb3d | 76 | continue; |
7a6d69cb SL |
77 | if (strcmp(fsp->fs_type, FSTAB_RO) && |
78 | strcmp(fsp->fs_type, FSTAB_RW) && | |
fa95fc59 | 79 | strcmp(fsp->fs_type, FSTAB_RQ)) |
943c4275 | 80 | continue; |
7a6d69cb | 81 | mountfs(fsp->fs_spec, fsp->fs_file, fsp->fs_type); |
9dc9fb3d | 82 | } |
df611cb9 | 83 | exit(0); |
9dc9fb3d | 84 | } |
7a6d69cb SL |
85 | if (argc == 2) { |
86 | struct fstab *fs; | |
87 | ||
88 | if (setfsent() == 0) | |
89 | perror(FSTAB), exit(1); | |
90 | fs = getfsfile(argv[1]); | |
91 | if (fs == NULL) | |
92 | goto argcnt; | |
93 | mountfs(fs->fs_spec, fs->fs_file, type); | |
94 | exit(0); | |
95 | } | |
a3c5c1bf | 96 | if (argc != 3) { |
df611cb9 | 97 | argcnt: |
046739cf | 98 | fprintf(stderr, |
7a6d69cb | 99 | "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ] [ dir ]\n"); |
df611cb9 BJ |
100 | exit(1); |
101 | } | |
7a6d69cb | 102 | mountfs(argv[1], argv[2], type); |
abab6b8f | 103 | } |
9dc9fb3d | 104 | |
7a6d69cb SL |
105 | prmtab(mp) |
106 | register struct mtab *mp; | |
107 | { | |
108 | ||
109 | printf("%s on %s", mp->m_dname, mp->m_path); | |
110 | if (strcmp(mp->m_type, FSTAB_RO) == 0) | |
111 | printf("\t(read-only)"); | |
112 | if (strcmp(mp->m_type, FSTAB_RQ) == 0) | |
113 | printf("\t(with quotas)"); | |
114 | putchar('\n'); | |
115 | } | |
116 | ||
117 | mountfs(spec, name, type) | |
118 | char *spec, *name, *type; | |
9dc9fb3d | 119 | { |
df611cb9 BJ |
120 | register char *np; |
121 | register struct mtab *mp; | |
122 | int mf; | |
9dc9fb3d | 123 | |
7a6d69cb SL |
124 | if (!fake) { |
125 | if (mount(spec, name, strcmp(type, FSTAB_RO) == 0) < 0) { | |
78b57350 SL |
126 | extern int errno; |
127 | char *cp; | |
128 | ||
df611cb9 | 129 | fprintf(stderr, "%s on ", spec); |
78b57350 SL |
130 | switch (errno) { |
131 | ||
132 | case EMFILE: | |
133 | cp = "Mount table full"; | |
134 | break; | |
135 | ||
136 | case EINVAL: | |
137 | cp = "Bogus super block"; | |
138 | break; | |
139 | ||
140 | default: | |
141 | perror(name); | |
142 | return; | |
143 | } | |
144 | fprintf(stderr, "%s: %s\n", name, cp); | |
df611cb9 BJ |
145 | return; |
146 | } | |
7a6d69cb SL |
147 | /* we don't do quotas.... */ |
148 | if (strcmp(type, FSTAB_RQ) == 0) | |
149 | type = FSTAB_RW; | |
df611cb9 | 150 | } |
7a6d69cb | 151 | np = index(spec, '\0'); |
df611cb9 | 152 | while (*--np == '/') |
9dc9fb3d | 153 | *np = '\0'; |
7a6d69cb SL |
154 | np = rindex(spec, '/'); |
155 | if (np) { | |
156 | *np++ = '\0'; | |
157 | spec = np; | |
158 | } | |
df611cb9 | 159 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) |
7a6d69cb | 160 | if (strcmp(mp->m_dname, spec) == 0) |
df611cb9 BJ |
161 | goto replace; |
162 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) | |
7a6d69cb | 163 | if (mp->m_path[0] == '\0') |
df611cb9 BJ |
164 | goto replace; |
165 | return; | |
166 | replace: | |
7a6d69cb SL |
167 | strncpy(mp->m_dname, spec, DNMAX); |
168 | mp->m_dname[DNMAX] = '\0'; | |
169 | strncpy(mp->m_path, name, PNMAX); | |
170 | mp->m_path[PNMAX] = '\0'; | |
171 | strcpy(mp->m_type, type); | |
172 | if (verbose) | |
173 | prmtab(mp); | |
174 | mp = mtab + NMOUNT - 1; | |
175 | while (mp > mtab && mp->m_path[0] == '\0') | |
176 | --mp; | |
df611cb9 | 177 | mf = creat("/etc/mtab", 0644); |
7a6d69cb | 178 | write(mf, (char *)mtab, (mp - mtab + 1) * sizeof (struct mtab)); |
df611cb9 BJ |
179 | close(mf); |
180 | return; | |
9dc9fb3d | 181 | } |