]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/MAI/bios_emulator/scitech/src/pm/common/unixio.c
* Code cleanup:
[people/ms/u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / common / unixio.c
1 /****************************************************************************
2 *
3 * SciTech OS Portability Manager Library
4 *
5 * ========================================================================
6 *
7 * The contents of this file are subject to the SciTech MGL Public
8 * License Version 1.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.scitechsoft.com/mgl-license.txt
11 *
12 * Software distributed under the License is distributed on an
13 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
16 *
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
21 *
22 * ========================================================================
23 *
24 * Language: ANSI C
25 * Environment: Any
26 *
27 * Description: Module containing Unix I/O functions.
28 *
29 ****************************************************************************/
30
31 #include "pmapi.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <dirent.h>
38
39 /*----------------------------- Implementation ----------------------------*/
40
41 /* {secret} */
42 typedef struct {
43 DIR *d;
44 char path[PM_MAX_PATH];
45 char mask[PM_MAX_PATH];
46 } PM_findHandle;
47
48 /****************************************************************************
49 REMARKS:
50 Internal function to convert the find data to the generic interface.
51 ****************************************************************************/
52 static void convertFindData(
53 PM_findData *findData,
54 struct dirent *blk,
55 const char *path)
56 {
57 ulong dwSize = findData->dwSize;
58 struct stat st;
59 char filename[PM_MAX_PATH];
60
61 memset(findData,0,findData->dwSize);
62 findData->dwSize = dwSize;
63 strcpy(filename,path);
64 PM_backslash(filename);
65 strcat(filename,blk->d_name);
66 stat(filename,&st);
67 if (!(st.st_mode & S_IWRITE))
68 findData->attrib |= PM_FILE_READONLY;
69 if (st.st_mode & S_IFDIR)
70 findData->attrib |= PM_FILE_DIRECTORY;
71 findData->sizeLo = st.st_size;
72 findData->sizeHi = 0;
73 strncpy(findData->name,blk->d_name,PM_MAX_PATH);
74 findData->name[PM_MAX_PATH-1] = 0;
75 }
76
77 /****************************************************************************
78 REMARKS:
79 Determines if a file name matches the passed in pattern.
80 ****************************************************************************/
81 static ibool filematch(
82 char *pattern,
83 char *dirpath,
84 struct dirent *dire)
85 {
86 struct stat st;
87 int i = 0,j = 0,lastchar = '\0';
88 char fullpath[PM_MAX_PATH];
89
90 strcpy(fullpath,dirpath);
91 PM_backslash(fullpath);
92 strcat(fullpath, dire->d_name);
93 if (stat(fullpath, &st) != 0)
94 return false;
95 for (; i < (int)strlen(dire->d_name) && j < (int)strlen(pattern); i++, j++) {
96 if (pattern[j] == '*' && lastchar != '\\') {
97 if (pattern[j+1] == '\0')
98 return true;
99 while (dire->d_name[i++] != pattern[j+1]) {
100 if (dire->d_name[i] == '\0')
101 return false;
102 }
103 i -= 2;
104 }
105 else if (dire->d_name[i] != pattern[j] &&
106 !(pattern[j] == '?' && lastchar != '\\'))
107 return false;
108 lastchar = pattern[i];
109 }
110 if (j == (int)strlen(pattern) && i == (int)strlen(dire->d_name))
111 return true;
112 return false;
113 }
114
115 /****************************************************************************
116 REMARKS:
117 Function to find the first file matching a search criteria in a directory.
118 ****************************************************************************/
119 void * PMAPI PM_findFirstFile(
120 const char *filename,
121 PM_findData *findData)
122 {
123 PM_findHandle *d;
124 struct dirent *dire;
125 char name[PM_MAX_PATH];
126 char ext[PM_MAX_PATH];
127
128 if ((d = PM_malloc(sizeof(*d))) == NULL)
129 return PM_FILE_INVALID;
130 PM_splitpath(filename,NULL,d->path,name,ext);
131 strcpy(d->mask,name);
132 strcat(d->mask,ext);
133 if (strlen(d->path) == 0)
134 strcpy(d->path, ".");
135 if (d->path[strlen(d->path)-1] == '/')
136 d->path[strlen(d->path)-1] = 0;
137 if ((d->d = opendir(d->path)) != NULL) {
138 while ((dire = readdir(d->d)) != NULL) {
139 if (filematch(d->mask,d->path,dire)) {
140 convertFindData(findData,dire,d->path);
141 return d;
142 }
143 }
144 closedir(d->d);
145 }
146 PM_free(d);
147 return PM_FILE_INVALID;
148 }
149
150 /****************************************************************************
151 REMARKS:
152 Function to find the next file matching a search criteria in a directory.
153 ****************************************************************************/
154 ibool PMAPI PM_findNextFile(
155 void *handle,
156 PM_findData *findData)
157 {
158 PM_findHandle *d = handle;
159 struct dirent *dire;
160
161 while ((dire = readdir(d->d)) != NULL) {
162 if (filematch(d->mask,d->path,dire)) {
163 convertFindData(findData,dire,d->path);
164 return true;
165 }
166 }
167 return false;
168 }
169
170 /****************************************************************************
171 REMARKS:
172 Function to close the find process
173 ****************************************************************************/
174 void PMAPI PM_findClose(
175 void *handle)
176 {
177 PM_findHandle *d = handle;
178
179 closedir(d->d);
180 free(d);
181 }
182
183 /****************************************************************************
184 REMARKS:
185 Function to determine if a drive is a valid drive or not. Under Unix this
186 function will return false for anything except a value of 3 (considered
187 the root drive, and equivalent to C: for non-Unix systems). The drive
188 numbering is:
189
190 1 - Drive A:
191 2 - Drive B:
192 3 - Drive C:
193 etc
194
195 ****************************************************************************/
196 ibool PMAPI PM_driveValid(
197 char drive)
198 {
199 if (drive == 3)
200 return true;
201 return false;
202 }
203
204 /****************************************************************************
205 REMARKS:
206 Function to get the current working directory for the specififed drive.
207 Under Unix this will always return the current working directory regardless
208 of what the value of 'drive' is.
209 ****************************************************************************/
210 void PMAPI PM_getdcwd(
211 int drive,
212 char *dir,
213 int len)
214 {
215 (void)drive;
216 getcwd(dir,len);
217 }
218
219 /****************************************************************************
220 REMARKS:
221 Function to change the file attributes for a specific file.
222 ****************************************************************************/
223 void PMAPI PM_setFileAttr(
224 const char *filename,
225 uint attrib)
226 {
227 struct stat st;
228 mode_t mode;
229
230 stat(filename,&st);
231 mode = st.st_mode;
232 if (attrib & PM_FILE_READONLY)
233 mode &= ~S_IWRITE;
234 else
235 mode |= S_IWRITE;
236 chmod(filename,mode);
237 }
238
239 /****************************************************************************
240 REMARKS:
241 Function to get the file attributes for a specific file.
242 ****************************************************************************/
243 uint PMAPI PM_getFileAttr(
244 const char *filename)
245 {
246 struct stat st;
247
248 stat(filename,&st);
249 if (st.st_mode & S_IWRITE)
250 return 0;
251 return PM_FILE_READONLY;
252 }
253
254 /****************************************************************************
255 REMARKS:
256 Function to create a directory.
257 ****************************************************************************/
258 ibool PMAPI PM_mkdir(
259 const char *filename)
260 {
261 return mkdir(filename,0x1FF) == 0;
262 }
263
264 /****************************************************************************
265 REMARKS:
266 Function to remove a directory.
267 ****************************************************************************/
268 ibool PMAPI PM_rmdir(
269 const char *filename)
270 {
271 return rmdir(filename) == 0;
272 }
273
274 /****************************************************************************
275 REMARKS:
276 Function to get the file time and date for a specific file.
277 ****************************************************************************/
278 ibool PMAPI PM_getFileTime(
279 const char *filename,
280 ibool gmTime,
281 PM_time *time)
282 {
283 /* TODO: Implement this! */
284 (void)filename;
285 (void)gmTime;
286 (void)time;
287 PM_fatalError("PM_getFileTime not implemented yet!");
288 return false;
289 }
290
291 /****************************************************************************
292 REMARKS:
293 Function to set the file time and date for a specific file.
294 ****************************************************************************/
295 ibool PMAPI PM_setFileTime(
296 const char *filename,
297 ibool gmTime,
298 PM_time *time)
299 {
300 /* TODO: Implement this! */
301 (void)filename;
302 (void)gmTime;
303 (void)time;
304 PM_fatalError("PM_setFileTime not implemented yet!");
305 return false;
306 }