cvs: /php3/functions exec.c
| From: | Mike Gerdts | Date: | Mon, 19 Jul 1999 16:55:42 +0000 |
| Subject: | cvs: /php3/functions exec.c | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-8741@lists.php.net to get a copy of this message | ||
gerdts Mon Jul 19 16:55:42 1999 EDT
Modified files:
/php3/functions exec.c
Log:
exec() can now properly handle lines longer than 8190 (EXEC_INPUT_BUF-2)
characters.
Lines that that contain only "\n" now are trimmed down to "" to be
consistent with exec's whitespace trimming behavior for all other lines.
Index: php3/functions/exec.c
diff -u php3/functions/exec.c:1.84 php3/functions/exec.c:1.85
--- php3/functions/exec.c:1.84 Fri Jul 16 14:05:00 1999
+++ php3/functions/exec.c Mon Jul 19 16:55:41 1999
@@ -26,7 +26,7 @@
| Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
+----------------------------------------------------------------------+
*/
-/* $Id: exec.c,v 1.84 1999/07/16 18:05:00 jim Exp $ */
+/* $Id: exec.c,v 1.85 1999/07/19 20:55:41 gerdts Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
@@ -54,12 +54,20 @@
static int _Exec(int type, char *cmd, pval *array, pval *return_value)
{
FILE *fp;
- char buf[EXEC_INPUT_BUF], *tmp=NULL;
+ char *buf, *tmp=NULL;
+ int buflen=0;
int t, l, ret, output=1;
int overflow_limit, lcmd, ldir;
char *b, *c, *d=NULL;
TLS_VARS;
+ buf = (char*) emalloc(EXEC_INPUT_BUF);
+ if (!buf) {
+ php3_error(E_WARNING, "Unable to emalloc %d bytes", EXEC_INPUT_BUF);
+ return -1;
+ }
+ buflen = EXEC_INPUT_BUF;
+
#ifdef WIN32
(void)AllocConsole(); /* We don't care if this fails. */
#endif
@@ -73,6 +81,7 @@
if (c) *c = '\0';
if (strstr(cmd, "..")) {
php3_error(E_WARNING, "No '..' components allowed in path");
+ efree(buf);
return -1;
}
d = emalloc(l);
@@ -102,6 +111,7 @@
if (!fp) {
php3_error(E_WARNING, "Unable to fork [%s]", d);
efree(d);
+ efree(buf);
return -1;
}
} else { /* not safe_mode */
@@ -112,6 +122,7 @@
#endif
if (!fp) {
php3_error(E_WARNING, "Unable to fork [%s]", cmd);
+ efree(buf);
return -1;
}
}
@@ -126,7 +137,32 @@
}
}
if (type != 3) {
- while (fgets(buf, EXEC_INPUT_BUF - 1, fp)) {
+ l = 0;
+ while ( !feof(fp) || l != 0 ) {
+ l = 0;
+ /* Read a line or fill the buffer, whichever comes first */
+ do {
+ if ( buflen <= (l+1) ) {
+ buf = erealloc(buf, buflen + EXEC_INPUT_BUF);
+ if ( buf == NULL ) {
+ php3_error(E_WARNING, "Unable to erealloc %d bytes",
+ buflen + EXEC_INPUT_BUF);
+ return -1;
+ }
+ buflen += EXEC_INPUT_BUF;
+ }
+
+ if ( fgets(&(buf[l]), buflen - l, fp) == NULL ) {
+ /* eof */
+ break;
+ }
+ l += strlen(&(buf[l]));
+ } while ( (l > 0) && (buf[l-1] != '\n') );
+
+ if ( feof(fp) && (l == 0) ) {
+ break;
+ }
+
if (type == 1) {
if (output) PUTS(buf);
#if APACHE
@@ -152,7 +188,7 @@
/* strip trailing whitespaces */
l = strlen(buf);
t = l;
- while (l && isspace((int)buf[--l]));
+ while (l-- && isspace((int)buf[l]));
if (l < t) buf[l + 1] = '\0';
tmp.value.str.len = strlen(buf);
tmp.value.str.val = estrndup(buf,tmp.value.str.len);
@@ -194,6 +230,7 @@
#endif
if (d) efree(d);
+ efree(buf);
return ret;
}