]>
Commit | Line | Data |
---|---|---|
1fd5e000 | 1 | /* pathconv.cc -- convert pathnames between Windows and Unix format |
138d4f51 | 2 | Copyright 1998, 1999, 2000 Cygnus Solutions. |
1fd5e000 CF |
3 | Written by Ian Lance Taylor <ian@cygnus.com>. |
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 <string.h> | |
13 | #include <stdlib.h> | |
14 | #include <limits.h> | |
15 | #include <getopt.h> | |
e73a56e9 | 16 | #include <windows.h> |
138d4f51 CF |
17 | #include <io.h> |
18 | #include <sys/fcntl.h> | |
1fd5e000 | 19 | #include <sys/cygwin.h> |
418068d4 | 20 | #include <ctype.h> |
1fd5e000 CF |
21 | |
22 | static char *prog_name; | |
138d4f51 | 23 | static char *file_arg; |
418068d4 CF |
24 | static char *close_arg; |
25 | static int path_flag, unix_flag, windows_flag, absolute_flag; | |
1fd5e000 CF |
26 | |
27 | static struct option long_options[] = | |
28 | { | |
29 | { (char *) "help", no_argument, NULL, 'h' }, | |
418068d4 CF |
30 | { (char *) "absolute", no_argument, NULL, 'a'}, |
31 | { (char *) "option", no_argument, NULL, 'o'}, | |
1fd5e000 | 32 | { (char *) "path", no_argument, NULL, 'p' }, |
418068d4 | 33 | { (char *) "close", required_argument, (int *) &close_arg, 'c'}, |
1fd5e000 | 34 | { (char *) "unix", no_argument, NULL, 'u' }, |
138d4f51 | 35 | { (char *) "file", required_argument, (int *) &file_arg, 'f'}, |
1fd5e000 CF |
36 | { (char *) "version", no_argument, NULL, 'v' }, |
37 | { (char *) "windows", no_argument, NULL, 'w' }, | |
38 | { 0, no_argument, 0, 0 } | |
39 | }; | |
40 | ||
41 | static void | |
42 | usage (FILE *stream, int status) | |
43 | { | |
44 | fprintf (stream, "\ | |
45 | Usage: %s [-p|--path] (-u|--unix)|(-w|--windows) filename\n\ | |
418068d4 CF |
46 | -a|--absolute output absolute path\n\ |
47 | -c|--close handle close handle (for use in captured process)\n\ | |
48 | -f|--file file read file for path information\n\ | |
49 | -u|--unix print Unix form of filename\n\ | |
50 | -w|--windows print Windows form of filename\n\ | |
51 | -p|--path filename argument is a path\n", | |
1fd5e000 CF |
52 | prog_name); |
53 | exit (status); | |
54 | } | |
55 | ||
138d4f51 | 56 | static void |
418068d4 | 57 | doit (char *filename) |
138d4f51 CF |
58 | { |
59 | char *buf; | |
60 | size_t len; | |
61 | ||
62 | if (path_flag) | |
63 | { | |
64 | if (cygwin_posix_path_list_p (filename) | |
65 | ? unix_flag | |
66 | : windows_flag) | |
67 | { | |
68 | /* The path is already in the right format. */ | |
69 | puts (filename); | |
70 | exit (0); | |
71 | } | |
72 | } | |
73 | ||
74 | if (! path_flag) | |
75 | len = strlen (filename) + 100; | |
76 | else | |
77 | { | |
78 | if (unix_flag) | |
79 | len = cygwin_win32_to_posix_path_list_buf_size (filename); | |
80 | else | |
81 | len = cygwin_posix_to_win32_path_list_buf_size (filename); | |
82 | } | |
83 | ||
84 | if (len < PATH_MAX) | |
85 | len = PATH_MAX; | |
86 | ||
87 | buf = (char *) malloc (len); | |
88 | if (buf == NULL) | |
89 | { | |
90 | fprintf (stderr, "%s: out of memory\n", prog_name); | |
91 | exit (1); | |
92 | } | |
93 | ||
94 | if (path_flag) | |
95 | { | |
96 | if (unix_flag) | |
97 | cygwin_win32_to_posix_path_list (filename, buf); | |
98 | else | |
99 | cygwin_posix_to_win32_path_list (filename, buf); | |
100 | } | |
101 | else | |
102 | { | |
103 | if (unix_flag) | |
418068d4 | 104 | (absolute_flag ? cygwin_conv_to_full_posix_path : cygwin_conv_to_posix_path) (filename, buf); |
138d4f51 | 105 | else |
418068d4 | 106 | (absolute_flag ? cygwin_conv_to_full_win32_path : cygwin_conv_to_win32_path) (filename, buf); |
138d4f51 CF |
107 | } |
108 | ||
109 | puts (buf); | |
110 | } | |
111 | ||
1fd5e000 CF |
112 | int |
113 | main (int argc, char **argv) | |
114 | { | |
1fd5e000 | 115 | int c; |
418068d4 | 116 | int options_from_file_flag; |
1fd5e000 | 117 | char *filename; |
1fd5e000 CF |
118 | |
119 | prog_name = strrchr (argv[0], '/'); | |
120 | if (prog_name == NULL) | |
121 | prog_name = strrchr (argv[0], '\\'); | |
122 | if (prog_name == NULL) | |
123 | prog_name = argv[0]; | |
124 | ||
125 | path_flag = 0; | |
126 | unix_flag = 0; | |
127 | windows_flag = 0; | |
418068d4 CF |
128 | options_from_file_flag = 0; |
129 | while ((c = getopt_long (argc, argv, (char *) "hac:f:opuvw", long_options, (int *) NULL)) | |
1fd5e000 CF |
130 | != EOF) |
131 | { | |
132 | switch (c) | |
133 | { | |
418068d4 CF |
134 | case 'a': |
135 | absolute_flag = 1; | |
136 | break; | |
137 | ||
138 | case 'c': | |
139 | CloseHandle ((HANDLE) strtoul (optarg, NULL, 16)); | |
140 | break; | |
141 | ||
138d4f51 CF |
142 | case 'f': |
143 | file_arg = optarg; | |
144 | break; | |
145 | ||
418068d4 CF |
146 | case 'o': |
147 | options_from_file_flag = 1; | |
148 | break; | |
149 | ||
1fd5e000 CF |
150 | case 'p': |
151 | path_flag = 1; | |
152 | break; | |
153 | ||
154 | case 'u': | |
155 | if (unix_flag || windows_flag) | |
156 | usage (stderr, 1); | |
157 | unix_flag = 1; | |
158 | break; | |
159 | ||
160 | case 'w': | |
161 | if (unix_flag || windows_flag) | |
162 | usage (stderr, 1); | |
163 | windows_flag = 1; | |
164 | break; | |
165 | ||
166 | case 'h': | |
167 | usage (stdout, 0); | |
168 | break; | |
169 | ||
170 | case 'v': | |
171 | printf ("Cygwin pathconv version 1.0\n"); | |
172 | printf ("Copyright 1998 Cygnus Solutions\n"); | |
173 | exit (0); | |
174 | ||
175 | default: | |
176 | usage (stderr, 1); | |
177 | break; | |
178 | } | |
179 | } | |
180 | ||
418068d4 CF |
181 | if (options_from_file_flag && !file_arg) |
182 | usage (stderr, 1); | |
183 | ||
184 | if (! unix_flag && ! windows_flag && !options_from_file_flag) | |
1fd5e000 CF |
185 | usage (stderr, 1); |
186 | ||
138d4f51 | 187 | if (!file_arg) |
1fd5e000 | 188 | { |
138d4f51 CF |
189 | if (optind != argc - 1) |
190 | usage (stderr, 1); | |
1fd5e000 | 191 | |
138d4f51 | 192 | filename = argv[optind]; |
418068d4 | 193 | doit (filename); |
138d4f51 | 194 | } |
1fd5e000 CF |
195 | else |
196 | { | |
138d4f51 CF |
197 | FILE *fp; |
198 | char buf[PATH_MAX * 2 + 1]; | |
1fd5e000 | 199 | |
138d4f51 CF |
200 | if (argv[optind]) |
201 | usage (stderr, 1); | |
1fd5e000 | 202 | |
138d4f51 CF |
203 | if (strcmp (file_arg, "-") != 0) |
204 | fp = fopen (file_arg, "rt"); | |
1fd5e000 | 205 | else |
138d4f51 CF |
206 | { |
207 | fp = stdin; | |
208 | setmode (0, O_TEXT); | |
209 | } | |
210 | if (fp == NULL) | |
211 | { | |
212 | perror ("cygpath"); | |
213 | exit (1); | |
214 | } | |
1fd5e000 | 215 | |
418068d4 | 216 | setbuf (stdout, NULL); |
138d4f51 CF |
217 | while (fgets (buf, sizeof (buf), fp) != NULL) |
218 | { | |
418068d4 CF |
219 | char *s = buf; |
220 | char *p = strchr (s, '\n'); | |
138d4f51 CF |
221 | if (p) |
222 | *p = '\0'; | |
418068d4 CF |
223 | if (options_from_file_flag && *s == '-') |
224 | { | |
225 | char c; | |
226 | for (c = *++s; c && !isspace (c); c = *++s) | |
227 | switch (c) | |
228 | { | |
229 | case 'a': | |
230 | absolute_flag = 1; | |
231 | break; | |
232 | case 'w': | |
233 | unix_flag = 0; | |
234 | windows_flag = 1; | |
235 | break; | |
236 | case 'u': | |
237 | windows_flag = 0; | |
238 | unix_flag = 1; | |
239 | break; | |
240 | case 'p': | |
241 | path_flag = 1; | |
242 | } | |
243 | if (*s) | |
244 | do | |
245 | s++; | |
246 | while (*s && isspace (*s)); | |
247 | } | |
248 | if (*s) | |
249 | doit (s); | |
138d4f51 CF |
250 | } |
251 | } | |
1fd5e000 CF |
252 | |
253 | exit (0); | |
254 | } |