📄 pathlib.c
字号:
/* pathLib.c - file/directory path library *//* Copyright 1984-1992 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02b,01oct93,jmm removed the last change (02a)02a,20jul93,jmm changed pathArrayReduce() to cope with "../../../..."01z,18jul92,smb Changed errno.h to errnoLib.h.01y,04jul92,jcf scalable/ANSI/cleanup effort.01x,26may92,rrr the tree shuffle01w,20jan92,jmm changed pathBuild to check for null dereference01v,13dec91,gae ANSI fixes.01u,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed VOID to void -changed copyright notice01t,01oct90,dnw fixed pathCat(). made entire library and all routines NOMANUAL.01s,30jul90,dnw changed pathLastName() back to 4.0.2 calling sequence and added pathLastNamePtr() with new calling sequence. added forward declaration of void routines.01r,19jul90,kdl mangen fixes for backslashes.01q,19jul90,dnw deleted unused pathSlashIndex(); made pathSlashRindex() LOCAL01p,18may90,llk small tweaks to pathCat().01o,01may90,llk changed pathBuild() and pathCat() to check that the path names they construct are within MAX_FILENAME_LENGTH chars. They both return STATUS now. small documentation improvements. lint reduction.01n,01apr90,llk added buffer parameter to pathParse to improve mem usage. removed pathArrayFree().01m,14mar90,kdl allow "\" as well as "/" for path separator, for MS-DOS.01l,22feb90,jdi documentation cleanup.01k,12oct89,llk fixed pathArrayReduce. "../.." only went up 1 directory.01j,12jul89,llk lint.01i,06jul89,llk cut down number of malloc's to 1 in pathParse. rewrote many routines. changed parameters to pathBuild(). added pathArrayFree(), pathArrayReduce(). deleted pathAppend(), pathRemoveTail(). got NOMANUAL and LOCAL straightened out. made pathLastName() return char *. delinted.01h,19oct88,llk changed pathCat to insert a slash between a device name and file name if no '/' or ':' is already there.01g,23sep88,gae documentation touchup.01f,07jul88,jcf fixed malloc to match new declaration.01e,30jun88,llk added pathBuild().01d,16jun88,llk moved pathSplit() here.01c,04jun88,llk cleaned a little.01b,30may88,dnw changed to v4 names.01a,25may88,llk written.*//*This library provides functions for manipulating and parsing directoryand file path names for heirarchical file systems.The path names are UNIX style, with directory names separated by a "/".(Optionally, a "\\" may be used; this is primarily useful for MS-DOSfiles.)The directory "." refers to the current directory.The directory ".." refers to the parent directory.Path names are handled in two forms:as a null-terminated string (such as "/abc/dir/file"), oras an array of directory names(such as a \f2char**\fP array with the entries "abc", "dir", "file", and NULL).SEE ALSOioLib(1), iosDevFind(2)NOMANUAL*//* LINTLIBRARY */#include "vxWorks.h"#include "iosLib.h"#include "ioLib.h"#include "errnoLib.h"#include "string.h"#include "memLib.h"#include "pathLib.h"/* forward static functions */static void pathArrayReduce (char ** nameArray);static char *strcatlim (char *s1, char *s2, int limit);static char *pathSlashRindex (char *pString);/********************************************************************************* pathParse - parse a full pathname into an array of directory/file names** Parses a UNIX style directory or file name which has directory names* separated by '/'s. It copies the directory and file names, separated by* EOSs, into the user supplied buffer. The buffer is filled with "." for a* null path name.* The last entry in the returned array will be set to NULL.* It is assumed that the caller has created a buffer large enough to* contain the parsed pathname.** For instance, "/usr/vw/dir/file" gets parsed into** nameArray* |---------|* ---------------------------o |* | |---------|* | -----------------------o |* | | |---------|* | | --------------------o |* | | | |---------|* | | | ----------------o |* | | | | |---------|* v v v v | NULL |* |----------------| |---------|* |usr vw dir file |* |----------------|* nameBuf** RETURNS: parsed directory name returned in `nameBuf' pointed to by `nameArray'** NOMANUAL*/void pathParse ( char *longName, /* full path name */ char **nameArray, /* resulting array of pointers to directory names */ char *nameBuf /* buffer containing resulting dir names pointed to by nameArray pointers */ ) { FAST char *pName = nameBuf; /* ptr to name in nameArray */ FAST char *p0; /* ptr to character in longName */ FAST char *p1; /* ptr to character in longName */ int nameCount = 0; /* # of names encountered so far */ int nChars; /* # of characters in a name */ p0 = p1 = longName; /* * Copy names delimitted by '/'s and '\'s to array of strings. * nameArray entries point to strings */ while (*p0 != EOS) { while ((*p1 != '/') && (*p1 != '\\') && (*p1 != EOS)) p1++; if (p0 == p1) /* p1 hasn't moved forward, probably hit another '/' */ { p0++; p1++; continue; } nChars = p1 - p0; /* how many characters in dir name? */ bcopy (p0, pName, nChars); /* copy dir name to buffer */ pName [nChars] = EOS; /* terminate with EOS */ nameArray [nameCount] = pName; /* index into buffer with nameArray */ pName += nChars + 1; /* move ptr to next open spot in buf */ if ((*p1 == '/') || (*p1 == '\\')) p1++; p0 = p1; nameCount++; } /* while */ /* return "." (current directory) if no directories were specified */ if (nameCount == 0) { (void) strcpy (nameBuf, "."); *nameArray = nameBuf; nameCount++; } nameArray [nameCount] = NULL; }/********************************************************************************* pathCondense - condense a file or directory path** This routine condenses a path name by removing any occurrences of:** .RS* /../* /./* //* .RE** NOMANUAL*/void pathCondense ( FAST char *pathName /* directory or file path */ ) { char *nameArray [MAX_DIRNAMES]; char nameBuf [MAX_FILENAME_LENGTH]; char newPathName [MAX_FILENAME_LENGTH]; char *pTail; /* parse the stuff after the device name */ (void) iosDevFind (pathName, &pTail); /* parse path name into individual dir names -- has the added * benefit of eliminating "//"s */ pathParse (pTail, nameArray, nameBuf); /* reduce occurances of ".." and "." */ pathArrayReduce (nameArray); /* * Rebuild path name. Start with an empty path name. * Preserve initial '/' if necessary */ if ((*pTail == '/') || (*pTail == '\\')) { newPathName [0] = *pTail; newPathName [1] = EOS; } else newPathName [0] = EOS; /* * pathBuild() will return ERROR if newPathName gets too big. * We're assuming it won't here, since it has either stayed the * same size or been reduced. */ (void) pathBuild (nameArray, (char **) NULL, newPathName); /* newPathName should be the same size or smaller than pathName */ (void) strcpy (pTail, newPathName); }/********************************************************************************* pathArrayReduce - reduce the number of entries in a nameArray by* eliminating occurances of "." and "..".** pathArrayReduce eliminates occurances of "." and ".." in a nameArray.* Each instance of "." is set to EOS. Each instance of ".." and the previous* directory name is set to EOS.*/LOCAL void pathArrayReduce ( char **nameArray /* array of directory names, last entry is NULL */ ) { FAST char **pArray = nameArray; FAST char **ppPrevString; FAST char *pString; while ((pString = *pArray) != NULL) { if (strcmp (pString, ".") == 0) { /* "." means same directory, ignore */ *pString = EOS; } else if (strcmp (pString, "..") == 0) { /* up one directory and down one, * ignore this directory and previous non-nullified directory */ *pString = EOS; /* clear out this directory name */ ppPrevString = pArray - 1; while (ppPrevString >= nameArray) { /* find last non-nullified string */ if (**ppPrevString == EOS) ppPrevString--; else { **ppPrevString = EOS; break; } } } pArray++; } /* while */ }/********************************************************************************* pathBuild - generate path name by concatenating a list of directory names** This routine uses an array of names to build a path name. It appends the* names in nameArray to the given destination name destString. It* successively uses the names in nameArray up to but not including* nameArrayEnd, or up to a * NULL entry in the nameArray, whichever* comes first.** To use the entire path name array, pass NULL for nameArrayEnd.* destString must be NULL terminated before calling pathBuild.** It assumes that destString should end up no longer than MAX_FILENAME_LENGTH.** RETURNS:* OK and copies resulting path name to <destString>* ERROR if resulting path name would have more than MAX_FILENAME_LENGTH chars.** NOMANUAL*/STATUS pathBuild ( FAST char **nameArray, /* array of directory names */ char **nameArrayEnd, /* ptr to first name in array which should * NOT be added to path */ FAST char *destString /* destination string */ ) { FAST char **pp; int len = strlen (destString); int newlen = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -