]> sourceware.org Git - newlib-cygwin.git/blame - winsup/utils/cygpath.cc
* dcrt0.cc (_dll_crt0): Don't check sync_startup if threadfunc_ix is set.
[newlib-cygwin.git] / winsup / utils / cygpath.cc
CommitLineData
e05f3227 1/* cygpath.cc -- convert pathnames between Windows and Unix format
7c03f799 2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
1fd5e000
CF
3
4This file is part of Cygwin.
5
6This software is a copyrighted work licensed under the terms of the
7Cygwin license. Please consult the file "CYGWIN_LICENSE" for
8details. */
9
3cdacffc
CV
10#define NOCOMATTRIBUTE
11
12#include <shlobj.h>
1fd5e000
CF
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16#include <limits.h>
17#include <getopt.h>
e73a56e9 18#include <windows.h>
138d4f51
CF
19#include <io.h>
20#include <sys/fcntl.h>
1fd5e000 21#include <sys/cygwin.h>
418068d4 22#include <ctype.h>
7c03f799 23#include <errno.h>
1fd5e000 24
6a344609
CF
25static const char version[] = "$Revision$";
26
1fd5e000 27static char *prog_name;
138d4f51 28static char *file_arg;
418068d4 29static int path_flag, unix_flag, windows_flag, absolute_flag;
5bb52de4
CV
30static int shortname_flag, longname_flag;
31static int ignore_flag, allusers_flag, output_flag;
2bd6505b 32static int mixed_flag;
1050e57c 33static const char *format_type_arg;
1fd5e000 34
6a344609 35static struct option long_options[] = {
6a344609 36 {(char *) "absolute", no_argument, NULL, 'a'},
1050e57c
CF
37 {(char *) "close", required_argument, NULL, 'c'},
38 {(char *) "dos", no_argument, NULL, 'd'},
39 {(char *) "file", required_argument, NULL, 'f'},
40 {(char *) "help", no_argument, NULL, 'h'},
41 {(char *) "ignore", no_argument, NULL, 'i'},
42 {(char *) "long-name", no_argument, NULL, 'l'},
43 {(char *) "mixed", no_argument, NULL, 'm'},
6a344609
CF
44 {(char *) "option", no_argument, NULL, 'o'},
45 {(char *) "path", no_argument, NULL, 'p'},
1050e57c
CF
46 {(char *) "short-name", no_argument, NULL, 's'},
47 {(char *) "type", required_argument, NULL, 't'},
6a344609 48 {(char *) "unix", no_argument, NULL, 'u'},
6a344609
CF
49 {(char *) "version", no_argument, NULL, 'v'},
50 {(char *) "windows", no_argument, NULL, 'w'},
6a344609
CF
51 {(char *) "allusers", no_argument, NULL, 'A'},
52 {(char *) "desktop", no_argument, NULL, 'D'},
e355de81 53 {(char *) "homeroot", no_argument, NULL, 'H'},
1050e57c
CF
54 {(char *) "smprograms", no_argument, NULL, 'P'},
55 {(char *) "sysdir", no_argument, NULL, 'S'},
56 {(char *) "windir", no_argument, NULL, 'W'},
6a344609 57 {0, no_argument, 0, 0}
1fd5e000
CF
58};
59
9b566b96
JDF
60static char options[] = "ac:df:hilmopst:uvwADHPSW";
61
1fd5e000 62static void
6a344609 63usage (FILE * stream, int status)
1fd5e000 64{
45b80bb4
CF
65 if (!ignore_flag || !status)
66 fprintf (stream, "\
1050e57c
CF
67Usage: %s (-d|-m|-u|-w|-t TYPE) [-c HANDLE] [-f FILE] [options] NAME\n\
68 %s [-ADHPSW] \n\
aa275fe0 69Convert Unix and Windows format paths, or output system path information\n\
bd79b736 70\n\
1050e57c
CF
71Output type options:\n\
72 -d, --dos print DOS (short) form of NAME (C:\\PROGRA~1\\)\n\
73 -m, --mixed like --windows, but with regular slashes (C:/WINNT)\n\
74 -u, --unix (default) print Unix form of NAME (/cygdrive/c/winnt)\n\
75 -w, --windows print Windows form of NAME (C:\\WINNT)\n\
76 -t, --type TYPE print TYPE form: 'dos', 'mixed', 'unix', or 'windows'\n\
33bd2d12 77Path conversion options:\n\
1050e57c
CF
78 -a, --absolute output absolute path\n\
79 -l, --long-name print Windows long form of NAME (with -w, -m only)\n\
80 -p, --path NAME is a PATH list (i.e., '/bin:/usr/bin')\n\
81 -s, --short-name print DOS (short) form of NAME (with -w, -m only)\n\
82System information:\n\
83 -A, --allusers use `All Users' instead of current user for -D, -P\n\
84 -D, --desktop output `Desktop' directory and exit\n\
85 -H, --homeroot output `Profiles' directory (home root) and exit\n\
86 -P, --smprograms output Start Menu `Programs' directory and exit\n\
87 -S, --sysdir output system directory and exit\n\
88 -W, --windir output `Windows' directory and exit\n\
89", prog_name, prog_name);
90 if (stream == stdout)
91 {
92 fprintf (stream, "\
33bd2d12 93Other options:\n\
1050e57c
CF
94 -f, --file FILE read FILE for input; use - to read from STDIN\n\
95 -o, --option read options from FILE as well (for use with --file)\n\
96 -c, --close HANDLE close HANDLE (for use in captured process)\n\
97 -i, --ignore ignore missing argument\n\
98 -h, --help output usage information and exit\n\
02eb4ad4 99 -v, --version output version information and exit\n\
1050e57c
CF
100");
101 }
102 else
02eb4ad4 103 fprintf(stream, "Try `%s --help' for more information.\n", prog_name);
45b80bb4
CF
104 exit (ignore_flag ? 0 : status);
105}
106
107static char *
108get_short_paths (char *path)
109{
110 char *sbuf;
111 char *sptr;
112 char *next;
113 char *ptr = path;
114 char *end = strrchr (path, 0);
115 DWORD acc = 0;
116 DWORD len;
117
118 while (ptr != NULL)
45b80bb4 119 {
6a344609
CF
120 next = ptr;
121 ptr = strchr (ptr, ';');
122 if (ptr)
123 *ptr++ = 0;
124 len = GetShortPathName (next, NULL, 0);
cc3ce0bb 125 if (!len)
6a344609
CF
126 {
127 fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
128 next);
129 exit (2);
130 }
131 acc += len + 1;
45b80bb4 132 }
6a344609 133 sptr = sbuf = (char *) malloc (acc + 1);
45b80bb4 134 if (sbuf == NULL)
45b80bb4 135 {
6a344609
CF
136 fprintf (stderr, "%s: out of memory\n", prog_name);
137 exit (1);
45b80bb4 138 }
6a344609
CF
139 ptr = path;
140 for (;;)
141 {
e355de81 142 len = GetShortPathName (ptr, sptr, acc);
cc3ce0bb 143 if (!len)
6a344609
CF
144 {
145 fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
146 ptr);
147 exit (2);
148 }
45b80bb4 149
6a344609
CF
150 ptr = strrchr (ptr, 0);
151 sptr = strrchr (sptr, 0);
152 if (ptr == end)
153 break;
154 *sptr = ';';
155 ++ptr, ++sptr;
5bb52de4 156 acc -= len + 1;
6a344609 157 }
45b80bb4
CF
158 return sbuf;
159}
160
161static char *
162get_short_name (const char *filename)
163{
5bb52de4
CV
164 char *sbuf, buf[MAX_PATH];
165 DWORD len = GetShortPathName (filename, buf, MAX_PATH);
cc3ce0bb 166 if (!len)
6a344609
CF
167 {
168 fprintf (stderr, "%s: cannot create short name of %s\n", prog_name,
169 filename);
170 exit (2);
171 }
172 sbuf = (char *) malloc (++len);
45b80bb4 173 if (sbuf == NULL)
6a344609
CF
174 {
175 fprintf (stderr, "%s: out of memory\n", prog_name);
176 exit (1);
177 }
5bb52de4
CV
178 return strcpy (sbuf, buf);
179}
180
cf157504 181static DWORD WINAPI
5bb52de4
CV
182get_long_path_name_w32impl (LPCSTR src, LPSTR sbuf, DWORD)
183{
184 char buf1[MAX_PATH], buf2[MAX_PATH], *ptr;
185 const char *pelem, *next;
186 WIN32_FIND_DATA w32_fd;
187 int len;
cf157504 188
5bb52de4
CV
189 strcpy (buf1, src);
190 *buf2 = 0;
191 pelem = src;
192 ptr = buf2;
193 while (pelem)
194 {
195 next = pelem;
196 if (*next == '\\')
197 {
198 strcat (ptr++, "\\");
199 pelem++;
200 if (!*pelem)
201 break;
202 continue;
203 }
204 pelem = strchr (next, '\\');
205 len = pelem ? (pelem++ - next) : strlen (next);
206 strncpy (ptr, next, len);
207 ptr[len] = 0;
208 if (next[1] != ':' && strcmp(next, ".") && strcmp(next, ".."))
209 {
210 if (FindFirstFile (buf2, &w32_fd) != INVALID_HANDLE_VALUE)
211 strcpy (ptr, w32_fd.cFileName);
212 }
213 ptr += strlen (ptr);
214 if (pelem)
215 {
216 *ptr++ = '\\';
217 *ptr = 0;
218 }
219 }
220 if (sbuf)
221 strcpy (sbuf, buf2);
222 SetLastError (0);
223 return strlen (buf2) + (sbuf ? 0 : 1);
224}
225
226static char *
cf157504 227get_long_name (const char *filename, DWORD& len)
5bb52de4 228{
cf157504
CF
229 char *sbuf, buf[MAX_PATH];
230 static HINSTANCE k32 = LoadLibrary ("kernel32.dll");
231 static DWORD (WINAPI *GetLongPathName) (LPCSTR, LPSTR, DWORD) =
232 (DWORD (WINAPI *) (LPCSTR, LPSTR, DWORD)) GetProcAddress (k32, "GetLongPathName");
233 if (!GetLongPathName)
234 GetLongPathName = get_long_path_name_w32impl;
5bb52de4 235
cf157504
CF
236 len = GetLongPathName (filename, buf, MAX_PATH);
237 if (len == 0 && GetLastError () == ERROR_INVALID_PARAMETER)
5bb52de4 238 {
cf157504
CF
239 fprintf (stderr, "%s: cannot create long name of %s\n", prog_name,
240 filename);
241 exit (2);
5bb52de4 242 }
cf157504
CF
243 sbuf = (char *) malloc (len + 1);
244 if (!sbuf)
5bb52de4
CV
245 {
246 fprintf (stderr, "%s: out of memory\n", prog_name);
247 exit (1);
248 }
cf157504 249 return strcpy (sbuf, buf);
5bb52de4
CV
250}
251
252static char *
cf157504 253get_long_paths (char *path)
5bb52de4 254{
cf157504
CF
255 char *sbuf;
256 char *ptr;
257 int n = 1;
258
259 ptr = path;
260 while ((ptr = strchr (ptr, ';')))
6a344609 261 {
cf157504
CF
262 ptr++;
263 n++;
6a344609 264 }
cf157504
CF
265
266 char *paths[n];
267 DWORD acc = 0;
268 int i;
269 if (!n)
270 return strdup ("");
271
272 for (i = 0, ptr = path; ptr; i++)
273 {
274 DWORD len;
275 char *next = ptr;
276 ptr = strchr (ptr, ';');
277 if (ptr)
278 *ptr++ = 0;
279 paths[i] = get_long_name (next, len);
280 acc += len + 1;
281 }
282
283 sbuf = (char *) malloc (acc + 1);
5bb52de4
CV
284 if (sbuf == NULL)
285 {
286 fprintf (stderr, "%s: out of memory\n", prog_name);
287 exit (1);
288 }
cf157504
CF
289
290 sbuf[0] = '\0';
291 for (i = 0; i < n; i++)
292 {
293 strcat (strcat (sbuf, paths[i]), ";");
294 free (paths[i]);
295 }
296
297 strchr (sbuf, '\0')[-1] = '\0';
298 return sbuf;
5bb52de4
CV
299}
300
33bd2d12
CF
301static void
302convert_slashes (char* name)
303{
304 while ((name = strchr (name, '\\')) != NULL)
305 {
306 if (*name == '\\')
307 *name = '/';
308 name++;
309 }
310}
311
312static char *
313get_mixed_name (const char* filename)
314{
315 char* mixed_buf = strdup (filename);
316
317 if (mixed_buf == NULL)
318 {
319 fprintf (stderr, "%s: out of memory\n", prog_name);
320 exit (1);
321 }
322
323 convert_slashes (mixed_buf);
324
325 return mixed_buf;
326}
327
5bb52de4
CV
328static void
329dowin (char option)
330{
331 char *buf, buf1[MAX_PATH], buf2[MAX_PATH];
332 DWORD len = MAX_PATH;
333 WIN32_FIND_DATA w32_fd;
30352c26 334 LPITEMIDLIST id;
cf157504 335 HINSTANCE k32;
5bb52de4 336 BOOL (*GetProfilesDirectoryAPtr) (LPSTR, LPDWORD) = 0;
cf157504 337
5bb52de4
CV
338 buf = buf1;
339 switch (option)
340 {
341 case 'D':
30352c26
CV
342 SHGetSpecialFolderLocation (NULL, allusers_flag ?
343 CSIDL_COMMON_DESKTOPDIRECTORY : CSIDL_DESKTOPDIRECTORY, &id);
344 SHGetPathFromIDList (id, buf);
345 /* This if clause is a Fix for Win95 without any "All Users" */
346 if (strlen (buf) == 0)
347 {
348 SHGetSpecialFolderLocation (NULL, CSIDL_DESKTOPDIRECTORY, &id);
349 SHGetPathFromIDList (id, buf);
350 }
5bb52de4
CV
351 break;
352
353 case 'P':
30352c26
CV
354 SHGetSpecialFolderLocation (NULL, allusers_flag ?
355 CSIDL_COMMON_PROGRAMS : CSIDL_PROGRAMS, &id);
356 SHGetPathFromIDList (id, buf);
357 /* This if clause is a Fix for Win95 without any "All Users" */
358 if (strlen (buf) == 0)
359 {
360 SHGetSpecialFolderLocation (NULL, CSIDL_PROGRAMS, &id);
361 SHGetPathFromIDList (id, buf);
362 }
5bb52de4
CV
363 break;
364
365 case 'H':
cf157504
CF
366 k32 = LoadLibrary ("userenv");
367 if (k32)
5bb52de4 368 GetProfilesDirectoryAPtr = (BOOL (*) (LPSTR, LPDWORD))
cf157504 369 GetProcAddress (k32, "GetProfilesDirectoryA");
5bb52de4 370 if (GetProfilesDirectoryAPtr)
33bd2d12 371 (*GetProfilesDirectoryAPtr) (buf, &len);
5bb52de4
CV
372 else
373 {
374 GetWindowsDirectory (buf, MAX_PATH);
375 strcat (buf, "\\Profiles");
376 }
377 break;
378
379 case 'S':
380 GetSystemDirectory (buf, MAX_PATH);
381 FindFirstFile (buf, &w32_fd);
382 strcpy (strrchr (buf, '\\') + 1, w32_fd.cFileName);
383 break;
384
385 case 'W':
386 GetWindowsDirectory (buf, MAX_PATH);
387 break;
388
389 default:
390 usage (stderr, 1);
391 }
392
393 if (!windows_flag)
394 {
7c03f799
CF
395 if (cygwin_conv_to_posix_path (buf, buf2))
396 fprintf (stderr, "%s: error converting \"%s\" - %s\n",
397 prog_name, buf, strerror (errno));
398 else
399 buf = buf2;
5bb52de4
CV
400 }
401 else
402 {
403 if (shortname_flag)
33bd2d12
CF
404 buf = get_short_name (buf);
405 if (mixed_flag)
406 buf = get_mixed_name (buf);
5bb52de4
CV
407 }
408 printf ("%s\n", buf);
409 exit (0);
1fd5e000
CF
410}
411
138d4f51 412static void
418068d4 413doit (char *filename)
138d4f51
CF
414{
415 char *buf;
cf157504 416 DWORD len;
7c03f799 417 int err;
6a344609 418 int (*conv_func) (const char *, char *);
138d4f51 419
6a344609 420 if (!path_flag)
c02e32c9 421 {
cf157504
CF
422 len = strlen (filename);
423 if (len)
7ca68b7e 424 len += MAX_PATH + 1001;
cf157504
CF
425 else if (ignore_flag)
426 exit (0);
427 else
6a344609 428 {
cf157504
CF
429 fprintf (stderr, "%s: can't convert empty path\n", prog_name);
430 exit (1);
6a344609 431 }
c02e32c9 432 }
cf157504
CF
433 else if (unix_flag)
434 len = cygwin_win32_to_posix_path_list_buf_size (filename);
138d4f51 435 else
cf157504 436 len = cygwin_posix_to_win32_path_list_buf_size (filename);
138d4f51
CF
437
438 buf = (char *) malloc (len);
439 if (buf == NULL)
440 {
441 fprintf (stderr, "%s: out of memory\n", prog_name);
442 exit (1);
443 }
444
445 if (path_flag)
446 {
447 if (unix_flag)
7c03f799 448 err = cygwin_win32_to_posix_path_list (filename, buf);
138d4f51 449 else
6a344609 450 {
7c03f799
CF
451 err = cygwin_posix_to_win32_path_list (filename, buf);
452 if (err)
453 /* oops */;
6a344609
CF
454 if (shortname_flag)
455 buf = get_short_paths (buf);
5bb52de4
CV
456 if (longname_flag)
457 buf = get_long_paths (buf);
2bd6505b
CF
458 if (mixed_flag)
459 buf = get_mixed_name (buf);
6a344609 460 }
7c03f799
CF
461 if (err)
462 {
463 fprintf (stderr, "%s: error converting \"%s\" - %s\n",
464 prog_name, filename, strerror (errno));
465 exit (1);
466 }
138d4f51
CF
467 }
468 else
469 {
470 if (unix_flag)
6a344609
CF
471 conv_func = (absolute_flag ? cygwin_conv_to_full_posix_path :
472 cygwin_conv_to_posix_path);
138d4f51 473 else
6a344609
CF
474 conv_func = (absolute_flag ? cygwin_conv_to_full_win32_path :
475 cygwin_conv_to_win32_path);
7c03f799 476 err = conv_func (filename, buf);
2bd6505b
CF
477 if (mixed_flag)
478 buf = get_mixed_name (buf);
7c03f799 479 if (err)
6a344609 480 {
7c03f799
CF
481 fprintf (stderr, "%s: error converting \"%s\" - %s\n",
482 prog_name, filename, strerror (errno));
6a344609
CF
483 exit (1);
484 }
5bb52de4
CV
485 if (!unix_flag)
486 {
7ca68b7e
CF
487 if (shortname_flag)
488 buf = get_short_name (buf);
489 if (longname_flag)
490 buf = get_long_name (buf, len);
5bb52de4 491 }
138d4f51
CF
492 }
493
494 puts (buf);
495}
496
6a344609
CF
497static void
498print_version ()
499{
500 const char *v = strchr (version, ':');
501 int len;
502 if (!v)
503 {
504 v = "?";
505 len = 1;
506 }
507 else
508 {
509 v += 2;
510 len = strchr (v, ' ') - v;
511 }
512 printf ("\
513cygpath (cygwin) %.*s\n\
514Path Conversion Utility\n\
515Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.\n\
9b566b96
JDF
516Compiled on %s\n\
517", len, v, __DATE__);
6a344609
CF
518}
519
1fd5e000
CF
520int
521main (int argc, char **argv)
522{
6a344609 523 int c, o = 0;
418068d4 524 int options_from_file_flag;
1fd5e000 525 char *filename;
1fd5e000
CF
526
527 prog_name = strrchr (argv[0], '/');
528 if (prog_name == NULL)
529 prog_name = strrchr (argv[0], '\\');
530 if (prog_name == NULL)
531 prog_name = argv[0];
c02e32c9
CF
532 else
533 prog_name++;
1fd5e000
CF
534
535 path_flag = 0;
1050e57c 536 unix_flag = 1;
1fd5e000 537 windows_flag = 0;
45b80bb4 538 shortname_flag = 0;
5bb52de4 539 longname_flag = 0;
2bd6505b 540 mixed_flag = 0;
45b80bb4 541 ignore_flag = 0;
418068d4 542 options_from_file_flag = 0;
3cdacffc
CV
543 allusers_flag = 0;
544 output_flag = 0;
9b566b96 545 while ((c = getopt_long (argc, argv, options,
2bd6505b 546 long_options, (int *) NULL)) != EOF)
1fd5e000
CF
547 {
548 switch (c)
549 {
418068d4
CF
550 case 'a':
551 absolute_flag = 1;
552 break;
553
554 case 'c':
555 CloseHandle ((HANDLE) strtoul (optarg, NULL, 16));
556 break;
557
1050e57c 558 case 'd':
cf157504 559 if (windows_flag)
1050e57c
CF
560 usage (stderr, 1);
561 unix_flag = 0;
562 windows_flag = 1;
563 shortname_flag = 1;
564 break;
565
138d4f51
CF
566 case 'f':
567 file_arg = optarg;
568 break;
569
418068d4
CF
570 case 'o':
571 options_from_file_flag = 1;
572 break;
573
1fd5e000
CF
574 case 'p':
575 path_flag = 1;
576 break;
577
578 case 'u':
1050e57c 579 if (windows_flag || mixed_flag)
1fd5e000
CF
580 usage (stderr, 1);
581 unix_flag = 1;
582 break;
583
584 case 'w':
1050e57c 585 if (windows_flag || mixed_flag)
1fd5e000 586 usage (stderr, 1);
1050e57c 587 unix_flag = 0;
1fd5e000
CF
588 windows_flag = 1;
589 break;
590
1050e57c
CF
591 case 'm':
592 unix_flag = 0;
593 windows_flag = 1;
594 mixed_flag = 1;
595 break;
596
5bb52de4 597 case 'l':
5bb52de4
CV
598 longname_flag = 1;
599 break;
600
45b80bb4 601 case 's':
45b80bb4
CF
602 shortname_flag = 1;
603 break;
604
2bd6505b 605 case 't':
1050e57c 606 if (optarg == NULL)
2bd6505b
CF
607 usage (stderr, 1);
608
1050e57c
CF
609 format_type_arg = (*optarg == '=') ? (optarg + 1) : (optarg);
610 if (strcasecmp (format_type_arg, "dos") == 0)
cf157504
CF
611 {
612 if (windows_flag || longname_flag)
613 usage (stderr, 1);
614 unix_flag = 0;
615 windows_flag = 1;
616 shortname_flag = 1;
617 }
1050e57c 618 else if (strcasecmp (format_type_arg, "mixed") == 0)
cf157504
CF
619 {
620 unix_flag = 0;
2bd6505b 621 mixed_flag = 1;
cf157504 622 }
1050e57c 623 else if (strcasecmp (format_type_arg, "unix") == 0)
cf157504
CF
624 {
625 if (windows_flag)
626 usage (stderr, 1);
1050e57c 627 unix_flag = 1;
cf157504 628 }
1050e57c 629 else if (strcasecmp (format_type_arg, "windows") == 0)
cf157504
CF
630 {
631 if (mixed_flag)
632 usage (stderr, 1);
633 unix_flag = 0;
634 windows_flag = 1;
635 }
2bd6505b
CF
636 else
637 usage (stderr, 1);
638 break;
639
3cdacffc
CV
640 case 'A':
641 allusers_flag = 1;
642 break;
643
644 case 'D':
e355de81 645 case 'H':
3cdacffc 646 case 'P':
f00c1d2c 647 case 'S':
3cdacffc
CV
648 case 'W':
649 if (output_flag)
650 usage (stderr, 1);
651 output_flag = 1;
e355de81 652 o = c;
3cdacffc 653 break;
f00c1d2c 654
45b80bb4
CF
655 case 'i':
656 ignore_flag = 1;
657 break;
658
1fd5e000
CF
659 case 'h':
660 usage (stdout, 0);
661 break;
662
663 case 'v':
6a344609 664 print_version ();
1fd5e000
CF
665 exit (0);
666
667 default:
668 usage (stderr, 1);
669 break;
670 }
671 }
672
418068d4
CF
673 if (options_from_file_flag && !file_arg)
674 usage (stderr, 1);
675
1050e57c
CF
676 if (longname_flag && !windows_flag)
677 usage (stderr, 1);
678
679 if (shortname_flag && !windows_flag)
680 usage (stderr, 1);
681
682 if (!unix_flag && !windows_flag && !mixed_flag && !options_from_file_flag)
1fd5e000
CF
683 usage (stderr, 1);
684
138d4f51 685 if (!file_arg)
1fd5e000 686 {
5bb52de4
CV
687 if (output_flag)
688 dowin (o);
689
0a5ea947 690 if (optind > argc - 1)
138d4f51 691 usage (stderr, 1);
1fd5e000 692
0a5ea947
CV
693 for (int i=optind; argv[i]; i++) {
694 filename = argv[i];
695 doit (filename);
696 }
138d4f51 697 }
1fd5e000
CF
698 else
699 {
138d4f51
CF
700 FILE *fp;
701 char buf[PATH_MAX * 2 + 1];
1fd5e000 702
138d4f51
CF
703 if (argv[optind])
704 usage (stderr, 1);
1fd5e000 705
138d4f51
CF
706 if (strcmp (file_arg, "-") != 0)
707 fp = fopen (file_arg, "rt");
1fd5e000 708 else
138d4f51
CF
709 {
710 fp = stdin;
711 setmode (0, O_TEXT);
712 }
713 if (fp == NULL)
714 {
715 perror ("cygpath");
716 exit (1);
717 }
1fd5e000 718
418068d4 719 setbuf (stdout, NULL);
138d4f51
CF
720 while (fgets (buf, sizeof (buf), fp) != NULL)
721 {
418068d4
CF
722 char *s = buf;
723 char *p = strchr (s, '\n');
138d4f51
CF
724 if (p)
725 *p = '\0';
418068d4
CF
726 if (options_from_file_flag && *s == '-')
727 {
728 char c;
729 for (c = *++s; c && !isspace (c); c = *++s)
730 switch (c)
731 {
732 case 'a':
733 absolute_flag = 1;
734 break;
45b80bb4
CF
735 case 'i':
736 ignore_flag = 1;
737 break;
738 case 's':
739 shortname_flag = 1;
5bb52de4
CV
740 longname_flag = 0;
741 break;
742 case 'l':
743 shortname_flag = 0;
744 longname_flag = 1;
45b80bb4 745 break;
1050e57c
CF
746 case 'm':
747 unix_flag = 0;
748 windows_flag = 1;
749 mixed_flag = 1;
418068d4
CF
750 case 'w':
751 unix_flag = 0;
752 windows_flag = 1;
753 break;
754 case 'u':
755 windows_flag = 0;
756 unix_flag = 1;
757 break;
758 case 'p':
759 path_flag = 1;
5bb52de4
CV
760 break;
761 case 'D':
762 case 'H':
763 case 'P':
764 case 'S':
765 case 'W':
2bd6505b 766 output_flag = 1;
5bb52de4 767 o = c;
2bd6505b 768 break;
418068d4
CF
769 }
770 if (*s)
771 do
772 s++;
773 while (*s && isspace (*s));
774 }
5bb52de4 775 if (*s && !output_flag)
418068d4 776 doit (s);
5bb52de4
CV
777 if (!*s && output_flag)
778 dowin (o);
138d4f51
CF
779 }
780 }
1fd5e000
CF
781
782 exit (0);
783}
This page took 0.20434 seconds and 5 git commands to generate.