2626extern char * * environ ;
2727
2828#define BUFSIZE 1024
29- #define POPEN_BUFFSIZE 255
3029
3130#if defined(_Win32 )
3231
@@ -35,9 +34,9 @@ extern char **environ;
3534 *
3635 * returns a newly allocated string with the result or NULL
3736 *
38- * warning: if the cmd is a GUI process, the pw_shell will hang
37+ * warning: if the cmd is a GUI process, the shell will hang
3938 */
40- char * pw_shell (const char * cmd ) {
39+ char * shell (const char * cmd ) {
4140 HANDLE h_inppip , h_outpip , h_errpip , h_pid ;
4241 char buf [BUFSIZE + 1 ], cv_buf [BUFSIZE + 1 ];
4342 char * result = NULL ;
@@ -52,7 +51,9 @@ char *pw_shell(const char *cmd) {
5251 sa .nLength = sizeof (sa );
5352 sa .bInheritHandle = TRUE;
5453
54+ log_printf ("shell: %s\n" , cmd );
5555 if (!CreatePipe (& h_inppip , & h_outpip , & sa , BUFSIZE )) {
56+ log_printf ("CreatePipe failed" );
5657 return NULL ;
5758 }
5859
@@ -66,7 +67,6 @@ char *pw_shell(const char *cmd) {
6667 si .cb = sizeof (si );
6768 si .dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
6869 si .wShowWindow = SW_HIDE ;
69-
7070 si .hStdOutput = h_outpip ;
7171 si .hStdError = h_errpip ;
7272
@@ -92,9 +92,10 @@ char *pw_shell(const char *cmd) {
9292 strcat (result , cv_buf );
9393 }
9494 CloseHandle (pi .hProcess );
95+ log_printf ("shell completed %d bytes\n" , strlen (result ));
9596 }
9697 else {
97- // could not run it
98+ log_printf ( "Failed to launch %s\n" , cmd );
9899 result = NULL ;
99100 }
100101
@@ -112,7 +113,7 @@ char *pw_shell(const char *cmd) {
112113int dev_run (const char * src , var_t * r , int wait ) {
113114 int result = 1 ;
114115 if (r != NULL ) {
115- char * buf = pw_shell (src );
116+ char * buf = shell (src );
116117 if (buf != NULL ) {
117118 r -> type = V_STR ;
118119 r -> v .p .ptr = buf ;
@@ -121,7 +122,7 @@ int dev_run(const char *src, var_t *r, int wait) {
121122 result = 0 ;
122123 }
123124 } else if (wait ) {
124- char * out = pw_shell (src );
125+ char * out = shell (src );
125126 if (out != NULL ) {
126127 free (out );
127128 } else {
@@ -139,25 +140,24 @@ int dev_run(const char *src, var_t *r, int wait) {
139140 int result = 1 ;
140141 if (r != NULL ) {
141142 r -> type = V_STR ;
142- r -> v .p .size = POPEN_BUFFSIZE + 1 ;
143+ r -> v .p .size = BUFSIZE + 1 ;
143144 r -> v .p .ptr = malloc (r -> v .p .size );
144- * r -> v .p .ptr = '\0' ;
145+ r -> v .p .ptr [ 0 ] = '\0' ;
145146
146147 int bytes = 0 ;
147148 int total = 0 ;
148- char buf [256 ];
149-
149+ char buf [BUFSIZE + 1 ];
150150 FILE * fin = popen (src , "r" );
151151 if (fin ) {
152152 while (!feof (fin )) {
153- bytes = fread (buf , 1 , POPEN_BUFFSIZE , fin );
154- total += bytes ;
153+ bytes = fread (buf , 1 , BUFSIZE , fin );
155154 buf [bytes ] = '\0' ;
156- strcat ( r -> v . p . ptr , buf ) ;
157- if (total + POPEN_BUFFSIZE + 1 >= r -> v .p .size ) {
158- r -> v .p .size += POPEN_BUFFSIZE + 1 ;
155+ total += bytes ;
156+ if (total >= r -> v .p .size ) {
157+ r -> v .p .size += BUFSIZE + 1 ;
159158 r -> v .p .ptr = realloc (r -> v .p .ptr , r -> v .p .size );
160159 }
160+ strcat (r -> v .p .ptr , buf );
161161 }
162162 pclose (fin );
163163 } else {
0 commit comments