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

📄 iolib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 3 页
字号:
    int fd;    char fullFileName [MAX_FILENAME_LENGTH];    char *pPartFileName;    int  linkCount = 0;		/* number of symlinks traversed */    /* don't allow null filename (for user protection) but change     * "." into what null filename would be if we allowed it */    if (name[0] == EOS)	{	errnoSet (S_ioLib_NO_FILENAME);	return (ERROR);	}    if (strcmp (".", name) == 0)	++name;		/* point to EOS (turn it into null filename) */    if (ioFullFileNameGet (name, &pDevHdr1, fullFileName) == ERROR)	return (ERROR);    if ((fd = iosFdNew (pDevHdr1, (char *) NULL, 0)) == ERROR)	return (ERROR);    value = iosOpen (pDevHdr1, fullFileName, flags, mode);    if (value == ERROR)	{	iosFdFree (fd);	return (ERROR);	}    while (value == FOLLOW_LINK)	{	/* if a file name contains a link, the driver's open routine changed	 * fullFileName to incorporate the link's name. Try to open the file	 * that the driver's open routine handed back.	 */	if ((pDevHdr2 = iosDevFind (fullFileName, &pPartFileName)) == NULL)	    {	    iosFdFree (fd);	    return (ERROR);	    }	if (fullFileName != pPartFileName)	    {	    /* link file name starts with a vxWorks device name,	     * possibly a different device from the current one.	     */	    (void) strcpy (fullFileName, pPartFileName);	    }	else	    {	    /* link file name does not start with a vxWorks device name.	     * open the file on the current device.	     */	    pDevHdr2 = pDevHdr1;	    }		value = iosOpen (pDevHdr2, fullFileName, flags, mode);	if (value == FOLLOW_LINK)	    if (linkCount++ > ioMaxLinkLevels)	        {		errno = ELOOP;		value = ERROR;		}	if (value == ERROR)	    {	    iosFdFree (fd);	    return (ERROR);	    }	} /* while */    iosFdSet (fd, pDevHdr1, CHAR_FROM_CONST(name), value);    return (fd);    }/********************************************************************************* 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 */    )    {    return (iosClose (fd));    }/********************************************************************************* 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 */    )    {    return (iosRead (fd, buffer, (int) maxbytes));    }/********************************************************************************* 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              */    )    {    return (iosWrite (fd, buffer, (int) nbytes));    }/********************************************************************************* 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;        while (bytesToWrite > 0)            {            if ((bytesWritten = iosWrite (fd, pData, bytesToWrite)) == ERROR)                {                if (totalBytesWritten > 0)                    return (totalBytesWritten);                else                    return (ERROR);                }            totalBytesWritten += bytesWritten;            bytesToWrite -= bytesWritten;            pData += bytesWritten;            }        }    return (totalBytesWritten);    }/********************************************************************************* ioFullFileNameGet - resolve path name to device and full file name** This routine resolves the specified path name into a device and full* filename on that device.  If the specified path name is not a full,* absolute path name, then it is concatenated to the current default* path to derive a full path name.  This full path name is separated* into the device specification and the remaining filename on that device.* A pointer to the device header is returned in <ppDevHdr> and the* filename on that device is returned in <fullFileName>.** RETURNS:* OK, or* ERROR if the full filename has more than MAX_FILENAME_LENGTH characters.** NOMANUAL*/

⌨️ 快捷键说明

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