cvs: php4 /ext/standard basic_functions.c file.c file.h

From: Date: Sun, 07 Oct 2001 18:34:45 +0000
Subject: cvs: php4 /ext/standard basic_functions.c file.c file.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7713@lists.php.net to get a copy of this message
derick Sun Oct 7 14:34:45 2001 EDT Modified files: /php4/ext/standard basic_functions.c file.c file.h Log: @- Added mmapfile(), read a file into a variabele with mmap (Ilia A, Derick) - Added mmapfile(), read a file into a variabele with mmap (Patch by: Ilia A. <ilia@prohost.org>) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.404 php4/ext/standard/basic_functions.c:1.405 --- php4/ext/standard/basic_functions.c:1.404 Fri Oct 5 19:56:19 2001 +++ php4/ext/standard/basic_functions.c Sun Oct 7 14:34:44 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.404 2001/10/05 23:56:19 ssb Exp $ */ +/* $Id: basic_functions.c,v 1.405 2001/10/07 18:34:44 derick Exp $ */ #include "php.h" #include "php_main.h" @@ -576,6 +576,9 @@ PHP_FE(pclose, NULL) PHP_FE(popen, NULL) PHP_FE(readfile, NULL) +#if HAVE_MMAP + PHP_FE(mmapfile, NULL) +#endif PHP_FE(rewind, NULL) PHP_FE(rmdir, NULL) PHP_FE(umask, NULL) Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.186 php4/ext/standard/file.c:1.187 --- php4/ext/standard/file.c:1.186 Tue Sep 25 17:58:22 2001 +++ php4/ext/standard/file.c Sun Oct 7 14:34:44 2001 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.186 2001/09/25 21:58:22 jeroen Exp $ */ +/* $Id: file.c,v 1.187 2001/10/07 18:34:44 derick Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -1633,6 +1633,89 @@ } /* }}} */ + +#if HAVE_MMAP + +/* {{{ proto string mmapfile (string filename [, int bytes_to_read]) + Read a file into a variable with mmap */ +PHP_FUNCTION(mmapfile) +{ + zval **arg1, **arg2; + int in, bytes_to_read = 0; + char *data; + char *in_addr; + struct stat f_stat; + + memset (&f_stat, 0, sizeof (stat)); + + /* check args */ + switch (ARG_COUNT(ht)) { + case 1: + if (zend_get_parameters_ex (1, &arg1) == FAILURE) { + WRONG_PARAM_COUNT; + } + break; + case 2: + if (zend_get_parameters_ex (2, &arg1, &arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex (arg2); + bytes_to_read = (*arg2)->value.lval; + break; + default: + WRONG_PARAM_COUNT; + } + convert_to_string_ex (arg1); + + if ((in = open ((*arg1)->value.str.val, O_RDONLY)) == -1) { + php_error (E_WARNING, "can't re-open input file for mmap"); + RETURN_FALSE; + } + + if (fstat (in, &f_stat)) { + php_error (E_WARNING, "cannot get file information"); + RETURN_FALSE; + } + + if (!bytes_to_read) { + bytes_to_read = f_stat.st_size; + } else { + bytes_to_read = (bytes_to_read < f_stat.st_size) ? bytes_to_read : f_stat.st_size; + } + + if ((in_addr = mmap (NULL, bytes_to_read, PROT_READ, MAP_SHARED, in, 0)) == (char*) -1) { + php_error (E_WARNING, "can't mmap file"); + RETURN_FALSE; + } + + data = emalloc (bytes_to_read + 1); + data[bytes_to_read + 1] = '\0'; + + if (!memcpy(data, in_addr, bytes_to_read)) { + efree (data); + php_error (E_WARNING, "cannot copy file to memory"); + RETURN_FALSE; + } + + if (munmap(in_addr, bytes_to_read)) { + efree (data); + php_error (E_WARNING, "cannot unmap the file"); + RETURN_FALSE; + } + + if (close(in)) { + efree (data); + php_error (E_WARNING, "cannot close file"); + RETURN_FALSE; + } + + RETURN_STRINGL(data, bytes_to_read + 1, 0); +} +/* }}} */ + +#endif + + /* {{{ proto int umask([int mask]) Return or change the umask */ PHP_FUNCTION(umask) @@ -1659,6 +1742,8 @@ } /* }}} */ + + /* {{{ proto int fpassthru(int fp) Output all remaining data from a file pointer */ Index: php4/ext/standard/file.h diff -u php4/ext/standard/file.h:1.40 php4/ext/standard/file.h:1.41 --- php4/ext/standard/file.h:1.40 Sat Aug 4 21:42:42 2001 +++ php4/ext/standard/file.h Sun Oct 7 14:34:44 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.h,v 1.40 2001/08/05 01:42:42 zeev Exp $ */ +/* $Id: file.h,v 1.41 2001/10/07 18:34:44 derick Exp $ */ /* Synced with php 3.0 revision 1.30 1999-06-16 [ssb] */ @@ -48,6 +48,9 @@ PHP_FUNCTION(rmdir); PHP_FUNCTION(fpassthru); PHP_FUNCTION(readfile); +#if HAVE_MMAP +PHP_FUNCTION(mmapfile); +#endif PHP_FUNCTION(umask); PHP_FUNCTION(rename); PHP_FUNCTION(unlink);

« previous php.cvs (#7713) next »