[9af1d7a9] | 1 | /* OS dependent filename manipulation routines */ |
---|
| 2 | #include "filename.h" |
---|
| 3 | #include <string.h> |
---|
| 4 | |
---|
| 5 | extern FILE *safe_fopen( const char *fnm, const char *mode ) { |
---|
| 6 | return ( fDirectory(fnm) ? (FILE *)NULL : fopen( fnm, mode ) ); |
---|
| 7 | } |
---|
| 8 | |
---|
| 9 | extern char * FAR PthFromFnm( const char *fnm ) { |
---|
| 10 | char *pth; |
---|
| 11 | const char *lf; |
---|
| 12 | int lenpth = 0; |
---|
| 13 | |
---|
| 14 | lf = strrchr( fnm, FNM_SEP_LEV ); |
---|
| 15 | #ifdef FNM_SEP_LEV2 |
---|
| 16 | if (!lf) lf = strrchr( fnm, FNM_SEP_LEV2 ); |
---|
| 17 | #endif |
---|
| 18 | #ifdef FNM_SEP_DRV |
---|
| 19 | if (!lf) lf = strrchr( fnm, FNM_SEP_DRV ); |
---|
| 20 | #endif |
---|
| 21 | if (lf) lenpth = lf - fnm + 1; |
---|
| 22 | |
---|
| 23 | pth = osmalloc( lenpth + 1 ); |
---|
| 24 | strncpy( pth, fnm, lenpth ); |
---|
| 25 | pth[lenpth] = '\0'; |
---|
| 26 | |
---|
| 27 | return pth; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | extern char * FAR LfFromFnm( const char *fnm ) { |
---|
| 31 | char *lf; |
---|
| 32 | if ((lf = strrchr( fnm, FNM_SEP_LEV )) != NULL |
---|
| 33 | #ifdef FNM_SEP_LEV2 |
---|
| 34 | || (lf = strrchr( fnm, FNM_SEP_LEV2)) != NULL |
---|
| 35 | #endif |
---|
| 36 | #ifdef FNM_SEP_DRV |
---|
| 37 | || (lf = strrchr( fnm, FNM_SEP_DRV )) != NULL |
---|
| 38 | #endif |
---|
| 39 | ) { |
---|
| 40 | return osstrdup(lf + 1); |
---|
| 41 | } |
---|
| 42 | return osstrdup(fnm); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /* Make fnm from pth and lf, inserting an FNM_SEP_LEV if appropriate */ |
---|
| 46 | extern char * FAR UsePth( const char *pth, const char *lf ) { |
---|
| 47 | char *fnm; |
---|
| 48 | int len, len_total; |
---|
| 49 | bool fAddSep = fFalse; |
---|
| 50 | |
---|
| 51 | len = strlen(pth); |
---|
| 52 | len_total = len + strlen(lf) + 1; |
---|
| 53 | |
---|
| 54 | /* if there's a path and it doesn't end in a separator, insert one */ |
---|
| 55 | if (len != 0 && pth[len-1] != FNM_SEP_LEV) { |
---|
| 56 | #ifdef FNM_SEP_LEV2 |
---|
| 57 | if (pth[len-1] != FNM_SEP_LEV2) { |
---|
| 58 | #endif |
---|
| 59 | #ifdef FNM_SEP_DRV |
---|
| 60 | if (pth[len-1] != FNM_SEP_DRV) { |
---|
| 61 | #endif |
---|
| 62 | fAddSep = fTrue; |
---|
| 63 | len_total++; |
---|
| 64 | #ifdef FNM_SEP_DRV |
---|
| 65 | } |
---|
| 66 | #endif |
---|
| 67 | #ifdef FNM_SEP_LEV2 |
---|
| 68 | } |
---|
| 69 | #endif |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | fnm = osmalloc(len_total); |
---|
| 73 | strcpy( fnm, pth ); |
---|
| 74 | if (fAddSep) fnm[len++] = FNM_SEP_LEV; |
---|
| 75 | strcpy( fnm+len, lf ); |
---|
| 76 | return fnm; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* Add ext to fnm, inserting an FNM_SEP_EXT if appropriate */ |
---|
| 80 | extern char * FAR AddExt( const char *fnm, const char *ext ) { |
---|
| 81 | char * fnmNew; |
---|
| 82 | int len, len_total; |
---|
| 83 | #ifdef FNM_SEP_EXT |
---|
| 84 | bool fAddSep = fFalse; |
---|
| 85 | #endif |
---|
| 86 | |
---|
| 87 | len = strlen(fnm); |
---|
| 88 | len_total = len + strlen(ext) + 1; |
---|
| 89 | #ifdef FNM_SEP_EXT |
---|
| 90 | if (ext[0] != FNM_SEP_EXT) { |
---|
| 91 | fAddSep = fTrue; |
---|
| 92 | len_total++; |
---|
| 93 | } |
---|
| 94 | #endif |
---|
| 95 | |
---|
| 96 | fnmNew = osmalloc(len_total); |
---|
| 97 | strcpy( fnmNew, fnm ); |
---|
| 98 | #ifdef FNM_SEP_EXT |
---|
| 99 | if (fAddSep) fnmNew[len++] = FNM_SEP_EXT; |
---|
| 100 | #endif |
---|
| 101 | strcpy( fnmNew+len, ext ); |
---|
| 102 | return fnmNew; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /* fopen file, found using pth and fnm |
---|
| 106 | * pfnmUsed used to return filename used to open file (ignored if NULL) |
---|
| 107 | * or NULL if file didn't open |
---|
| 108 | */ |
---|
| 109 | extern FILE FAR * fopenWithPthAndExt( const char * pth, const char * fnm, const char * szExt, const char * szMode, |
---|
| 110 | char **pfnmUsed ) { |
---|
| 111 | char *fnmFull = NULL; |
---|
| 112 | FILE *fh = NULL; |
---|
| 113 | bool fAbs; |
---|
| 114 | /* if no pth treat fnm as absolute */ |
---|
| 115 | fAbs = (pth == NULL || *pth == '\0' || fAbsoluteFnm(fnm)); |
---|
| 116 | |
---|
| 117 | /* if appropriate, try it without pth */ |
---|
| 118 | if (fAbs || fAmbiguousFnm(fnm)) { |
---|
| 119 | fh = safe_fopen(fnm, szMode); |
---|
| 120 | if (fh) { |
---|
| 121 | if (pfnmUsed) fnmFull = osstrdup(fnm); |
---|
| 122 | } else { |
---|
| 123 | if (szExt && *szExt) { /* szExt!="" => got an extension to try */ |
---|
| 124 | fnmFull = AddExt( fnm, szExt ); |
---|
| 125 | fh = safe_fopen( fnmFull, szMode ); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* if filename could be relative, and file not opened yet */ |
---|
| 131 | if (!fAbs && fh == NULL) { |
---|
| 132 | osfree(fnmFull); |
---|
| 133 | /* try using path given - first of all without the extension */ |
---|
| 134 | fnmFull = UsePth( pth, fnm ); |
---|
| 135 | fh = safe_fopen( fnmFull, szMode ); |
---|
| 136 | if (!fh) { |
---|
| 137 | if (szExt && *szExt) { |
---|
| 138 | /* we've been given an extension so try using it */ |
---|
| 139 | char *fnmTmp; |
---|
| 140 | fnmTmp = fnmFull; |
---|
| 141 | fnmFull = AddExt( fnmFull, szExt ); |
---|
| 142 | osfree(fnmTmp); |
---|
| 143 | fh = safe_fopen( fnmFull, szMode ); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /* either it opened or didn't. If not, fh==NULL from safe_fopen() */ |
---|
| 149 | if (fh == NULL || pfnmUsed == NULL) |
---|
| 150 | osfree(fnmFull); /* free name if it didn't open or name isn't wanted */ |
---|
| 151 | if (pfnmUsed) *pfnmUsed = (fh ? fnmFull : NULL); |
---|
| 152 | return fh; |
---|
| 153 | } |
---|