cvs: /php4/ext/standard exec.c

From: Date: Tue, 20 Jul 1999 16:19:58 +0000
Subject: cvs: /php4/ext/standard exec.c
Groups: php.version4 
Request: Send a blank email to php-version4+get-2345@lists.php.net to get a copy of this message
gerdts Tue Jul 20 16:19:58 1999 EDT Modified files: /php4/ext/standard exec.c Log: exec() can now properly handle lines longer than (EXEC_INPUT_BUF-2) bytes. Lines being returned via the array that that contain only "\n" now are trimmed down to "" to be consistent with exec's whitespace trimming behavior for all other lines. Index: php4/ext/standard/exec.c diff -u php4/ext/standard/exec.c:1.10 php4/ext/standard/exec.c:1.11 --- php4/ext/standard/exec.c:1.10 Fri Jul 16 09:12:55 1999 +++ php4/ext/standard/exec.c Tue Jul 20 16:19:58 1999 @@ -15,7 +15,7 @@ | Author: Rasmus Lerdorf | +----------------------------------------------------------------------+ */ -/* $Id: exec.c,v 1.10 1999/07/16 13:12:55 zeev Exp $ */ +/* $Id: exec.c,v 1.11 1999/07/20 20:19:58 gerdts Exp $ */ #include <stdio.h> #include "php.h" @@ -41,12 +41,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; PLS_FETCH(); + buf = (char*) emalloc(EXEC_INPUT_BUF); + if (!buf) { + php3_error(E_WARNING, "Unable to emalloc %d bytes for exec buffer", EXEC_INPUT_BUF); + return -1; + } + buflen = EXEC_INPUT_BUF; + if (PG(safe_mode)) { lcmd = strlen(cmd); ldir = strlen(PG(safe_mode_exec_dir)); @@ -56,6 +64,7 @@ if (c) *c = '\0'; if (strstr(cmd, "..")) { php3_error(E_WARNING, "No '..' components allowed in path"); + efree(buf); return -1; } d = emalloc(l); @@ -85,6 +94,7 @@ if (!fp) { php3_error(E_WARNING, "Unable to fork [%s]", d); efree(d); + efree(buf); return -1; } } else { /* not safe_mode */ @@ -95,6 +105,7 @@ #endif if (!fp) { php3_error(E_WARNING, "Unable to fork [%s]", cmd); + efree(buf); return -1; } } @@ -106,7 +117,33 @@ } } 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 for exec buffer", + 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) { SLS_FETCH(); @@ -132,7 +169,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'; } @@ -173,6 +210,7 @@ #endif if (d) efree(d); + efree(buf); return ret; }

« previous php.version4 (#2345) next »