/*! Sets the Tcl stderr and stdout channels to be the same as the C stderr and stdout streams. \author jfpatry */ void setup_tcl_std_channels() { /* Only need to do this under Win32 */ #if defined( NATIVE_WIN32_COMPILER ) Tcl_Channel stdout_chnl, stderr_chnl; /* I'm not sure why the _dup is necessary under Windows. See the Tcl_SetStdChannel manpage for more info. */ /* Create new stdout channel */ Tcl_SetStdChannel( NULL, TCL_STDOUT ); stdout_chnl = Tcl_MakeFileChannel( (ClientData) _get_osfhandle( _dup( _fileno(stdout) ) ), TCL_WRITABLE ); check_assertion( stdout_chnl, "Couldn't create new stdout channel" ); /* Create a new stderr channel */ Tcl_SetStdChannel( NULL, TCL_STDERR ); stderr_chnl = Tcl_MakeFileChannel( (ClientData) _get_osfhandle( _dup( _fileno(stderr) ) ), TCL_WRITABLE ); check_assertion( stderr_chnl, "Couldn't create new stderr channel" ); #endif }
ShellWrapper() { int iReturn; iReturn = _pipe(iPipeIn_, 512, _O_TEXT | O_NOINHERIT); assert(iReturn != -1); iReturn = _pipe(iPipeOut_, 512, _O_TEXT | O_NOINHERIT); assert(iReturn != -1); //save old handle int iStdin = _dup(_fileno(stdin)); int iStdout = _dup(_fileno(stdout)); int iStderr = _dup(_fileno(stderr)); iReturn = _dup2(iPipeOut_[0], _fileno(stdin)); assert(iReturn == 0); iReturn = _dup2(iPipeIn_[1], _fileno(stdout)); assert(iReturn == 0); iReturn = _dup2(iPipeIn_[1], _fileno(stderr)); assert(iReturn == 0); char *arg[] = {"cmd.exe", "/Q", "/A", NULL}; iSubProcess_ = spawnvp(P_NOWAIT, arg[0], arg); //restore old handle iReturn = _dup2(iStdin, _fileno(stdin)); iReturn = _dup2(iStdout, _fileno(stdout)); iReturn = _dup2(iStderr, _fileno(stderr)); }
int main(int ac, char **av) { int savo, savi; FILE *fnul; char name[256]; char fullname[256]; char *p; kpse_set_program_name(av[0], "mkocp"); if(ac != 2) { fprintf(stderr,"%s : Usage %s ocpname\n", av[0], av[0]); return 1; } p = strrchr(av[1], '.'); if(p) *p = '\0'; strcpy(name, av[1]); strcpy(fullname, name); strcat(fullname, ".ocp"); /* save stdout and stdin */ savo = _dup(fileno(stdout)); savi = _dup(fileno(stdin)); /* connect stdout to stderr */ _dup2(fileno(stderr), fileno(stdout)); /* connect stdin to nul device */ if(!(fnul = fopen("nul", "r"))) { fprintf(stderr, "Cannot open nul device to read\n"); exit(100); } _dup2(fileno(fnul), fileno(stdin)); /* COMMAND */ _spawnlp(_P_WAIT, "otp2ocp", "otp2ocp", name, NULL); /* END COMMAND */ /* return to original stdout and stdin */ _dup2(savo, fileno(stdout)); close(savo); _dup2(savi, fileno(stdin)); close(savi); /* close nul device */ fclose(fnul); if(_access(fullname, 0) == 0) printf("%s\n", fullname); return 0; }
int uio_init(){ if (_open("/tty", O_RW) < 0){ if (_mknod("/tty", TTY) < 0 || _open("/tty", O_RW)){ return -1; } } if (_dup(0) < 0 || _dup(0) < 0){ return -1; } return 0; }
void vprintf_stderr(const char* aFmt, va_list aArgs) { if (sStderrCallback) { va_list argsCpy; VARARGS_ASSIGN(argsCpy, aArgs); sStderrCallback(aFmt, aArgs); va_end(argsCpy); } if (IsDebuggerPresent()) { char buf[2048]; va_list argsCpy; VARARGS_ASSIGN(argsCpy, aArgs); vsnprintf(buf, sizeof(buf), aFmt, argsCpy); buf[sizeof(buf) - 1] = '\0'; va_end(argsCpy); OutputDebugStringA(buf); } FILE* fp = _fdopen(_dup(2), "a"); if (!fp) { return; } vfprintf(fp, aFmt, aArgs); fclose(fp); }
void __stdcall jitStartup(ICorJitHost* jitHost) { g_jitHost = jitHost; // `jitStartup` may be called multiple times // when pre-jitting. We should not reinitialize // config values each time. if (!JitConfig.isInitialized()) { JitConfig.initialize(jitHost); } #if defined(PLATFORM_UNIX) jitstdout = stdout; #else if (jitstdout == nullptr) { int jitstdoutFd = _dup(_fileno(stdout)); _setmode(jitstdoutFd, _O_TEXT); jitstdout = _fdopen(jitstdoutFd, "w"); assert(jitstdout != nullptr); } #endif #ifdef FEATURE_TRACELOGGING JitTelemetry::NotifyDllProcessAttach(); #endif Compiler::compStartup(); }
int DUP(int fd) { int result = _dup(fd); if(result < 0) { throwErrNoOnSysCallException(_T("dup")); } return result; }
/*! * \brief pixReadStreamGif() * * \param[in] fp file stream * \return pix, or NULL on error */ PIX * pixReadStreamGif(FILE *fp) { l_int32 fd; GifFileType *gif; PROCNAME("pixReadStreamGif"); #if GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 1 && GIFLIB_RELEASE == 2 /* 5.1.2 */ L_ERROR("Can't use giflib-5.1.2; suggest 5.1.1 or earlier\n", procName); return NULL; #endif /* 5.1.2 */ if ((fd = fileno(fp)) < 0) return (PIX *)ERROR_PTR("invalid file descriptor", procName, NULL); #ifdef _WIN32 fd = _dup(fd); #endif /* _WIN32 */ #ifndef _MSC_VER lseek(fd, 0, SEEK_SET); #else _lseek(fd, 0, SEEK_SET); #endif /* _MSC_VER */ if ((gif = DGifOpenFileHandle(fd, NULL)) == NULL) return (PIX *)ERROR_PTR("invalid file or file not found", procName, NULL); return gifToPix(gif); }
int duplicate(int fd, int newfd, Bool useDup2) { if (useDup2) { return _dup2(fd, newfd); } return _dup(fd); }
int EbrIOFile::Dup() { int newHandle = _dup(filefd); EbrIOFile* newFile = new EbrIOFile(); newFile->filefd = newHandle; EbrFile* ret = EbrAllocFile(newFile); return ret->idx; }
/* * Integer. */ explicit CarlaString(const int value) noexcept : fBuffer(_null()), fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%d", value); strBuf[0xff] = '\0'; _dup(strBuf); }
/* * Unsigned integer, possibly in hexadecimal. */ explicit CarlaString(const unsigned int value, const bool hexadecimal = false) noexcept : fBuffer(_null()), fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value); strBuf[0xff] = '\0'; _dup(strBuf); }
/* * Simple character. */ explicit CarlaString(const char c) noexcept : fBuffer(_null()), fBufferLen(0) { char ch[2]; ch[0] = c; ch[1] = '\0'; _dup(ch); }
static int do_dump_do(sc_card_t *card, unsigned int tag) { int r; size_t length; unsigned char buffer[254]; // Private DO are specified up to 254 bytes memset(buffer, '\0', sizeof(buffer)); if (tag < 0x101 || tag > 0x104) { util_error("illegal DO identifier %04X", tag); return SC_ERROR_INVALID_ARGUMENTS; } r = sc_get_data(card, tag, buffer, sizeof(buffer)); if (r < 0) { util_error("failed to get data object DO %04X: %s", tag, sc_strerror(r)); if (SC_ERROR_SECURITY_STATUS_NOT_SATISFIED == r) { util_error("make sure the 'verify' and 'pin' parameters are correct"); } return r; } length = (size_t) r; /* r is guaranteed to be non-negative */ if (opt_raw) { int tmp; FILE *fp; #ifndef _WIN32 tmp = dup(fileno(stdout)); #else tmp = _dup(_fileno(stdout)); #endif if (tmp < 0) return EXIT_FAILURE; fp = freopen(NULL, "wb", stdout); if (fp) { r = (int) fwrite(buffer, sizeof(char), length, fp); } #ifndef _WIN32 dup2(tmp, fileno(stdout)); #else _dup2(tmp, _fileno(stdout)); #endif clearerr(stdout); close(tmp); if (length != (size_t) r) /* fail on write errors */ return EXIT_FAILURE; } else { util_hex_dump_asc(stdout, buffer, length, -1); } return EXIT_SUCCESS; }
extern "C" void __stdcall jitStartup(ICorJitHost* jitHost) { if (g_jitInitialized) { return; } g_jitHost = jitHost; assert(!JitConfig.isInitialized()); JitConfig.initialize(jitHost); #if defined(PLATFORM_UNIX) jitstdout = procstdout(); #else if (jitstdout == nullptr) { int stdoutFd = _fileno(procstdout()); // Check fileno error output(s) -1 may overlap with errno result // but is included for completness. // We want to detect the case where the initial handle is null // or bogus and avoid making further calls. if ((stdoutFd != -1) && (stdoutFd != -2) && (errno != EINVAL)) { int jitstdoutFd = _dup(_fileno(procstdout())); // Check the error status returned by dup. if (jitstdoutFd != -1) { _setmode(jitstdoutFd, _O_TEXT); jitstdout = _fdopen(jitstdoutFd, "w"); assert(jitstdout != nullptr); // Prevent the FILE* from buffering its output in order to avoid calls to // `fflush()` throughout the code. setvbuf(jitstdout, nullptr, _IONBF, 0); } } } // If jitstdout is still null, fallback to whatever procstdout() was // initially set to. if (jitstdout == nullptr) { jitstdout = procstdout(); } #endif // PLATFORM_UNIX #ifdef FEATURE_TRACELOGGING JitTelemetry::NotifyDllProcessAttach(); #endif Compiler::compStartup(); g_jitInitialized = true; }
//! //! Nullify given open file descriptor. //! DevNull::DevNull(int fd) { orig_ = fd; clone_ = _dup(orig_); #pragma warning(push) #pragma warning(disable: 4996) null_ = _wopen(DEV_NULL, _O_RDWR); #pragma warning(pop) _dup2(null_, orig_); }
KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL krb5_storage_from_fd(int fd_in) { krb5_storage *sp; int saved_errno; int fd; #ifdef _MSC_VER /* * This function used to try to pass the input to * _get_osfhandle() to test if the value is a HANDLE * but this doesn't work because doing so throws an * exception that will result in Watson being triggered * to file a Windows Error Report. */ fd = _dup(fd_in); #else fd = dup(fd_in); #endif if (fd < 0) return NULL; errno = ENOMEM; sp = malloc(sizeof(krb5_storage)); if (sp == NULL) { saved_errno = errno; close(fd); errno = saved_errno; return NULL; } errno = ENOMEM; sp->data = malloc(sizeof(fd_storage)); if (sp->data == NULL) { saved_errno = errno; close(fd); free(sp); errno = saved_errno; return NULL; } sp->flags = 0; sp->eof_code = HEIM_ERR_EOF; FD(sp) = fd; sp->fetch = fd_fetch; sp->store = fd_store; sp->seek = fd_seek; sp->trunc = fd_trunc; sp->fsync = fd_sync; sp->free = fd_free; sp->max_alloc = UINT_MAX/8; return sp; }
static FILE* fopenZIPped(const char* filename, const char* mode) { // check mode if (mode[0] == 'r') { // create the pipe int hPipe[2]; if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1) { fprintf(stderr, "could not create pipe\n"); return NULL; } // duplicate stdin/stdout handle so we can restore them later int hStdOut = _dup(_fileno(stdout)); // make the write end of pipe go to stdout if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0) { fprintf(stderr, "could not set pipe output\n"); return NULL; } // redirect read end of pipe to input file if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0) { fprintf(stderr, "could not redirect input file\n"); return NULL; } // close original write end of pipe close(hPipe[WRITE_HANDLE]); // Spawn process HANDLE hProcess = (HANDLE) spawnlp(P_NOWAIT, "unzip", "unzip", "-p", filename, NULL); // redirect stdout back into stdout if (_dup2(hStdOut, _fileno(stdout)) != 0) { fprintf(stderr, "could not reconstruct stdout\n"); return NULL; } // return redirected stdin return stdin; } else { return NULL; } }
OutputLogger::OutputLogger(FILE *file, const std::string &filename) : fd(-1), fileOld(NULL), filenameLog(filename), fileLog(NULL), fdPipeRead(-1), pThread(NULL) { // Get the underlying file descriptor for the FILE stream fd = _fileno(file); // Probably on windows and there is no stdout/stderr in Subsystem Windows if (fd < 0) { fileLog = freopen(filenameLog.c_str(),"w",file); #ifdef WIN32 DWORD stdhandle = 0; if (fileLog == stdout) stdhandle = STD_OUTPUT_HANDLE; else if (fileLog == stderr) stdhandle = STD_ERROR_HANDLE; if (stdhandle) SetStdHandle(stdhandle,(HANDLE)_get_osfhandle(_fileno(fileLog))); #endif return; } // Create the pipe int fdsPipe[2]; if (_pipe(fdsPipe,256,_O_BINARY) != 0) { return; } fdPipeRead = fdsPipe[0]; int fdPipeWrite = fdsPipe[1]; // Make sure that buffering is turned off on the incoming file std::setvbuf(file,NULL,_IONBF,0); // Duplicate the fd and create new filestream for it fileOld = _fdopen(_dup(fd),"w"); // If the file was stderr, make sure the new file doesn't have buffering if (fileOld && file == stderr) std::setvbuf(fileOld,NULL,_IONBF,0); // Create the output log file fileLog = std::fopen(filenameLog.c_str(),"w"); // Duplicate pipe write file descriptor and replace original _dup2(fdPipeWrite,fd); // Close the fd for the write end of the pipe _close(fdPipeWrite); fdPipeWrite = -1; // Create the thread pThread = SDL_CreateThread( (int (SDLCALL*)(void*)) &OutputLogger::sThreadMain,this); }
void WindowsHeadlessHost::LoadNativeAssets() { // Native is kinda talkative, but that's annoying in our case. out = _fdopen(_dup(_fileno(stdout)), "wt"); freopen("NUL", "wt", stdout); VFSRegister("", new DirectoryAssetReader("assets/")); VFSRegister("", new DirectoryAssetReader("")); VFSRegister("", new DirectoryAssetReader("../")); gl_lost_manager_init(); // See SendDebugOutput() for how things get back on track. }
void printf_stderr(const char *fmt, ...) { FILE *fp = _fdopen(_dup(2), "a"); if (!fp) return; va_list args; va_start(args, fmt); vfprintf(fp, fmt, args); va_end(args); fclose(fp); }
int fd_safer (int fd) { if (0 <= fd && fd <= 2) { int f = _dup (fd); int e = errno; _close (fd); errno = e; fd = f; } return fd; }
inline void reliable_fsync(const int fd) { #ifdef _MSC_VER osmium::detail::disable_invalid_parameter_handler diph; #endif #ifdef _WIN32 if (_commit(fd) != 0) { #else if (::fsync(fd) != 0) { #endif throw std::system_error{errno, std::system_category(), "Fsync failed"}; } } inline void reliable_close(const int fd) { if (fd < 0) { return; } #ifdef _MSC_VER osmium::detail::disable_invalid_parameter_handler diph; #endif #ifdef _WIN32 if (_close(fd) != 0) { #else if (::close(fd) != 0) { #endif throw std::system_error{errno, std::system_category(), "Close failed"}; } } inline int reliable_dup(const int fd) { #ifdef _MSC_VER osmium::detail::disable_invalid_parameter_handler diph; #endif #ifdef _WIN32 const int fd2 = _dup(fd); #else const int fd2 = ::dup(fd); #endif if (fd2 < 0) { throw std::system_error{errno, std::system_category(), "Dup failed"}; } return fd2; } } // namespace detail
static int do_dump_do(sc_card_t *card, unsigned int tag) { int r, tmp; FILE *fp; // Private DO are specified up to 254 bytes unsigned char buffer[254]; memset(buffer, '\0', sizeof(buffer)); r = sc_get_data(card, tag, buffer, sizeof(buffer)); if (r < 0) { printf("Failed to get data object: %s\n", sc_strerror(r)); if(SC_ERROR_SECURITY_STATUS_NOT_SATISFIED == r) { printf("Make sure the 'verify' and 'pin' parameters are correct.\n"); } return r; } if(opt_raw) { r = 0; #ifndef _WIN32 tmp = dup(fileno(stdout)); #else tmp = _dup(_fileno(stdout)); #endif if (tmp < 0) return EXIT_FAILURE; fp = freopen(NULL, "wb", stdout); if (fp) { r = (int)fwrite(buffer, sizeof(char), sizeof(buffer), fp); } #ifndef _WIN32 dup2(tmp, fileno(stdout)); #else _dup2(tmp, _fileno(stdout)); #endif clearerr(stdout); close(tmp); if (sizeof(buffer) != r) return EXIT_FAILURE; } else { util_hex_dump_asc(stdout, buffer, sizeof(buffer), -1); } return EXIT_SUCCESS; }
/** * Duplicates the file descriptor for an open file \c fd. This allocates a new * file descriptor and clones the references of the open file \c fd into it. * * @param fd An open file to operate on. * @returns A new file descriptor or \c -1 on error with \c errno set to an * error value. */ int dup(int fd) { posixio_fdlock(); struct iofile *file = posixio_file_fromfd(fd); if (file == NULL) { posixio_fdunlock(); errno = EBADF; return -1; } int ret = _dup(file); posixio_fdunlock(); return ret; }
/*! * \brief pixWriteStreamGif() * * \param[in] fp file stream * \param[in] pix 1, 2, 4, 8, 16 or 32 bpp * \return 0 if OK, 1 on error * * <pre> * Notes: * (1) All output gif have colormaps. If the pix is 32 bpp rgb, * this quantizes the colors and writes out 8 bpp. * If the pix is 16 bpp grayscale, it converts to 8 bpp first. * (2) We can't write to memory using open_memstream() because * the gif functions write through a file descriptor, not a * file stream. * </pre> */ l_int32 pixWriteStreamGif(FILE *fp, PIX *pix) { l_int32 result; l_int32 fd; GifFileType *gif; #if (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) || GIFLIB_MAJOR > 5 int giferr; #endif /* 5.1 and beyond */ PROCNAME("pixWriteStreamGif"); /* See version information at top of this file */ #if GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 1 && GIFLIB_RELEASE == 2 /* 5.1.2 */ return ERROR_INT("Can't use giflib-5.1.2; suggest 5.1.1 or earlier\n", procName, 1); #endif /* 5.1.2 */ if (!fp) return ERROR_INT("stream not open", procName, 1); if (!pix) return ERROR_INT("pix not defined", procName, 1); rewind(fp); if ((fd = fileno(fp)) < 0) return ERROR_INT("invalid file descriptor", procName, 1); #ifdef _WIN32 fd = _dup(fd); #endif /* _WIN32 */ /* Get the gif file handle */ if ((gif = EGifOpenFileHandle(fd, NULL)) == NULL) { return ERROR_INT("failed to create GIF image handle", procName, 1); } result = pixToGif(pix, gif); EGifCloseFile(gif, &giferr); return result; }
// Duplicated from xpcom glue. Ideally this should be shared. static void printf_stderr(const char *fmt, ...) { if (IsDebuggerPresent()) { char buf[2048]; va_list args; va_start(args, fmt); VsprintfLiteral(buf, fmt, args); va_end(args); OutputDebugStringA(buf); } FILE *fp = _fdopen(_dup(2), "a"); if (!fp) return; va_list args; va_start(args, fmt); vfprintf(fp, fmt, args); va_end(args); fclose(fp); }
std::shared_ptr<SeekableByteStream> AsyncFileByteStream::Clone() const { if (_file == nullptr) return nullptr; #if EPUB_OS(WINDOWS) int fd = _dup(_fileno(_file)); #else int fd = dup(fileno(_file)); #endif if (fd == -1) return nullptr; #if EPUB_OS(WINDOWS) FILE* newFile = _fdopen(fd, fmode_from_openmode(_mode)); #else FILE* newFile = fdopen(fd, fmode_from_openmode(_mode)); #endif if (newFile == nullptr) { #if EPUB_OS(WINDOWS) _close(fd); #else close(fd); #endif return nullptr; } auto result = std::make_shared<AsyncFileByteStream>(); if (bool(result)) { result->_file = newFile; result->_mode = _mode; } return result; }
/** * Implements \c fcntl() on an open file by passing the call through to the * \c fcntl handler of the underlying device. * * @param fd An open file to operate on. * @param cmd A file control command. * @param arg Argument for the command. * @returns \c \c -1 on error with \c errno set to an * error value; otherwise it may be the result of the file control * command. */ int _fcntl(int fd, int cmd, int arg) { posixio_fdlock(); struct iofile *file = posixio_file_fromfd(fd); if (file == NULL) { posixio_fdunlock(); errno = EBADF; return -1; } int ret = INT_MAX; // Some commands we handle locally. switch (cmd) { case F_DUPFD: ret = _dup(file); break; default: break; } if (ret == INT_MAX) { if (file->dev->fcntl != NULL) { ret = file->dev->fcntl(file->fh, cmd, arg); } else { errno = ENOENT; ret = -1; } } posixio_fdunlock(); return ret; }
void* _data_copy(void) const { if(_dup) return _dup(_data, _count); assert(!_data && !_count); return nullptr; }