?? iolib.c
字號:
/* ioLib.c - I/O interface library *//* Copyright 1984-2003 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------04z,31jan03,pmr SPR 82273: fixed linkCount declaration in ioCreateOrOpen(). SPR 80447: fixed remove() to follow symbolic links.04y,13jan03,gls moved check for NULL name in ioCreateOrOpen() (SPR #82909)04x,02Aug00,jgn merge DOT-4 pthreads code + add validation of null pointer for name as well as null string04w,03oct01,dcb Fix SPR 20033 side effect --- handle new return code from iosFdSet. Combine open() and creat() into one function.04v,01mar99,yp SPR 21655 - if iosDevFind() fails open() dosn't free fd04u,21feb99,jdi doc: listed errnos.04t,03sep97,dgp doc: SPR 9023 - correct reference to FIOGETNAME in ioctl()04s,10jul97,dgp doc: fix SPR 6117, rename() not supported on all devices04r,20oct95,jdi doc: addition for rename() (SPR 4167); added bit values for open() and creat() flags (SPR 4276).04q,10feb95,rhp added names of stdio fd macros to ioLib man page04p,26jul94,dvs added doc for read/write if no driver routine (SPR #2019/2020).04o,30sep93,jmm resubmitted change originally in scd 1014804n,08feb93,smb changed int to size_t for read and write.04m,21jan93,jdi documentation cleanup for 5.1.04l,29jul92,smb added isatty for use by stdio library.04k,22jul92,kdl Replaced delete() with remove().04j,18jul92,smb Changed errno.h to errnoLib.h.04i,04jul92,jcf scalable/ANSI/cleanup effort.04h,26may92,rrr the tree shuffle04g,28feb92,wmd added bzero() call in ioFullFileNameGet() to init to 0's fullPathName[] buffer before it is passed to subordinates.04f,05dec91,rrr made lseek take posix arguments (SEEK_)04e,25nov91,llk ansi stuff. Modified remove() and rename(). included stdio.h.04d,25nov91,rrr cleanup of some ansi warings.04c,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed READ, WRITE and UPDATE to O_RDONLY O_WRONLY and ... -changed VOID to void -changed copyright notice04b,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.04a,08feb91,jaa documentation cleanup.03z,15oct90,dnw made ioFullFileNameGet() be NOMANUAL03y,05oct90,dnw made readv, writev, ioDefDevGet, ioDefDirGet be NOMANUAL made ioFullFileNameGet() use pathCat() added chdir(), getcwd(), rename() for POSIX compliance added remove() for ANSI compliance added ioDefPathCat() (NOMANUAL)03x,06jun90,rdc added readv and writev.03w,25may90,dnw changed creat and open to NOT malloc filename before calling iosFdSet. This is now left up to iosFdSet.03v,11may90,yao added missing modification history (03u) for the last checkin.03u,09may90,yao typecasted malloc to (char *).03t,01may90,llk changed call to pathCat(). Now, pathCat() checks if a name is too long, and returns ERROR if it is.03s,14apr90,jcf removed tcb extension dependencies.03r,01apr90,llk changed ioFullFileNameGet() and ioDefPathSet() so that only filenames of MAX_FILENAME_LENGTH characters or less will be accepted.03q,14mar90,jdi documentation cleanup.03p,12dec89,dab documentation.03o,15nov89,dab changed variable name mode to flag in creat(). documentation. rdc added unlink, getwd for unix compatibility.03n,02may89,dab documentation touchup in lseek().03m,23mar89,dab made L_XTND option work in lseek(); fixed L_INCR bug in lseek(); documentation touchup in creat().03l,23nov88,llk fixed bug in handling multiple links. dnw changed ioTaskStd{Set,Get} to take taskId arg.03k,08oct88,gae made global standard fd array local.03j,07sep88,gae documentation.03i,29aug88,gae documentation.03h,15aug88,jcf lint.03g,15jul88,llk changed to allocate an fd name before calling iosFdSet or iosFdNew.03f,30jun88,llk changed open and create so that they handle links.03e,04jun88,llk uses a default path instead of a default device. changed ioDefDev to ioDefPath. added ioDefPathPrompt, ioDefPathShow, ioDefPathSet, ioDefPathGet, ioDefDirGet. rewrote ioDefDevGet. removed ioSetDefDev. added ioFullFileNameGet.03d,30may88,dnw changed to v4 names.03c,28may88,dnw deleted obsolete create().03b,27may88,llk see change 03e above.03a,30mar88,gae made I/O system, i.e. iosLib, responsible for low-level creat,open,read,write,close calls to drivers as fdTable & drvTable are now hidden. added parameters to open() for UNIX compatibility. made L_INCR option work in lseek(). added io{G,S}et{Global,Task}Std().02u,19apr88,llk added a parameter to open. Inspired by NFS and aspirations toward UNIX compatibility.02t,31dec87,jlf made creat and open follow links.02s,05nov87,jlf documentation02r,30sep87,gae removed usage of fdTable by using new iosFdSet(). added FIOGETNAME to ioctl.02q,28aug87,dnw changed creat() and open() to only treat ERROR (-1) as error indication from driver routines instead of < 0 to allow driver values (i.e. structure pointers) to be in memory with high bit set.02p,28apr87,gae close() now returns status; added UNIX compatible creat().02o,24mar87,jlf documentation02n,25feb87,ecs minor documentation. added include of strLib.h.02m,14feb87,dnw changed create(), open(), delete() to return ERROR if null filename is specified. However, filename "." is turned into null filename before being passed to driver routine.02l,20dec86,dnw fixed ioGetDefDevTail() to require complete match of dev name. changed to not get include files from default directories.02k,07oct86,gae added ioGetDefDevTail().02j,04sep86,jlf minor documentation02i,01jul86,jlf minor documentation02h,03apr86,llk fixed syntax error in lseek.02g,03mar86,jlf changed ioctrl calls to ioctl. added lseek....deleted pre 86 history - see RCS*//*DESCRIPTIONThis library contains the interface to the basic I/O system.It includes:.iP "" 4Interfaces to the seven basic driver-provided functions:creat(), remove(), open(), close(), read(), write(), and ioctl()..iPInterfaces to several file system functions, includingrename() and lseek()..iPRoutines to set and get the current working directory..iPRoutines to assign task and global standard file descriptors.FILE DESCRIPTORSAt the basic I/O level, files are referred to by a file descriptor.A file descriptor is a small integer returned by a call to open() or creat(). The other basic I/O calls take a file descriptor as a parameter to specify the intended file.Three file descriptors are reserved and have special meanings: 0 (`STD_IN') - standard input 1 (`STD_OUT') - standard output 2 (`STD_ERR') - standard error outputVxWorks allows two levels of redirection. First, there is a globalassignment of the three standard file descriptors. By default, new tasks usethis global assignment. The global assignment of the three standardfile descriptors is controlled by the routines ioGlobalStdSet() and ioGlobalStdGet().Second, individual tasks may override the global assignment of thesefile descriptors with their own assignments that apply only to that task. The assignment of task-specific standard file descriptors is controlled by the routines ioTaskStdSet() and ioTaskStdGet(). INCLUDE FILES: ioLib.hSEE ALSO: iosLib, ansiStdio,.pG "I/O System"*//* LINTLIBRARY */#include "vxWorks.h"#include "sys/types.h"#include "memLib.h"#include "ioLib.h"#include "iosLib.h"#include "string.h"#include "taskLib.h"#include "net/uio.h"#include "errnoLib.h"#include "stdio.h"#include "fcntl.h"#include "unistd.h"#include "pathLib.h"#include "string.h"#define __PTHREAD_SRC#include "pthread.h"IMPORT char ioDefPath [MAX_FILENAME_LENGTH]; /* current default i/o prefix */#define STD_VALID(fd) (((fd) >= 0) && ((fd) < 3))/* globals */BOOL ioMaxLinkLevels = 20; /* maximum number of symlinks to traverse *//* forward declarations */LOCAL int ioCreateOrOpen (const char *name, int flags, int mode, BOOL create);/* locals */LOCAL int ioStdFd [3]; /* global standard input/output/error *//********************************************************************************* creat - create a file** This routine creates a file called <name> and opens it with a specified* <flag>. This routine determines on which device to create the file; * it then calls the create routine of the device driver to do most of the work.* Therefore, much of what transpires is device/driver-dependent.** The parameter <flag> is set to O_RDONLY (0), O_WRONLY (1), or O_RDWR (2)* for the duration of time the file is open. To create NFS files with a* UNIX chmod-type file mode, call open() with the file mode specified in* the third argument.** INTERNAL* A driver's create routine will return FOLLOW_LINK if any part of the file name* contains a link (in the directory path). In this case, it will also* have changed the name of the file being opened to incorporate the name of* the link. The new file name is then repeatedly resubmitted to the driver's* open routine until all links are resolved.** NOTE* For more information about situations when there are no file descriptors* available, see the manual entry for iosInit().** RETURNS:* A file descriptor number, or ERROR if a filename is not specified, the* device does not exist, no file descriptors are available, or the driver* returns ERROR.** SEE ALSO: open()**/int creat ( const char *name, /* name of the file to create */ int flag /* O_RDONLY, O_WRONLY, or O_RDWR */ ) { return ioCreateOrOpen (name, flag, 0, TRUE); }/********************************************************************************* open - open a file** This routine opens a file for reading, writing, or updating, and returns* a file descriptor for that file. The arguments to open() are the filename * and the type of access:** .TS* tab(|);* 8l l l.* O_RDONLY (0) |(or READ) |- open for reading only.* O_WRONLY (1) |(or WRITE) |- open for writing only.* O_RDWR (2) |(or UPDATE) |- open for reading and writing.* O_CREAT (0x0200)| |- create a file.* .TE** In general, open() can only open pre-existing devices and files. However,* for NFS network devices only, files can also be created with open() by* performing a logical OR operation with O_CREAT and the <flags> argument.* In this case, the file is created with a UNIX chmod-style file mode, as* indicated with <mode>. For example:* .CS* fd = open ("/usr/myFile", O_CREAT | O_RDWR, 0644);* .CE* Only the NFS driver uses the <mode> argument.** INTERNAL* A driver's open routine will return FOLLOW_LINK if any part of the file name* contains a link (directory path or file name). In this case, it will also* have changed the name of the file being opened to incorporate the name of* the link. The new file name is then repeatedly resubmitted to the driver's* open routine until all links are resolved.** NOTE* For more information about situations when there are no file descriptors* available, see the manual entry for iosInit().** RETURNS:* A file descriptor number, or ERROR if a file name is not specified, the* device does not exist, no file descriptors are available, or the driver* returns ERROR.** ERRNO: ELOOP** SEE ALSO: creat()** VARARGS2**/int open ( const char *name, /* name of the file to open */ int flags, /* O_RDONLY, O_WRONLY, O_RDWR, or O_CREAT */ int mode /* mode of file to create (UNIX chmod style) */ ) { return ioCreateOrOpen (name, flags, mode, FALSE); }/********************************************************************************* ioCreateOrOpen - creat() or open() a file** This is the worker function for creat() and for open(). See their* descriptions.** NOMANUAL*/LOCAL int ioCreateOrOpen ( const char *name, /* name of the file to create */ int flags, /* O_RDONLY, O_WRONLY, O_RDWR or O_CREAT (open call) */ int mode, /* mode of ile to create (if open call) */ BOOL create /* set to TRUE if this is a creat() call */ ) { DEV_HDR *pDevHdr1; DEV_HDR *pDevHdr2 = NULL; int value; int fd = (int)ERROR; char fullFileName [MAX_FILENAME_LENGTH]; int error = 0; int linkCount = 1; char *pPartFileName; int savtype; /* don't allow null filename (for user protection) but change * "." into what null filename would be if we allowed it */ if ((name == NULL) || (name[0] == EOS)) { errnoSet (S_ioLib_NO_FILENAME); /* fd is already defined as ERROR */ return (fd); } if (_func_pthread_setcanceltype != NULL) { _func_pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &savtype); } if (strcmp (name, ".") == 0) { /* point to EOS (turn it into null filename) */ ++name; } if (ioFullFileNameGet (name, &pDevHdr1, fullFileName) == ERROR) { error = 1; goto handleError; } if ((fd = iosFdNew (pDevHdr1, (char *)NULL, 0)) == ERROR) { error = 1; goto handleError; } if (create == TRUE) { value = iosCreate (pDevHdr1, fullFileName, flags); } else { value = iosOpen (pDevHdr1, fullFileName, flags, mode); } if (value == ERROR) { error = 2; goto handleError; } /* In case the path does not follow the while loop, get the pDevHdr1 recorded in pDevHdr2 for error handling. */ pDevHdr2 = pDevHdr1; while (value == FOLLOW_LINK) { /* if a file name contains a link, the driver's create routine changed * fullFileName to incorporate the link's name. Try to create the file * that the driver's create routine handed back. */ if (linkCount++ > ioMaxLinkLevels) { errno = ELOOP; error = 2; goto handleError; } if ((pDevHdr2 = iosDevFind (fullFileName, &pPartFileName)) == NULL) { error = 2; goto handleError; } if (fullFileName != pPartFileName) { /* link file name starts with a vxWorks device name, * possibly a different device from the current one. */ strncpy (fullFileName, pPartFileName, MAX_FILENAME_LENGTH); pDevHdr1 = pDevHdr2; } if (create == TRUE) { value = iosCreate (pDevHdr1, fullFileName, flags); } else { value = iosOpen (pDevHdr1, fullFileName, flags, mode); } if (value == ERROR) { error = 2; goto handleError; } } if ((iosFdSet (fd, pDevHdr1, CHAR_FROM_CONST(name), value)) == ERROR) { error = 3; goto handleError; } if (_func_pthread_setcanceltype != NULL) { _func_pthread_setcanceltype(savtype, NULL); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -