📄 usrfslib.c
字号:
* brevity and backward compatibility. It produces a list of files* and directories, without details such as file size and date,* and without recursion into subdirectories.** <dirName> is a name of a directory or file, and may contain wildcards.* <doLong> is provided for backward compatibility.** .iP NOTE* This is a target resident function, which manipulates the target I/O* system. It must be preceded with the* '@' letter if executed from the Tornado Shell (windsh), which has a* built-in command of the same name that operates on the Host's I/O* system.** RETURNS: OK or ERROR.** SEE ALSO: dirList()*/STATUS ls ( char *dirName, /* name of dir to list */ BOOL doLong /* switch on details */ ) { return (dirList (STD_OUT, dirName, doLong, FALSE)); }/******************************************************************************** ll - generate a long listing of directory contents** This command causes a long listing of a directory's contents to be* displayed. It is equivalent to:* .CS* -> dirList 1, dirName, TRUE, FALSE* .CE** <dirName> is a name of a directory or file, and may contain wildcards.** .iP "NOTE 1:"* This is a target resident function, which manipulates the target I/O* system. It must be preceded with the* '@' letter if executed from the Tornado Shell (windsh), which has a* built-in command of the same name that operates on the Host's I/O* system.** .iP "NOTE 2:"* When used with netDrv devices (FTP or RSH), ll() does not give* directory information. It is equivalent to an ls() call with no* long-listing option.** RETURNS: OK or ERROR.** SEE ALSO: dirList()*/STATUS ll ( char *dirName /* name of directory to list */ ) { return (dirList (STD_OUT, dirName, TRUE, FALSE)); }/********************************************************************************* lsr - list the contents of a directory and any of its subdirectories** This function is simply a front-end for dirList(), intended for* brevity and backward compatibility. It produces a list of files* and directories, without details such as file size and date,* with recursion into subdirectories.** <dirName> is a name of a directory or file, and may contain wildcards.** RETURNS: OK or ERROR.** SEE ALSO: dirList()*/STATUS lsr ( char *dirName /* name of dir to list */ ) { return (dirList( STD_OUT, dirName, FALSE, TRUE)); }/******************************************************************************** llr - do a long listing of directory and all its subdirectories contents** This command causes a long listing of a directory's contents to be* displayed. It is equivalent to:* .CS* -> dirList 1, dirName, TRUE, TRUE* .CE ** <dirName> is a name of a directory or file, and may contain wildcards.** NOTE: When used with netDrv devices (FTP or RSH), ll() does not give* directory information. It is equivalent to an ls() call with no* long-listing option.** RETURNS: OK or ERROR.** SEE ALSO: dirList()*/STATUS llr ( char *dirName /* name of directory to list */ ) { return (dirList (STD_OUT, dirName, TRUE, TRUE)); }/********************************************************************************* cp - copy file into other file/directory.** This command copies from the input file to the output file.* If destination name is directory, a source file is copied into* this directory, using the last element of the source file name* to be the name of the destination file.** This function is very similar to copy(), except it is somewhat* more similar to the UNIX "cp" program in its handling of the* destination.** <src> may contain a wildcard pattern, in which case all files* matching the pattern will be copied to the directory specified in* <dest>.* This function does not copy directories, and is not recursive.* To copy entire subdirectories recursively, use xcopy().** EXAMPLES* .CS* -> cp( "/sd0/FILE1.DAT","/sd0/dir2/f001.dat")* -> cp( "/sd0/dir1/file88","/sd0/dir2")* -> cp( "/sd0/@.tmp","/sd0/junkdir")* .CE** RETURNS: OK or ERROR if destination is not a directory while <src> is* a wildcard pattern, or if any of the files could not be copied.** SEE ALSO; xcopy()**/STATUS cp ( const char *src, /* source file or wildcard pattern */ const char *dest /* destination file name or directory */ ) { FAST DIR *pDir; /* ptr to directory descriptor */ FAST struct dirent *pDirEnt; /* ptr to dirent */ STATUS status = OK ; char * pattern = NULL ; char * dirName = NULL ; char dir[MAX_FILENAME_LENGTH] ; char fileName[MAX_FILENAME_LENGTH] ; char destName[MAX_FILENAME_LENGTH] ; if (src == NULL) { errno = EINVAL ; return ERROR; } if (dest == NULL) dest = "." ; /* dest does not exist or regular file */ if (!nameIsDir (dest)) { if (dirNameWildcard (src)) { printErr("mv: destination \"%s\" not a directory\n", dest ); errno = ENOTDIR ; return ERROR; } printf("copying file %s -> %s\n", src, dest ); return copy( src, dest ); } strncpy( dir, src, MAX_FILENAME_LENGTH-1 ); pattern = rindex(dir, '/'); if ( pattern != NULL && pattern != dir && dirNameWildcard(pattern)) { /* dir and pattern e.g. dir1/a*.c */ *pattern++ = EOS ; dirName = dir ; } else if (dirNameWildcard (dir)) { /* just pattern e.g. *.c or abc.? */ pattern = dir; dirName = "." ; } else { pattern = NULL ; dirName = dir ; } if (pattern == NULL) { printf("copying file %s -> %s\n", src, dest ); return copy(src, dest ) ; } pDir = opendir (dirName) ; if (pDir == NULL) { perror(dirName); return ERROR; } errno = OK; do { pDirEnt = readdir (pDir); if (pDirEnt != NULL) { if (pattern != NULL && dirListPattern( pattern, pDirEnt->d_name) == FALSE) continue ; if (strcmp(pDirEnt->d_name,"." ) == 0 && strcmp(pDirEnt->d_name,".." ) == 0) continue ; /* Construct path/filename for stat */ usrPathCat( dirName, pDirEnt->d_name, fileName ); usrPathCat( dest, pDirEnt->d_name, destName ); if (nameIsDir( fileName )) { printf("skipping directory %s\n", fileName ); continue; } printf("copying file %s -> %s\n", fileName, destName ); status |= copy( fileName, destName ); } } while (pDirEnt != NULL); /* until end of dir */ status |= closedir (pDir); return status ; }/* cp() *//********************************************************************************* mv - mv file into other directory.** This function is similar to rename() but behaves somewhat more* like the UNIX program "mv", it will overwrite files.** This command moves the <src> file or directory into* a file which name is passed in the <dest> argument, if <dest> is* a regular file or does not exist.* If <dest> name is a directory, the source object is moved into* this directory as with the same name,* if <dest> is NULL, the current directory is assumed as the destination* directory.* <src> may be a single file name or a path containing a wildcard* pattern, in which case all files or directories matching the pattern* will be moved to <dest> which must be a directory in this case.** EXAMPLES* .CS* -> mv( "/sd0/dir1","/sd0/dir2")* -> mv( "/sd0/@.tmp","/sd0/junkdir")* -> mv( "/sd0/FILE1.DAT","/sd0/dir2/f001.dat")* .CE** RETURNS: OK or error if any of the files or directories could not be* moved, or if <src> is a pattern but the destination is not a* directory.*/STATUS mv ( const char *src, /* source file name or wildcard */ const char *dest /* destination name or directory */ ) { FAST DIR *pDir; /* ptr to directory descriptor */ FAST struct dirent *pDirEnt; /* ptr to dirent */ STATUS status = OK ; char * pattern = NULL ; char * dirName = NULL ; char dir[MAX_FILENAME_LENGTH] ; char fileName[MAX_FILENAME_LENGTH] ; char destName[MAX_FILENAME_LENGTH] ; if (src == NULL) { errno = EINVAL ; return ERROR; } if (dest == NULL) dest = "." ; /* dest does not exist or regular file */ if (nameIsDir( dest ) == nameIsDir( src )) { if (dirNameWildcard( src)) { printErr("mv: destination \"%s\" not a directory\n", dest ); errno = ENOTDIR ; return ERROR; } printf("moving file %s -> %s\n", src, dest ); return mvFile ( src, dest ); } strncpy( dir, src, MAX_FILENAME_LENGTH-1 ); pattern = rindex(dir, '/'); if ( pattern != NULL && pattern != dir && dirNameWildcard(pattern)) { /* dir and pattern e.g. dir1/a*.c */ *pattern++ = EOS ; dirName = dir ; } else if (dirNameWildcard( dir)) { /* just pattern e.g. *.c or abc.? */ pattern = dir; dirName = "." ; } else { pattern = NULL ; dirName = dir ; } if (pattern == NULL) { printf("moving file %s -> %s\n", src, dest ); return (mvFile (src, dest )); } pDir = opendir (dirName) ; if (pDir == NULL) { perror(dirName); return ERROR; } errno = OK; do { pDirEnt = readdir (pDir); if (pDirEnt != NULL) { if (pattern != NULL && dirListPattern( pattern, pDirEnt->d_name) == FALSE) continue ; if (strcmp(pDirEnt->d_name,"." ) == 0 && strcmp(pDirEnt->d_name,".." ) == 0) continue ; /* Construct path/filename for stat */ usrPathCat( dirName, pDirEnt->d_name, fileName ); usrPathCat( dest, pDirEnt->d_name, destName ); printf("moving file %s -> %s\n", fileName, destName ); status |= mvFile( fileName, destName ); } } while (pDirEnt != NULL); /* until end of dir */ status |= closedir (pDir); return status ; } /* mv() *//********************************************************************************* mvFile - move a file from one place to another. ** This routine does some of the work of the mv() function.** RETURNS: OK, or ERROR if the file could not be opened or moved.*/LOCAL int mvFile ( const char *oldname, /* name of file to move */ const char *newname /* name with which to move file */ ) { int fd; int status; if ((oldname == NULL) || (newname == NULL) || (newname[0] == EOS)) { errnoSet (ENOENT); return (ERROR); } /* try to open file */ if (ERROR == (fd = open ((char *) oldname, O_RDONLY, 0))) return (ERROR); /* move it */ status = ioctl (fd, FIOMOVE, (int) newname); close (fd); return (status); }/******************************************************************************** xcopy - copy a hierarchy of files with wildcards** <source> is a string containing a name of a directory, or a wildcard* or both which will cause this function to make a recursive copy of all* files residing in that directory and matching the wildcard pattern into* the <dest> directory, preserving the file names and subdirectories.** CAVEAT* This function may call itself in accordance with the depth of the* source directory, and occupies approximately 800 bytes per stack* frame, meaning that to accommodate the maximum depth of subdirectories* which is 20, at least 16 Kbytes of stack space should be available to* avoid stack overflow.** RETURNS: OK or ERROR if any operation has failed.** SEE ALSO: tarLib, checkStack(), cp()*/STATUS xcopy ( const char *source, /* source directory or wildcard name */ const char *dest /* destination directory */ ) { FAST DIR *pDir; /* ptr to directory descriptor */ FAST struct dirent *pDirEnt; /* ptr to dirent */ STATUS status = OK ; char * pattern = NULL ; char * dirName = NULL ; char dir[MAX_FILENAME_LENGTH] ; char fileName[MAX_FILENAME_LENGTH] ; char destName[MAX_FILENAME_LENGTH] ; if (!nameIsDir( dest )) { printErr("xcopy: error: destination \"%s\" is not a directory\n", dest ); errno = ENOTDIR ; return ERROR ; } strncpy( dir, source, MAX_FILENAME_LENGTH-1 ); pattern = rindex(dir, '/'); if ( pattern != NULL && pattern != dir && dirNameWildcard(pattern)) { /* dir and pattern e.g. dir1/a*.c */ *pattern++ = EOS ; dirName = dir ; } else if (dirNameWildcard( dir)) { /* just pattern e.g. *.c or abc.? */ pattern = dir; dirName = "." ; } else { pattern = NULL ; dirName = dir ; } if (!nameIsDir (dirName)) { printf("copying file %s -> %s\n", source, dest ); return copy(source, dest ) ; } pDir = opendir (dirName) ; if (pDir == NULL) { perror(dirName); return ERROR; } errno = OK; do { pDirEnt = readdir (pDir); if (pDirEnt != NULL) { if (pattern != NULL && dirListPattern( pattern, pDirEnt->d_name) == FALSE) continue ; /* Construct path/filename for stat */ usrPathCat( dirName, pDirEnt->d_name, fileName ); usrPathCat( dest, pDirEnt->d_name, destName ); if (!nameIsDir( fileName )) { printf("copying file %s -> %s\n", fileName, destName );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -