// BCC 5.5 and 5.5.1 have a bug in _wopen where files are created read only // regardless of the mode parameter. This hack works around the problem by // setting the mode with _wchmod. // int wxCRT_OpenW(const wchar_t *pathname, int flags, mode_t mode) { int moreflags = 0; // we only want to fix the mode when the file is actually created, so // when creating first try doing it O_EXCL so we can tell if the file // was already there. if ((flags & O_CREAT) && !(flags & O_EXCL) && (mode & wxS_IWUSR) != 0) moreflags = O_EXCL; int fd = _wopen(pathname, flags | moreflags, mode); // the file was actually created and needs fixing if (fd != -1 && (flags & O_CREAT) != 0 && (mode & wxS_IWUSR) != 0) { close(fd); _wchmod(pathname, mode); fd = _wopen(pathname, flags & ~(O_EXCL | O_CREAT)); } // the open failed, but it may have been because the added O_EXCL stopped // the opening of an existing file, so try again without. else if (fd == -1 && moreflags != 0) { fd = _wopen(pathname, flags & ~O_CREAT); } return fd; }
int sys_open(const char *path, int oflag, ...) { int i, fd; char pathbuf[MAXPDSTRING]; wchar_t ucs2path[MAXPDSTRING]; sys_bashfilename(path, pathbuf); u8_utf8toucs2(ucs2path, MAXPDSTRING, pathbuf, MAXPDSTRING-1); /* For the create mode, Win32 does not have the same possibilities, * so we ignore the argument and just hard-code read/write. */ if (oflag & O_CREAT) fd = _wopen(ucs2path, oflag | O_BINARY, _S_IREAD | _S_IWRITE); else fd = _wopen(ucs2path, oflag | O_BINARY); return fd; }
int vlc_open (const char *filename, int flags, ...) { unsigned int mode = 0; va_list ap; va_start (ap, flags); if (flags & O_CREAT) mode = va_arg (ap, unsigned int); va_end (ap); /* Defaults to binary mode */ if ((flags & O_TEXT) == 0) flags |= O_BINARY; #ifdef UNDER_CE /*_open translates to wchar internally on WinCE*/ return _open (filename, flags, mode); #else /* * open() cannot open files with non-“ANSI” characters on Windows. * We use _wopen() instead. Same thing for mkdir() and stat(). */ wchar_t *wpath = widen_path (filename); if (wpath == NULL) return -1; int fd = _wopen (wpath, flags, mode); free (wpath); return fd; #endif }
inline int openFile(const FChar *s) { #ifdef SP_WIDE_SYSTEM return _wopen(s, O_RDONLY|O_BINARY); #else return ::open(s, O_RDONLY|O_BINARY); #endif }
/** * g_open: * @filename: (type filename): a pathname in the GLib file name encoding * (UTF-8 on Windows) * @flags: as in open() * @mode: as in open() * * A wrapper for the POSIX open() function. The open() function is * used to convert a pathname into a file descriptor. * * On POSIX systems file descriptors are implemented by the operating * system. On Windows, it's the C library that implements open() and * file descriptors. The actual Win32 API for opening files is quite * different, see MSDN documentation for CreateFile(). The Win32 API * uses file handles, which are more randomish integers, not small * integers like file descriptors. * * Because file descriptors are specific to the C library on Windows, * the file descriptor returned by this function makes sense only to * functions in the same C library. Thus if the GLib-using code uses a * different C library than GLib does, the file descriptor returned by * this function cannot be passed to C library functions like write() * or read(). * * See your C library manual for more details about open(). * * Returns: a new file descriptor, or -1 if an error occurred. * The return value can be used exactly like the return value * from open(). * * Since: 2.6 */ int g_open (const gchar *filename, int flags, int mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wopen (wfilename, flags, mode); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else int fd; do fd = open (filename, flags, mode); while (G_UNLIKELY (fd == -1 && errno == EINTR)); return fd; #endif }
int win_open(const char *pathname, int flags, ...) { va_list args; mode_t mode; int fd; wchar_t *winpath; va_start(args, flags); mode = va_arg(args, int); va_end(args); winpath = intpath2winpath(pathname); if (!winpath) { errno = EINVAL; return -1; } fd = _wopen(winpath, flags | O_BINARY, mode); free(winpath); if (fd < 0) { return fd; } return add_fdname(fd, pathname); }
/** * Funkcja dokonuje zrzutu zawartości GenBuf_t (buf) do pliku o nazwie (filename). * @param[in] buf - bufor źródłowy. * @param[in,out] filename - nazwa pliku docelowego. * @return status zakończenia operacji. * @retval BMD_OK - zakończenie pomyślne. * @retval BMD_ERR_PARAM1 - niepoprawny adres węzła GenBuf_t (buf) lub * niepoprawna zawartość węzła GenBuf_t (buf) lub * niepoprawny rozmiar zawartości węzła GenBuf_t (buf). * @retval BMD_ERR_PARAM2 - niepoprawna nazwa pliku docelowego (filename). * @retval BMD_ERR_DISK - błąd deskryptora pliku (filename) lub * błąd zapisu do pliku (filename). * @retval BMD_ERR_PARAM3 - niepoprawny adres struktury GenBuf_t. * @retval BMD_ERR_PARAM3 - struktura ma już przydzielona pamięć. */ long bmd_save_buf_wide(GenBuf_t *buf,wchar_t *filename) { long fd; long nwrite; if(buf==NULL) return BMD_ERR_PARAM1; if(buf->buf==NULL) return BMD_ERR_PARAM1; if(buf->size<0) return BMD_ERR_PARAM1; if(filename==NULL) return BMD_ERR_PARAM2; fd=_wopen(filename,O_CREAT|O_TRUNC|O_BINARY|O_WRONLY,0666); if(fd<0) return BMD_ERR_DISK; nwrite=write(fd,buf->buf,buf->size); if(nwrite!=buf->size) return BMD_ERR_DISK; close(fd); return BMD_OK; }
int compat_open(const char *filename, int flags) { int ret; #if defined (WANT_WIN32_UNICODE) const wchar_t *frag = NULL; ret = win32_utf8_wide(filename, &frag, NULL); if ((frag == NULL) || (ret == 0)) goto fallback; /* Fallback to plain open when ucs-2 conversion fails */ ret = _wopen(frag, flags); /*Try _wopen */ if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */ fallback: #endif #if (defined(WIN32) && !defined (__CYGWIN__)) /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */ ret = _open(filename, flags); /* Try plain old _open(), if it fails, do nothing */ #else /* On UNIX, we always add a default permission mask in case flags|O_CREAT. */ ret = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); #endif #if defined (WANT_WIN32_UNICODE) open_ok: free ((void *)frag); /* Freeing a NULL should be OK */ #endif return ret; }
fz_stream * fz_open_file(fz_context *ctx, const char *name) { #ifdef _WIN32 char *s = (char*)name; wchar_t *wname, *d; int c, fd; /* SumatraPDF: prefer ANSI to UTF-8 for consistency with remaining API */ fd = open(name, O_BINARY | O_RDONLY, 0); if (fd == -1) { d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t)); while (*s) { s += fz_chartorune(&c, s); *d++ = c; } *d = 0; fd = _wopen(wname, O_BINARY | O_RDONLY, 0); fz_free(ctx, wname); } #else int fd = open(name, O_BINARY | O_RDONLY, 0); #endif if (fd == -1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name); return fz_open_fd(ctx, fd); }
/** * g_open: * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows) * @flags: as in open() * @mode: as in open() * * A wrapper for the POSIX open() function. The open() function is * used to convert a pathname into a file descriptor. * * On POSIX systems file descriptors are implemented by the operating * system. On Windows, it's the C library that implements open() and * file descriptors. The actual Win32 API for opening files is quite * different, see MSDN documentation for CreateFile(). The Win32 API * uses file handles, which are more randomish integers, not small * integers like file descriptors. * * Because file descriptors are specific to the C library on Windows, * the file descriptor returned by this function makes sense only to * functions in the same C library. Thus if the GLib-using code uses a * different C library than GLib does, the file descriptor returned by * this function cannot be passed to C library functions like write() * or read(). * * See your C library manual for more details about open(). * * Returns: a new file descriptor, or -1 if an error occurred. The * return value can be used exactly like the return value from open(). * * Since: 2.6 */ int g_open (const gchar *filename, int flags, int mode) { #ifdef G_OS_WIN32 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; if (wfilename == NULL) { errno = EINVAL; return -1; } retval = _wopen (wfilename, flags, mode); save_errno = errno; g_free (wfilename); errno = save_errno; return retval; #else return open (filename, flags, mode); #endif }
void File::open(ssh_wcs name, int flags) { if((h = _wopen(name, opens[flags & 7].flag | (flags & (~7) | _O_BINARY), opens[flags & 7].mode)) == -1) SSH_THROW(L"Не удалось открыть файл (%s)", name); path = name; // memcpy(path, name, wcslen(name) * 2 + 2); }
int S_windows_open(const char *pathname, int flags, int mode) { wchar_t wpathname[PATH_MAX]; if (MultiByteToWideChar(CP_UTF8,0,pathname,-1,wpathname,PATH_MAX) == 0) return _open(pathname,flags, mode); else return _wopen(wpathname,flags,mode); }
int vlc_open (const char *filename, int flags, ...) { int mode = 0; va_list ap; flags |= O_NOINHERIT; /* O_CLOEXEC */ /* Defaults to binary mode */ if ((flags & O_TEXT) == 0) flags |= O_BINARY; va_start (ap, flags); if (flags & O_CREAT) mode = va_arg (ap, int); va_end (ap); /* * open() cannot open files with non-“ANSI” characters on Windows. * We use _wopen() instead. Same thing for mkdir() and stat(). */ wchar_t *wpath = widen_path (filename); if (wpath == NULL) return -1; int fd = _wopen (wpath, flags, mode); free (wpath); return fd; }
int compat_open(const char *filename, int mode) { int ret; #if defined (WANT_WIN32_UNICODE) const wchar_t *frag = NULL; ret = win32_utf8_wide(filename, &frag, NULL); if ((frag == NULL) || (ret == 0)) goto fallback; /* Fallback to plain open when ucs-2 conversion fails */ ret = _wopen(frag, mode); /*Try _wopen */ if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */ fallback: #endif #ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */ ret = _open (filename, mode); /* Try plain old _open(), if it fails, do nothing */ #else ret = open (filename, mode); #endif #if defined (WANT_WIN32_UNICODE) open_ok: free ((void *)frag); /* Freeing a NULL should be OK */ #endif return ret; }
fz_stream * fz_open_file_w(fz_context *ctx, const wchar_t *name) { int fd = _wopen(name, O_BINARY | O_RDONLY, 0); if (fd == -1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file %ls", name); return fz_open_fd(ctx, fd); }
fz_stream * fz_open_file_w(fz_context *ctx, const wchar_t *name) { int fd = _wopen(name, O_BINARY | O_RDONLY, 0); if (fd == -1) return NULL; return fz_open_fd(ctx, fd); }
static int file_open(URLContext *h, const char *filename, int flags) { int access; int fd; av_strstart(filename, "file:", &filename); if (flags & URL_RDWR) { access = O_CREAT | O_TRUNC | O_RDWR; } else if (flags & URL_WRONLY) { access = O_CREAT | O_TRUNC | O_WRONLY; } else { access = O_RDONLY; } #ifdef O_BINARY access |= O_BINARY; #endif #ifdef __MINGW32__ // Check for UTF-8 unicode pathname int strl = strlen(filename); wchar_t* wfilename = av_malloc(sizeof(wchar_t) * (1 + strl)); int wpos = 0; int i = 0; for (i = 0; i < strl; i++) { wfilename[wpos] = 0; if ((filename[i] & 0x80) == 0) { // ASCII character wfilename[wpos++] = filename[i]; } else if (i + 1 < strl && ((filename[i] & 0xE0) == 0xC0) && ((filename[i + 1] & 0xC0) == 0x80)) { // two octets for this character wfilename[wpos++] = ((filename[i] & 0x1F) << 6) + (filename[i + 1] & 0x3F); i++; } else if (i + 2 < strl && ((filename[i] & 0xF0) == 0xE0) && ((filename[i + 1] & 0xC0) == 0x80) && ((filename[i + 2] & 0xC0) == 0x80)) { // three octets for this character wfilename[wpos++] = ((filename[i] & 0x0F) << 12) + ((filename[i + 1] & 0x3F) << 6) + (filename[i + 2] & 0x3F); i+=2; } else wfilename[wpos++] = filename[i]; } wfilename[wpos] = 0; fd = _wopen(wfilename, access, 0666); av_free(wfilename); #else fd = open(filename, access, 0666); #endif if (fd == -1) return AVERROR(errno); h->priv_data = (void *) (intptr_t) fd; return 0; }
int p_open(const char *path, int flags) { int fd; wchar_t* buf = gitwin_to_utf16(path); fd = _wopen(buf, flags | _O_BINARY); git__free(buf); return fd; }
/* * @implemented */ FILE* _wfopen(const wchar_t *file, const wchar_t *mode) { FILE *f; int fd, rw, oflags = 0; if (file == 0) return 0; if (mode == 0) return 0; f = __alloc_file(); if (f == NULL) return NULL; rw = (wcschr(mode, L'+') == NULL) ? 0 : 1; if (wcschr(mode, L'a')) oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY); if (wcschr(mode, L'r')) oflags = rw ? O_RDWR : O_RDONLY; if (wcschr(mode, L'w')) oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY); if (wcschr(mode, L't')) oflags |= O_TEXT; else if (wcschr(mode, L'b')) oflags |= O_BINARY; else oflags |= (_fmode & (O_TEXT|O_BINARY)); fd = _wopen(file, oflags, 0); if (fd < 0) return NULL; // msvcrt ensures that writes will end up at the end of file in append mode // we just move the file pointer to the end of file initially if (wcschr(mode, 'a')) lseek(fd, 0, SEEK_END); f->_cnt = 0; f->_file = fd; f->_bufsiz = 0; if (rw) f->_flag = _IOREAD | _IOWRT; else if (wcschr(mode, L'r')) f->_flag = _IOREAD; else f->_flag = _IOWRT; if (wcschr(mode, L't')) f->_flag |= _IOTEXT; else if (wcschr(mode, L'b')) f->_flag |= _IOBINARY; else if (_fmode & O_BINARY) f->_flag |= _IOBINARY; f->_base = f->_ptr = NULL; return f; }
int ns_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */ #ifdef _WIN32 wchar_t wpath[MAX_PATH_SIZE]; to_wchar(path, wpath, ARRAY_SIZE(wpath)); return _wopen(wpath, flag, mode); #else return open(path, flag, mode); /* LCOV_EXCL_LINE */ #endif }
int p_creat(const char *path, mode_t mode) { int fd; wchar_t* buf = gitwin_to_utf16(path); fd = _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode); git__free(buf); return fd; }
int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */ #if defined(_WIN32) && !defined(WINCE) wchar_t wpath[MG_MAX_PATH]; to_wchar(path, wpath, ARRAY_SIZE(wpath)); return _wopen(wpath, flag, mode); #else return open(path, flag, mode); /* LCOV_EXCL_LINE */ #endif }
/* _al_win_open: * Open a file with open() or _wopen() depending on whether Unicode filenames * are supported by this version of Windows and compiler. */ int _al_win_open(const char *filename, int mode, int perm) { if (get_filename_encoding() != U_UNICODE) { return open(filename, mode, perm); } else { return _wopen((wchar_t*)filename, mode, perm); } }
/* _al_win_open: * Open a file with open() or _wopen() depending on whether Unicode filenames * are supported by this version of Windows and compiler. */ int _al_win_open(const char *filename, int mode, int perm) { if (!_al_win_unicode_filenames) { return open(filename, mode, perm); } else { return _wopen((wchar_t*)filename, mode, perm); } }
int32_t C4Network2Res::OpenFileWrite() { CStdLock FileLock(&FileCSec); // FIXME: Use standard OC file access api here #ifdef _WIN32 return _wopen(GetWideChar(szStandalone), _O_BINARY | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); #else return open(szStandalone, _O_BINARY | O_CLOEXEC | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); #endif }
//! //! Construct a null device. //! DevNull::DevNull() { orig_ = INVALID_FD; clone_ = INVALID_FD; #pragma warning(push) #pragma warning(disable: 4996) null_ = _wopen(DEV_NULL, _O_RDWR); #pragma warning(pop) }
long bmd_load_binary_content_wide(const wchar_t *filename, GenBuf_t **buffer) { GenBuf_t *tmpBuffer=NULL; long fd=-1; struct stat file_info; long nread=-1; if( (fd=_wopen(filename, O_RDONLY|_O_BINARY)) == -1) { PRINT_ERROR("LIBBMDUTILSERR Disk error. Error=%i\n",BMD_ERR_DISK); return BMD_ERR_DISK; } if( (fstat(fd,&file_info)==-1) ) { close(fd); PRINT_ERROR("LIBBMDUTILSERR Disk error. Error=%i\n",BMD_ERR_DISK); return BMD_ERR_DISK; } tmpBuffer=(GenBuf_t *)calloc(1, sizeof(GenBuf_t)); if ( tmpBuffer == NULL ) { close(fd); PRINT_ERROR("LIBBMDUTILSERR Memory error. Error=%i\n",NO_MEMORY); return NO_MEMORY; } tmpBuffer->buf=(char *)calloc(file_info.st_size, sizeof(char)); if( tmpBuffer->buf == NULL ) { free(tmpBuffer); close(fd); PRINT_ERROR("LIBBMDUTILSERR Memory error. Error=%i\n",NO_MEMORY); return NO_MEMORY; } tmpBuffer->size=file_info.st_size; nread=read(fd,tmpBuffer->buf, tmpBuffer->size); if ( nread == -1 ) { free(tmpBuffer->buf); free(tmpBuffer); close(fd); PRINT_ERROR("LIBBMDUTILSERR Disk error. Error=%i\n",BMD_ERR_DISK); return BMD_ERR_DISK; } close(fd); *buffer=tmpBuffer; tmpBuffer=NULL; return BMD_OK; }
int file_storage::open_file(const path& filename) { #ifdef _WIN32 int handle = _wopen(filename.wstring().c_str(), (O_RDWR | _O_BINARY | _O_RANDOM), (_S_IREAD | _S_IWRITE)); #else int handle = ::open(filename.string().c_str(), (O_RDWR), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); #endif return handle; }
int __zbx_open(const char *pathname, int flags) { int ret; wchar_t *wpathname; wpathname = zbx_utf8_to_unicode(pathname); ret = _wopen(wpathname, flags); zbx_free(wpathname); return ret; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int argc; LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); char argv0[256]; MSG msg; int fd; int code; fz_context *ctx; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } pdfapp_init(ctx, &gapp); GetModuleFileNameA(NULL, argv0, sizeof argv0); install_app(argv0); winopen(); if (argc == 2) { wcscpy(wbuf, argv[1]); } else { if (!winfilename(wbuf, nelem(wbuf))) exit(0); } fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666); if (fd < 0) winerror(&gapp, "cannot open file"); code = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, filename, sizeof filename, NULL, NULL); if (code == 0) winerror(&gapp, "cannot convert filename to utf-8"); pdfapp_open(&gapp, filename, fd, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } pdfapp_close(&gapp); return 0; }