-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
file.c
3004 lines (2344 loc) · 71.8 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* File I/O.
*
* By Shawn Hargreaves.
*
* _pack_fdopen() and related modifications by Annie Testes.
*
* Evert Glebbeek added the support for relative filenames:
* make_absolute_filename(), make_relative_filename() and
* is_relative_filename().
*
* Peter Wang added support for packfile vtables.
*
* See readme.txt for copyright information.
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "allegro.h"
#include "allegro/internal/aintern.h"
#ifndef ALLEGRO_LEGACY_MPW
#include <sys/stat.h>
#endif
#ifdef ALLEGRO_LEGACY_UNIX
#include <pwd.h> /* for tilde expansion */
#endif
#ifdef ALLEGRO_LEGACY_WINDOWS
#include "winalleg.h" /* for GetTempPath */
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
/* permissions to use when opening files */
#ifndef ALLEGRO_LEGACY_MPW
/* some OSes have no concept of "group" and "other" */
#ifndef S_IRGRP
#define S_IRGRP 0
#define S_IWGRP 0
#endif
#ifndef S_IROTH
#define S_IROTH 0
#define S_IWOTH 0
#endif
#define OPEN_PERMS (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#endif /* !ALLEGRO_LEGACY_MPW */
static char the_password[256] = EMPTY_STRING;
int _packfile_filesize = 0;
int _packfile_datasize = 0;
int _packfile_type = 0;
static PACKFILE_VTABLE normal_vtable;
static PACKFILE *pack_fopen_special_file(AL_CONST char *filename, AL_CONST char *mode);
static int filename_encoding = U_UTF8;
#define FA_DAT_FLAGS (FA_RDONLY | FA_ARCH)
typedef struct RESOURCE_PATH
{
int priority;
char path[1024];
struct RESOURCE_PATH *next;
} RESOURCE_PATH;
static RESOURCE_PATH *resource_path_list = NULL;
static void destroy_resource_path_list(void);
/***************************************************
****************** Path handling ******************
***************************************************/
/* fix_filename_case:
* Converts filename to upper case.
*/
char *fix_filename_case(char *filename)
{
ASSERT(filename);
if (!ALLEGRO_LFN)
ustrupr(filename);
return filename;
}
/* fix_filename_slashes:
* Converts '/' or '\' to system specific path separator.
*/
char *fix_filename_slashes(char *filename)
{
int pos, c;
ASSERT(filename);
for (pos=0; ugetc(filename+pos); pos+=uwidth(filename+pos)) {
c = ugetc(filename+pos);
if ((c == '/') || (c == '\\'))
usetat(filename+pos, 0, OTHER_PATH_SEPARATOR);
}
return filename;
}
/* Canonicalize_filename:
* Returns the canonical form of the specified filename, i.e. the
* minimal absolute filename describing the same file.
*/
char *canonicalize_filename(char *dest, AL_CONST char *filename, int size)
{
int saved_errno = errno;
char buf[1024], buf2[1024];
char *p;
int pos = 0;
int drive = -1;
int c1, i;
ASSERT(dest);
ASSERT(filename);
ASSERT(size >= 0);
#if (DEVICE_SEPARATOR != 0) && (DEVICE_SEPARATOR != '\0')
/* check whether we have a drive letter */
c1 = utolower(ugetc(filename));
if ((c1 >= 'a') && (c1 <= 'z')) {
int c2 = ugetat(filename, 1);
if (c2 == DEVICE_SEPARATOR) {
drive = c1 - 'a';
filename += uwidth(filename);
filename += uwidth(filename);
}
}
/* if not, use the current drive */
if (drive < 0)
drive = _al_getdrive();
pos += usetc(buf+pos, drive+'a');
pos += usetc(buf+pos, DEVICE_SEPARATOR);
#endif
#ifdef ALLEGRO_LEGACY_UNIX
/* if the filename starts with ~ then it's relative to a home directory */
if ((ugetc(filename) == '~')) {
AL_CONST char *tail = filename + uwidth(filename); /* could be the username */
char *home = NULL; /* their home directory */
if (ugetc(tail) == '/' || !ugetc(tail)) {
/* easy */
home = getenv("HOME");
if (home)
home = _al_strdup(home);
}
else {
/* harder */
char *username = (char *)tail, *ascii_username, *ch;
int userlen;
struct passwd *pwd;
/* find the end of the username */
tail = ustrchr(username, '/');
if (!tail)
tail = ustrchr(username, '\0');
/* this ought to be the ASCII length, but I can't see a Unicode
* function to return the difference in characters between two
* pointers. This code is safe on the assumption that ASCII is
* the most efficient encoding, but wasteful of memory */
userlen = tail - username + ucwidth('\0');
ascii_username = _AL_MALLOC_ATOMIC(userlen);
if (ascii_username) {
/* convert the username to ASCII, find the password entry,
* and copy their home directory. */
do_uconvert(username, U_CURRENT, ascii_username, U_ASCII, userlen);
if ((ch = strchr(ascii_username, '/')))
*ch = '\0';
setpwent();
while (((pwd = getpwent()) != NULL) &&
(strcmp(pwd->pw_name, ascii_username) != 0))
;
_AL_FREE(ascii_username);
if (pwd)
home = _al_strdup(pwd->pw_dir);
endpwent();
}
}
/* If we got a home directory, prepend it to the filename. Otherwise
* we leave the filename alone, like bash but not tcsh; bash is better
* anyway. :)
*/
if (home) {
do_uconvert(home, U_ASCII, buf+pos, U_CURRENT, sizeof(buf)-pos);
_AL_FREE(home);
pos = ustrsize(buf);
filename = tail;
goto no_relativisation;
}
}
#endif /* Unix */
/* if the filename is relative, make it absolute */
if ((ugetc(filename) != '/') && (ugetc(filename) != OTHER_PATH_SEPARATOR) && (ugetc(filename) != '#')) {
_al_getdcwd(drive, buf2, sizeof(buf2) - ucwidth(OTHER_PATH_SEPARATOR));
put_backslash(buf2);
p = buf2;
if ((utolower(p[0]) >= 'a') && (utolower(p[0]) <= 'z') && (p[1] == DEVICE_SEPARATOR))
p += 2;
ustrzcpy(buf+pos, sizeof(buf)-pos, p);
pos = ustrsize(buf);
}
#ifdef ALLEGRO_LEGACY_UNIX
no_relativisation:
#endif
/* add our filename, and clean it up a bit */
ustrzcpy(buf+pos, sizeof(buf)-pos, filename);
fix_filename_case(buf);
fix_filename_slashes(buf);
/* remove duplicate slashes */
pos = usetc(buf2, OTHER_PATH_SEPARATOR);
pos += usetc(buf2+pos, OTHER_PATH_SEPARATOR);
usetc(buf2+pos, 0);
while ((p = ustrstr(buf, buf2)) != NULL)
uremove(p, 0);
/* remove /./ patterns */
pos = usetc(buf2, OTHER_PATH_SEPARATOR);
pos += usetc(buf2+pos, '.');
pos += usetc(buf2+pos, OTHER_PATH_SEPARATOR);
usetc(buf2+pos, 0);
while ((p = ustrstr(buf, buf2)) != NULL) {
uremove(p, 0);
uremove(p, 0);
}
/* collapse /../ patterns */
pos = usetc(buf2, OTHER_PATH_SEPARATOR);
pos += usetc(buf2+pos, '.');
pos += usetc(buf2+pos, '.');
pos += usetc(buf2+pos, OTHER_PATH_SEPARATOR);
usetc(buf2+pos, 0);
while ((p = ustrstr(buf, buf2)) != NULL) {
for (i=0; buf+uoffset(buf, i) < p; i++)
;
while (--i > 0) {
c1 = ugetat(buf, i);
if (c1 == OTHER_PATH_SEPARATOR)
break;
if (c1 == DEVICE_SEPARATOR) {
i++;
break;
}
}
if (i < 0)
i = 0;
p += ustrsize(buf2);
memmove(buf+uoffset(buf, i+1), p, ustrsizez(p));
}
/* all done! */
ustrzcpy(dest, size, buf);
errno = saved_errno;
return dest;
}
/* make_absolute_filename:
* Makes the absolute filename corresponding to the specified relative
* filename using the specified base (PATH is absolute and represents
* the base, FILENAME is the relative filename), stores it in DEST
* whose size in bytes is SIZE and returns a pointer to it.
* It does not append '/' to the path.
*/
char *make_absolute_filename(char *dest, AL_CONST char *path, AL_CONST char *filename, int size)
{
char tmp[1024];
ASSERT(dest);
ASSERT(path);
ASSERT(filename);
ASSERT(size >= 0);
replace_filename(tmp, path, filename, sizeof(tmp));
canonicalize_filename(dest, tmp, size);
return dest;
}
/* make_relative_filename:
* Makes the relative filename corresponding to the specified absolute
* filename using the specified base (PATH is absolute and represents
* the base, FILENAME is the absolute filename), stores it in DEST
* whose size in bytes is SIZE and returns a pointer to it, or returns
* NULL if it cannot do so.
* It does not append '/' to the path.
*/
char *make_relative_filename(char *dest, AL_CONST char *path, AL_CONST char *filename, int size)
{
char *my_path, *my_filename;
char *reduced_path = NULL, *reduced_filename = NULL;
char *p1, *p2;
int c, c1, c2, pos;
ASSERT(dest);
ASSERT(path);
ASSERT(filename);
ASSERT(size >= 0);
/* The first check under DOS/Windows would be for the drive: since the
* paths are absolute, they will always contain a drive letter. Do this
* check under Unix too where the first character should always be '/'
* in order not to screw up existing DOS/Windows paths.
*/
if (ugetc(path) != ugetc(filename))
return NULL;
my_path = _al_ustrdup(path);
if (!my_path)
return NULL;
my_filename = _al_ustrdup(filename);
if (!my_filename) {
_AL_FREE(my_path);
return NULL;
}
/* Strip the filenames to keep only the directories. */
usetc(get_filename(my_path), 0);
usetc(get_filename(my_filename), 0);
/* Both paths are on the same device. There are three cases:
* - the filename is a "child" of the path in the directory tree,
* - the filename is a "brother" of the path,
* - the filename is only a "cousin" of the path.
* In the two former cases, we will only need to keep a suffix of the
* filename. In the latter case, we will need to back-paddle through
* the directory tree.
*/
p1 = my_path;
p2 = my_filename;
while (((c1=ugetx(&p1)) == (c2=ugetx(&p2))) && c1 && c2) {
if ((c1 == '/') || (c1 == OTHER_PATH_SEPARATOR)) {
reduced_path = p1;
reduced_filename = p2;
}
}
if (!c1) {
/* If the path is exhausted, we are in one of the two former cases. */
if (!c2) {
/* If the filename is also exhausted, we are in the second case.
* Prepend './' to the reduced filename.
*/
pos = usetc(dest, '.');
pos += usetc(dest+pos, OTHER_PATH_SEPARATOR);
usetc(dest+pos, 0);
}
else {
/* Otherwise we are in the first case. Nothing to do. */
usetc(dest, 0);
}
}
else {
/* Bail out if previously something went wrong (eg. user supplied
* paths are not canonical and we can't understand them). */
if (!reduced_path) {
_AL_FREE(my_path);
_AL_FREE(my_filename);
return NULL;
}
/* Otherwise, we are in the latter case and need to count the number
* of remaining directories in the reduced path and prepend the same
* number of '../' to the reduced filename.
*/
pos = 0;
while ((c=ugetx(&reduced_path))) {
if ((c == '/') || (c == OTHER_PATH_SEPARATOR)) {
pos += usetc(dest+pos, '.');
pos += usetc(dest+pos, '.');
pos += usetc(dest+pos, OTHER_PATH_SEPARATOR);
}
}
usetc(dest+pos, 0);
}
/* Bail out if previously something went wrong (eg. user supplied
* paths are not canonical and we can't understand them). */
if (!reduced_filename) {
_AL_FREE(my_path);
_AL_FREE(my_filename);
return NULL;
}
ustrzcat(dest, size, reduced_filename);
ustrzcat(dest, size, get_filename(filename));
_AL_FREE(my_path);
_AL_FREE(my_filename);
/* Harmonize path separators. */
return fix_filename_slashes(dest);
}
/* is_relative_filename:
* Checks whether the specified filename is relative.
*/
int is_relative_filename(AL_CONST char *filename)
{
ASSERT(filename);
/* All filenames that start with a '.' are relative. */
if (ugetc(filename) == '.')
return TRUE;
/* Filenames that contain a device separator (DOS/Windows)
* or start with a '/' (Unix) are considered absolute.
*/
#if (defined ALLEGRO_LEGACY_DOS) || (defined ALLEGRO_LEGACY_WINDOWS)
if (ustrchr(filename, DEVICE_SEPARATOR))
return FALSE;
#endif
if ((ugetc(filename) == '/') || (ugetc(filename) == OTHER_PATH_SEPARATOR))
return FALSE;
return TRUE;
}
/* replace_filename:
* Replaces filename in path with different one.
* It does not append '/' to the path.
*/
char *replace_filename(char *dest, AL_CONST char *path, AL_CONST char *filename, int size)
{
char tmp[1024];
int pos, c;
ASSERT(dest);
ASSERT(path);
ASSERT(filename);
ASSERT(size >= 0);
pos = ustrlen(path);
while (pos>0) {
c = ugetat(path, pos-1);
if ((c == '/') || (c == OTHER_PATH_SEPARATOR) || (c == DEVICE_SEPARATOR))
break;
pos--;
}
ustrzncpy(tmp, sizeof(tmp), path, pos);
ustrzcat(tmp, sizeof(tmp), filename);
ustrzcpy(dest, size, tmp);
return dest;
}
/* replace_extension:
* Replaces extension in filename with different one.
* It appends '.' if it is not present in the filename.
*/
char *replace_extension(char *dest, AL_CONST char *filename, AL_CONST char *ext, int size)
{
char tmp[1024], tmp2[16];
int pos, end, c;
ASSERT(dest);
ASSERT(filename);
ASSERT(ext);
ASSERT(size >= 0);
pos = end = ustrlen(filename);
while (pos>0) {
c = ugetat(filename, pos-1);
if ((c == '.') || (c == '/') || (c == OTHER_PATH_SEPARATOR) || (c == DEVICE_SEPARATOR))
break;
pos--;
}
if (ugetat(filename, pos-1) == '.')
end = pos-1;
ustrzncpy(tmp, sizeof(tmp), filename, end);
ustrzcat(tmp, sizeof(tmp), uconvert_ascii(".", tmp2));
ustrzcat(tmp, sizeof(tmp), ext);
ustrzcpy(dest, size, tmp);
return dest;
}
/* append_filename:
* Append filename to path, adding separator if necessary.
*/
char *append_filename(char *dest, AL_CONST char *path, AL_CONST char *filename, int size)
{
char tmp[1024];
int pos, c;
ASSERT(dest);
ASSERT(path);
ASSERT(filename);
ASSERT(size >= 0);
ustrzcpy(tmp, sizeof(tmp), path);
pos = ustrlen(tmp);
if ((pos > 0) && (uoffset(tmp, pos) < ((int)sizeof(tmp) - ucwidth(OTHER_PATH_SEPARATOR) - ucwidth(0)))) {
c = ugetat(tmp, pos-1);
if ((c != '/') && (c != OTHER_PATH_SEPARATOR) && (c != DEVICE_SEPARATOR)) {
pos = uoffset(tmp, pos);
pos += usetc(tmp+pos, OTHER_PATH_SEPARATOR);
usetc(tmp+pos, 0);
}
}
ustrzcat(tmp, sizeof(tmp), filename);
ustrzcpy(dest, size, tmp);
return dest;
}
/* get_filename:
* When passed a completely specified file path, this returns a pointer
* to the filename portion. Both '\' and '/' are recognized as directory
* separators.
*/
char *get_filename(AL_CONST char *path)
{
int c;
const char *ptr, *ret;
ASSERT(path);
ptr = path;
ret = ptr;
for (;;) {
c = ugetxc(&ptr);
if (!c) break;
if ((c == '/') || (c == OTHER_PATH_SEPARATOR) || (c == DEVICE_SEPARATOR))
ret = (char*)ptr;
}
return (char*)ret;
}
/* get_extension:
* When passed a complete filename (with or without path information)
* this returns a pointer to the file extension.
*/
char *get_extension(AL_CONST char *filename)
{
int pos, c;
ASSERT(filename);
pos = ustrlen(filename);
while (pos>0) {
c = ugetat(filename, pos-1);
if ((c == '.') || (c == '/') || (c == OTHER_PATH_SEPARATOR) || (c == DEVICE_SEPARATOR))
break;
pos--;
}
if ((pos>0) && (ugetat(filename, pos-1) == '.'))
return (char *)filename + uoffset(filename, pos);
return (char *)filename + ustrsize(filename);
}
/* put_backslash:
* If the last character of the filename is not a \, /, or #, or a device
* separator (eg. : under DOS), this routine will concatenate a \ or / on
* to it (depending on platform).
*/
void put_backslash(char *filename)
{
int c;
ASSERT(filename);
if (ugetc(filename)) {
c = ugetat(filename, -1);
if ((c == '/') || (c == OTHER_PATH_SEPARATOR) || (c == DEVICE_SEPARATOR) || (c == '#'))
return;
}
filename += ustrsize(filename);
filename += usetc(filename, OTHER_PATH_SEPARATOR);
usetc(filename, 0);
}
/***************************************************
******************* Filesystem ********************
***************************************************/
/* set_filename_encoding:
* Sets the encoding to use for filenames. By default, UTF8 is assumed.
*/
void set_filename_encoding(int encoding)
{
filename_encoding = encoding;
}
/* get_filename_encoding:
* Returns the encoding currently assumed for filenames.
*/
int get_filename_encoding(void)
{
return filename_encoding ;
}
/* file_exists:
* Checks whether a file matching the given name and attributes exists,
* returning non zero if it does. The file attribute may contain any of
* the FA_* constants from dir.h. If aret is not null, it will be set
* to the attributes of the matching file. If an error occurs the system
* error code will be stored in errno.
*/
int file_exists(AL_CONST char *filename, int attrib, int *aret)
{
struct al_ffblk info;
ASSERT(filename);
if (ustrchr(filename, '#')) {
PACKFILE *f = pack_fopen_special_file(filename, F_READ);
if (f) {
pack_fclose(f);
if (aret)
*aret = FA_DAT_FLAGS;
return ((attrib & FA_DAT_FLAGS) == FA_DAT_FLAGS) ? TRUE : FALSE;
}
}
if (!_al_file_isok(filename))
return FALSE;
if (al_findfirst(filename, &info, attrib) != 0) {
/* no entry is not an error for file_exists() */
if (*allegro_errno == ENOENT)
*allegro_errno = 0;
return FALSE;
}
al_findclose(&info);
if (aret)
*aret = info.attrib;
return TRUE;
}
/* exists:
* Shortcut version of file_exists().
*/
int exists(AL_CONST char *filename)
{
ASSERT(filename);
return file_exists(filename, FA_ARCH | FA_RDONLY, NULL);
}
/* file_size_ex:
* Returns the size of a file, in bytes.
* If the file does not exist or an error occurs, it will return zero
* and store the system error code in errno.
*/
uint64_t file_size_ex(AL_CONST char *filename)
{
ASSERT(filename);
if (ustrchr(filename, '#')) {
PACKFILE *f = pack_fopen_special_file(filename, F_READ);
if (f) {
long ret;
ASSERT(f->is_normal_packfile);
ret = f->normal.todo;
pack_fclose(f);
return ret;
}
}
if (!_al_file_isok(filename))
return 0;
return _al_file_size_ex(filename);
}
/* For binary compatibility with 4.2.0. */
long file_size(AL_CONST char *filename)
{
return file_size_ex(filename);
}
/* For binary compatibility with 4.2.0.
* This is an internal symbol and only required because _al_file_size was
* exposed in the Windows DLL.
*/
long _al_file_size(AL_CONST char *filename)
{
return _al_file_size_ex(filename);
}
/* file_time:
* Returns a file time-stamp.
* If the file does not exist or an error occurs, it will return zero
* and store the system error code in errno.
*/
time_t file_time(AL_CONST char *filename)
{
ASSERT(filename);
if (!_al_file_isok(filename))
return 0;
return _al_file_time(filename);
}
/* delete_file:
* Removes a file from the disk.
*/
int delete_file(AL_CONST char *filename)
{
char tmp[1024];
ASSERT(filename);
if (!_al_file_isok(filename))
return -1;
if (_al_unlink(uconvert_tofilename(filename, tmp)) != 0) {
*allegro_errno = errno;
return -1;
}
return 0;
}
/* for_each_file:
* Finds all the files on the disk which match the given wildcard
* specification and file attributes, and executes callback() once for
* each. callback() will be passed three arguments, the first a string
* which contains the completed filename, the second being the attributes
* of the file, and the third an int which is simply a copy of param (you
* can use this for whatever you like). If an error occurs an error code
* will be stored in errno, and callback() can cause for_each_file() to
* abort by setting errno itself. Returns the number of successful calls
* made to callback(). The file attribute may contain any of the FA_*
* flags from dir.h.
*/
int for_each_file(AL_CONST char *name, int attrib, void (*callback)(AL_CONST char *filename, int attrib, int param), int param)
{
char buf[1024];
struct al_ffblk info;
int c = 0;
ASSERT(name);
if (!_al_file_isok(name))
return 0;
if (al_findfirst(name, &info, attrib) != 0) {
/* no entry is not an error for for_each_file() */
if (*allegro_errno == ENOENT)
*allegro_errno = 0;
return 0;
}
*allegro_errno = 0;
do {
replace_filename(buf, name, info.name, sizeof(buf));
(*callback)(buf, info.attrib, param);
if (*allegro_errno) /* evil, evil, evil! */
break;
c++;
} while (al_findnext(&info) == 0);
al_findclose(&info);
/* no entry is not an error for for_each_file() */
if (*allegro_errno == ENOENT)
*allegro_errno = 0;
return c;
}
/* for_each_file_ex:
* Finds all the files on disk which match the given wildcard specification
* and file attributes, and executes callback() once for each. callback()
* will be passed three arguments: the first is a string which contains the
* completed filename, the second is the actual attributes of the file, and
* the third is a void pointer which is simply a copy of param (you can use
* this for whatever you like). It must return 0 to let the enumeration
* proceed, or any non-zero value to stop it. If an error occurs, the error
* code will be stored in errno but the enumeration won't stop. Returns the
* number of successful calls made to callback(), that is the number of
* times callback() was called and returned 0. The file attribute masks may
* contain any of the FA_* flags from dir.h.
*/
int for_each_file_ex(AL_CONST char *name, int in_attrib, int out_attrib, int (*callback)(AL_CONST char *filename, int attrib, void *param), void *param)
{
char buf[1024*2];
struct al_ffblk info;
int ret, c = 0;
ASSERT(name);
if (!_al_file_isok(name))
return 0;
if (al_findfirst(name, &info, ~out_attrib) != 0) {
/* no entry is not an error for for_each_file_ex() */
if (*allegro_errno == ENOENT)
*allegro_errno = 0;
return 0;
}
do {
if ((~info.attrib & in_attrib) == 0) {
// TODO: 'info.name' is the _full path_ of the file (except for . and ..), so `replace_filename` gets confused
// and doubles-up the filename into buf. In practice this doesn't matter because we call `get_filename` in the callback.
// To be safe and account for this silliness, for now the above `buf` has been doubled.
replace_filename(buf, name, info.name, sizeof(buf));
ret = (*callback)(buf, info.attrib, param);
if (ret != 0)
break;
c++;
}
} while (al_findnext(&info) == 0);
al_findclose(&info);
/* no entry is not an error for for_each_file_ex() */
if (*allegro_errno == ENOENT)
*allegro_errno = 0;
return c;
}
/* find_resource:
* Tries lots of different places that a resource file might live.
*/
static int find_resource(char *dest, AL_CONST char *path, AL_CONST char *name, AL_CONST char *datafile, AL_CONST char *objectname, AL_CONST char *subdir, int size)
{
char _name[128], _objectname[128], hash[8];
char tmp[16];
int i;
/* convert from name.ext to name_ext (datafile object name format) */
ustrzcpy(_name, sizeof(_name), name);
for (i=0; i<ustrlen(_name); i++) {
if (ugetat(_name, i) == '.')
usetat(_name, i, '_');
}
if (objectname) {
ustrzcpy(_objectname, sizeof(_objectname), objectname);
for (i=0; i<ustrlen(_objectname); i++) {
if (ugetat(_objectname, i) == '.')
usetat(_objectname, i, '_');
}
}
else
usetc(_objectname, 0);
usetc(hash+usetc(hash, '#'), 0);
/* try path/name */
if (ugetc(name)) {
ustrzcpy(dest, size, path);
ustrzcat(dest, size, name);
if (file_exists(dest, FA_RDONLY | FA_ARCH, NULL))
return 0;
}
/* try path#_name */
if ((ustrchr(path, '#')) && (ugetc(name))) {
ustrzcpy(dest, size, path);
ustrzcat(dest, size, _name);
if (file_exists(dest, FA_RDONLY | FA_ARCH, NULL))
return 0;
}
/* try path/name#_objectname */
if ((ustricmp(get_extension(name), uconvert_ascii("dat", tmp)) == 0) && (objectname)) {
ustrzcpy(dest, size, path);
ustrzcat(dest, size, name);
ustrzcat(dest, size, hash);
ustrzcat(dest, size, _objectname);