Commit | Line | Data |
---|---|---|
a3c5c1bf SL |
1 | #ifndef lint |
2 | static char *sccsid = "@(#)mount.c 4.8 (Berkeley) %G%"; | |
3 | #endif | |
9dc9fb3d BJ |
4 | |
5 | /* | |
943c4275 | 6 | * mount |
9dc9fb3d | 7 | */ |
a3c5c1bf SL |
8 | #include <stdio.h> |
9 | #include <fstab.h> | |
9dc9fb3d | 10 | |
9dc9fb3d BJ |
11 | #define NMOUNT 16 |
12 | #define NAMSIZ 32 | |
13 | ||
14 | struct mtab { | |
15 | char file[NAMSIZ]; | |
16 | char spec[NAMSIZ]; | |
17 | } mtab[NMOUNT]; | |
18 | ||
df611cb9 | 19 | int all; |
abab6b8f | 20 | int ro; |
adf52a13 | 21 | int fake; |
df611cb9 | 22 | int verbose; |
62c2bd36 | 23 | |
9dc9fb3d | 24 | main(argc, argv) |
62c2bd36 BJ |
25 | int argc; |
26 | char **argv; | |
9dc9fb3d | 27 | { |
9dc9fb3d BJ |
28 | register struct mtab *mp; |
29 | register char *np; | |
30 | int mf; | |
31 | ||
9dc9fb3d BJ |
32 | mf = open("/etc/mtab", 0); |
33 | read(mf, (char *)mtab, NMOUNT*2*NAMSIZ); | |
34 | if (argc==1) { | |
35 | for (mp = mtab; mp < &mtab[NMOUNT]; mp++) | |
36 | if (mp->file[0]) | |
37 | printf("%s on %s\n", mp->spec, mp->file); | |
38 | exit(0); | |
39 | } | |
df611cb9 BJ |
40 | top: |
41 | if (argc > 1) { | |
62c2bd36 | 42 | if (!strcmp(argv[1], "-a")) { |
df611cb9 BJ |
43 | all++; |
44 | argc--, argv++; | |
45 | goto top; | |
9dc9fb3d | 46 | } |
62c2bd36 BJ |
47 | if (!strcmp(argv[1], "-r")) { |
48 | ro++; | |
49 | argc--, argv++; | |
50 | goto top; | |
51 | } | |
df611cb9 BJ |
52 | if (!strcmp(argv[1], "-f")) { |
53 | fake++; | |
54 | argc--, argv++; | |
55 | goto top; | |
9dc9fb3d | 56 | } |
df611cb9 BJ |
57 | if (!strcmp(argv[1], "-v")) { |
58 | verbose++; | |
59 | argc--, argv++; | |
60 | goto top; | |
61 | } | |
62 | } | |
63 | if (all) { | |
62c2bd36 BJ |
64 | struct fstab *fsp; |
65 | ||
df611cb9 BJ |
66 | if (argc > 1) |
67 | goto argcnt; | |
abab6b8f BJ |
68 | close(2); dup(1); |
69 | if (setfsent() == 0) | |
70 | perror(FSTAB), exit(1); | |
df611cb9 | 71 | while ( (fsp = getfsent()) != 0) { |
abab6b8f | 72 | if (strcmp(fsp->fs_file, "/") == 0) |
9dc9fb3d | 73 | continue; |
abab6b8f BJ |
74 | ro = !strcmp(fsp->fs_type, FSTAB_RO); |
75 | if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW)) | |
943c4275 | 76 | continue; |
df611cb9 | 77 | mountfs(fsp->fs_spec, fsp->fs_file, ro); |
9dc9fb3d | 78 | } |
df611cb9 | 79 | exit(0); |
9dc9fb3d | 80 | } |
a3c5c1bf | 81 | if (argc != 3) { |
df611cb9 | 82 | argcnt: |
046739cf BJ |
83 | fprintf(stderr, |
84 | "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n"); | |
df611cb9 BJ |
85 | exit(1); |
86 | } | |
df611cb9 | 87 | mountfs(argv[1], argv[2], ro); |
abab6b8f | 88 | } |
9dc9fb3d BJ |
89 | |
90 | mountfs(spec, name, ro) | |
df611cb9 | 91 | char *spec, *name; |
9dc9fb3d | 92 | { |
df611cb9 BJ |
93 | register char *np; |
94 | register struct mtab *mp; | |
95 | int mf; | |
9dc9fb3d | 96 | |
df611cb9 BJ |
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" : ""); | |
9dc9fb3d | 107 | np = spec; |
df611cb9 | 108 | while (*np++) |
9dc9fb3d BJ |
109 | ; |
110 | np--; | |
df611cb9 | 111 | while (*--np == '/') |
9dc9fb3d | 112 | *np = '\0'; |
df611cb9 | 113 | while (np > spec && *--np != '/') |
9dc9fb3d | 114 | ; |
df611cb9 | 115 | if (*np == '/') |
9dc9fb3d BJ |
116 | np++; |
117 | spec = np; | |
df611cb9 BJ |
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; | |
9dc9fb3d | 138 | } |