/* This is sick and incomplete... in the meantime, I have discovered six new illegal file name formats * M$ sucks! */ void fname_atow (const char *src, char *dst, int size) { char *lastslash = dst, *strt = dst, *posn = NULL, *temp = NULL; int i, j; temp = xmalloc( size ); while (size-- > 0) { if (!(*dst = *src++)) break; if (*dst == '~' || *dst == '|' || *dst == '*' || *dst == '?') { if (size > 2) { sprintf (dst, "~%02x", *dst); size -= 2; dst += 2; } } else if (*dst == '/') { if (checkspace (lastslash, ' ', 0xa0) && (dst - lastslash == 3 || (dst - lastslash > 3 && lastslash[3] == '.')) && isillegal (lastslash)) { i = dst - lastslash - 3; dst++; for (j = i + 1; j--; dst--) *dst = dst[-1]; *(dst++) = 0xa0; dst += i; size--; } else if (*lastslash == '.' && (dst - lastslash == 1 || (lastslash[1] == '.' && dst - lastslash == 2)) && size) { *(dst++) = 0xa0; size--; } *dst = '\\'; lastslash = dst + 1; } dst++; } if (checkspace (lastslash, ' ', 0xa0) && (dst - lastslash == 3 || (dst - lastslash > 3 && lastslash[3] == '.')) && isillegal (lastslash) && size > 1) { i = dst - lastslash - 3; dst++; for (j = i + 1; j--; dst--) *dst = dst[-1]; *(dst++) = 0xa0; } else if (!strcmp (lastslash, ".") || !strcmp (lastslash, "..")) strcat (lastslash, "\xa0"); /* Major kludge, because I can't find the problem... */ if( ( posn = strstr( strt, "..\xA0\\" ) ) == strt && temp) { strcpy( temp, "..\\" ); strcat( temp, strt + 4 ); strcpy( strt, temp ); } /* Another major kludge, for the MUI installation... */ if( *strt == ' ' ) /* first char as a space is illegal in Windoze */ { sprintf( temp, "~%02x%s", ' ', strt+1 ); strcpy( strt, temp ); } }
/* Win32 file name restrictions suck... */ void fname_wtoa (unsigned char *ptr) { unsigned char *lastslash = ptr; while (*ptr) { if (*ptr == '~') { *ptr = hextol (ptr[1]) * 16 + hextol (ptr[2]); strcpy (ptr + 1, ptr + 3); } else if (*ptr == '\\') { if (checkspace (lastslash, ' ', 0xa0) && ptr - lastslash > 3 && lastslash[3] == 0xa0 && isillegal (lastslash)) { ptr--; strcpy (lastslash + 3, lastslash + 4); } *ptr = '/'; lastslash = ptr + 1; } ptr++; } if (checkspace (lastslash, ' ', 0xa0) && ptr - lastslash > 3 && lastslash[3] == 0xa0 && isillegal (lastslash)) strcpy (lastslash + 3, lastslash + 4); }
/* ************************************************************************** ** * Return True if the name is a valid DOS name in 8.3 DOS format. * * Input: fname - File name to be checked. * check_case - If True, then the * name will be checked to see if all characters * are the correct case. * * Output: True if the name is a valid DOS name, else FALSE. * * ************************************************************************** ** */ int is_8_3( char *fname) { int len; int l, i; char *p; char *dot_pos; char *slash_pos = strrchr( fname, '/' ); /* If there is a directory path, skip it. */ if (slash_pos) fname = slash_pos + 1; len = strlen(fname); /* Can't be 0 chars or longer than 12 chars */ if ((len == 0) || (len > 12)) return FALSE; /* Mustn't be an MS-DOS Special file such as lpt1 or even lpt1.txt */ if (is_reserved_msdos(fname)) return FALSE; init_chartest(); for (i = 0; i < strlen(fname); i++) { if (isillegal(fname[i])) { Syslog('+', "Illegal character in filename"); return FALSE; } } /* Can't contain invalid dos chars */ p = fname; dot_pos = NULL; while (*p) { if (*p == '.' && !dot_pos) dot_pos = (char *)p; p++; } /* no dot and less than 9 means OK */ if (!dot_pos) return (len <= 8); l = PTR_DIFF(dot_pos, fname); /* base must be at least 1 char except special cases . and .. */ if (l == 0) return(0 == strcmp( fname, "." ) || 0 == strcmp( fname, ".." )); /* base can't be greater than 8 */ if (l > 8) return FALSE; if (len - l == 1 && !strchr( dot_pos + 1, '.' )) { *dot_pos = 0; return TRUE; } /* extension must be between 1 and 3 */ if ((len - l < 2 ) || (len - l > 4)) return FALSE; /* extensions may not have a dot */ if (strchr( dot_pos+1, '.' )) return FALSE; /* must be in 8.3 format */ return TRUE; }