diff options
Diffstat (limited to 'ext/standard/file.c')
-rw-r--r-- | ext/standard/file.c | 712 |
1 files changed, 382 insertions, 330 deletions
diff --git a/ext/standard/file.c b/ext/standard/file.c index 56325f5879..92abdaeba2 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | PHP HTML Embedded Scripting Language Version 3.0 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-1999 PHP Development Team (See Credits file) | + | Copyright (c) 1997,1998 PHP Development Team (See Credits file) | +----------------------------------------------------------------------+ | This program is free software; you can redistribute it and/or modify | | it under the terms of one of the following licenses: | @@ -13,7 +13,7 @@ | | | B) the PHP License as published by the PHP Development Team and | | included in the distribution in the file: LICENSE | - | | + | | | This program is distributed in the hope that it will be useful, | | but WITHOUT ANY WARRANTY; without even the implied warranty of | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | @@ -29,6 +29,7 @@ /* $Id$ */ #include "php.h" #include "php_globals.h" +#include "ext/standard/flock_compat.h" #include <stdio.h> #include <stdlib.h> @@ -47,8 +48,10 @@ #include <sys/socket.h> /* #include <sys/uio.h> */ #endif +#include "ext/standard/head.h" #include "safe_mode.h" -#include "php3_standard.h" +#include "php3_string.h" +#include "file.h" #if HAVE_PWD_H #if MSVC5 #include "win32/pwd.h" @@ -59,25 +62,17 @@ #if HAVE_SYS_TIME_H #include <sys/time.h> #endif -#if MSVC5 -#include <winsock.h> -#else -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#endif #include "snprintf.h" #include "fsock.h" #include "fopen-wrappers.h" - -#if HAVE_SYS_FILE_H -#include <sys/file.h> -#endif +#include "php_globals.h" #if MISSING_FCLOSE_DECL extern int fclose(); #endif +static void _php3_closesocket(int *); + #ifndef THREAD_SAFE static int fgetss_state = 0; int le_fp,le_pp; @@ -130,8 +125,8 @@ extern int le_uploads; # endif #endif -char * -tempnam(const char *dir, const char *pfx) + +char *tempnam(const char *dir, const char *pfx) { int save_errno; char *f, *name; @@ -176,37 +171,37 @@ tempnam(const char *dir, const char *pfx) } #endif + function_entry php3_file_functions[] = { - PHP_FE(pclose, NULL) - PHP_FE(popen, NULL) - PHP_FE(readfile, NULL) - PHP_FE(rewind, NULL) + {"pclose", php3_pclose, NULL}, + {"popen", php3_popen, NULL}, + {"readfile", php3_readfile, NULL}, + {"rewind", php3_rewind, NULL}, + {"rmdir", php3_rmdir, NULL}, {"umask", php3_fileumask, NULL}, - PHP_FE(rmdir, NULL) - PHP_FE(fclose, NULL) - PHP_FE(feof, NULL) - PHP_FE(fgetc, NULL) - PHP_FE(fgets, NULL) - PHP_FE(fgetss, NULL) - PHP_FE(fpassthru, NULL) - PHP_FE(fread, NULL) - PHP_FE(fopen, NULL) - PHP_FE(fseek, NULL) - PHP_FE(ftell, NULL) - PHP_FE(fwrite, NULL) - PHP_FE(set_file_buffer, NULL) + {"fclose", php3_fclose, NULL}, + {"feof", php3_feof, NULL}, + {"fgetc", php3_fgetc, NULL}, + {"fgets", php3_fgets, NULL}, + {"fgetss", php3_fgetss, NULL}, + {"fread", php3_fread, NULL}, + {"fopen", php3_fopen, NULL}, + {"fpassthru", php3_fpassthru, NULL}, + {"fseek", php3_fseek, NULL}, + {"ftell", php3_ftell, NULL}, + {"fwrite", php3_fwrite, NULL}, {"fputs", php3_fwrite, NULL}, - PHP_FE(mkdir, NULL) - PHP_FE(rename, NULL) + {"mkdir", php3_mkdir, NULL}, + {"rename", php3_rename, NULL}, {"copy", php3_file_copy, NULL}, - PHP_FE(tempnam, NULL) - PHP_FE(file, NULL) - PHP_FE(flock, NULL) - PHP_FE(fgetcsv, NULL) - PHP_FE(get_meta_tags, NULL) - PHP_FE(set_socket_blocking, NULL) + {"tempnam", php3_tempnam, NULL}, + {"file", php3_file, NULL}, + {"fgetcsv", php3_fgetcsv, NULL}, + PHP_FE(flock, NULL) + {"get_meta_tags", php3_get_meta_tags, NULL}, + {"set_socket_blocking", php3_set_socket_blocking, NULL}, #if (0 && HAVE_SYS_TIME_H && HAVE_SETSOCKOPT && defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)) - PHP_FE(set_socket_timeout, NULL) + {"set_socket_timeout", php3_set_socket_timeout, NULL}, #endif {NULL, NULL, NULL} }; @@ -215,9 +210,64 @@ php3_module_entry php3_file_module_entry = { "PHP_file", php3_file_functions, php3_minit_file, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES }; + +static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; + +/* {{{ proto bool flock(int fp, int operation) + portable file locking */ +PHP_FUNCTION(flock) +{ + pval *arg1, *arg2; + FILE *fp; + int type; + int issock=0; + int *sock, fd=0; + int act = 0; + + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_long(arg1); + convert_to_long(arg2); + + fp = php3_list_find(arg1->value.lval, &type); + if (type == wsa_fp){ + issock = 1; + sock = php3_list_find(arg1->value.lval, &type); + fd = *sock; + } + + if ((!fp || (type!=le_fp && type!=le_pp)) && (!fd || type!=wsa_fp)) { + php3_error(E_WARNING,"Unable to find file identifier %d",arg1->value.lval); + RETURN_FALSE; + } + + if (!issock) { + fd = fileno(fp); + } + + act = arg2->value.lval & 3; + if(act < 1 || act > 3) { + php3_error(E_WARNING, "illegal value for second argument"); + RETURN_FALSE; + } + /* flock_values contains all possible actions + if (arg2 & 4) we won't block on the lock */ + act = flock_values[act - 1] | (arg2->value.lval & 4 ? LOCK_NB : 0); + if (flock(fd, act) == -1) { + RETURN_FALSE; + } + + RETURN_TRUE; +} +/* }}} */ + + /* {{{ proto array get_meta_tags(string filename [, int use_include_path]) Extracts all meta tag content attributes from a file and returns an array */ -void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(get_meta_tags) +{ pval *filename, *arg2; FILE *fp; char buf[8192]; @@ -229,20 +279,20 @@ void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS) { /* check args */ switch (ARG_COUNT(ht)) { - case 1: - if (getParameters(ht,1,&filename) == FAILURE) { - WRONG_PARAM_COUNT; - } - break; - case 2: - if (getParameters(ht,2,&filename,&arg2) == FAILURE) { + case 1: + if (getParameters(ht,1,&filename) == FAILURE) { + WRONG_PARAM_COUNT; + } + break; + case 2: + if (getParameters(ht,2,&filename,&arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(arg2); + use_include_path = arg2->value.lval; + break; + default: WRONG_PARAM_COUNT; - } - convert_to_long(arg2); - use_include_path = arg2->value.lval; - break; - default: - WRONG_PARAM_COUNT; } convert_to_string(filename); @@ -257,7 +307,11 @@ void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS) { if (array_init(return_value)==FAILURE) { if (issock) { - SOCK_FCLOSE(socketd); +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif } else { fclose(fp); } @@ -265,7 +319,7 @@ void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS) { } /* Now loop through the file and do the magic quotes thing if needed */ memset(buf,0,8191); - while((issock?SOCK_FGETS(buf,8191,socketd):fgets(buf,8191,fp)) != NULL + while((issock?SOCK_FGETS(buf,8191,socketd):fgets(buf,8191,fp) != NULL) && !php3i_stristr(buf,"</head>")) { if(php3i_stristr(buf,"<meta")) { @@ -329,41 +383,46 @@ void php3_get_meta_tags(INTERNAL_FUNCTION_PARAMETERS) { } } if (issock) { - SOCK_FCLOSE(socketd); +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif } else { fclose(fp); } } /* }}} */ -/* {{{ proto array file(string filename [, int use_include_path]) + +/* {{{ proto array file(string filename) Read entire file into an array */ -void php3_file(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(file) +{ pval *filename, *arg2; FILE *fp; char *slashed, buf[8192]; register int i=0; int use_include_path = 0; - int issock=0, socketd=0; PLS_FETCH(); /* check args */ switch (ARG_COUNT(ht)) { - case 1: - if (getParameters(ht,1,&filename) == FAILURE) { - WRONG_PARAM_COUNT; - } - break; - case 2: - if (getParameters(ht,2,&filename,&arg2) == FAILURE) { + case 1: + if (getParameters(ht,1,&filename) == FAILURE) { + WRONG_PARAM_COUNT; + } + break; + case 2: + if (getParameters(ht,2,&filename,&arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(arg2); + use_include_path = arg2->value.lval; + break; + default: WRONG_PARAM_COUNT; - } - convert_to_long(arg2); - use_include_path = arg2->value.lval; - break; - default: - WRONG_PARAM_COUNT; } convert_to_string(filename); @@ -383,7 +442,7 @@ void php3_file(INTERNAL_FUNCTION_PARAMETERS) { /* Now loop through the file and do the magic quotes thing if needed */ memset(buf,0,8191); - while((issock?SOCK_FGETS(buf,8191,socketd):fgets(buf,8191,fp)) != NULL) { + while(issock?SOCK_FGETS(buf,8191,socketd):fgets(buf,8191,fp) != NULL) { if (PG(magic_quotes_runtime)) { int len; @@ -394,7 +453,11 @@ void php3_file(INTERNAL_FUNCTION_PARAMETERS) { } } if (issock) { - SOCK_FCLOSE(socketd); +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif } else { fclose(fp); } @@ -404,46 +467,52 @@ void php3_file(INTERNAL_FUNCTION_PARAMETERS) { static void __pclose(FILE *pipe) { - PLS_FETCH(); pclose_ret = pclose(pipe); } + static void _php3_closesocket(int *sock) { - if(sock) { - SOCK_FCLOSE(*sock); + int socketd=*sock; + if (socketd){ +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif +#if HAVE_SHUTDOWN + shutdown(socketd, 0); +#endif efree(sock); } } -static void _php3_unlink_uploaded_file(char *file) { + +static void _php3_unlink_uploaded_file(char *file) +{ if(file) { unlink(file); } } -static void php3i_destructor_fclose(FILE *fp) { - (void)fclose(fp); -} int php3_minit_file(INIT_FUNC_ARGS) { - PLS_FETCH(); - - le_fp = register_list_destructors(php3i_destructor_fclose,NULL); + le_fp = register_list_destructors(fclose,NULL); le_pp = register_list_destructors(__pclose,NULL); wsa_fp = register_list_destructors(_php3_closesocket,NULL); le_uploads = register_list_destructors(_php3_unlink_uploaded_file,NULL); return SUCCESS; } + /* {{{ proto string tempnam(string dir, string prefix) Create a unique filename in a directory */ -void php3_tempnam(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(tempnam) +{ pval *arg1, *arg2; char *d; char *t; char p[64]; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; @@ -459,9 +528,11 @@ void php3_tempnam(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int fopen(string filename, string mode [, int use_include_path]) Open a file or a URL and return a file pointer */ -void php3_fopen(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fopen) +{ pval *arg1, *arg2, *arg3; FILE *fp; char *p; @@ -469,23 +540,22 @@ void php3_fopen(INTERNAL_FUNCTION_PARAMETERS) { int id; int use_include_path = 0; int issock=0, socketd=0; - PLS_FETCH(); switch(ARG_COUNT(ht)) { - case 2: - if (getParameters(ht,2,&arg1,&arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - break; - case 3: - if (getParameters(ht,3,&arg1,&arg2,&arg3) == FAILURE) { + case 2: + if (getParameters(ht,2,&arg1,&arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + break; + case 3: + if (getParameters(ht,3,&arg1,&arg2,&arg3) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(arg3); + use_include_path = arg3->value.lval; + break; + default: WRONG_PARAM_COUNT; - } - convert_to_long(arg3); - use_include_path = arg3->value.lval; - break; - default: - WRONG_PARAM_COUNT; } convert_to_string(arg1); convert_to_string(arg2); @@ -518,13 +588,14 @@ void php3_fopen(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int fclose(int fp) Close an open file pointer */ -void php3_fclose(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fclose) +{ pval *arg1; int id, type; FILE *fp; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -541,9 +612,11 @@ void php3_fclose(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int popen(string command, string mode) Execute a command and open either a read or a write pipe to it */ -void php3_popen(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(popen) +{ pval *arg1, *arg2; FILE *fp; int id; @@ -558,47 +631,51 @@ void php3_popen(INTERNAL_FUNCTION_PARAMETERS) { convert_to_string(arg2); p = estrndup(arg2->value.str.val,arg2->value.str.len); if (PG(safe_mode)){ - b = strchr(arg1->value.str.val,' '); - if (!b) { - b = strrchr(arg1->value.str.val,'/'); - } else { - char *c; - c = arg1->value.str.val; - while((*b!='/')&&(b!=c)) b--; - if (b==c) b=NULL; - } - if (b) { - snprintf(buf,sizeof(buf),"%s%s",PG(safe_mode_exec_dir),b); - } else { - snprintf(buf,sizeof(buf),"%s/%s",PG(safe_mode_exec_dir),arg1->value.str.val); - } - fp = popen(buf,p); - if (!fp) { - php3_error(E_WARNING,"popen(\"%s\",\"%s\") - %s",buf,p,strerror(errno)); - RETURN_FALSE; - } + b = strchr(arg1->value.str.val,' '); + if (!b) { + b = strrchr(arg1->value.str.val,'/'); + } else { + char *c; + c = arg1->value.str.val; + while((*b!='/')&&(b!=c)) { + b--; + } + if (b==c) { + b=NULL; + } + } + if (b) { + snprintf(buf,sizeof(buf),"%s%s",PG(safe_mode_exec_dir),b); + } else { + snprintf(buf,sizeof(buf),"%s/%s",PG(safe_mode_exec_dir),arg1->value.str.val); + } + fp = popen(buf,p); + if (!fp) { + php3_error(E_WARNING,"popen(\"%s\",\"%s\") - %s",buf,p,strerror(errno)); + RETURN_FALSE; + } } else { - fp = popen(arg1->value.str.val,p); - if (!fp) { - php3_error(E_WARNING,"popen(\"%s\",\"%s\") - %s",arg1->value.str.val,p,strerror(errno)); - efree(p); - RETURN_FALSE; - } + fp = popen(arg1->value.str.val,p); + if (!fp) { + php3_error(E_WARNING,"popen(\"%s\",\"%s\") - %s",arg1->value.str.val,p,strerror(errno)); + efree(p); + RETURN_FALSE; + } } -/* #endif */ id = php3_list_insert(fp,le_pp); efree(p); RETURN_LONG(id); } /* }}} */ + /* {{{ proto int pclose(int fp) Close a file pointer opened by popen() */ -void php3_pclose(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(pclose) +{ pval *arg1; int id,type; FILE *fp; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -616,15 +693,16 @@ void php3_pclose(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int feof(int fp) Test for end-of-file on a file pointer */ -void php3_feof(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(feof) +{ pval *arg1; FILE *fp; int id, type; int issock=0; int socketd=0, *sock; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -642,7 +720,7 @@ void php3_feof(INTERNAL_FUNCTION_PARAMETERS) { /* we're at the eof if the file doesn't exist */ RETURN_TRUE; } - if ((issock?SOCK_FEOF(socketd):feof(fp))) { + if ((issock?(SOCK_FEOF(socketd)):feof(fp))) { RETURN_TRUE; } else { RETURN_FALSE; @@ -650,15 +728,15 @@ void php3_feof(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int set_socket_blocking(int socket descriptor, int mode) Set blocking/non-blocking mode on a socket */ -void php3_set_socket_blocking(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(set_socket_blocking) { pval *arg1, *arg2; int id, type, block; int flags; int socketd=0, *sock; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; @@ -674,7 +752,6 @@ void php3_set_socket_blocking(INTERNAL_FUNCTION_PARAMETERS) RETURN_FALSE; } socketd=*sock; - _php3_sock_set_blocking(socketd, block == 0 ? 0 : 1); #if WIN32|WINNT /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */ flags=block; @@ -716,7 +793,7 @@ void php3_set_socket_blocking(INTERNAL_FUNCTION_PARAMETERS) #if (0 && HAVE_SYS_TIME_H && HAVE_SETSOCKOPT && defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)) /* this doesn't work, as it appears those properties are read-only :( */ -void php3_set_socket_timeout(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(set_socket_timeout) { pval *socket,*timeout; int type, *sock; @@ -741,9 +818,10 @@ void php3_set_socket_timeout(INTERNAL_FUNCTION_PARAMETERS) } #endif + /* {{{ proto string fgets(int fp, int length) Get a line from file pointer */ -void php3_fgets(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(fgets) { pval *arg1, *arg2; FILE *fp; @@ -774,7 +852,7 @@ void php3_fgets(INTERNAL_FUNCTION_PARAMETERS) buf = emalloc(sizeof(char) * (len + 1)); /* needed because recv doesnt put a null at the end*/ memset(buf,0,len+1); - if ((issock?SOCK_FGETS(buf,len,socketd):fgets(buf,len,fp)) == NULL) { + if (!(issock?SOCK_FGETS(buf,len,socketd):fgets(buf,len,fp) != NULL)) { efree(buf); RETVAL_FALSE; } else { @@ -790,16 +868,16 @@ void php3_fgets(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ + /* {{{ proto string fgetc(int fp) Get a character from file pointer */ -void php3_fgetc(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fgetc) { pval *arg1; FILE *fp; int id, type; char *buf; int issock=0; int *sock, socketd=0; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -827,21 +905,22 @@ void php3_fgetc(INTERNAL_FUNCTION_PARAMETERS) { return_value->value.str.len = 1; return_value->type = IS_STRING; } + return; } /* }}} */ + /* Strip any HTML tags while reading */ /* {{{ proto string fgetss(int fp, int length) Get a line from file pointer and strip HTML tags */ -void php3_fgetss(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(fgetss) { pval *fd, *bytes; FILE *fp; - int id, len, type; - char *buf; + int id, len, br, type; + char *buf, *p, *rbuf, *rp, c, lc; int issock=0; int *sock,socketd=0; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &fd, &bytes) == FAILURE) { WRONG_PARAM_COUNT; @@ -867,23 +946,98 @@ void php3_fgetss(INTERNAL_FUNCTION_PARAMETERS) buf = emalloc(sizeof(char) * (len + 1)); /*needed because recv doesnt set null char at end*/ memset(buf,0,len+1); - if (!((issock?SOCK_FGETS(buf,len,socketd):fgets(buf, len, fp)) != NULL)) { + if (!(issock?SOCK_FGETS(buf,len,socketd):fgets(buf, len, fp) != NULL)) { efree(buf); RETURN_FALSE; } - _php3_strip_tags(buf,fgetss_state); - RETURN_STRING(buf,0); + rbuf = estrdup(buf); + c = *buf; + lc = '\0'; + p = buf; + rp = rbuf; + br = 0; + + while (c) { + switch (c) { + case '<': + if (fgetss_state == 0) { + lc = '<'; + fgetss_state = 1; + } + break; + + case '(': + if (fgetss_state == 2) { + if (lc != '\"') { + lc = '('; + br++; + } + } else if (fgetss_state == 0) { + *(rp++) = c; + } + break; + + case ')': + if (fgetss_state == 2) { + if (lc != '\"') { + lc = ')'; + br--; + } + } else if (fgetss_state == 0) { + *(rp++) = c; + } + break; + + case '>': + if (fgetss_state == 1) { + lc = '>'; + fgetss_state = 0; + } else if (fgetss_state == 2) { + if (!br && lc != '\"') { + fgetss_state = 0; + } + } + break; + + case '\"': + if (fgetss_state == 2) { + if (lc == '\"') { + lc = '\0'; + } else if (lc != '\\') { + lc = '\"'; + } + } else if (fgetss_state == 0) { + *(rp++) = c; + } + break; + + case '?': + if (fgetss_state==1) { + br=0; + fgetss_state=2; + break; + } + /* fall-through */ + + default: + if (fgetss_state == 0) { + *(rp++) = c; + } + } + c = *(++p); + } + *rp = '\0'; + efree(buf); + RETVAL_STRING(rbuf,1); + efree(rbuf); } /* }}} */ -/* {{{ proto int fputs(int fp, string str [, int length]) - An alias for fwrite */ -/* }}} */ /* {{{ proto int fwrite(int fp, string str [, int length]) Binary-safe file write */ -void php3_fwrite(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(fwrite) { pval *arg1, *arg2, *arg3=NULL; FILE *fp; @@ -892,7 +1046,7 @@ void php3_fwrite(INTERNAL_FUNCTION_PARAMETERS) int issock=0; int *sock, socketd=0; PLS_FETCH(); - + switch (ARG_COUNT(ht)) { case 2: if (getParameters(ht, 2, &arg1, &arg2)==FAILURE) { @@ -942,66 +1096,14 @@ void php3_fwrite(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto int set_file_buffer(int fp, int buffer) -Set file write buffer */ -/* - wrapper for setvbuf() -*/ -void php3_set_file_buffer(INTERNAL_FUNCTION_PARAMETERS) -{ - pval *arg1, *arg2; - FILE *fp; - int ret,id,type,buff; - int issock=0; - int *sock, socketd=0; - PLS_FETCH(); - - switch (ARG_COUNT(ht)) { - case 2: - if (getParameters(ht, 2, &arg1, &arg2)==FAILURE) { - RETURN_FALSE; - } - convert_to_long(arg1); - convert_to_long(arg2); - break; - default: - WRONG_PARAM_COUNT; - /* NOTREACHED */ - break; - } - - id = arg1->value.lval; - buff = arg2->value.lval; - fp = php3_list_find(id,&type); - if (type==wsa_fp){ - issock=1; - sock = php3_list_find(id,&type); - socketd=*sock; - } - if ((!fp || (type!=le_fp && type!=le_pp)) && (!socketd || type!=wsa_fp)) { - php3_error(E_WARNING,"Unable to find file identifier %d",id); - RETURN_FALSE; - } - - /* if buff is 0 then set to non-buffered */ - if(buff==0){ - ret=setvbuf(fp, NULL, _IONBF, 0); - } - else{ - ret=setvbuf(fp, NULL, _IOFBF, buff); - } - - RETURN_LONG(ret); -} -/* }}} */ /* {{{ proto int rewind(int fp) Rewind the position of a file pointer */ -void php3_rewind(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(rewind) +{ pval *arg1; int id,type; FILE *fp; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -1018,14 +1120,15 @@ void php3_rewind(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int ftell(int fp) Get file pointer's read/write position */ -void php3_ftell(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(ftell) +{ pval *arg1; int id, type; long pos; FILE *fp; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -1042,14 +1145,15 @@ void php3_ftell(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int fseek(int fp, int offset) Seek on a file pointer */ -void php3_fseek(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fseek) +{ pval *arg1, *arg2; int ret,id,type; long pos; FILE *fp; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; @@ -1078,9 +1182,11 @@ void php3_fseek(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int mkdir(string pathname, int mode) Create a directory */ -void php3_mkdir(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(mkdir) +{ pval *arg1, *arg2; int ret,mode; PLS_FETCH(); @@ -1103,9 +1209,11 @@ void php3_mkdir(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int rmdir(string dirname) Remove a directory */ -void php3_rmdir(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(rmdir) +{ pval *arg1; int ret; PLS_FETCH(); @@ -1126,17 +1234,17 @@ void php3_rmdir(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int readfile(string filename [, int use_include_path]) Output a file or a URL */ -void php3_readfile(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(readfile) +{ pval *arg1, *arg2; char buf[8192]; FILE *fp; int b, size; int use_include_path = 0; - int issock=0, socketd=0; - PLS_FETCH(); /* check args */ switch (ARG_COUNT(ht)) { @@ -1170,14 +1278,16 @@ void php3_readfile(INTERNAL_FUNCTION_PARAMETERS) { RETURN_FALSE; } size= 0; - if (php3_header()) { /* force header if not already sent */ - while(issock?(b=SOCK_FREAD(buf,sizeof(buf),socketd)):(b = fread(buf, 1, sizeof(buf), fp)) > 0) { - PHPWRITE(buf,b); - size += b ; - } + while(issock?(b=SOCK_FGETS(buf,sizeof(buf),socketd)):(b = fread(buf, 1, sizeof(buf), fp)) > 0) { + PHPWRITE(buf,b); + size += b ; } if (issock) { - SOCK_FCLOSE(socketd); +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif } else { fclose(fp); } @@ -1185,13 +1295,14 @@ void php3_readfile(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int umask([int mask]) Return or change the umask */ -void php3_fileumask(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fileumask) +{ pval *arg1; int oldumask; int arg_count = ARG_COUNT(ht); - PLS_FETCH(); oldumask = umask(077); @@ -1209,19 +1320,20 @@ void php3_fileumask(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* * Read to EOF on a file descriptor and write the output to stdout. */ /* {{{ proto int fpassthru(int fp) Output all remaining data from a file pointer */ -void php3_fpassthru(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fpassthru) +{ pval *arg1; FILE *fp; char buf[8192]; int id, size, b, type; int issock=0; int socketd=0, *sock; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; @@ -1240,38 +1352,51 @@ void php3_fpassthru(INTERNAL_FUNCTION_PARAMETERS) { } size = 0; if (php3_header()) { /* force headers if not already sent */ - while(issock?(b=SOCK_FREAD(buf,sizeof(buf),socketd)):(b = fread(buf, 1, sizeof(buf), fp)) > 0) { + while(issock?(b=SOCK_FGETS(buf,sizeof(buf),socketd)):(b = fread(buf, 1, sizeof(buf), fp)) > 0) { PHPWRITE(buf,b); size += b ; } } +/* + if (issock) { +#if WIN32|WINNT + closesocket(socketd); +#else + close(socketd); +#endif + } else { + fclose(fp); + } +*/ php3_list_delete(id); RETURN_LONG(size); } /* }}} */ + /* {{{ proto int rename(string old_name, string new_name) Rename a file */ -void php3_rename(INTERNAL_FUNCTION_PARAMETERS) { - pval *OLD, *NEW; - char *old, *new; +PHP_FUNCTION(rename) +{ + pval *old_arg, *new_arg; + char *old_name, *new_name; int ret; PLS_FETCH(); - if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &OLD, &NEW) == FAILURE) { + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &old_arg, &new_arg) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_string(OLD); - convert_to_string(NEW); + convert_to_string(old_arg); + convert_to_string(new_arg); - old = OLD->value.str.val; - new = NEW->value.str.val; + old_name = old_arg->value.str.val; + new_name = new_arg->value.str.val; - if (PG(safe_mode) &&(!_php3_checkuid(old,2))) { + if (PG(safe_mode) &&(!_php3_checkuid(old_name, 2))) { RETURN_FALSE; } - ret = rename(old, new); + ret = rename(old_name, new_name); if (ret == -1) { php3_error(E_WARNING, @@ -1283,9 +1408,10 @@ void php3_rename(INTERNAL_FUNCTION_PARAMETERS) { } /* }}} */ + /* {{{ proto int copy(string source_file, string destination_file) Copy a file */ -void php3_file_copy(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(file_copy) { pval *source, *target; char buffer[8192]; @@ -1337,9 +1463,10 @@ void php3_file_copy(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ + /* {{{ proto int fread(int fp, int length) Binary-safe file read */ -void php3_fread(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(fread) { pval *arg1, *arg2; FILE *fp; @@ -1373,7 +1500,7 @@ void php3_fread(INTERNAL_FUNCTION_PARAMETERS) return_value->value.str.len = fread(return_value->value.str.val, 1, len, fp); return_value->value.str.val[return_value->value.str.len] = 0; } else { - return_value->value.str.len = SOCK_FREAD(return_value->value.str.val, len, socketd); + return_value->value.str.len = _php3_sock_fread(return_value->value.str.val, len, socketd); } if (PG(magic_quotes_runtime)) { return_value->value.str.val = _php3_addslashes(return_value->value.str.val,return_value->value.str.len,&return_value->value.str.len,1); @@ -1382,105 +1509,30 @@ void php3_fread(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* aparently needed for pdf to be compiled as a module under windows */ -PHPAPI int php3i_get_le_fp(void){ - PLS_FETCH(); - return le_fp; -} - -static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; -/* {{{ proto bool flock(int fp, int operation) - portable file locking */ -PHP_FUNCTION(flock) +/* aparently needed for pdf to be compiled as a module under windows */ +PHPAPI int php3i_get_le_fp(void) { - pval *arg1, *arg2; - FILE *fp; - int type; - int issock=0; - int *sock, fd=0; - int act = 0; - PLS_FETCH(); - - if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - - convert_to_long(arg1); - convert_to_long(arg2); - - fp = php3_list_find(arg1->value.lval, &type); - if (type == wsa_fp){ - issock = 1; - sock = php3_list_find(arg1->value.lval, &type); - fd = *sock; - } - - if ((!fp || (type!=le_fp && type!=le_pp)) && (!fd || type!=wsa_fp)) { - php3_error(E_WARNING,"Unable to find file identifier %d",arg1->value.lval); - RETURN_FALSE; - } - - if (!issock) { - fd = fileno(fp); - } - - act = arg2->value.lval & 3; - if(act < 1 || act > 3) { - php3_error(E_WARNING, "illegal value for second argument"); - RETURN_FALSE; - } - /* flock_values contains all possible actions - if (arg2 & 4) we won't block on the lock */ - act = flock_values[act - 1] | (arg2->value.lval & 4 ? LOCK_NB : 0); - if (flock(fd, act) == -1) { - RETURN_FALSE; - } - - RETURN_TRUE; + return le_fp; } -/* }}} */ - /* {{{ proto array fgetcsv(int fp, int length) get line from file pointer and parse for CSV fields */ -void php3_fgetcsv(INTERNAL_FUNCTION_PARAMETERS) { +PHP_FUNCTION(fgetcsv) { char *temp, *tptr, *bptr; - char delimiter = ','; /* allow this to be set as parameter */ + char delimiter = ','; /* allow this to be set as parameter if required in future version? */ /* first section exactly as php3_fgetss */ - pval *fd, *bytes, *p_delim; + pval *fd, *bytes; FILE *fp; int id, len, type; char *buf; int issock=0; int *sock,socketd=0; - PLS_FETCH(); - switch(ARG_COUNT(ht)) { - case 2: - if (getParameters(ht, 2, &fd, &bytes) == FAILURE) { - WRONG_PARAM_COUNT; - } - break; - - case 3: - if (getParameters(ht, 3, &fd, &bytes, &p_delim) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string(p_delim); - /* Make sure that there is at least one character in string */ - if (p_delim->value.str.len < 1) { - WRONG_PARAM_COUNT; - } - /* use first character from string */ - delimiter = p_delim->value.str.val[0]; - break; - - default: - WRONG_PARAM_COUNT; - break; + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &fd, &bytes) == FAILURE) { + WRONG_PARAM_COUNT; } convert_to_long(fd); @@ -1503,7 +1555,7 @@ void php3_fgetcsv(INTERNAL_FUNCTION_PARAMETERS) { buf = emalloc(sizeof(char) * (len + 1)); /*needed because recv doesnt set null char at end*/ memset(buf,0,len+1); - if ((issock?SOCK_FGETS(buf,len,socketd):fgets(buf, len, fp)) == NULL) { + if (!(issock?SOCK_FGETS(buf,len,socketd):fgets(buf, len, fp) != NULL)) { efree(buf); RETURN_FALSE; } @@ -1537,7 +1589,7 @@ void php3_fgetcsv(INTERNAL_FUNCTION_PARAMETERS) { do { /* 1. Strip any leading space */ - while(isspace(*bptr)) bptr++; + while isspace(*bptr) bptr++; /* 2. Read field, leaving bptr pointing at start of next field */ if (*bptr == '"') { /* 2A. handle quote delimited field */ |