1 /* license: http://en.wikipedia.org/wiki/WTFPL */
9 int get_args(char** options
, char** executable
,
10 int argc
, char** argv
) {
12 while ((c
= getopt(argc
, argv
, "i:e:")) != -1) {
27 void lowerize(char* line
) {
28 for (; *line
!= 0; ++line
) {
29 *line
= tolower(*line
);
33 int is_one_of(const char* argument
, const char* options
) {
35 char* options_bak
= strdup(options
);
36 char* argument_bak
= strdup(argument
);
40 lowerize(argument_bak
);
42 token
= strtok_r(options_bak
, ":", &state
);
43 while (rv
== 0 && token
!= 0) {
44 if (strcmp(token
, argument_bak
) == 0) {
47 token
= strtok_r(0, ":", &state
);
55 char* must_be_converted(const char* argument
, const char* options
) {
57 if ((first_slash
= strchr(argument
, '/')) && !is_one_of(argument
, options
)) {
65 void convert_slashes(char* line
) {
66 for (; *line
!= 0; ++line
) {
73 char* make_full_path(const char* relative_name
) {
75 size_t rnl
= strlen(relative_name
);
79 full_path
= malloc(rnl
);
80 strcpy(full_path
, "z:");
82 while (0 == getcwd(full_path
+ 2, rnl
- 2)) {
84 full_path
= realloc(full_path
, rnl
);
87 strcat(full_path
, "/");
88 strcat(full_path
, relative_name
);
93 void convert_arg(char** argument
, const char* options
) {
95 if ((first_slash
= must_be_converted(*argument
, options
))) {
97 if (first_slash
== *argument
) {
98 /* full path, we need to add z: */
99 size_t len
= strlen(*argument
);
101 new_argument
= malloc(len
);
102 strcpy(new_argument
, "z:");
103 strcat(new_argument
, *argument
);
104 convert_slashes(new_argument
);
107 /* relative path, we need just to change the slashes */
108 new_argument
= make_full_path(*argument
);
109 convert_slashes(new_argument
);
111 *argument
= new_argument
;
115 void convert(char** arguments
, char* options
) {
118 for (;*arguments
!= 0; ++arguments
) {
119 convert_arg(arguments
, options
);
124 int explain(char* program_name
) {
125 fprintf(stderr
, "Usage: %s -e prg [-i ignore_options ] -- arguments\n\n",
127 fprintf(stderr
, "Starts wine converting filenames from Linux to Wine format.\n"
128 " prg is what wine will execute.\n"
129 " ignore_options is a `:' separed list of strings we must not touch\n"
130 " (usually arguments with /).\n"
131 " arguments is what we pass to the application. Filenames will be converted.\n"
132 " %s converts every argument contains a / and it is not in the"
133 " ignore_options list.\n\n", program_name
);
134 fprintf(stderr
, " e.g.:\n"
135 " %s -e utorrent.exe -i '/directory:/hide' -- \\\n"
136 " /directory /home/jack/rob /home/jack/rob.torrent'\n"
138 " wine utorrent.exe /directory z:\\home\\jack\\rob \\\n"
139 " z:\\home\\jack\\rob.torrent\n\n", program_name
);
143 int main(int argc
, char** argv
) {
146 char* executable
= 0;
148 while (split
< argc
&& strcmp(argv
[split
], "--") != 0) {
152 return explain(argv
[0]);
156 if (get_args(&options
, &executable
, split
, argv
) != 0
157 || executable
== 0) {
158 return explain(argv
[0]);
161 argv
[split
] = executable
;
163 argv
[split
] = "wine";
166 convert(argv
+ 2, options
);
171 fprintf(stderr
, "Executing: \n-- begin\n");
172 for (; *check
!= 0; ++check
) {
173 fprintf(stderr
, "%s\n", *check
);
175 fprintf(stderr
, "-- end\n");
178 execvp(argv
[0], argv
);
179 fprintf(stderr
, "execvp() failed!\n");