dbhead_t *dbf_open(char *dp, int o_flags TSRMLS_DC) { int fd; char *cp; dbhead_t *dbh; cp = dp; if ((fd = VCWD_OPEN(cp, o_flags|O_BINARY)) < 0) { return NULL; } if ((dbh = get_dbf_head(fd)) == NULL) { return NULL; } dbh->db_cur_rec = 0; return dbh; }
void phpdbg_list_file(const char *filename, long count, long offset TSRMLS_DC) /* {{{ */ { unsigned char *mem, *pos, *last_pos, *end_pos; struct stat st; #ifndef _WIN32 int fd; #else HANDLE fd, map; #endif int all_content = (count == 0); unsigned int line = 0, displayed = 0; if (VCWD_STAT(filename, &st) == -1) { phpdbg_error("Failed to stat file %s", filename); return; } #ifndef _WIN32 if ((fd = VCWD_OPEN(filename, O_RDONLY)) == -1) { phpdbg_error("Failed to open file %s to list", filename); return; } last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); end_pos = mem + st.st_size; #else fd = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (fd == INVALID_HANDLE_VALUE) { phpdbg_error("Failed to open file!"); return; } map = CreateFileMapping(fd, NULL, PAGE_EXECUTE_READ, 0, 0, 0); if (map == NULL) { phpdbg_error("Failed to map file!"); CloseHandle(fd); return; } last_pos = mem = (char*) MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); if (mem == NULL) { phpdbg_error("Failed to map file in memory"); CloseHandle(map); CloseHandle(fd); return; } end_pos = mem + st.st_size; #endif while (1) { pos = memchr(last_pos, '\n', end_pos - last_pos); if (!pos) { /* No more line breaks */ break; } ++line; if (!offset || offset <= line) { /* Without offset, or offset reached */ phpdbg_writeln("%05u: %.*s", line, (int)(pos - last_pos), last_pos); ++displayed; } last_pos = pos + 1; if (!all_content && displayed == count) { /* Reached max line to display */ break; } } #ifndef _WIN32 munmap(mem, st.st_size); close(fd); #else UnmapViewOfFile(mem); CloseHandle(map); CloseHandle(fd); #endif } /* }}} */
/* {{{ expand_filepath_use_realpath */ PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len, int realpath_mode) { cwd_state new_state; char cwd[MAXPATHLEN]; int copy_len; int path_len; if (!filepath[0]) { return NULL; } path_len = (int)strlen(filepath); if (IS_ABSOLUTE_PATH(filepath, path_len)) { cwd[0] = '\0'; } else { const char *iam = SG(request_info).path_translated; const char *result; if (relative_to) { if (relative_to_len > MAXPATHLEN-1U) { return NULL; } result = relative_to; memcpy(cwd, relative_to, relative_to_len+1U); } else { result = VCWD_GETCWD(cwd, MAXPATHLEN); } if (!result && (iam != filepath)) { int fdtest = -1; fdtest = VCWD_OPEN(filepath, O_RDONLY); if (fdtest != -1) { /* return a relative file path if for any reason * we cannot cannot getcwd() and the requested, * relatively referenced file is accessible */ copy_len = path_len > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : path_len; if (real_path) { memcpy(real_path, filepath, copy_len); real_path[copy_len] = '\0'; } else { real_path = estrndup(filepath, copy_len); } close(fdtest); return real_path; } else { cwd[0] = '\0'; } } else if (!result) { cwd[0] = '\0'; } } new_state.cwd = estrdup(cwd); new_state.cwd_length = (int)strlen(cwd); if (virtual_file_ex(&new_state, filepath, NULL, realpath_mode)) { efree(new_state.cwd); return NULL; } if (real_path) { copy_len = new_state.cwd_length > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : new_state.cwd_length; memcpy(real_path, new_state.cwd, copy_len); real_path[copy_len] = '\0'; } else { real_path = estrndup(new_state.cwd, new_state.cwd_length); } efree(new_state.cwd); return real_path; }
/* {{{ php_oci_lob_import() Import LOB contents from the given file */ int php_oci_lob_import (php_oci_descriptor *descriptor, char *filename) { int fp; ub4 loblen; OCILobLocator *lob = (OCILobLocator *)descriptor->descriptor; php_oci_connection *connection = descriptor->connection; char buf[8192]; ub4 offset = 1; sword errstatus; #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5) /* Safe mode has been removed in PHP 5.4 */ if (php_check_open_basedir(filename)) { #else if ((PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename)) { #endif return 1; } if ((fp = VCWD_OPEN(filename, O_RDONLY|O_BINARY)) == -1) { php_error_docref(NULL, E_WARNING, "Can't open file %s", filename); return 1; } while ((loblen = read(fp, &buf, sizeof(buf))) > 0) { PHP_OCI_CALL_RETURN(errstatus, OCILobWrite, ( connection->svc, connection->err, lob, &loblen, offset, (dvoid *) &buf, loblen, OCI_ONE_PIECE, (dvoid *)0, (OCICallbackLobWrite) 0, (ub2) descriptor->charset_id, (ub1) descriptor->charset_form ) ); if (errstatus != OCI_SUCCESS) { connection->errcode = php_oci_error(connection->err, errstatus); PHP_OCI_HANDLE_ERROR(connection, connection->errcode); close(fp); return 1; } else { connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */ } offset += loblen; } close(fp); return 0; } /* }}} */ /* {{{ php_oci_lob_append() Append data to the end of the LOB */ int php_oci_lob_append (php_oci_descriptor *descriptor_dest, php_oci_descriptor *descriptor_from) { php_oci_connection *connection = descriptor_dest->connection; OCILobLocator *lob_dest = descriptor_dest->descriptor; OCILobLocator *lob_from = descriptor_from->descriptor; ub4 dest_len, from_len; sword errstatus; if (php_oci_lob_get_length(descriptor_dest, &dest_len)) { return 1; } if (php_oci_lob_get_length(descriptor_from, &from_len)) { return 1; } if (from_len <= 0) { return 0; } PHP_OCI_CALL_RETURN(errstatus, OCILobAppend, (connection->svc, connection->err, lob_dest, lob_from)); if (errstatus != OCI_SUCCESS) { connection->errcode = php_oci_error(connection->err, errstatus); PHP_OCI_HANDLE_ERROR(connection, connection->errcode); return 1; } connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */ return 0; }
static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p) { char *trailing_slash; #ifdef PHP_WIN32 char *opened_path = NULL; size_t opened_path_len; wchar_t *cwdw, *pfxw, pathw[MAXPATHLEN]; #else char opened_path[MAXPATHLEN]; #endif char cwd[MAXPATHLEN]; cwd_state new_state; int fd = -1; #ifndef HAVE_MKSTEMP int open_flags = O_CREAT | O_TRUNC | O_RDWR #ifdef PHP_WIN32 | _O_BINARY #endif ; #endif if (!path || !path[0]) { return -1; } #ifdef PHP_WIN32 if (!php_win32_check_trailing_space(pfx, (const int)strlen(pfx))) { SetLastError(ERROR_INVALID_NAME); return -1; } #endif if (!VCWD_GETCWD(cwd, MAXPATHLEN)) { cwd[0] = '\0'; } new_state.cwd = estrdup(cwd); new_state.cwd_length = (int)strlen(cwd); if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) { efree(new_state.cwd); return -1; } if (IS_SLASH(new_state.cwd[new_state.cwd_length - 1])) { trailing_slash = ""; } else { trailing_slash = "/"; } #ifndef PHP_WIN32 if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) { efree(new_state.cwd); return -1; } #endif #ifdef PHP_WIN32 cwdw = php_win32_ioutil_any_to_w(new_state.cwd); pfxw = php_win32_ioutil_any_to_w(pfx); if (!cwdw || !pfxw) { free(cwdw); free(pfxw); efree(new_state.cwd); return -1; } if (GetTempFileNameW(cwdw, pfxw, 0, pathw)) { opened_path = php_win32_ioutil_conv_w_to_any(pathw, PHP_WIN32_CP_IGNORE_LEN, &opened_path_len); if (!opened_path || opened_path_len >= MAXPATHLEN) { free(cwdw); free(pfxw); efree(new_state.cwd); return -1; } assert(strlen(opened_path) == opened_path_len); /* Some versions of windows set the temp file to be read-only, * which means that opening it will fail... */ if (VCWD_CHMOD(opened_path, 0600)) { free(cwdw); free(pfxw); efree(new_state.cwd); free(opened_path); return -1; } fd = VCWD_OPEN_MODE(opened_path, open_flags, 0600); } free(cwdw); free(pfxw); #elif defined(HAVE_MKSTEMP) fd = mkstemp(opened_path); #else if (mktemp(opened_path)) { fd = VCWD_OPEN(opened_path, open_flags); } #endif #ifdef PHP_WIN32 if (fd != -1 && opened_path_p) { *opened_path_p = zend_string_init(opened_path, opened_path_len, 0); } free(opened_path); #else if (fd != -1 && opened_path_p) { *opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0); } #endif efree(new_state.cwd); return fd; }