source: trunk/minix/commands/i386/mtools-3.9.7/file_name.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.1 KB
Line 
1#include "sysincludes.h"
2#include "msdos.h"
3#include "mtools.h"
4#include "vfat.h"
5#include "codepage.h"
6
7/* Write a DOS name + extension into a legal unix-style name. */
8char *unix_normalize (char *ans, char *name, char *ext)
9{
10 char *a;
11 int j;
12
13 for (a=ans,j=0; (j<8) && (name[j] > ' '); ++j,++a)
14 *a = name[j];
15 if(*ext > ' ') {
16 *a++ = '.';
17 for (j=0; j<3 && ext[j] > ' '; ++j,++a)
18 *a = ext[j];
19 }
20 *a++ = '\0';
21 return ans;
22}
23
24typedef enum Case_l {
25 NONE,
26 UPPER,
27 LOWER
28} Case_t;
29
30static void TranslateToDos(const char *s, char *t, int count,
31 char *end, Case_t *Case, int *mangled)
32{
33 *Case = NONE;
34 for( ; *s && (s < end || !end); s++) {
35 if(!count) {
36 *mangled |= 3;
37 break;
38 }
39 /* skip spaces & dots */
40 if(*s == ' ' || *s == '.') {
41 *mangled |= 3;
42 continue;
43 }
44
45 /* convert to dos */
46 if((*s) & 0x80) {
47 *mangled |= 1;
48 *t = to_dos(*s);
49 }
50
51 if ((*s & 0x7f) < ' ' ) {
52 *mangled |= 3;
53 *t = '_';
54 } else if (islower((unsigned char)*s)) {
55 *t = toupper(*s);
56 if(*Case == UPPER && !mtools_no_vfat)
57 *mangled |= 1;
58 else
59 *Case = LOWER;
60 } else if (isupper((unsigned char)*s)) {
61 *t = *s;
62 if(*Case == LOWER && !mtools_no_vfat)
63 *mangled |= 1;
64 else
65 *Case = UPPER;
66 } else if((*s) & 0x80)
67 *t = mstoupper(*t); /* upper case */
68 else
69 *t = *s;
70 count--;
71 t++;
72 }
73}
74
75/* dos_name
76 *
77 * Convert a Unix-style filename to a legal MSDOS name and extension.
78 * Will truncate file and extension names, will substitute
79 * the character '~' for any illegal character(s) in the name.
80 */
81char *dos_name(char *name, int verbose, int *mangled, char *ans)
82{
83 char *s, *ext;
84 register int i;
85 Case_t BaseCase, ExtCase;
86
87 *mangled = 0;
88
89 /* skip drive letter */
90 name = skip_drive(name);
91
92 /* zap the leading path */
93 name = (char *) _basename(name);
94 if ((s = strrchr(name, '\\')))
95 name = s + 1;
96
97 memset(ans, ' ', 11);
98 ans[11]='\0';
99
100 /* skip leading dots and spaces */
101 i = strspn(name, ". ");
102 if(i) {
103 name += i;
104 *mangled = 3;
105 }
106
107 ext = strrchr(name, '.');
108
109 /* main name */
110 TranslateToDos(name, ans, 8, ext, &BaseCase, mangled);
111 if(ext)
112 TranslateToDos(ext+1, ans+8, 3, 0, &ExtCase, mangled);
113
114 if(*mangled & 2)
115 autorename_short(ans, 0);
116
117 if(!*mangled) {
118 if(BaseCase == LOWER)
119 *mangled |= BASECASE;
120 if(ExtCase == LOWER)
121 *mangled |= EXTCASE;
122 if((BaseCase == LOWER || ExtCase == LOWER) &&
123 !mtools_no_vfat) {
124 *mangled |= 1;
125 }
126 }
127 return ans;
128}
129
130
131/*
132 * Get rid of spaces in an MSDOS 'raw' name (one that has come from the
133 * directory structure) so that it can be used for regular expression
134 * matching with a Unix filename. Also used to 'unfix' a name that has
135 * been altered by dos_name().
136 */
137
138char *unix_name(char *name, char *ext, char Case, char *ans)
139{
140 char *s, tname[9], text[4];
141 int i;
142
143 strncpy(tname, (char *) name, 8);
144 tname[8] = '\0';
145 if ((s = strchr(tname, ' ')))
146 *s = '\0';
147
148 if(!(Case & (BASECASE | EXTCASE)) && mtools_ignore_short_case)
149 Case |= BASECASE | EXTCASE;
150
151 if(Case & BASECASE)
152 for(i=0;i<8 && tname[i];i++)
153 tname[i] = tolower(tname[i]);
154
155 strncpy(text, (char *) ext, 3);
156 text[3] = '\0';
157 if ((s = strchr(text, ' ')))
158 *s = '\0';
159
160 if(Case & EXTCASE)
161 for(i=0;i<3 && text[i];i++)
162 text[i] = tolower(text[i]);
163
164 if (*text) {
165 strcpy(ans, tname);
166 strcat(ans, ".");
167 strcat(ans, text);
168 } else
169 strcpy(ans, tname);
170
171 /* fix special characters (above 0x80) */
172 to_unix(ans,11);
173 return(ans);
174}
175
176/* If null encountered, set *end to 0x40 and write nulls rest of way
177 * 950820: Win95 does not like this! It complains about bad characters.
178 * So, instead: If null encountered, set *end to 0x40, write the null, and
179 * write 0xff the rest of the way (that is what Win95 seems to do; hopefully
180 * that will make it happy)
181 */
182/* Always return num */
183int unicode_write(char *in, struct unicode_char *out, int num, int *end_p)
184{
185 int j;
186
187 for (j=0; j<num; ++j) {
188 out->uchar = '\0'; /* Hard coded to ASCII */
189 if (*end_p)
190 /* Fill with 0xff */
191 out->uchar = out->lchar = (char) 0xff;
192 else {
193 out->lchar = *in;
194 if (! *in) {
195 *end_p = VSE_LAST;
196 }
197 }
198
199 ++out;
200 ++in;
201 }
202 return num;
203}
Note: See TracBrowser for help on using the repository browser.