Debian Codegraph @

acorn/riscos.c

raw
1 /*
2 Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
3
4 See the accompanying file LICENSE, version 2000-Apr-09 or later
5 (the contents of which are also included in zip.h) for terms of use.
6 If, for some reason, all these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
8 */
9 /* riscos.c */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "zip.h"
15 #include "riscos.h"
16
17 #define MAXEXT 256
18
19 /* External globals */
20 extern int scanimage;
21
22 /* Local globals (!?!?) */
23 char *exts2swap = NULL; /* Extensions to swap (actually, directory names) */
24
25 int stat(char *filename,struct stat *res)
26 {
27 int attr; /* object attributes */
28 unsigned int load; /* load address */
29 unsigned int exec; /* exec address */
30 int type; /* type: 0 not found, 1 file, 2 dir, 3 image */
31
32 if (!res)
33 return -1;
34
35 if (SWI_OS_File_5(filename,&type,&load,&exec,(int *)&res->st_size,&attr)!=NULL)
36 return -1;
37
38 if (type==0)
39 return -1;
40
41 res->st_dev=0;
42 res->st_ino=0;
43 res->st_nlink=0;
44 res->st_uid=1;
45 res->st_gid=1;
46 res->st_rdev=0;
47 res->st_blksize=1024;
48
49 res->st_mode = ((attr & 0001) << 8) | ((attr & 0002) << 6) |
50 ((attr & 0020) >> 2) | ((attr & 0040) >> 4);
51
52 switch (type) {
53 case 1: /* File */
54 res->st_mode |= S_IFREG;
55 break;
56 case 2: /* Directory */
57 res->st_mode |= S_IFDIR | 0700;
58 break;
59 case 3: /* Image file */
60 if (scanimage)
61 res->st_mode |= S_IFDIR | 0700;
62 else
63 res->st_mode |= S_IFREG;
64 break;
65 }
66
67 if ((((unsigned int) load) >> 20) == 0xfff) { /* date stamped file */
68 unsigned int t1, t2, tc;
69
70 t1 = (unsigned int) (exec);
71 t2 = (unsigned int) (load & 0xff);
72
73 tc = 0x6e996a00U;
74 if (t1 < tc)
75 t2--;
76 t1 -= tc;
77 t2 -= 0x33; /* 00:00:00 Jan. 1 1970 = 0x336e996a00 */
78
79 t1 = (t1 / 100) + (t2 * 42949673U); /* 0x100000000 / 100 = 42949672.96 */
80 t1 -= (t2 / 25); /* compensate for .04 error */
81
82 res->st_atime = res->st_mtime = res->st_ctime = t1;
83 }
84 else
85 res->st_atime = res->st_mtime = res->st_ctime = 0;
86
87 return 0;
88 }
89
90 #ifndef SFX
91
92 DIR *opendir(char *dirname)
93 {
94 DIR *thisdir;
95 int type;
96 int attr;
97 os_error *er;
98
99 thisdir=(DIR *)malloc(sizeof(DIR));
100 if (thisdir==NULL)
101 return NULL;
102
103 thisdir->dirname=(char *)malloc(strlen(dirname)+1);
104 if (thisdir->dirname==NULL) {
105 free(thisdir);
106 return NULL;
107 }
108
109 strcpy(thisdir->dirname,dirname);
110 if (thisdir->dirname[strlen(thisdir->dirname)-1]=='.')
111 thisdir->dirname[strlen(thisdir->dirname)-1]=0;
112
113 if (er=SWI_OS_File_5(thisdir->dirname,&type,NULL,NULL,NULL,&attr),er!=NULL ||
114 type<=1 || (type==3 && !scanimage))
115 {
116 free(thisdir->dirname);
117 free(thisdir);
118 return NULL;
119 }
120
121 thisdir->buf=malloc(DIR_BUFSIZE);
122 if (thisdir->buf==NULL) {
123 free(thisdir->dirname);
124 free(thisdir);
125 return NULL;
126 }
127
128 thisdir->size=DIR_BUFSIZE;
129 thisdir->offset=0;
130 thisdir->read=0;
131
132 return thisdir;
133 }
134
135 struct dirent *readdir(DIR *d)
136 {
137 static struct dirent dent;
138
139 if (d->read==0) { /* no more objects read in the buffer */
140 if (d->offset==-1) { /* no more objects to read */
141 return NULL;
142 }
143
144 d->read=255;
145 if (SWI_OS_GBPB_9(d->dirname,d->buf,&d->read,&d->offset,DIR_BUFSIZE,NULL)!=NULL)
146 return NULL;
147
148 if (d->read==0) {
149 d->offset=-1;
150 return NULL;
151 }
152 d->read--;
153 d->act=(char *)d->buf;
154 }
155 else { /* some object is ready in buffer */
156 d->read--;
157 d->act=(char *)(d->act+strlen(d->act)+1);
158 }
159
160 strcpy(dent.d_name,d->act);
161 dent.d_namlen=strlen(dent.d_name);
162
163 return &dent;
164 }
165
166 void closedir(DIR *d)
167 {
168 if (d->buf!=NULL)
169 free(d->buf);
170 if (d->dirname!=NULL)
171 free(d->dirname);
172 free(d);
173 }
174
175 int unlink(f)
176 char *f; /* file to delete */
177 /* Delete the file *f, returning non-zero on failure. */
178 {
179 os_error *er;
180 char canon[256];
181 int size=255;
182
183 er=SWI_OS_FSControl_37(f,canon,&size);
184 if (er==NULL) {
185 er=SWI_OS_FSControl_27(canon,0x100);
186 }
187 else {
188 er=SWI_OS_FSControl_27(f,0x100);
189 }
190 return (int)er;
191 }
192
193 int deletedir(char *d)
194 {
195 int objtype;
196 char *s;
197 int len;
198 os_error *er;
199
200 len = strlen(d);
201 if ((s = malloc(len + 1)) == NULL)
202 return -1;
203
204 strcpy(s,d);
205 if (s[len-1]=='.')
206 s[len-1]=0;
207
208 if (er=SWI_OS_File_5(s,&objtype,NULL,NULL,NULL,NULL),er!=NULL) {
209 free(s);
210 return -1;
211 }
212 if (objtype<2 || (!scanimage && objtype==3)) {
213 /* this is a file or it doesn't exist */
214 free(s);
215 return -1;
216 }
217
218 if (er=SWI_OS_File_6(s),er!=NULL) {
219 /* maybe this is a problem with the DDEUtils module, try to canonicalise the path */
220 char canon[256];
221 int size=255;
222
223 if (er=SWI_OS_FSControl_37(s,canon,&size),er!=NULL) {
224 free(s);
225 return -1;
226 }
227 if (er=SWI_OS_File_6(canon),er!=NULL) {
228 free(s);
229 return -1;
230 }
231 }
232 free(s);
233 return 0;
234 }
235
236 #endif /* !SFX */
237
238 int chmod(char *file, int mode)
239 {
240 /*************** NOT YET IMPLEMENTED!!!!!! ******************/
241 /* I don't know if this will be needed or not... */
242 file=file;
243 mode=mode;
244 return 0;
245 }
246
247 void setfiletype(char *fname,int ftype)
248 {
249 char str[256];
250 sprintf(str,"SetType %s &%3.3X",fname,ftype);
251 SWI_OS_CLI(str);
252 }
253
254 void getRISCOSexts(char *envstr)
255 {
256 char *envptr; /* value returned by getenv */
257
258 envptr = getenv(envstr);
259 if (envptr == NULL || *envptr == 0) return;
260
261 exts2swap=malloc(1+strlen(envptr));
262 if (exts2swap == NULL)
263 return;
264
265 strcpy(exts2swap, envptr);
266 }
267
268 int checkext(char *suff)
269 {
270 register char *extptr=exts2swap;
271 register char *suffptr;
272 register int e,s;
273
274 if (extptr != NULL) while(*extptr) {
275 suffptr=suff;
276 e=*extptr; s=*suffptr;
277 while (e && e!=':' && s && s!='.' && s!='/' && e==s) {
278 e=*++extptr; s=*++suffptr;
279 }
280 if (e==':') e=0;
281 if (s=='.' || s=='/') s=0;
282 if (!e && !s) {
283 return 1;
284 }
285 while(*extptr!=':' && *extptr!='\0') /* skip to next extension */
286 extptr++;
287 if (*extptr!='\0')
288 extptr++;
289 }
290 return 0;
291 }
292
293 int swapext(char *name, char *exptr)
294 {
295 char *ext;
296 char *p1=exptr;
297 char *p2;
298 int extchar=*exptr;
299 unsigned int i=0;
300
301 while(*++p1 && *p1!='.' && *p1!='/')
302 ;
303 ext=malloc(i=p1-exptr);
304 if (!ext)
305 return 1;
306 memcpy(ext, exptr+1, i);
307 p2=exptr-1;
308 p1=exptr+i-1;
309 while(p2 >= name)
310 *p1--=*p2--;
311 strcpy(name,ext);
312 *p1=(extchar=='/'?'.':'/');
313 free(ext);
314 return 0;
315 }
316
317 void remove_prefix(void)
318 {
319 SWI_DDEUtils_Prefix(NULL);
320 }
321
322 void set_prefix(void)
323 {
324 char *pref;
325 int size=0;
326
327 if (SWI_OS_FSControl_37("@",pref,&size)!=NULL)
328 return;
329
330 size=1-size;
331
332 if (pref=malloc(size),pref!=NULL) {
333 if (SWI_OS_FSControl_37("@",pref,&size)!=NULL) {
334 free(pref);
335 return;
336 }
337
338 if (SWI_DDEUtils_Prefix(pref)==NULL) {
339 atexit(remove_prefix);
340 }
341
342 free(pref);
343 }
344 }
345
346 #ifdef localtime
347 # undef localtime
348 #endif
349
350 #ifdef gmtime
351 # undef gmtime
352 #endif
353
354 /* Acorn's implementation of localtime() and gmtime()
355 * doesn't consider the timezone offset, so we have to
356 * add it before calling the library functions
357 */
358
359 struct tm *riscos_localtime(const time_t *timer)
360 {
361 time_t localt=*timer;
362
363 localt+=SWI_Read_Timezone()/100;
364
365 return localtime(&localt);
366 }
367
368 struct tm *riscos_gmtime(const time_t *timer)
369 {
370 time_t localt=*timer;
371
372 localt+=SWI_Read_Timezone()/100;
373
374 return gmtime(&localt);
375 }
376
377
378 int riscos_fseek(FILE *fd, long offset, int whence)
379 {
380 int ret;
381 switch (whence)
382 {
383 case SEEK_END:
384 ret = (fseek) (fd, 0, SEEK_END);
385 if (ret)
386 return ret;
387 /* fall through */
388 case SEEK_CUR:
389 offset += ftell (fd);
390 /* fall through */
391 default: /* SEEK_SET */
392 return (fseek) (fd, offset < 0 ? 0 : offset, SEEK_SET);
393 }
394 }