1
- //
2
- // pdfsizeopt_winstub.c: Start Python on Win32.
3
- // by pts@fazekas.hu at Wed Jun 27 14:07:04 CEST 2012
4
- //
5
- // Compile with: i586-mingw32msvc-gcc -mconsole -s -W -Wall -o pdfsizeopt.exe pdfsizeopt_winstub.c
1
+ /*
2
+ * pdfsizeopt_winstub.c: Start Python on Win32.
3
+ * by pts@fazekas.hu at Wed Jun 27 14:07:04 CEST 2012
4
+ *
5
+ * Compile with: i586-mingw32msvc-gcc -mconsole -s -W -Wall -o ../pdfsizeopt.exe pdfsizeopt_winstub.c
6
+ */
6
7
7
8
#include <errno.h>
8
9
#include <limits.h> // PATH_MAX is 259.
@@ -33,6 +34,11 @@ static char is_in_dir(const char *file, const char *p, const char *q) {
33
34
return 0 == stat (pathname , & st ) && S_ISREG (st .st_mode );
34
35
}
35
36
37
+ static char is_file (const char * pathname ) {
38
+ struct stat st ;
39
+ return 0 == stat (pathname , & st ) && S_ISREG (st .st_mode );
40
+ }
41
+
36
42
static void find_on_path (const char * prog , char * dir_out ) {
37
43
const char * path = getenv ("PATH" ), * p , * q ;
38
44
if (path != NULL || * path != '\0' ) {
@@ -58,12 +64,14 @@ static void find_on_path(const char *prog, char *dir_out) {
58
64
* dir_out = '\0' ;
59
65
}
60
66
61
- static const char python_exe [] = "pdfsizeopt_python.exe" ;
62
- static const char pdfsizeopt_py [] = "pdfsizeopt.py" ;
67
+ /* FILE_SEP inlined here. */
68
+ static const char python_exe [] = "pdfsizeopt_win32exec\\pdfsizeopt_python.exe" ;
69
+ static const char pdfsizeopt_py0 [] = "pdfsizeopt" ;
70
+ static const char pdfsizeopt_py1 [] = "pdfsizeopt.single" ;
63
71
64
72
int main (int argc , char * * argv ) {
65
73
char python_bin [PATH_MAX + 1 ], argv0_bin [PATH_MAX + 1 ], * p , * q ;
66
- char prog_py [PATH_MAX + 1 ];
74
+ char prog_py [PATH_MAX + 2 ];
67
75
const char * moreargv [ARGV_MAX + 1 ];
68
76
int i ;
69
77
(void )argc ;
@@ -110,19 +118,27 @@ int main(int argc, char **argv) {
110
118
strcpy (python_bin + i + 1 , python_exe );
111
119
112
120
i = strlen (prog_py );
113
- if (i + strlen (pdfsizeopt_py ) > PATH_MAX ) {
114
- i = PATH_MAX - strlen (pdfsizeopt_py );
121
+ if (i + strlen (pdfsizeopt_py0 ) > PATH_MAX ) {
122
+ i = PATH_MAX - strlen (pdfsizeopt_py0 );
123
+ }
124
+ if (i + strlen (pdfsizeopt_py1 ) > PATH_MAX ) {
125
+ i = PATH_MAX - strlen (pdfsizeopt_py0 );
126
+ }
127
+ prog_py [i ++ ] = FILE_SEP ;
128
+ strcpy (prog_py + i , pdfsizeopt_py0 );
129
+ if (!is_file (prog_py )) {
130
+ strcpy (prog_py + i , pdfsizeopt_py1 );
131
+ if (!is_file (prog_py )) {
132
+ fprintf (stderr , "error: Python script missing: %s\n" , prog_py );
133
+ }
115
134
}
116
- prog_py [i ] = FILE_SEP ;
117
- strcpy (prog_py + i + 1 , pdfsizeopt_py );
118
135
119
136
moreargv [0 ] = "python26" ;
120
137
moreargv [1 ] = prog_py ;
121
138
for (i = 1 ; argv [i ] != NULL ; ++ i ) {
122
139
moreargv [i + 1 ] = argv [i ];
123
140
}
124
141
moreargv [i + 1 ] = NULL ;
125
-
126
142
127
143
// execv(...) and P_OVERLAY don't work well in wine-1.2.2 and Windows XP,
128
144
// because they make this process return before the started process finishes.
0 commit comments