⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 w16io.c

📁 Netscape NSPR库源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//*  * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape Portable Runtime (NSPR). *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "primpl.h"#include <sys/types.h>#include <sys/stat.h>#include <share.h>#include <sys/locking.h>/*** Sleep this many milliseconds on each I/O operation** to cause an intentional thread switch. */#define _PR_MD_WIN16_DELAY 1/*** PR_MD_RegisterW16StdioCallbacks() -- Register Win16 stdio callback functions**** This public function call is unique to Win16. ** ... Sigh ... So much for platform independence.** ** To get stdio to work from a command line executable, the stdio stream** calls must be issued from the .EXE file; calling them from the .DLL** sends the output to the bit-bucket. Therefore, the .EXE wanting to** do stdio to the console window (must be built as a "quickwin" application)** must have the wrapper functions defined in this module statically linked** into the .EXE.**** There appears to be nothing you can do to get stdio to work from a** Win16 GUI application. Oh Well!***/PRStdinRead   _pr_md_read_stdin = 0;PRStdoutWrite _pr_md_write_stdout = 0;PRStderrWrite _pr_md_write_stderr = 0;PRStatusPR_MD_RegisterW16StdioCallbacks( PRStdinRead inReadf, PRStdoutWrite outWritef, PRStderrWrite errWritef ){    _pr_md_write_stdout = outWritef;    _pr_md_write_stderr = errWritef;    _pr_md_read_stdin   = inReadf;        return(PR_SUCCESS);} /* end PR_MD_RegisterW16StdioCallbacks() *//*** _PR_MD_OPEN() -- Open a file**** Returns: a fileHandle or -1*****/PRInt32_PR_MD_OPEN(const char *name, PRIntn osflags, int mode){    PRInt32 file;    int     access = O_BINARY;    int     rights = 0;            /*    ** Map NSPR open flags to os open flags    */    if (osflags & PR_RDONLY )        access |= O_RDONLY;    if (osflags & PR_WRONLY )        access |= O_WRONLY;    if (osflags & PR_RDWR )        access |= O_RDWR;    if (osflags & PR_CREATE_FILE )    {        access |= O_CREAT;        rights |= S_IRWXU;    }    if (osflags & PR_TRUNCATE)        access |= O_TRUNC;    if (osflags & PR_APPEND)        access |= O_APPEND;    else        access |= O_RDONLY;            /*    ** Open the file    */            file = (PRInt32) sopen( name, access, SH_DENYNO, rights );    if ( -1 == (PRInt32)file )    {        _PR_MD_MAP_OPEN_ERROR( errno );    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return file;}/*** _PR_MD_READ() - Read something**** Returns: bytes read or -1***/PRInt32_PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 len){    PRInt32     rv;        if ( (PR_GetDescType(fd) == PR_DESC_FILE) &&         ( fd->secret->md.osfd == PR_StandardInput ) &&         ( _pr_md_write_stdout ))    {        rv = (*_pr_md_read_stdin)( buf, len);        }    else    {        rv = read( fd->secret->md.osfd, buf, len );    }        if ( rv == -1)    {        _PR_MD_MAP_READ_ERROR( errno );    }        PR_Sleep( _PR_MD_WIN16_DELAY );        return rv;}/*** _PR_MD_WRITE() - Write something**** Returns:  bytes written or -1**** Note: for file handles 1 and 2 (stdout and stderr)** call the Win16 NSPR stdio callback functions, if they are ** registered.***/PRInt32_PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len){    PRInt32     rv;    if ( (PR_GetDescType(fd) == PR_DESC_FILE))    {        switch ( fd->secret->md.osfd )        {            case  PR_StandardOutput :                if ( _pr_md_write_stdout )                    rv = (*_pr_md_write_stdout)( (void *)buf, len);                else                    rv = len; /* fake success */                break;                            case  PR_StandardError  :                if ( _pr_md_write_stderr )                    rv = (*_pr_md_write_stderr)( (void *)buf, len);                    else                    rv = len; /* fake success */                break;                            default:                rv = write( fd->secret->md.osfd, buf, len );                if ( rv == -1 )                {                    _PR_MD_MAP_WRITE_ERROR( errno );                }                break;        }    }    else    {        rv = write( fd->secret->md.osfd, buf, len );        if ( rv == -1 )        {            _PR_MD_MAP_WRITE_ERROR( errno );        }    }        PR_Sleep( _PR_MD_WIN16_DELAY );        return rv;} /* --- end _PR_MD_WRITE() --- *//*** _PR_MD_LSEEK() - Seek to position in a file**** Note: 'whence' maps directly to PR_...**** Returns:***/PRInt32_PR_MD_LSEEK(PRFileDesc *fd, PRInt32 offset, int whence){    PRInt32     rv;        rv = lseek( fd->secret->md.osfd, offset, whence );    if ( rv == -1 )    {        _PR_MD_MAP_LSEEK_ERROR( errno );            }    PR_Sleep( _PR_MD_WIN16_DELAY );        return( rv );}/*** _PR_MD_LSEEK64() -- Seek to position in file, 64bit offset.***/PRInt64_PR_MD_LSEEK64( PRFileDesc *fd, PRInt64 offset, int whence ){    PRInt64 test;    PRInt32 rv, off;    LL_SHR(test, offset, 32);    if (!LL_IS_ZERO(test))    {        PR_SetError(PR_FILE_TOO_BIG_ERROR, 0);        LL_I2L(test, -1);        return test;    }    LL_L2I(off, offset);    rv = _PR_MD_LSEEK(fd, off, whence);    LL_I2L(test, rv);    return test;} /* end _PR_MD_LSEEK64() *//*** _PR_MD_FSYNC() - Flush file buffers.**** Returns:*****/PRInt32_PR_MD_FSYNC(PRFileDesc *fd){    PRInt32     rv;        rv = (PRInt32) fsync( fd->secret->md.osfd );    if ( rv == -1 )    {        _PR_MD_MAP_FSYNC_ERROR( errno );    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return(rv);}/*** _PR_MD_CLOSE() - Close an open file handle**** Returns:*****/PRInt32_PR_MD_CLOSE_FILE(PRInt32 osfd){    PRInt32     rv;        rv = (PRInt32) close( osfd );    if ( rv == -1 )    {        _PR_MD_MAP_CLOSE_ERROR( errno );    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return(rv);} /* --- end _MD_CloseFile() --- *//* --- DIR IO ------------------------------------------------------------ */#define GetFileFromDIR(d)       (d)->d_entry.cFileName/*** FlipSlashes() - Make forward slashes ('/') into backslashes**** Returns: void*****/void FlipSlashes(char *cp, int len){    while (--len >= 0) {    if (cp[0] == '/') {        cp[0] = PR_DIRECTORY_SEPARATOR;    }    cp++;    }}/*** _PR_MD_OPEN_DIR() - Open a Directory.**** Returns:*****/PRStatus_PR_MD_OPEN_DIR(_MDDir *d, const char *name){    d->dir = opendir( name );        if ( d->dir == NULL )    {        _PR_MD_MAP_OPENDIR_ERROR( errno );        return( PR_FAILURE );    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return( PR_SUCCESS );}/*** _PR_MD_READ_DIR() - read next directory entry*****/char *_PR_MD_READ_DIR(_MDDir *d, PRIntn flags){    struct dirent *de;    int err;	for (;;)     {		de = readdir( d->dir );		if ( de == NULL ) {			_PR_MD_MAP_READDIR_ERROR( errno);			return 0;		}				if ((flags & PR_SKIP_DOT) &&		    (de->d_name[0] == '.') && (de->d_name[1] == 0))			continue;		if ((flags & PR_SKIP_DOT_DOT) &&		    (de->d_name[0] == '.') && (de->d_name[1] == '.') &&		    (de->d_name[2] == 0))			continue;		break;	}    PR_Sleep( _PR_MD_WIN16_DELAY );    	return de->d_name;}/*** _PR_MD_CLOSE_DIR() - Close a directory.*****/PRInt32_PR_MD_CLOSE_DIR(_MDDir *d){    PRInt32     rv;        if ( d->dir )     {        rv = closedir( d->dir );        if (rv != 0)         {            _PR_MD_MAP_CLOSEDIR_ERROR( errno );        }    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return rv;}/*** _PR_MD_DELETE() - Delete a file.**** Returns:*****/PRInt32_PR_MD_DELETE(const char *name){    PRInt32     rv;        rv = (PRInt32) remove( name );    if ( rv != 0 )    {        _PR_MD_MAP_DELETE_ERROR( errno );    }    PR_Sleep( _PR_MD_WIN16_DELAY );        return(rv);}/*** _PR_MD_STAT() - Get file attributes by filename**** Returns:****

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -