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

📄 iolib.c

📁 VxWorks操作系统内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    return (fd);    handleError:    if (_func_pthread_setcanceltype != NULL)	{	_func_pthread_setcanceltype(savtype, NULL);	}    if (error > 2)	{	iosDelete (pDevHdr1, fullFileName);	}    if (error > 1)	{	iosFdFree (fd);	}    if (error > 0)	{	fd = (int)ERROR;	}        return (fd);    }/********************************************************************************* unlink - delete a file (POSIX)** This routine deletes a specified file.  It performs the same function* as remove() and is provided for POSIX compatibility.** RETURNS:* OK if there is no delete routine for the device or the driver returns OK;* ERROR if there is no such device or the driver returns ERROR.** SEE ALSO: remove()*/STATUS unlink    (    char *name          /* name of the file to remove */    )    {    return (remove (name));    }/********************************************************************************* remove - remove a file (ANSI)** This routine deletes a specified file.  It calls the driver for the* particular device on which the file is located to do the work.** RETURNS:* OK if there is no delete routine for the device or the driver returns OK;* ERROR if there is no such device or the driver returns ERROR.** SEE ALSO:* .I "American National Standard for Information Systems -"* .I "Programming Language - C, ANSI X3.159-1989: Input/Output (stdio.h),"*/ STATUS remove    (    const char *name          /* name of the file to remove */    )    {    DEV_HDR *pDevHdr;    char fullFileName [MAX_FILENAME_LENGTH];    STATUS value;    /* don't allow null filename (for user protection) */     if ((name == NULL) || (name[0] == EOS))        {        errnoSet (S_ioLib_NO_FILENAME);        return (ERROR);        }     if (ioFullFileNameGet (name, &pDevHdr, fullFileName) == ERROR)        return (ERROR);     value = iosDelete (pDevHdr, fullFileName);    while (value == FOLLOW_LINK)	{	DEV_HDR * pDevHdr2;	int       linkCount = 1;	char *    pPartFileName;	if (linkCount++ > ioMaxLinkLevels)	    {	    errno = ELOOP;	    return ERROR;	    }	if ((pDevHdr2 = iosDevFind (fullFileName, &pPartFileName)) == NULL)	    {	    return ERROR;	    }	if (fullFileName != pPartFileName)	    {	    /* link file name starts with a vxWorks device name,	     * switch to a different device.	     */	    strncpy (fullFileName, pPartFileName, MAX_FILENAME_LENGTH);	    pDevHdr = pDevHdr2;	    }		value = iosDelete (pDevHdr, fullFileName);	}    return value;    }/********************************************************************************* close - close a file** This routine closes the specified file and frees the file descriptor.* It calls the device driver to do the work.** RETURNS:* The status of the driver close routine, or ERROR if the file descriptor * is invalid.*/STATUS close    (    int fd              /* file descriptor to close */    )    {    int ret, savtype;    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &savtype);        }    ret = iosClose (fd);    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(savtype, NULL);        }    return (ret);    }/********************************************************************************* rename - change the name of a file** This routine changes the name of a file from <oldfile> to <newfile>.** NOTE: Only certain devices support rename().  To confirm that your device* supports it, consult the respective xxDrv or xxFs listings to verify that* ioctl FIORENAME exists.  For example, dosFs and rt11Fs support rename(),* but netDrv and nfsDrv do not.** RETURNS: OK, or ERROR if the file could not be opened or renamed.*/int rename    (    const char *oldname,	/* name of file to rename         */    const char *newname		/* name with which to rename file */    )    {    int fd;    int status;    if ((oldname == NULL) || (newname == NULL) || (newname[0] == EOS))	{	errnoSet (ENOENT);	return (ERROR);	}    /* try to open file */    if ((fd = open ((char *) oldname, O_RDONLY, 0)) < OK)	return (ERROR);    /* rename it */    status = ioctl (fd, FIORENAME, (int) newname);    close (fd);    return (status);    }/********************************************************************************* read - read bytes from a file or device** This routine reads a number of bytes (less than or equal to <maxbytes>)* from a specified file descriptor and places them in <buffer>.  It calls* the device driver to do the work.** RETURNS:* The number of bytes read (between 1 and <maxbytes>, 0 if end of file), or* ERROR if the file descriptor does not exist, the driver does not have* a read routines, or the driver returns ERROR. If the driver does not * have a read routine, errno is set to ENOTSUP.*/int read    (    int fd,             /* file descriptor from which to read   */    char *buffer,       /* pointer to buffer to receive bytes   */    size_t maxbytes     /* max no. of bytes to read into buffer */    )    {    int ret, savtype;    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &savtype);        }    ret = iosRead (fd, buffer, (int) maxbytes);    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(savtype, NULL);        }    return (ret);    }/********************************************************************************* write - write bytes to a file** This routine writes <nbytes> bytes from <buffer> to a specified file* descriptor <fd>.  It calls the device driver to do the work.** RETURNS:* The number of bytes written (if not equal to <nbytes>, an error has* occurred), or ERROR if the file descriptor does not exist, the driver* does not have a write routine, or the driver returns ERROR. If the driver* does not have a write routine, errno is set to ENOTSUP.*/int write    (    int fd,             /* file descriptor on which to write     */    char *buffer,       /* buffer containing bytes to be written */    size_t nbytes       /* number of bytes to write              */    )    {    int ret, savtype;    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &savtype);        }    ret = iosWrite (fd, buffer, (int) nbytes);    if (_func_pthread_setcanceltype != NULL)        {        _func_pthread_setcanceltype(savtype, NULL);        }    return (ret);    }/********************************************************************************* ioctl - perform an I/O control function** This routine performs an I/O control function on a device.  The control* functions used by VxWorks device drivers are defined in the header file* ioLib.h.  Most requests are passed on to the driver for handling.* Since the availability of ioctl() functions is driver-specific, these* functions are discussed separately in tyLib, pipeDrv, nfsDrv, dosFsLib,* rt11FsLib, and rawFsLib.** The following example renames the file or directory to the string "newname":** .CS*     ioctl (fd, FIORENAME, "newname");* .CE** Note that the function FIOGETNAME is handled by the I/O interface level and* is not passed on to the device driver itself.  Thus this function code value* should not be used by customer-written drivers.** RETURNS:* The return value of the driver, or ERROR if the file descriptor does * not exist.** SEE ALSO: tyLib, pipeDrv, nfsDrv, dosFsLib, rt11FsLib, rawFsLib,* .pG "I/O System, Local File Systems"** VARARGS2*/int ioctl    (    int fd,             /* file descriptor    */    int function,       /* function code      */    int arg             /* arbitrary argument */    )    {    return (iosIoctl (fd, function, arg));    }/********************************************************************************* lseek - set a file read/write pointer** This routine sets the file read/write pointer of file <fd> * to <offset>.* The argument <whence>, which affects the file position pointer,* has three values:* * .TS* tab(|);* 8l l.* SEEK_SET  (0) |- set to <offset>* SEEK_CUR  (1) |- set to current position plus <offset>* SEEK_END  (2) |- set to the size of the file plus <offset>* .TE* * This routine calls ioctl() with functions FIOWHERE, FIONREAD, and FIOSEEK.** RETURNS:* The new offset from the beginning of the file, or ERROR.** ARGSUSED*/int lseek    (    int fd,             /* file descriptor            */    long offset,        /* new byte offset to seek to */    int whence          /* relative file position     */    )    {    int where;    long nBytes;    switch (whence)	{	case SEEK_SET:	    return ((ioctl (fd, FIOSEEK, offset) == OK) ? offset : ERROR);	case SEEK_CUR:	    if ((where = ioctl (fd, FIOWHERE, 0 /*XXX*/)) == ERROR)		return (ERROR);	    offset += where;	    return ((ioctl (fd, FIOSEEK, offset) == OK) ? offset : ERROR);	case SEEK_END:	    if ((where = ioctl (fd, FIOWHERE, 0 /*XXX*/)) == ERROR)		return (ERROR);	    if (ioctl (fd, FIONREAD, (int) (&nBytes)) == ERROR)		return (ERROR);	    offset += where + nBytes;	    return ((ioctl (fd, FIOSEEK, offset) == OK) ? offset : ERROR);	default:	    return (ERROR);	}    }/********************************************************************************* readv - read data from a device into scattered buffers** readv takes as a parameter a list of buffers (iov) into which it should* place data read from the specified file descriptor (fd).  iov is a pointer* to an array of length iovcnt of (struct iovec) structures, which describe* the location and length of each individual buffer.  readv will always* completely fill a buffer before moving on to the next buffer in the list.** RETURNS:* Number of bytes read (0 if end of file), or* ERROR if the file descriptor does not exist or the driver returns ERROR.** NOMANUAL*/int readv    (    int fd,             /* file descriptor                   */    struct iovec *iov,  /* buffer list                       */    int iovcnt          /* number of elements in buffer list */    )    {    int i;    FAST char *pBuf;    FAST int bytesToRead;    FAST int totalBytesRead = 0;    FAST int bytesRead;    for (i = 0; i < iovcnt; i++)        {        pBuf = (char *) iov[i].iov_base;        bytesToRead = iov[i].iov_len;        while (bytesToRead > 0)            {            if ((bytesRead = iosRead (fd, pBuf, bytesToRead)) == ERROR)                {                if (totalBytesRead > 0)                    return (totalBytesRead);                else                    return (ERROR);                }	    if (bytesRead == 0)		return (totalBytesRead);            totalBytesRead += bytesRead;            bytesToRead -= bytesRead;            pBuf += bytesRead;            }        }    return (totalBytesRead);    }/********************************************************************************* writev - write data from scattered buffers to a device** writev takes as a parameter a list of buffers (iov) from which it should* write data to the specified file descriptor (fd).  iov is a pointer* to an array of length iovcnt of (struct iovec) structures, which describe* the location and length of each individual buffer.  writev will always* completely empty a buffer before moving on to the next buffer in the list.** RETURNS:* Number of bytes written, or* ERROR if the file descriptor does not exist or the driver returns ERROR.** NOMANUAL*/int writev    (    int fd,    register struct iovec *iov,    int iovcnt    )    {    int i;    register char *pData;    register int bytesToWrite;    register int totalBytesWritten = 0;    register int bytesWritten;    for (i = 0; i < iovcnt; i++)        {        pData = (char *) iov[i].iov_base;        bytesToWrite = iov[i].iov_len;

⌨️ 快捷键说明

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