Fossil SCM

A better fix for the intptr_t casting problem - this one works on older compilers. Ticket [6585b6c5d9058212ceb]

drh 2011-01-17 02:38 trunk
Commit c1f6df703a9b8693040e7810563cc5837351809f
1 file changed +28 -2
+28 -2
--- src/popen.c
+++ src/popen.c
@@ -29,10 +29,36 @@
2929
static void win32_fatal_error(const char *zMsg){
3030
fossil_fatal("%s");
3131
}
3232
#endif
3333
34
+/*
35
+** The following macros are used to cast pointers to integers and
36
+** integers to pointers. The way you do this varies from one compiler
37
+** to the next, so we have developed the following set of #if statements
38
+** to generate appropriate macros for a wide range of compilers.
39
+**
40
+** The correct "ANSI" way to do this is to use the intptr_t type.
41
+** Unfortunately, that typedef is not available on all compilers, or
42
+** if it is available, it requires an #include of specific headers
43
+** that vary from one machine to the next.
44
+**
45
+** This code is copied out of SQLite.
46
+*/
47
+#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
48
+# define INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
49
+# define PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
50
+#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
51
+# define INT_TO_PTR(X) ((void*)&((char*)0)[X])
52
+# define PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
53
+#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
54
+# define INT_TO_PTR(X) ((void*)(intptr_t)(X))
55
+# define PTR_TO_INT(X) ((int)(intptr_t)(X))
56
+#else /* Generates a warning - but it always works */
57
+# define INT_TO_PTR(X) ((void*)(X))
58
+# define PTR_TO_INT(X) ((int)(X))
59
+#endif
3460
3561
3662
#ifdef _WIN32
3763
/*
3864
** On windows, create a child process and specify the stdin, stdout,
@@ -116,12 +142,12 @@
116142
SetHandleInformation( hStdinWr, HANDLE_FLAG_INHERIT, FALSE);
117143
118144
win32_create_child_process((char*)zCmd,
119145
hStdinRd, hStdoutWr, hStderr,&childPid);
120146
*pChildPid = childPid;
121
- *pfdIn = _open_osfhandle((intptr_t)hStdoutRd, 0);
122
- fd = _open_osfhandle((intptr_t)hStdinWr, 0);
147
+ *pfdIn = _open_osfhandle(PTR_TO_INT(hStdoutRd), 0);
148
+ fd = _open_osfhandle(PTR_TO_INT(hStdinWr), 0);
123149
*ppOut = _fdopen(fd, "w");
124150
CloseHandle(hStdinRd);
125151
CloseHandle(hStdoutWr);
126152
return 0;
127153
#else
128154
--- src/popen.c
+++ src/popen.c
@@ -29,10 +29,36 @@
29 static void win32_fatal_error(const char *zMsg){
30 fossil_fatal("%s");
31 }
32 #endif
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36 #ifdef _WIN32
37 /*
38 ** On windows, create a child process and specify the stdin, stdout,
@@ -116,12 +142,12 @@
116 SetHandleInformation( hStdinWr, HANDLE_FLAG_INHERIT, FALSE);
117
118 win32_create_child_process((char*)zCmd,
119 hStdinRd, hStdoutWr, hStderr,&childPid);
120 *pChildPid = childPid;
121 *pfdIn = _open_osfhandle((intptr_t)hStdoutRd, 0);
122 fd = _open_osfhandle((intptr_t)hStdinWr, 0);
123 *ppOut = _fdopen(fd, "w");
124 CloseHandle(hStdinRd);
125 CloseHandle(hStdoutWr);
126 return 0;
127 #else
128
--- src/popen.c
+++ src/popen.c
@@ -29,10 +29,36 @@
29 static void win32_fatal_error(const char *zMsg){
30 fossil_fatal("%s");
31 }
32 #endif
33
34 /*
35 ** The following macros are used to cast pointers to integers and
36 ** integers to pointers. The way you do this varies from one compiler
37 ** to the next, so we have developed the following set of #if statements
38 ** to generate appropriate macros for a wide range of compilers.
39 **
40 ** The correct "ANSI" way to do this is to use the intptr_t type.
41 ** Unfortunately, that typedef is not available on all compilers, or
42 ** if it is available, it requires an #include of specific headers
43 ** that vary from one machine to the next.
44 **
45 ** This code is copied out of SQLite.
46 */
47 #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
48 # define INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
49 # define PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
50 #elif !defined(__GNUC__) /* Works for compilers other than LLVM */
51 # define INT_TO_PTR(X) ((void*)&((char*)0)[X])
52 # define PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
53 #elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
54 # define INT_TO_PTR(X) ((void*)(intptr_t)(X))
55 # define PTR_TO_INT(X) ((int)(intptr_t)(X))
56 #else /* Generates a warning - but it always works */
57 # define INT_TO_PTR(X) ((void*)(X))
58 # define PTR_TO_INT(X) ((int)(X))
59 #endif
60
61
62 #ifdef _WIN32
63 /*
64 ** On windows, create a child process and specify the stdin, stdout,
@@ -116,12 +142,12 @@
142 SetHandleInformation( hStdinWr, HANDLE_FLAG_INHERIT, FALSE);
143
144 win32_create_child_process((char*)zCmd,
145 hStdinRd, hStdoutWr, hStderr,&childPid);
146 *pChildPid = childPid;
147 *pfdIn = _open_osfhandle(PTR_TO_INT(hStdoutRd), 0);
148 fd = _open_osfhandle(PTR_TO_INT(hStdinWr), 0);
149 *ppOut = _fdopen(fd, "w");
150 CloseHandle(hStdinRd);
151 CloseHandle(hStdoutWr);
152 return 0;
153 #else
154

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button