scoutos / prex-0.9.0 / usr / server / fs / fatfs / fatfs_subr.c @ 03e9c04a
History | View | Annotate | Download (3.95 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2005-2008, Kohsuke Ohtani
|
3 |
* All rights reserved.
|
4 |
*
|
5 |
* Redistribution and use in source and binary forms, with or without
|
6 |
* modification, are permitted provided that the following conditions
|
7 |
* are met:
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
9 |
* notice, this list of conditions and the following disclaimer.
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
12 |
* documentation and/or other materials provided with the distribution.
|
13 |
* 3. Neither the name of the author nor the names of any co-contributors
|
14 |
* may be used to endorse or promote products derived from this software
|
15 |
* without specific prior written permission.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 |
* SUCH DAMAGE.
|
28 |
*/
|
29 |
|
30 |
#include <sys/prex.h> |
31 |
|
32 |
#include <ctype.h> |
33 |
#include <string.h> |
34 |
#include <unistd.h> |
35 |
#include <errno.h> |
36 |
#include <stdlib.h> |
37 |
|
38 |
#include "fatfs.h" |
39 |
|
40 |
|
41 |
/*
|
42 |
* Convert file name to 8.3 format
|
43 |
* Ex. "foo.bar" => "foo bar"
|
44 |
*/
|
45 |
void
|
46 |
fat_convert_name(char *org, char *name) |
47 |
{ |
48 |
int i;
|
49 |
|
50 |
memset(name, (int)' ', 11); |
51 |
for (i = 0; i <= 11; i++) { |
52 |
if (!*org)
|
53 |
break;
|
54 |
if (*org == '/') |
55 |
break;
|
56 |
if (*org == '.') { |
57 |
i = 7;
|
58 |
org++; |
59 |
continue;
|
60 |
} |
61 |
*(name + i) = *org; |
62 |
org++; |
63 |
} |
64 |
} |
65 |
|
66 |
/*
|
67 |
* Restore file name to normal format
|
68 |
* Ex. "foo bar" => "foo.bar"
|
69 |
*/
|
70 |
void
|
71 |
fat_restore_name(char *org, char *name) |
72 |
{ |
73 |
int i;
|
74 |
|
75 |
memset(name, 0, 13); |
76 |
for (i = 0; i < 8; i++) { |
77 |
if (*org != ' ') |
78 |
*name++ = *org; |
79 |
org++; |
80 |
} |
81 |
if (*org != ' ') |
82 |
*name++ = '.';
|
83 |
for (i = 0; i < 3; i++) { |
84 |
if (*org != ' ') |
85 |
*name++ = *org; |
86 |
org++; |
87 |
} |
88 |
} |
89 |
|
90 |
/*
|
91 |
* Compare 2 file names.
|
92 |
*
|
93 |
* Return 0 if it matches.
|
94 |
*/
|
95 |
int
|
96 |
fat_compare_name(char *n1, char *n2) |
97 |
{ |
98 |
int i;
|
99 |
|
100 |
for (i = 0; i < 11; i++, n1++, n2++) { |
101 |
if (toupper((int)*n1) != toupper((int)*n2)) |
102 |
return -1; |
103 |
} |
104 |
return 0; |
105 |
} |
106 |
|
107 |
/*
|
108 |
* Check specified name is valid as FAT file name.
|
109 |
* Return true if valid.
|
110 |
*/
|
111 |
int
|
112 |
fat_valid_name(char *name)
|
113 |
{ |
114 |
static char invalid_char[] = "*?<>|\"+=,;[] \345"; |
115 |
int len = 0; |
116 |
|
117 |
/* . or .. */
|
118 |
if (*name == '.') { |
119 |
name++; |
120 |
if (*name == '.') |
121 |
name++; |
122 |
return (*(name + 1) == '\0') ? 1 : 0; |
123 |
} |
124 |
/* First char must be alphabet or numeric */
|
125 |
if (!isalnum((int)*name)) |
126 |
return 0; |
127 |
while (*name != '\0') { |
128 |
if (strchr(invalid_char, *name))
|
129 |
return 0; |
130 |
if (*name == '.') |
131 |
break; /* Start of extension */ |
132 |
if (++len > 8) |
133 |
return 0; /* Too long name */ |
134 |
name++; |
135 |
} |
136 |
if (*name == '\0') |
137 |
return 1; |
138 |
name++; |
139 |
if (*name == '\0') /* Empty extension */ |
140 |
return 1; |
141 |
len = 0;
|
142 |
while (*name != '\0') { |
143 |
if (strchr(invalid_char, *name))
|
144 |
return 0; |
145 |
if (*name == '.') |
146 |
return 0; /* Second extention */ |
147 |
if (++len > 3) |
148 |
return 0; /* Too long name */ |
149 |
name++; |
150 |
} |
151 |
return 1; |
152 |
} |
153 |
|
154 |
/*
|
155 |
* mode -> attribute
|
156 |
*/
|
157 |
void
|
158 |
fat_mode_to_attr(mode_t mode, u_char *attr) |
159 |
{ |
160 |
|
161 |
*attr = 0;
|
162 |
if (!(mode & S_IWRITE))
|
163 |
*attr |= FA_RDONLY; |
164 |
if (!(mode & S_IREAD))
|
165 |
*attr |= FA_HIDDEN; |
166 |
if (S_ISDIR(mode))
|
167 |
*attr |= FA_SUBDIR; |
168 |
} |
169 |
|
170 |
/*
|
171 |
* attribute -> mode
|
172 |
*/
|
173 |
void
|
174 |
fat_attr_to_mode(u_char attr, mode_t *mode) |
175 |
{ |
176 |
|
177 |
if (attr & FA_RDONLY)
|
178 |
*mode = |
179 |
S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | |
180 |
S_IXOTH; |
181 |
else
|
182 |
*mode = S_IRWXU | S_IRWXG | S_IRWXO; |
183 |
|
184 |
if (attr & FA_SUBDIR)
|
185 |
*mode |= S_IFDIR; |
186 |
else
|
187 |
*mode |= S_IFREG; |
188 |
} |