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

📄 util.c

📁 ertfs文件系统里面既有完整ucos程序
💻 C
📖 第 1 页 / 共 3 页
字号:
#else
    char tbuf[EMAXPATH];
    char *t = &tbuf[0];
#endif
    p = path;

    if (!p)  /* Path must exist */
        return (0);
    while (*p) 
    {
        if (*p == char_backslash)
        {
            p++;
            break;
        }
        else
            *t++ = *p++;
    }
    *t = '\0';

#if (VFAT)
    ARGSUSED_PVOID((PFVOID)fileext);
    return (p);
#else
    if (pc_fileparse(filename, fileext, &tbuf[0]))
        return (p);
    else
        return (0);
#endif
}

RTFS_FILE(prspath.c, pc_parsepath)

#ifndef __PCDISK__  /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif

/****************************************************************************
        PC_PARSEPATH -  Parse a path specifier into path,file,ext

 Description
        Take a path specifier in path and break it into three null terminated
        strings topath,filename and file ext.
        The result pointers must contain enough storage to hold the results.
        Filename and fileext are BLANK filled to [8,3] spaces.

        Rules:
        
        SPEC                                        PATH            FILE                EXT
        B:JOE                                       B:              'JOE        '   '   '
        B:\JOE                                  B:\             'JOE        '   '   '
        B:\DIR\JOE                          B:\DIR      'JOE        '   '   '
        B:DIR\JOE                               B:DIR           'JOE        '   '   '
 Returns
        Returns TRUE.


****************************************************************************/


#if (VFAT)
BOOLEAN pc_parsepath(char *topath, char *filename, char *fileext, char *path) /*__fn__*/
{
    int n,i,c;  
    ARGSUSED_PVOID((PFVOID)fileext);

    for(i=0,n=0,c=0; path[n]!=0; n++)
    {
        if (path[n]==char_backslash ||  path[n]==':') 
        {
            i=n+1;
            if (path[n]==char_backslash) c++;
        }
    }

    if (c>1) c=1; 
        else c=0;

    for(n=0;n<i-c;n++,topath++)
        *topath = path[n];

    for(path+=i; *path!=0; filename++,path++)
        *filename = *path;

    *filename = 0;
    *topath = 0;
    
    return(TRUE);
}
#else

BOOLEAN pc_parsepath(char *topath, char *filename, char *fileext, char *path) /*__fn__*/
{
        char *pfile;        
        char *pto;      
        char *pfr;      
        char *pslash;       
        BOOLEAN colon;
        
        pto = topath;       
        pfr = path;     
        pslash = 0;
                
        if (path[0] && path[1] == ':')
                colon = TRUE;
        else
                colon = FALSE;

        *pto = 0;
        while (*pfr)
        {
                *pto = *pfr++;
                if (*pto == char_backslash)
                    pslash = pto;
                pto++; 
        }
        *pto = '\0';
        
        if (pslash)
                pfile = pslash+1;
        else
        {
                if (colon)
                        pfile = topath + 2;
                else
                        pfile = topath;
        }
        if (!pc_fileparse(filename, fileext, pfile))
                return (FALSE);
        if (!pslash)
        {
                if (colon)
                    *(topath+2) = '\0';
                else
                        *topath = '\0';
        }
        else
        {
                if ( (colon && (pslash == topath + 2)) ||
                        (!colon && (pslash == topath)) )
                        *(pslash+1) = '\0';
                else
                        *pslash = '\0';
        }
        return(TRUE);
}
#endif

RTFS_FILE(patcmp.c, pc_patcmp)

#ifndef __PCDISK__  /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif


/******************************************************************************
        PC_PATCMP  - Compare a pattern with a string

 Description
        Compare size bytes of p against pattern. Applying the following rules.
                If size == 8. 
                        (To handle the way dos handles deleted files)
                        if p[0] = DELETED, never match
                        if pattern[0] == DELETED, match with 0x5 

                '?' in pattern always matches the current char in p.
                '*' in pattern always matches the rest of p.
 Returns
        Returns TRUE if they match

****************************************************************************/

#if (VFAT)
BOOLEAN pc_patcmp(byte *pat, byte *name, BOOLEAN dowildcard)    /*__fn__*/
{
    int p,n,i;
    BOOLEAN res = FALSE;

    if (pat[0] == '*' && pat[1] == '.' && pat[2] == '*' && pat[3] == 0)
        pat[1] = 0;

    if (*name == (byte)PCDELETE)
        return (FALSE);

    for(p=0,n=0;pat[p]!=0;p++,n++)
    {
        if(pat[p]=='*' && dowildcard)
        {
            for(i=n; name[i]!=0; i++)
                res = (BOOLEAN)((int)res | (int)pc_patcmp(&(pat[p+1]),&(name[i]),dowildcard));            
            res = (BOOLEAN)((int)res | (int)pc_patcmp(&(pat[p+1]),&(name[i]),dowildcard));
            return(res);
        }

        if(name[n]==0)
        {
            /* Match * with end of string */
            if(pat[p]=='*' && pat[p+1] == 0 && dowildcard)
                return(TRUE);
            else
                return(FALSE);
        }

        if((pat[p]!='?' || !dowildcard) && pc_byte2upper(pat[p])!=pc_byte2upper(name[n]))
                return(FALSE);
                
    }
    if(name[n]==0)
        return(TRUE);
    else
        return(FALSE);
}
#else
BOOLEAN pc_patcmp(byte *p, byte *pattern, int size, BOOLEAN dowildcard)  /* __fn__*/
{
    /* Kludge. never match a deleted file */
    if (size == 8)
    {
        if (*p == PCDELETE)
            return (FALSE);
        else if (*pattern == PCDELETE)  /* But E5 in the Pattern matches 0x5 */
        {
            if ( (*p == 0x5)  || ( dowildcard && (*p == '?') ))
            {
                size -= 1;
                p++;
                pattern++;
            }
            else
                return (FALSE);
        }
    }

    while (size--)
    {
    if(dowildcard)
    {
            if (*pattern == '*')    /* '*' matches the rest of the name */
                return (TRUE);
            if (*pattern != *p)
                if (pc_byte2upper(*pattern) != *p)
                  return (FALSE);
    }
    else
    {
            if (*pattern != *p)
                if (pc_byte2upper(*pattern) != *p)
                      return (FALSE);
    }
        pattern++;
        p++;
    }
    return (TRUE);
}
#endif

RTFS_FILE(str2uppr.c, pc_str2upper)

#ifndef __PCDISK__  /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif
 
/******************************************************************************
        PC_STR2UPPER  - Copy a string and make sure the dest is in Upper case

 Description
        Copy a null termed string. Change all lower case chars to upper case

 Returns
        Nothing

****************************************************************************/

char pc_byte2upper(char c)  /* __fn__*/
{
        if  ((c >= 'a') && (c <= 'z'))
                c = (char) ('A' + c - 'a');
        return(c);
}

void pc_str2upper(char *to, char *from)  /* __fn__*/
{
        char c;
        while(*from)
        {
                c = *from++;
                if  ((c >= 'a') && (c <= 'z'))
                        c = (char) ('A' + c - 'a');
                *to++ = c;          
        }
        *to = '\0';
}


void pc_strn2upper(char *to, char *from, int n)  /* __fn__*/
{
    int i;
    char c;
    for (i = 0; i < n; i++)
    {
            c = *from++;
            if  ((c >= 'a') && (c <= 'z'))
                    c = (char) ('A' + c - 'a');
            *to++ = c;          
    }
}

#if (!VFAT)

static BOOLEAN valid_char(char c)
{
    /* Test for valid chars. Note: we accept lower case because that 
       is patched in pc_ino2dos() at a lower level */
    if ( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') )
        return(TRUE);
    if (c == '_' || c == '^' || c == '$' || c == '~')
        return(TRUE); 
    if (c == '!' || c == '#' || c == '%' || c == '&')
        return(TRUE); 
    if (c == '-' || c == '{' || c == '}' || c == '@')
        return(TRUE); 
    if (c == '\''  || c == '`')
        return(TRUE); 
    return(FALSE);
}

#endif
BOOLEAN validate_filename(PFCHAR name, int len) /*__fn__*/
{
    int i;
    for (i = 0; i < len; i++)
    {
        /* If we hit a space make sure the rest of the string is spaces */
        if (name[i] == ' ')
        {
#if (!VFAT) /* Win95 LFN's can contain spaces */
            for (; i < len; i++)
            {
                if (name[i] != ' ')
                    return(FALSE);
            }
#endif
#if (VFAT)
            /* LFN. Can't start with space */
            if (name[0] == ' ')
                return(FALSE);
#else
            /* If it is a filename it can't be all spaces */
            if (name[0] == ' ' && len == 8)
                return(FALSE);
#endif
            break;
        }
        else
        {
#if (VFAT)
            if (_illegal_lfn_char(name[i]))
                return(FALSE);
#else
            /* Validate individual characters */
            if (!valid_char(name[i]))
                return(FALSE);
#endif
        }
    }
    return(TRUE);
} 

#if (VFAT)

BOOLEAN name_is_reserved(char *filename)    /* __fn__*/
{
    int n,i,s;
    BOOLEAN b;
    
    for(n=0; _reserved_names[n]!=0; n++)
    {
        b = TRUE;
        for(i=0; _reserved_names[n][i]!=0 && filename[i]!=0; i++)
        {
            if( (_reserved_names[n][i] != filename[i]) &&
                 (_reserved_names[n][i]+('a'-'A') != filename[i]) )
                b = FALSE;
        }
        if (b)
        {
            if(_reserved_names[n][i]==0)
            {
                for(s=i; filename[s]==' '; s++);
                if(filename[s]==0) return(TRUE);
            }
        }
    }   
    return(FALSE);
}

RTFS_FILE(validsfn.c, pc_valid_sfn)

#ifndef __PCDISK__  /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif

/*****************************************************************************
    PC_VALID_SFN - See if filename is a valid short file name

Description
    Determines validity of a short file name based on the following criteria:
        - the file name must be between 0 and 8 characters
        - the file extension must be between 0 and 3 characters
        - the file name must not begin with a period
        - it must not be a reserved DOS file name
        - it must not contain any characters that are illegal within sfn's  
Returns
    TRUE if filename is a valid sfn, FALSE otherwise
*****************************************************************************/

BOOLEAN pc_valid_sfn(char *filename)    /* __fn__ */
{
    int len,period_count,ext_start;
    BOOLEAN badchar;
    
    if(name_is_reserved(filename)) return(FALSE);

    for(len=0,badchar=FALSE,period_count=0,ext_start=0; filename[len]!=0; len++)
    {
        if(_illegal_alias_char(filename[len])) badchar = TRUE;
        if(filename[len] == '.') 
        {
            ext_start = len+1;
            period_count++;
        }
    }

    if( (filename[0] == ' ') ||                  /* 1st char is a space */
        (len == 0) ||                            /* no name */
        badchar ||                               /* contains illegal chars */
        (period_count > 1) ||                    /* contains more than one extension */
        ((len-ext_start)>3 && period_count>0) || /* extension is longer than 3 chars */
        (period_count==0 && len > 8) ||          /* name is longer than 8 chars */
        (ext_start > 9) ||                       /* name is longer than 8 chars */
        (ext_start==1) ) return(FALSE);          /* no name; 1st char is a period */
    
    return(TRUE);
}

RTFS_FILE(validlfn.c, pc_valid_lfn)

#ifndef __PCDISK__  /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif

/*****************************************************************************
    PC_VALID_LFN - See if filename is a valid long file name

Description
    Determines validity of a long file name based on the following criteria:
        - the file must be between 0 and 256 characters in length
        - it must not be a reserved DOS file name
        - it must not contain any characters that are illegal within lfn's  
Returns
    TRUE if filename is a valid lfn, FALSE otherwise
*****************************************************************************/

BOOLEAN pc_valid_lfn(char *filename)        /*__fn__*/
{
    int len,n;
    BOOLEAN badchar;

    if(name_is_reserved(filename)) return(FALSE);

    for(n=0; filename[n]==' ' || filename[n]=='.'; n++);
    
    for(len=0,badchar=FALSE; filename[n]!=0; len++,n++)
    {
        if(_illegal_lfn_char(filename[n])) badchar = TRUE;
    }

⌨️ 快捷键说明

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