2626extern char * * environ ;
2727
2828#define BUFSIZE 1024
29+ #define POPEN_BUFFSIZE 255
2930
3031#if defined(_Win32 )
3132
@@ -60,8 +61,7 @@ char *pw_shell(const char *cmd) {
6061 DuplicateHandle (h_pid , h_inppip , h_pid , & h_inppip , 0 , FALSE,
6162 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE );
6263 DuplicateHandle (h_pid , h_outpip , h_pid , & h_errpip , 0 , TRUE, DUPLICATE_SAME_ACCESS );
63-
64- // run
64+
6565 memset (& si , 0 , sizeof (si ));
6666 si .cb = sizeof (si );
6767 si .dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
@@ -91,11 +91,11 @@ char *pw_shell(const char *cmd) {
9191 }
9292 strcat (result , cv_buf );
9393 }
94-
9594 CloseHandle (pi .hProcess );
9695 }
9796 else {
98- result = NULL ; // could not run it
97+ // could not run it
98+ result = NULL ;
9999 }
100100
101101 // clean up
@@ -108,55 +108,91 @@ char *pw_shell(const char *cmd) {
108108 }
109109 return result ;
110110}
111- #endif
112111
113- /**
114- * run a program (if retflg wait and return; otherwise just exec())
115- */
116- int dev_run (const char * src , int retflg ) {
117- #if defined(_Win32 )
118- int r ;
119- char * out ;
120-
121- r = ((out = pw_shell (src )) != NULL );
122- if (r ) {
123- free (out );
112+ int dev_run (const char * src , var_t * r , int wait ) {
113+ int result = 1 ;
114+ if (r != NULL ) {
115+ char * buf = pw_shell (src );
116+ if (buf != NULL ) {
117+ r -> type = V_STR ;
118+ r -> v .p .ptr = buf ;
119+ r -> v .p .size = strlen (buf ) + 1 ;
120+ } else {
121+ result = 0 ;
122+ }
123+ } else if (wait ) {
124+ char * out = pw_shell (src );
125+ if (out != NULL ) {
126+ free (out );
127+ } else {
128+ result = 0 ;
129+ }
130+ } else {
131+ HWND hwnd = GetActiveWindow ();
132+ ShellExecute (hwnd , "open" , src , 0 , 0 , SW_SHOWNORMAL );
124133 }
134+ return result ;
135+ }
125136
126- if (r && !retflg ) {
127- exit (1 ); // ok, what to do now ?
128- }
129- return r ;
130137#else
131- if (retflg ) {
132- return (system (src ) != -1 );
133- } else {
134- // execl(src, src, NULL);
135- // call the shell if we want to behave same like system() function
136- // -c means the next argument is the command string to execute
137- // this allow us to execute shell script too!
138- char * src1 ;
139- src1 = malloc (strlen (src ) + 3 );
140- if (src1 == NULL ) {
141- exit (-1 ); // ok, what to do now ?
138+ int dev_run (const char * src , var_t * r , int wait ) {
139+ int result = 1 ;
140+ if (r != NULL ) {
141+ r -> type = V_STR ;
142+ r -> v .p .size = POPEN_BUFFSIZE + 1 ;
143+ r -> v .p .ptr = malloc (r -> v .p .size );
144+ * r -> v .p .ptr = '\0' ;
145+
146+ int bytes = 0 ;
147+ int total = 0 ;
148+ char buf [256 ];
149+
150+ FILE * fin = popen (src , "r" );
151+ if (fin ) {
152+ while (!feof (fin )) {
153+ bytes = fread (buf , 1 , POPEN_BUFFSIZE , fin );
154+ total += bytes ;
155+ 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 ;
159+ r -> v .p .ptr = realloc (r -> v .p .ptr , r -> v .p .size );
160+ }
161+ }
162+ pclose (fin );
163+ } else {
164+ v_zerostr (r );
165+ result = 0 ;
166+ }
167+ } else if (wait ) {
168+ result = (system (src ) != -1 );
169+ }
170+ else if (fork () == 0 ) {
171+ // exec separate process
172+ int size = strlen (src ) + 3 ;
173+ char * src1 = malloc (size );
174+ if (src1 != NULL ) {
175+ memset (src1 , '\0' , size );
176+ // double quote the command
177+ * src1 = '"' ;
178+ strcat (src1 , src );
179+ * (src1 + strlen (src ) + 1 ) = '"' ;
180+ // -c means the next argument is the command string to execute
181+ // this allow us to execute shell script
182+ execlp ("sh" , "sh" , "-c" , src1 , NULL );
142183 }
143- memset (src1 , '\0' , strlen (src ) + 3 );
144- * src1 = '"' ;
145- strcat (src1 , src );
146- * (src1 + strlen (src ) + 1 ) = '"' ; // we need a doublequote around the command
147- execlp ("sh" , "sh" , "-c" , src1 , NULL );
148184 exit (-1 );
149- // o.k. some error happens - what to do??? we already closed the screen!!
150185 }
151- #endif
186+ return result ;
152187}
188+ #endif
153189
154190#ifndef IMPL_DEV_ENV
155191
156192/**
157- * The putenv() function adds or changes the value of environment variables. The argument string
193+ * The putenv() function adds or changes the value of environment variables. The argument string
158194 * is of the form name=value. If name does not already exist in the environment, then string is added
159- * to the environment. If name does exist, then the value of name in the environment is changed
195+ * to the environment. If name does exist, then the value of name in the environment is changed
160196 * to value. The string pointed to by string becomes part of the environment, so
161197 * altering the string changes the environment.
162198 *
@@ -170,7 +206,7 @@ int dev_putenv(const char *str) {
170206}
171207
172208/**
173- * The getenv() function searches the environment list for a string that matches the string pointed
209+ * The getenv() function searches the environment list for a string that matches the string pointed
174210 * to by name. The strings are of the form name = value.
175211 *
176212 * The getenv() function returns a pointer to the value in the environment, or NULL if there is no match.
0 commit comments