1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
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
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.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
27 * Description: Module containing Unix I/O functions.
29 ****************************************************************************/
39 /*----------------------------- Implementation ----------------------------*/
44 char path[PM_MAX_PATH];
45 char mask[PM_MAX_PATH];
48 /****************************************************************************
50 Internal function to convert the find data to the generic interface.
51 ****************************************************************************/
52 static void convertFindData(
53 PM_findData *findData,
57 ulong dwSize = findData->dwSize;
59 char filename[PM_MAX_PATH];
61 memset(findData,0,findData->dwSize);
62 findData->dwSize = dwSize;
63 strcpy(filename,path);
64 PM_backslash(filename);
65 strcat(filename,blk->d_name);
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;
73 strncpy(findData->name,blk->d_name,PM_MAX_PATH);
74 findData->name[PM_MAX_PATH-1] = 0;
77 /****************************************************************************
79 Determines if a file name matches the passed in pattern.
80 ****************************************************************************/
81 static ibool filematch(
87 int i = 0,j = 0,lastchar = '\0';
88 char fullpath[PM_MAX_PATH];
90 strcpy(fullpath,dirpath);
91 PM_backslash(fullpath);
92 strcat(fullpath, dire->d_name);
93 if (stat(fullpath, &st) != 0)
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')
99 while (dire->d_name[i++] != pattern[j+1]) {
100 if (dire->d_name[i] == '\0')
105 else if (dire->d_name[i] != pattern[j] &&
106 !(pattern[j] == '?' && lastchar != '\\'))
108 lastchar = pattern[i];
110 if (j == (int)strlen(pattern) && i == (int)strlen(dire->d_name))
115 /****************************************************************************
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)
125 char name[PM_MAX_PATH];
126 char ext[PM_MAX_PATH];
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);
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);
147 return PM_FILE_INVALID;
150 /****************************************************************************
152 Function to find the next file matching a search criteria in a directory.
153 ****************************************************************************/
154 ibool PMAPI PM_findNextFile(
156 PM_findData *findData)
158 PM_findHandle *d = handle;
161 while ((dire = readdir(d->d)) != NULL) {
162 if (filematch(d->mask,d->path,dire)) {
163 convertFindData(findData,dire,d->path);
170 /****************************************************************************
172 Function to close the find process
173 ****************************************************************************/
174 void PMAPI PM_findClose(
177 PM_findHandle *d = handle;
183 /****************************************************************************
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
195 ****************************************************************************/
196 ibool PMAPI PM_driveValid(
204 /****************************************************************************
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(
219 /****************************************************************************
221 Function to change the file attributes for a specific file.
222 ****************************************************************************/
223 void PMAPI PM_setFileAttr(
224 const char *filename,
232 if (attrib & PM_FILE_READONLY)
236 chmod(filename,mode);
239 /****************************************************************************
241 Function to get the file attributes for a specific file.
242 ****************************************************************************/
243 uint PMAPI PM_getFileAttr(
244 const char *filename)
249 if (st.st_mode & S_IWRITE)
251 return PM_FILE_READONLY;
254 /****************************************************************************
256 Function to create a directory.
257 ****************************************************************************/
258 ibool PMAPI PM_mkdir(
259 const char *filename)
261 return mkdir(filename,0x1FF) == 0;
264 /****************************************************************************
266 Function to remove a directory.
267 ****************************************************************************/
268 ibool PMAPI PM_rmdir(
269 const char *filename)
271 return rmdir(filename) == 0;
274 /****************************************************************************
276 Function to get the file time and date for a specific file.
277 ****************************************************************************/
278 ibool PMAPI PM_getFileTime(
279 const char *filename,
283 /* TODO: Implement this! */
287 PM_fatalError("PM_getFileTime not implemented yet!");
291 /****************************************************************************
293 Function to set the file time and date for a specific file.
294 ****************************************************************************/
295 ibool PMAPI PM_setFileTime(
296 const char *filename,
300 /* TODO: Implement this! */
304 PM_fatalError("PM_setFileTime not implemented yet!");