updated on Thu Jan 12 04:00:44 UTC 2012
[aur-mirror.git] / utorrent / wine-executor.c
blobdc4bd0cc6c4480390c145bdfb9555605884f8ba8
1 /* license: http://en.wikipedia.org/wiki/WTFPL */
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <unistd.h>
9 int get_args(char** options, char** executable,
10 int argc, char** argv) {
11 int c;
12 while ((c = getopt(argc, argv, "i:e:")) != -1) {
13 switch (c) {
14 case 'e':
15 *executable = optarg;
16 break;
17 case 'i':
18 *options = optarg;
19 break;
20 default:
21 return 1;
24 return 0;
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) {
34 int rv = 0;
35 char* options_bak = strdup(options);
36 char* argument_bak = strdup(argument);
37 char* state = 0;
38 char* token;
40 lowerize(argument_bak);
42 token = strtok_r(options_bak, ":", &state);
43 while (rv == 0 && token != 0) {
44 if (strcmp(token, argument_bak) == 0) {
45 rv = 1;
47 token = strtok_r(0, ":", &state);
50 free(argument_bak);
51 free(options_bak);
52 return rv;
55 char* must_be_converted(const char* argument, const char* options) {
56 char* first_slash;
57 if ((first_slash = strchr(argument, '/')) && !is_one_of(argument, options)) {
58 return first_slash;
60 else {
61 return 0;
65 void convert_slashes(char* line) {
66 for (; *line != 0; ++line) {
67 if (*line == '/') {
68 *line = '\\';
73 char* make_full_path(const char* relative_name) {
74 char* full_path;
75 size_t rnl = strlen(relative_name);
77 rnl += 128;
79 full_path = malloc(rnl);
80 strcpy(full_path, "z:");
82 while (0 == getcwd(full_path + 2, rnl - 2)) {
83 rnl *= 2;
84 full_path = realloc(full_path, rnl);
87 strcat(full_path, "/");
88 strcat(full_path, relative_name);
90 return full_path;
93 void convert_arg(char** argument, const char* options) {
94 char* first_slash;
95 if ((first_slash = must_be_converted(*argument, options))) {
96 char* new_argument;
97 if (first_slash == *argument) {
98 /* full path, we need to add z: */
99 size_t len = strlen(*argument);
100 len += 3;
101 new_argument = malloc(len);
102 strcpy(new_argument, "z:");
103 strcat(new_argument, *argument);
104 convert_slashes(new_argument);
106 else {
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) {
116 if (options != 0) {
117 lowerize(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",
126 program_name);
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"
137 " will start:\n"
138 " wine utorrent.exe /directory z:\\home\\jack\\rob \\\n"
139 " z:\\home\\jack\\rob.torrent\n\n", program_name);
140 return 1;
143 int main(int argc, char** argv) {
144 int split = 1;
145 char* options = 0;
146 char* executable = 0;
148 while (split < argc && strcmp(argv[split], "--") != 0) {
149 ++split;
151 if (split == argc) {
152 return explain(argv[0]);
155 argv[split] = 0;
156 if (get_args(&options, &executable, split, argv) != 0
157 || executable == 0) {
158 return explain(argv[0]);
161 argv[split] = executable;
162 --split;
163 argv[split] = "wine";
164 argv += split;
166 convert(argv + 2, options);
168 #ifndef NDEBUG
170 char** check = argv;
171 fprintf(stderr, "Executing: \n-- begin\n");
172 for (; *check != 0; ++check) {
173 fprintf(stderr, "%s\n", *check);
175 fprintf(stderr, "-- end\n");
177 #else
178 execvp(argv[0], argv);
179 fprintf(stderr, "execvp() failed!\n");
180 #endif
182 return 2;