Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hpfs / name.c
blobf24736d7a439218aa238f75df3338c3e3e64744f
1 /*
2 * linux/fs/hpfs/name.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * operations with filenames
7 */
9 #include "hpfs_fn.h"
11 static const char *text_postfix[]={
12 ".ASM", ".BAS", ".BAT", ".C", ".CC", ".CFG", ".CMD", ".CON", ".CPP", ".DEF",
13 ".DOC", ".DPR", ".ERX", ".H", ".HPP", ".HTM", ".HTML", ".JAVA", ".LOG", ".PAS",
14 ".RC", ".TEX", ".TXT", ".Y", ""};
16 static const char *text_prefix[]={
17 "AUTOEXEC.", "CHANGES", "COPYING", "CONFIG.", "CREDITS", "FAQ", "FILE_ID.DIZ",
18 "MAKEFILE", "READ.ME", "README", "TERMCAP", ""};
20 void hpfs_decide_conv(struct inode *inode, const unsigned char *name, unsigned len)
22 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
23 int i;
24 if (hpfs_inode->i_conv != CONV_AUTO) return;
25 for (i = 0; *text_postfix[i]; i++) {
26 int l = strlen(text_postfix[i]);
27 if (l <= len)
28 if (!hpfs_compare_names(inode->i_sb, text_postfix[i], l, name + len - l, l, 0))
29 goto text;
31 for (i = 0; *text_prefix[i]; i++) {
32 int l = strlen(text_prefix[i]);
33 if (l <= len)
34 if (!hpfs_compare_names(inode->i_sb, text_prefix[i], l, name, l, 0))
35 goto text;
37 hpfs_inode->i_conv = CONV_BINARY;
38 return;
39 text:
40 hpfs_inode->i_conv = CONV_TEXT;
41 return;
44 static inline int not_allowed_char(unsigned char c)
46 return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
47 c=='>' || c=='?' || c=='\\' || c=='|';
50 static inline int no_dos_char(unsigned char c)
51 { /* Characters that are allowed in HPFS but not in DOS */
52 return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
55 static inline unsigned char upcase(unsigned char *dir, unsigned char a)
57 if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
58 if (!dir) return a;
59 return dir[a-128];
62 unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
64 return upcase(dir, a);
67 static inline unsigned char locase(unsigned char *dir, unsigned char a)
69 if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
70 if (!dir) return a;
71 return dir[a];
74 int hpfs_chk_name(const unsigned char *name, unsigned *len)
76 int i;
77 if (*len > 254) return -ENAMETOOLONG;
78 hpfs_adjust_length(name, len);
79 if (!*len) return -EINVAL;
80 for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
81 if (*len == 1) if (name[0] == '.') return -EINVAL;
82 if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
83 return 0;
86 unsigned char *hpfs_translate_name(struct super_block *s, unsigned char *from,
87 unsigned len, int lc, int lng)
89 unsigned char *to;
90 int i;
91 if (hpfs_sb(s)->sb_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
92 printk("HPFS: Long name flag mismatch - name ");
93 for (i=0; i<len; i++) printk("%c", from[i]);
94 printk(" misidentified as %s.\n", lng ? "short" : "long");
95 printk("HPFS: It's nothing serious. It could happen because of bug in OS/2.\nHPFS: Set checks=normal to disable this message.\n");
97 if (!lc) return from;
98 if (!(to = kmalloc(len, GFP_KERNEL))) {
99 printk("HPFS: can't allocate memory for name conversion buffer\n");
100 return from;
102 for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
103 return to;
106 int hpfs_compare_names(struct super_block *s,
107 const unsigned char *n1, unsigned l1,
108 const unsigned char *n2, unsigned l2, int last)
110 unsigned l = l1 < l2 ? l1 : l2;
111 unsigned i;
112 if (last) return -1;
113 for (i = 0; i < l; i++) {
114 unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
115 unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
116 if (c1 < c2) return -1;
117 if (c1 > c2) return 1;
119 if (l1 < l2) return -1;
120 if (l1 > l2) return 1;
121 return 0;
124 int hpfs_is_name_long(const unsigned char *name, unsigned len)
126 int i,j;
127 for (i = 0; i < len && name[i] != '.'; i++)
128 if (no_dos_char(name[i])) return 1;
129 if (!i || i > 8) return 1;
130 if (i == len) return 0;
131 for (j = i + 1; j < len; j++)
132 if (name[j] == '.' || no_dos_char(name[i])) return 1;
133 return j - i > 4;
136 /* OS/2 clears dots and spaces at the end of file name, so we have to */
138 void hpfs_adjust_length(const unsigned char *name, unsigned *len)
140 if (!*len) return;
141 if (*len == 1 && name[0] == '.') return;
142 if (*len == 2 && name[0] == '.' && name[1] == '.') return;
143 while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
144 (*len)--;