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