]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/mount.cc
* mount.cc (main): Add "-X" option to specify a "Cygwin executable".
[newlib-cygwin.git] / winsup / utils / mount.cc
1 /* mount.cc
2
3 Copyright 1996, 1997, 1998, 1999 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include <stdio.h>
12 #include <sys/mount.h>
13 #include <sys/stat.h>
14 #include <mntent.h>
15 #include <windows.h>
16 #include <sys/cygwin.h>
17 #include "winsup.h"
18 #include "external.h"
19
20 #ifdef errno
21 #undef errno
22 #endif
23 #include <errno.h>
24
25 static void show_mounts (void);
26 static void change_cygdrive_prefix (const char *new_prefix, int flags);
27 static int mount_already_exists (const char *posix_path, int flags);
28
29 // static short create_missing_dirs = FALSE;
30 static short force = FALSE;
31
32 static const char *progname;
33
34 /* FIXME: do_mount should also print a warning message if the dev arg
35 is a non-existent Win32 path. */
36
37 static void
38 do_mount (const char *dev, const char *where, int flags)
39 {
40 struct stat statbuf;
41 char win32_path[MAX_PATH];
42 int statres;
43
44 cygwin_conv_to_win32_path (where, win32_path);
45
46 statres = stat (win32_path, &statbuf);
47
48 #if 0
49 if (statres == -1)
50 {
51 /* FIXME: this'll fail if mount dir is missing any parent dirs */
52 if (create_missing_dirs == TRUE)
53 {
54 if (mkdir (where, 0755) == -1)
55 fprintf (stderr, "Warning: unable to create %s!\n", where);
56 else
57 statres = 0; /* Pretend stat succeeded if we could mkdir. */
58 }
59 }
60 #endif
61
62 if (mount (dev, where, flags))
63 {
64 perror ("mount failed");
65 exit (1);
66 }
67
68 if (statres == -1)
69 {
70 if (force == FALSE)
71 fprintf (stderr, "%s: warning - %s does not exist.\n", progname, where);
72 }
73 else if (!(statbuf.st_mode & S_IFDIR))
74 {
75 if (force == FALSE)
76 fprintf (stderr, "%s: warning: %s is not a directory!\n", progname, where);
77 }
78
79 exit (0);
80 }
81
82 static void
83 usage (void)
84 {
85 fprintf (stderr, "usage %s [-bfstux] <win32path> <posixpath>
86 -b text files are equivalent to binary files (newline = \\n)
87 -f force mount, don't warn about missing mount point directories
88 -s add mount point to system-wide registry location
89 -t text files get \\r\\n line endings (default)
90 -u add mount point to user registry location (default)
91 -x treat all files under mount point as executables
92
93 [-bs] --change-cygdrive-prefix <posixpath>
94 change the cygdrive path prefix to <posixpath>
95 --import-old-mounts
96 copy old registry mount table mounts into the current mount areas
97 ", progname);
98 exit (1);
99 }
100
101 int
102 main (int argc, const char **argv)
103 {
104 int i;
105 int flags = 0;
106
107 progname = argv[0];
108
109 if (argc == 1)
110 {
111 show_mounts ();
112 exit (0);
113 }
114
115 for (i = 1; i < argc; ++i)
116 {
117 if (argv[i][0] != '-')
118 break;
119
120 if (strcmp (argv[i], "--change-cygdrive-prefix") == 0)
121 {
122 if ((i + 2) != argc)
123 usage ();
124
125 change_cygdrive_prefix (argv[i+1], flags);
126 }
127 else if (strcmp (argv[i], "--import-old-mounts") == 0)
128 {
129 if ((i + 1) != argc)
130 usage ();
131
132 cygwin_internal (CW_READ_V1_MOUNT_TABLES);
133 exit (0);
134 }
135 else if (strcmp (argv[i], "-b") == 0)
136 flags |= MOUNT_BINARY;
137 else if (strcmp (argv[i], "-t") == 0)
138 flags &= ~MOUNT_BINARY;
139 else if (strcmp (argv[i], "-X") == 0)
140 flags |= MOUNT_CYGWIN_EXEC;
141 #if 0
142 else if (strcmp (argv[i], "-x") == 0)
143 create_missing_dirs = TRUE;
144 #endif
145 else if (strcmp (argv[i], "-s") == 0)
146 flags |= MOUNT_SYSTEM;
147 else if (strcmp (argv[i], "-u") == 0)
148 flags &= ~MOUNT_SYSTEM;
149 else if (strcmp (argv[i], "-x") == 0)
150 flags |= MOUNT_EXEC;
151 else if (strcmp (argv[i], "-f") == 0)
152 force = TRUE;
153 else
154 usage ();
155 }
156
157 if ((i + 2) != argc)
158 usage ();
159
160 if ((force == FALSE) && (mount_already_exists (argv[i + 1], flags)))
161 {
162 errno = EBUSY;
163 perror ("mount failed");
164 exit (1);
165 }
166 else
167 do_mount (argv[i], argv[i + 1], flags);
168
169 /* NOTREACHED */
170 return 0;
171 }
172
173 static void
174 show_mounts (void)
175 {
176 FILE *m = setmntent ("/-not-used-", "r");
177 struct mntent *p;
178 const char *format = "%-18s %-18s %-11s %s\n";
179
180 printf (format, "Device", "Directory", "Type", "Flags");
181 while ((p = getmntent (m)) != NULL)
182 {
183 printf (format,
184 p->mnt_fsname,
185 p->mnt_dir,
186 p->mnt_type,
187 p->mnt_opts);
188 }
189 endmntent (m);
190 }
191
192 /* Return 1 if mountpoint from the same registry area is already in
193 mount table. Otherwise return 0. */
194 static int
195 mount_already_exists (const char *posix_path, int flags)
196 {
197 int found_matching = 0;
198
199 FILE *m = setmntent ("/-not-used-", "r");
200 struct mntent *p;
201
202 while ((p = getmntent (m)) != NULL)
203 {
204 /* if the paths match, and they're both the same type of mount. */
205 if (strcmp (p->mnt_dir, posix_path) == 0)
206 {
207 if (p->mnt_type[0] == 'u' && !(flags & MOUNT_SYSTEM)) /* both current_user */
208 {
209 found_matching = 1;
210 break;
211 }
212 else if (p->mnt_type[0] == 's' && (flags & MOUNT_SYSTEM)) /* both system */
213 {
214 found_matching = 1;
215 break;
216 }
217 else
218 {
219 fprintf (stderr, "%s: warning -- couldn't determine mount type.\n", progname);
220 break;
221 }
222 }
223 }
224 endmntent (m);
225
226 return found_matching;
227 }
228
229 /* change_cygdrive_prefix: Change the cygdrive prefix */
230 static void
231 change_cygdrive_prefix (const char *new_prefix, int flags)
232 {
233 flags |= MOUNT_AUTO;
234
235 if (mount (NULL, new_prefix, flags))
236 {
237 perror ("mount failed");
238 exit (1);
239 }
240
241 exit (0);
242 }
This page took 0.044866 seconds and 6 git commands to generate.