]>
Commit | Line | Data |
---|---|---|
1fd5e000 CF |
1 | /* pathconv.cc -- convert pathnames between Windows and Unix format |
2 | Copyright 1998 Cygnus Solutions. | |
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> | |
16 | #include <sys/cygwin.h> | |
17 | ||
18 | static char *prog_name; | |
19 | ||
20 | static struct option long_options[] = | |
21 | { | |
22 | { (char *) "help", no_argument, NULL, 'h' }, | |
23 | { (char *) "path", no_argument, NULL, 'p' }, | |
24 | { (char *) "unix", no_argument, NULL, 'u' }, | |
25 | { (char *) "version", no_argument, NULL, 'v' }, | |
26 | { (char *) "windows", no_argument, NULL, 'w' }, | |
27 | { 0, no_argument, 0, 0 } | |
28 | }; | |
29 | ||
30 | static void | |
31 | usage (FILE *stream, int status) | |
32 | { | |
33 | fprintf (stream, "\ | |
34 | Usage: %s [-p|--path] (-u|--unix)|(-w|--windows) filename\n\ | |
35 | -u|--unix print Unix form of filename\n\ | |
36 | -w|--windows print Windows form of filename\n\ | |
37 | -p|--path filename argument is a path\n", | |
38 | prog_name); | |
39 | exit (status); | |
40 | } | |
41 | ||
42 | int | |
43 | main (int argc, char **argv) | |
44 | { | |
45 | int path_flag, unix_flag, windows_flag; | |
46 | int c; | |
47 | char *filename; | |
48 | size_t len; | |
49 | char *buf; | |
50 | ||
51 | prog_name = strrchr (argv[0], '/'); | |
52 | if (prog_name == NULL) | |
53 | prog_name = strrchr (argv[0], '\\'); | |
54 | if (prog_name == NULL) | |
55 | prog_name = argv[0]; | |
56 | ||
57 | path_flag = 0; | |
58 | unix_flag = 0; | |
59 | windows_flag = 0; | |
60 | while ((c = getopt_long (argc, argv, (char *) "hpuvw", long_options, (int *) NULL)) | |
61 | != EOF) | |
62 | { | |
63 | switch (c) | |
64 | { | |
65 | case 'p': | |
66 | path_flag = 1; | |
67 | break; | |
68 | ||
69 | case 'u': | |
70 | if (unix_flag || windows_flag) | |
71 | usage (stderr, 1); | |
72 | unix_flag = 1; | |
73 | break; | |
74 | ||
75 | case 'w': | |
76 | if (unix_flag || windows_flag) | |
77 | usage (stderr, 1); | |
78 | windows_flag = 1; | |
79 | break; | |
80 | ||
81 | case 'h': | |
82 | usage (stdout, 0); | |
83 | break; | |
84 | ||
85 | case 'v': | |
86 | printf ("Cygwin pathconv version 1.0\n"); | |
87 | printf ("Copyright 1998 Cygnus Solutions\n"); | |
88 | exit (0); | |
89 | ||
90 | default: | |
91 | usage (stderr, 1); | |
92 | break; | |
93 | } | |
94 | } | |
95 | ||
96 | if (optind != argc - 1) | |
97 | usage (stderr, 1); | |
98 | ||
99 | if (! unix_flag && ! windows_flag) | |
100 | usage (stderr, 1); | |
101 | ||
102 | filename = argv[optind]; | |
103 | ||
104 | if (path_flag) | |
105 | { | |
106 | if (cygwin_posix_path_list_p (filename) | |
107 | ? unix_flag | |
108 | : windows_flag) | |
109 | { | |
110 | /* The path is already in the right format. */ | |
111 | puts (filename); | |
112 | exit (0); | |
113 | } | |
114 | } | |
115 | ||
116 | if (! path_flag) | |
117 | len = strlen (filename) + 100; | |
118 | else | |
119 | { | |
120 | if (unix_flag) | |
121 | len = cygwin_win32_to_posix_path_list_buf_size (filename); | |
122 | else | |
123 | len = cygwin_posix_to_win32_path_list_buf_size (filename); | |
124 | } | |
125 | ||
126 | if (len < PATH_MAX) | |
127 | len = PATH_MAX; | |
128 | ||
129 | buf = (char *) malloc (len); | |
130 | if (buf == NULL) | |
131 | { | |
132 | fprintf (stderr, "%s: out of memory\n", prog_name); | |
133 | exit (1); | |
134 | } | |
135 | ||
136 | if (path_flag) | |
137 | { | |
138 | if (unix_flag) | |
139 | cygwin_win32_to_posix_path_list (filename, buf); | |
140 | else | |
141 | cygwin_posix_to_win32_path_list (filename, buf); | |
142 | } | |
143 | else | |
144 | { | |
145 | if (unix_flag) | |
146 | cygwin_conv_to_posix_path (filename, buf); | |
147 | else | |
148 | cygwin_conv_to_win32_path (filename, buf); | |
149 | } | |
150 | ||
151 | puts (buf); | |
152 | ||
153 | exit (0); | |
154 | } |