*/
return fclose(stream->stdiocast);
}
-
+
php_stream_flush(stream);
}
-
+
ret = stream->ops->close(stream, call_dtor);
stream->abstract = NULL;
-
- if (call_dtor) {
+
+ if (call_dtor) {
/* tidy up any FILE* that might have been fdopened */
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
fclose(stream->stdiocast);
PHPAPI int php_stream_seek(php_stream *stream, off_t offset, int whence)
{
- if (stream->ops->seek) {
+ if (stream->ops->seek) {
return stream->ops->seek(stream, offset, whence);
}
}
return 0;
}
-
+
zend_error(E_WARNING, "streams of type %s do not support seeking", stream->ops->label);
return -1;
}
if (buf)
*buf = NULL;
-
+
if (maxlen == 0)
return 0;
* buffering layer.
* */
if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
- php_stream_tell(src) == 0 &&
+ php_stream_tell(src) == 0 &&
SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
{
struct stat sbuf;
if (maxlen > sbuf.st_size || maxlen == 0)
maxlen = sbuf.st_size;
-
+
srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
if (srcfile != (void*)MAP_FAILED) {
memcpy(*buf, srcfile, maxlen);
ret = maxlen;
}
-
+
munmap(srcfile, maxlen);
return ret;
}
/* fall through - we might be able to copy in smaller chunks */
}
#endif
-
+
ptr = *buf = pemalloc(persistent, step);
max_len = step;
if (maxlen == PHP_STREAM_COPY_ALL)
maxlen = 0;
-
+
#if HAVE_MMAP
/* try and optimize the case where we are copying from the start of a plain file.
* We could probably make this work in more situations, but I don't trust the stdio
* buffering layer.
* */
if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
- php_stream_tell(src) == 0 &&
+ php_stream_tell(src) == 0 &&
SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
{
struct stat sbuf;
if (fstat(srcfd, &sbuf) == 0) {
void *srcfile;
-
+
if (maxlen > sbuf.st_size || maxlen == 0)
maxlen = sbuf.st_size;
-
+
srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
if (srcfile != (void*)MAP_FAILED) {
haveread = php_stream_write(dest, srcfile, maxlen);
/* fall through - we might be able to copy in smaller chunks */
}
#endif
-
+
while(1) {
readchunk = sizeof(buf);
towrite = didread;
writeptr = buf;
haveread += didread;
-
+
while(towrite) {
didwrite = php_stream_write(dest, writeptr, towrite);
if (didwrite == 0)
return 0; /* error */
-
+
towrite -= didwrite;
writeptr += didwrite;
}
} else {
- return 0; /* error */
+ if ( !maxlen) {
+ return haveread;
+ } else {
+ return 0; /* error */
+ }
}
if (maxlen - haveread == 0) {
}
return haveread;
-
+
}
/* }}} */
fclose(fp);
zend_error(E_WARNING, "%s(): unable to allocate stream", get_active_function_name(TSRMLS_C));
-
+
return NULL;
}
return NULL;
{
FILE *fp;
php_stream *stream;
-
+
fp = tmpfile();
if (fp == NULL) {
zend_error(E_WARNING, "tmpfile(): %s", strerror(errno));
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
-
+
#if HAVE_FLUSHIO
if (data->last_op == 'r') {
fseek(data->file, 0, SEEK_CUR);
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
-
+
if (buf == NULL && count == 0) {
/* check for EOF condition */
if (feof(data->file)) {
fseek(data->file, 0, SEEK_CUR);
data->last_op = 'r';
#endif
-
+
return fread(buf, 1, count, data->file);
}
}
efree(data);
-
+
return ret;
}
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
-
+
return fflush(data->file);
}
if (offset == 0 && whence == SEEK_CUR)
return ftell(data->file);
-
+
return fseek(data->file, offset, whence);
}
}
data->last_op = 'r';
#endif
-
+
return fgets(buf, size, data->file);
}
static int php_stdiop_cast(php_stream *stream, int castas, void **ret)
{
int fd;
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
-
+
assert(data != NULL);
switch (castas) {
|| exec_fname_length<=0) {
/* [no active file] or no path */
pathbuf = estrdup(path);
- } else {
+ } else {
pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
memcpy(pathbuf, path, path_length);
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
char *realpath = NULL;
realpath = expand_filepath(filename, NULL TSRMLS_CC);
-
+
fp = fopen(realpath, mode);
if (fp) {
{
int flags = castas & PHP_STREAM_CAST_MASK;
castas &= ~PHP_STREAM_CAST_MASK;
-
+
if (castas == PHP_STREAM_AS_STDIO) {
if (stream->stdiocast) {
if (ret) {
php_stream_free(stream, 0);
}
}
-
+
return SUCCESS;
} /* }}} */
PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path TSRMLS_DC)
{
php_stream *stream = NULL;
-
+
if (opened_path)
*opened_path = NULL;
-
+
if (!path || !*path)
return NULL;
stream = php_stream_open_url(path, mode, options, opened_path TSRMLS_CC);
goto out;
}
-
+
if ((options & USE_PATH) && PG(include_path) != NULL) {
stream = php_stream_fopen_with_path(path, mode, PG(include_path), opened_path TSRMLS_CC);
goto out;
if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) && (!php_checkuid(path, mode, CHECKUID_CHECK_MODE_PARAM)))
return NULL;
-
+
stream = php_stream_fopen(path, mode, opened_path TSRMLS_CC);
out:
if (stream == NULL && (options & REPORT_ERRORS)) {