📄 file.c
字号:
short int ret; int fd; php_stream *stream; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &fp, &size) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, fp); convert_to_long_ex(size); if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't truncate sockets!"); RETURN_FALSE; } if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 1)) { ret = ftruncate(fd, Z_LVAL_PP(size)); RETURN_LONG(ret + 1); } RETURN_FALSE;}/* }}} *//* {{{ proto int fstat(resource fp) Stat() on a filehandle */PHP_NAMED_FUNCTION(php_if_fstat){ zval **fp; zval *stat_dev, *stat_ino, *stat_mode, *stat_nlink, *stat_uid, *stat_gid, *stat_rdev, *stat_size, *stat_atime, *stat_mtime, *stat_ctime, *stat_blksize, *stat_blocks; php_stream *stream; php_stream_statbuf stat_ssb; char *stat_sb_names[13]={"dev", "ino", "mode", "nlink", "uid", "gid", "rdev", "size", "atime", "mtime", "ctime", "blksize", "blocks"}; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &fp) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, fp); if (php_stream_stat(stream, &stat_ssb)) { RETURN_FALSE; } array_init(return_value); MAKE_LONG_ZVAL_INCREF(stat_dev, stat_ssb.sb.st_dev); MAKE_LONG_ZVAL_INCREF(stat_ino, stat_ssb.sb.st_ino); MAKE_LONG_ZVAL_INCREF(stat_mode, stat_ssb.sb.st_mode); MAKE_LONG_ZVAL_INCREF(stat_nlink, stat_ssb.sb.st_nlink); MAKE_LONG_ZVAL_INCREF(stat_uid, stat_ssb.sb.st_uid); MAKE_LONG_ZVAL_INCREF(stat_gid, stat_ssb.sb.st_gid);#ifdef HAVE_ST_RDEV MAKE_LONG_ZVAL_INCREF(stat_rdev, stat_ssb.sb.st_rdev);#else MAKE_LONG_ZVAL_INCREF(stat_rdev, -1);#endif MAKE_LONG_ZVAL_INCREF(stat_size, stat_ssb.sb.st_size);#ifdef NETWARE MAKE_LONG_ZVAL_INCREF(stat_atime, stat_ssb.sb.st_atime.tv_sec); MAKE_LONG_ZVAL_INCREF(stat_mtime, stat_ssb.sb.st_mtime.tv_sec); MAKE_LONG_ZVAL_INCREF(stat_ctime, stat_ssb.sb.st_ctime.tv_sec);#else MAKE_LONG_ZVAL_INCREF(stat_atime, stat_ssb.sb.st_atime); MAKE_LONG_ZVAL_INCREF(stat_mtime, stat_ssb.sb.st_mtime); MAKE_LONG_ZVAL_INCREF(stat_ctime, stat_ssb.sb.st_ctime);#endif#ifdef HAVE_ST_BLKSIZE MAKE_LONG_ZVAL_INCREF(stat_blksize, stat_ssb.sb.st_blksize);#else MAKE_LONG_ZVAL_INCREF(stat_blksize,-1);#endif#ifdef HAVE_ST_BLOCKS MAKE_LONG_ZVAL_INCREF(stat_blocks, stat_ssb.sb.st_blocks);#else MAKE_LONG_ZVAL_INCREF(stat_blocks,-1);#endif /* Store numeric indexes in propper order */ zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_dev, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ino, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mode, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_nlink, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_uid, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_gid, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_rdev, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_size, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_atime, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mtime, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ctime, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blksize, sizeof(zval *), NULL); zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blocks, sizeof(zval *), NULL); /* Store string indexes referencing the same zval*/ zend_hash_update(HASH_OF(return_value), stat_sb_names[0], strlen(stat_sb_names[0])+1, (void *)&stat_dev, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[1], strlen(stat_sb_names[1])+1, (void *)&stat_ino, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[2], strlen(stat_sb_names[2])+1, (void *)&stat_mode, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[3], strlen(stat_sb_names[3])+1, (void *)&stat_nlink, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[4], strlen(stat_sb_names[4])+1, (void *)&stat_uid, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[5], strlen(stat_sb_names[5])+1, (void *)&stat_gid, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[6], strlen(stat_sb_names[6])+1, (void *)&stat_rdev, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[7], strlen(stat_sb_names[7])+1, (void *)&stat_size, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[8], strlen(stat_sb_names[8])+1, (void *)&stat_atime, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[9], strlen(stat_sb_names[9])+1, (void *)&stat_mtime, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[10], strlen(stat_sb_names[10])+1, (void *)&stat_ctime, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[11], strlen(stat_sb_names[11])+1, (void *)&stat_blksize, sizeof(zval *), NULL); zend_hash_update(HASH_OF(return_value), stat_sb_names[12], strlen(stat_sb_names[12])+1, (void *)&stat_blocks, sizeof(zval *), NULL);}/* }}} *//* {{{ proto bool copy(string source_file, string destination_file) Copy a file */PHP_FUNCTION(copy){ zval **source, **target; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &source, &target) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(source); convert_to_string_ex(target); if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(source), NULL, CHECKUID_CHECK_FILE_AND_DIR))) { RETURN_FALSE; } if (php_check_open_basedir(Z_STRVAL_PP(source) TSRMLS_CC)) { RETURN_FALSE; } if (php_copy_file(Z_STRVAL_PP(source), Z_STRVAL_PP(target) TSRMLS_CC)==SUCCESS) { RETURN_TRUE; } else { RETURN_FALSE; }}/* }}} *//* {{{ php_copy_file */PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC){ php_stream *srcstream = NULL, *deststream = NULL; int ret = FAILURE; php_stream_statbuf src_s, dest_s; switch (php_stream_stat_path(src, &src_s)) { case -1: /* non-statable stream */ goto safe_to_copy; break; case 0: break; default: /* failed to stat file, does not exist? */ return ret; } if (php_stream_stat_path(dest, &dest_s) != 0) { goto safe_to_copy; } if (!src_s.sb.st_ino || !dest_s.sb.st_ino) { goto no_stat; } if (src_s.sb.st_ino == dest_s.sb.st_ino && src_s.sb.st_dev == dest_s.sb.st_dev) { return ret; } else { goto safe_to_copy; }no_stat: { char *sp, *dp; int res; if ((sp = expand_filepath(src, NULL TSRMLS_CC)) == NULL) { return ret; } if ((dp = expand_filepath(dest, NULL TSRMLS_CC)) == NULL) { efree(sp); goto safe_to_copy; } res = #ifndef PHP_WIN32 !strcmp(sp, dp);#else !strcasecmp(sp, dp);#endif efree(sp); efree(dp); if (res) { return ret; } }safe_to_copy: srcstream = php_stream_open_wrapper(src, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (!srcstream) return ret; deststream = php_stream_open_wrapper(dest, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (srcstream && deststream) ret = php_stream_copy_to_stream(srcstream, deststream, PHP_STREAM_COPY_ALL) == 0 ? FAILURE : SUCCESS; if (srcstream) php_stream_close(srcstream); if (deststream) php_stream_close(deststream); return ret;}/* }}} *//* {{{ proto string fread(resource fp, int length) Binary-safe file read */PHPAPI PHP_FUNCTION(fread){ zval **arg1, **arg2; int len; php_stream *stream; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } PHP_STREAM_TO_ZVAL(stream, arg1); convert_to_long_ex(arg2); len = Z_LVAL_PP(arg2); if (len <= 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0."); RETURN_FALSE; } Z_STRVAL_P(return_value) = emalloc(len + 1); Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len); /* needed because recv/read/gzread doesnt put a null at the end*/ Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0; if (PG(magic_quotes_runtime)) { Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), &Z_STRLEN_P(return_value), 1 TSRMLS_CC); } Z_TYPE_P(return_value) = IS_STRING;}/* }}} */static char *_php_fgetcsv_find_enclosure(char *start, int len, char enclosure, int end){ char *s=start, *p, *e=start+len; while (e > s && (p = memchr(s, enclosure, (e - s)))) { if (p > s && *(p - 1) == '\\') { /* check escape characters */ int enc_cnt=0; char *pp = p - 1; while (pp >= s && *pp == '\\') { enc_cnt++; pp--; } if ((enc_cnt % 2)) { s = p + 1; continue; } } if (end) { int i = 0; while (e > p && *p == enclosure) { s = ++p; i++; } if (!(i % 2)) { continue; } else { p--; } } return p; } return NULL;}static void _php_fgetcsv_trim_enclosed(char *buf2, int *buf2_len, char enclosure){ if (memchr(buf2, enclosure, *buf2_len)) { int esc = 0, enc_c = 0, pos = 0; while (pos < *buf2_len) { if (*(buf2 + pos) == '\\') { esc = !esc; enc_c = 0; } else if (*(buf2 + pos) == enclosure) { if (esc) { esc = 0; } else if (enc_c) { enc_c = 0; memmove(buf2 + pos, buf2 + pos + 1, *buf2_len - pos - 1); (*buf2_len)--; continue; } else if (!esc) { enc_c = 2; } } else { if (enc_c == 2) { memmove(buf2 + pos - 1, buf2 + pos, *buf2_len - pos); (*buf2_len)--; enc_c--; } esc = 0; } pos++; } if (enc_c && *(buf2 + pos - 1) == enclosure) { (*buf2_len)--; } } buf2[*buf2_len] = '\0';}/* {{{ proto array fgetcsv(resource fp, int length [, string delimiter [, string enclosure]]) Get line from file pointer and parse for CSV fields */PHP_FUNCTION(fgetcsv){ char delimiter = ','; /* allow this to be set as parameter */ char enclosure = '"'; /* allow this to be set as parameter */ /* first section exactly as php_fgetss */ zval **fd, **bytes, **p_delim, **p_enclosure; size_t len, buf_len; char *buf, *p, *s, *e, *re, *buf2=NULL; php_stream *stream; switch(ZEND_NUM_ARGS()) { case 2: if (zend_get_parameters_ex(2, &fd, &bytes) == FAILURE) { WRONG_PARAM_COUNT; } break; case 3: if (zend_get_parameters_ex(3, &fd, &bytes, &p_delim) == FAILURE) { WRONG_PARAM_COUNT; } break; case 4: if (zend_get_parameters_ex(4, &fd, &bytes, &p_delim, &p_enclosure) == FAILURE) { WRONG_PARAM_COUNT; } break; default: WRONG_PARAM_COUNT; /* NOTREACHED */ break; } if (ZEND_NUM_ARGS() >= 3) { convert_to_string_ex(p_delim); /* Make sure that there is at least one character in string */ if (Z_STRLEN_PP(p_delim) < 1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character"); RETURN_FALSE; } /* use first character from string */ delimiter = Z_STRVAL_PP(p_delim)[0]; } if (ZEND_NUM_ARGS() >= 4) { convert_to_string_ex(p_enclosure); if (Z_STRLEN_PP(p_enclosure) < 1) { /* no enclosure */ enclosure = 0; } else { /* use first character from string */ enclosure = Z_STRVAL_PP(p_enclosure)[0]; } } PHP_STREAM_TO_ZVAL(stream, fd); convert_to_long_ex(bytes); len = Z_LVAL_PP(bytes); if (Z_LVAL_PP(bytes) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter may not be negative"); RETURN_FALSE; } buf = emalloc(len + 1); if (php_stream_get_line(stream, buf, len, &buf_len) == NULL) { efree(buf); RETURN_FALSE; } s = buf; re = e = buf + buf_len; /* strip leading spaces */ while (isspace((int)*(unsigned char *)s) && *s != delimiter && s < re) { s++; } /* strip trailing spaces */ while (--e >= s && (*e == '\n' || *e == '\r') && *e != delimiter); e++; array_init(return_value);#define CSV_ADD_ENTRY(os, es, st) { \ if (es - st) { \ add_next_index_stringl(return_value, os, es - st, 1); \ } else { \ add_next_index_string(return_value, "", 1); \ } \} if (!(e - s)) { CSV_ADD_ENTRY(s, e, s); goto done; }csv_start: if (!enclosure || !(p = _php_fgetcsv_find_enclosure(s, (e - s), enclosure, 0))) {no_enclosure: while ((p = memchr(s, delimiter, (e - s)))) { CSV_ADD_ENTRY(s, p, s); s = p + 1; } } else { char *p2=NULL; int buf2_len=0;enclosure: /* handle complete fields before the enclosure */ while (s < p && (p2 = memchr(s, delimiter, (p - s)))) { CSV_ADD_ENTRY(s, p2, s); s = p2 + 1; } p++; /* strip leading spaces */ while (isspace((int)*(unsigned char *)s) && *s != delimiter && s < re) { s++; } if (*s != enclosure) { if ((p = memchr(s, delimiter, (e - s)))) { CSV_ADD_ENTRY(s, p, s); s = p + 1; goto csv_start; } else { goto no_enclosure; } } s++; /* try to find end of enclosure */ while (!(p2 = _php_fgetcsv_find_enclosure(p, (e - p), enclosure, 1))) { buf2 = erealloc(buf2, buf2_len + (re - p) + 1); memcpy(buf2 + buf2_len, p, (re - p)); buf2_len += (re - p); if (php_stream_get_line(stream, buf, len, &buf_len) == NULL) { goto enclosure_done; } s = p = buf; re = e = buf + buf_len; /* strip trailing spaces */ while (--e >= s && (*e == '\n' || *e == '\r') && *e != delimiter); e++; } /* end of enclosure found, now find the delimeter */ if ((p = memchr(p2, delimiter, (e - p2)))) { p2 = s; s = p + 1; /* copy data to buffer */ buf2 = erealloc(buf2, buf2_len + (p - p2) + 1); memcpy(buf2 + buf2_len, p2, (p - p2)); buf2_len += p - p2; _php_fgetcsv_trim_enclosed(buf2, &buf2_len, enclosure); CSV_ADD_ENTRY(buf2, buf2_len, 0); buf2_len = 0; if (!(p = _php_fgetcsv_find_enclosure(s, (e - s), enclosure, 0))) { goto no_enclosure; } else { goto enclosure; } } else { buf2 = erealloc(buf2, buf2_len + (e - s)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -