-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.c
889 lines (738 loc) · 19.9 KB
/
files.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
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (C) 1995 Ronny Wester
Copyright (C) 2003 Jeremy Chin
Copyright (C) 2003-2007 Lucas Martin-King
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
files.c - file handling functions
Author: $Author: lmartinking $
Rev: $Revision: 250 $
URL: $HeadURL: svn://svn.icculus.org/cdogs-sdl/trunk/src/files.c $
ID: $Id: files.c 250 2007-07-06 16:38:43Z lmartinking $
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef SYS_NDS
#include <dirent.h>
#endif
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <direct.h>
#endif
#include "files.h"
#include "utils.h"
#define MAX_STRING_LEN 1000
#define CAMPAIGN_MAGIC 690304
#define CAMPAIGN_VERSION 6
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
static const int bendian = 1;
#else
static const int bendian = 0;
#endif
void swap32 (void *d)
{
if (bendian) {
*((int *)d) = SDL_Swap32(*((int *)d));
}
}
ssize_t f_read(FILE *f, void *buf, size_t size)
{
return fread(buf, size, 1, f);
}
ssize_t f_read32(FILE *f, void *buf, size_t size)
{
ssize_t ret = 0;
if (buf) {
ret = f_read(f, buf, size);
swap32((int *)buf);
}
return ret;
}
ssize_t f_readarray32(FILE *f, void *buf, size_t size)
{
int i;
debug("%d, %p, %d\n", fileno(f), buf, (int)size);
if (buf) {
for (i = 0; i < (size/4); i++) {
// fprintf(stderr, " i: %d\n", i);
f_read32(f, buf, size);
#ifdef _MSC_VER
(char *)
#endif
buf += sizeof(int);
}
}
return size;
}
void swap16 (void *d)
{
if (bendian) {
*((short int *)d) = SDL_Swap16(*((short int *)d));
}
}
ssize_t f_read16(FILE *f, void *buf, size_t size)
{
ssize_t ret = 0;
if (buf) {
ret = f_read(f, buf, size);
swap16((short int*)buf);
}
return ret;
}
int ScanCampaign(const char *filename, char *title, int *missions)
{
FILE *f;
int i;
TCampaignSetting setting;
debug("f: %s\n", filename);
f = fopen(filename, "rb");
if (f >= 0) {
f_read32(f, &i, sizeof(i));
if (i != CAMPAIGN_MAGIC) {
fclose(f);
debug("Filename: %s\n", filename);
debug("Magic: %d FileM: %d\n", CAMPAIGN_MAGIC, i);
debug("ScanCampaign - bad file!\n");
return CAMPAIGN_BADFILE;
}
f_read32(f, &i, sizeof(i));
if (i != CAMPAIGN_VERSION) {
fclose(f);
debug("ScanCampaign - version mismatch!\n");
return CAMPAIGN_VERSIONMISMATCH;
}
f_read(f, setting.title, sizeof(setting.title));
f_read(f, setting.author, sizeof(setting.author));
f_read(f, setting.description, sizeof(setting.description));
f_read32(f, &setting.missionCount, sizeof(setting.missionCount));
strcpy(title, setting.title);
*missions = setting.missionCount;
fclose(f);
return CAMPAIGN_OK;
}
printf("ScanCampaign - bad path!?\n");
return CAMPAIGN_BADPATH;
}
void load_mission_objective(FILE *f, struct MissionObjective *o)
{
int offset = 0;
f_read(f, o->description, sizeof(o->description)); offset += sizeof(o->description);
f_read32(f, &o->type, sizeof(o->type));
f_read32(f, &o->index, sizeof(o->index));
f_read32(f, &o->count, sizeof(o->count));
f_read32(f, &o->required, sizeof(o->required));
f_read32(f, &o->flags, sizeof(o->flags));
//readarray32(fd, o + offset, 5 * sizeof(int));
debug(" >> Objective: %s data: %d %d %d %d %d\n",
o->description, o->type, o->index, o->count, o->required, o->flags);
}
#define R32(s,e) f_read32(f, &s->e, sizeof(s->e))
void load_mission(FILE *f, struct Mission *m)
{
int i;
int o = 0;
f_read(f, m->title, sizeof(m->title)); o += sizeof(m->title);
f_read(f, m->description, sizeof(m->description)); o += sizeof(m->description);
debug("== MISSION ==\n");
debug("t: %s\n", m->title);
debug("d: %s\n", m->description);
R32(m, wallStyle);
R32(m, floorStyle);
R32(m, roomStyle);
R32(m, exitStyle);
R32(m, keyStyle);
R32(m, doorStyle);
R32(m, mapWidth); R32(m, mapHeight);
R32(m, wallCount); R32(m, wallLength);
R32(m, roomCount);
R32(m, squareCount);
R32(m, exitLeft); R32(m, exitTop); R32(m, exitRight); R32(m, exitBottom);
R32(m, objectiveCount);
debug("number of objectives: %d\n", m->objectiveCount);
for (i = 0; i < OBJECTIVE_MAX; i++) {
load_mission_objective(f, &m->objectives[i]);
}
R32(m, baddieCount);
for (i = 0; i < BADDIE_MAX; i++) {
f_read32(f, &m->baddies[i], sizeof(int));
}
R32(m, specialCount);
for (i = 0; i < SPECIAL_MAX; i++) {
f_read32(f, &m->specials[i], sizeof(int));
}
R32(m, itemCount);
for (i = 0; i < ITEMS_MAX; i++) {
f_read32(f, &m->items[i], sizeof(int));
}
for (i = 0; i < ITEMS_MAX; i++) {
f_read32(f, &m->itemDensity[i], sizeof(int));
}
R32(m, baddieDensity);
R32(m, weaponSelection);
f_read(f, m->song, sizeof(m->song));
f_read(f, m->map, sizeof(m->map));
R32(m, wallRange);
R32(m, floorRange);
R32(m, roomRange);
R32(m, altRange);
debug("number of baddies: %d\n", m->baddieCount);
return;
}
void load_character(FILE *f, TBadGuy *c)
{
R32(c, armedBodyPic);
R32(c, unarmedBodyPic);
R32(c, facePic);
R32(c, speed);
R32(c, probabilityToMove);
R32(c, probabilityToTrack);
R32(c, probabilityToShoot);
R32(c, actionDelay);
R32(c, gun);
R32(c, skinColor);
R32(c, armColor);
R32(c, bodyColor);
R32(c, legColor);
R32(c, hairColor);
R32(c, health);
R32(c, flags);
// readarray32(fd, c, sizeof(TBadGuy));
// fprintf(stderr, " speed: %d gun: %d\n", c->speed, c->gun);
}
int LoadCampaign(const char *filename, TCampaignSetting * setting,
int max_missions, int max_characters)
{
FILE *f;
int i;
debug("f: %s\n", filename);
f = fopen(filename, "rb");
if (f) {
f_read32(f, &i, sizeof(i));
if (i != CAMPAIGN_MAGIC) {
fclose(f);
debug("LoadCampaign - bad file!\n");
return CAMPAIGN_BADFILE;
}
f_read32(f, &i, sizeof(i));
if (i != CAMPAIGN_VERSION) {
fclose(f);
debug("LoadCampaign - version mismatch!\n");
return CAMPAIGN_VERSIONMISMATCH;
}
f_read(f, setting->title, sizeof(setting->title));
f_read(f, setting->author, sizeof(setting->author));
f_read(f, setting->description, sizeof(setting->description));
f_read32(f, &setting->missionCount, sizeof(setting->missionCount));
if (max_missions <= 0) {
i = setting->missionCount * sizeof(struct Mission);
setting->missions = sys_mem_alloc(i);
memset(setting->missions, 0, i);
max_missions = setting->missionCount;
} else if (setting->missionCount < max_missions)
max_missions = setting->missionCount;
fprintf(stderr, "No. missions: %d\n", max_missions);
for (i = 0; i < max_missions; i++) {
load_mission(f, &setting->missions[i]);
}
f_read32(f, &setting->characterCount, sizeof(setting->characterCount));
if (max_characters <= 0) {
i = setting->characterCount * sizeof(TBadGuy);
setting->characters = sys_mem_alloc(i);
memset(setting->characters, 0, i);
max_characters = setting->characterCount;
} else if (setting->characterCount < max_characters)
max_characters = setting->characterCount;
fprintf(stderr, "No. characters: %d\n", max_characters);
for (i = 0; i < max_characters; i++) {
load_character(f, &setting->characters[i]);
}
fclose(f);
return CAMPAIGN_OK;
}
return CAMPAIGN_BADPATH;
}
int SaveCampaign(const char *filename, TCampaignSetting * setting)
{
int f;
int i;
f = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
if (f >= 0) {
i = CAMPAIGN_MAGIC;
write(f, &i, sizeof(i));
i = CAMPAIGN_VERSION;
write(f, &i, sizeof(i));
write(f, setting->title, sizeof(setting->title));
write(f, setting->author, sizeof(setting->author));
write(f, setting->description,
sizeof(setting->description));
write(f, &setting->missionCount,
sizeof(setting->missionCount));
for (i = 0; i < setting->missionCount; i++) {
write(f, &setting->missions[i],
sizeof(struct Mission));
}
write(f, &setting->characterCount,
sizeof(setting->characterCount));
for (i = 0; i < setting->characterCount; i++) {
write(f, &setting->characters[i], sizeof(TBadGuy));
}
//fchmod(f, S_IRUSR | S_IRGRP | S_IROTH);
close(f);
return CAMPAIGN_OK;
}
return CAMPAIGN_BADPATH;
}
static void OutputCString(FILE * f, const char *s, int indentLevel)
{
int length;
for (length = 0; length < indentLevel; length++)
fputc(' ', f);
fputc('\"', f);
while (*s) {
switch (*s) {
case '\"':
case '\\':
fputc('\\', f);
default:
fputc(*s, f);
}
s++;
length++;
if (length > 75) {
fputs("\"\n", f);
length = 0;
for (length = 0; length < indentLevel; length++)
fputc(' ', f);
fputc('\"', f);
}
}
fputc('\"', f);
}
void SaveCampaignAsC(const char *filename, const char *name,
TCampaignSetting * setting)
{
FILE *f;
int i, j;
f = fopen(filename, "w");
if (f) {
fprintf(f, "TBadGuy %s_badguys[ %d] =\n{\n", name,
setting->characterCount);
for (i = 0; i < setting->characterCount; i++) {
fprintf(f,
" {%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x}%s\n",
setting->characters[i].armedBodyPic,
setting->characters[i].unarmedBodyPic,
setting->characters[i].facePic,
setting->characters[i].speed,
setting->characters[i].probabilityToMove,
setting->characters[i].probabilityToTrack,
setting->characters[i].probabilityToShoot,
setting->characters[i].actionDelay,
setting->characters[i].gun,
setting->characters[i].skinColor,
setting->characters[i].armColor,
setting->characters[i].bodyColor,
setting->characters[i].legColor,
setting->characters[i].hairColor,
setting->characters[i].health,
setting->characters[i].flags,
i <
setting->characterCount - 1 ? "," : "");
}
fprintf(f, "};\n\n");
fprintf(f, "struct Mission %s_missions[ %d] =\n{\n", name,
setting->missionCount);
for (i = 0; i < setting->missionCount; i++) {
fprintf(f, " {\n");
OutputCString(f, setting->missions[i].title, 4);
fprintf(f, ",\n");
OutputCString(f, setting->missions[i].description,
4);
fprintf(f, ",\n");
fprintf(f,
" %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
setting->missions[i].wallStyle,
setting->missions[i].floorStyle,
setting->missions[i].roomStyle,
setting->missions[i].exitStyle,
setting->missions[i].keyStyle,
setting->missions[i].doorStyle,
setting->missions[i].mapWidth,
setting->missions[i].mapHeight,
setting->missions[i].wallCount,
setting->missions[i].wallLength,
setting->missions[i].roomCount,
setting->missions[i].squareCount);
fprintf(f, " %d,%d,%d,%d,\n",
setting->missions[i].exitLeft,
setting->missions[i].exitTop,
setting->missions[i].exitRight,
setting->missions[i].exitBottom);
fprintf(f, " %d,\n",
setting->missions[i].objectiveCount);
fprintf(f, " {\n");
for (j = 0; j < OBJECTIVE_MAX; j++) {
fprintf(f, " {\n");
OutputCString(f,
setting->missions[i].
objectives[j].description,
8);
fprintf(f, ",\n");
fprintf(f, " %d,%d,%d,%d,0x%x\n",
setting->missions[i].objectives[j].
type,
setting->missions[i].objectives[j].
index,
setting->missions[i].objectives[j].
count,
setting->missions[i].objectives[j].
required,
setting->missions[i].objectives[j].
flags);
fprintf(f, " }%s\n",
j < OBJECTIVE_MAX - 1 ? "," : "");
}
fprintf(f, " },\n");
fprintf(f, " %d,\n",
setting->missions[i].baddieCount);
fprintf(f, " {");
for (j = 0; j < BADDIE_MAX; j++) {
fprintf(f, "%d%s",
setting->missions[i].baddies[j],
j < BADDIE_MAX - 1 ? "," : "");
}
fprintf(f, "},\n");
fprintf(f, " %d,\n",
setting->missions[i].specialCount);
fprintf(f, " {");
for (j = 0; j < SPECIAL_MAX; j++) {
fprintf(f, "%d%s",
setting->missions[i].specials[j],
j < SPECIAL_MAX - 1 ? "," : "");
}
fprintf(f, "},\n");
fprintf(f, " %d,\n",
setting->missions[i].itemCount);
fprintf(f, " {");
for (j = 0; j < ITEMS_MAX; j++) {
fprintf(f, "%d%s",
setting->missions[i].items[j],
j < ITEMS_MAX - 1 ? "," : "");
}
fprintf(f, "},\n");
fprintf(f, " {");
for (j = 0; j < ITEMS_MAX; j++) {
fprintf(f, "%d%s",
setting->missions[i].
itemDensity[j],
j < ITEMS_MAX - 1 ? "," : "");
}
fprintf(f, "},\n");
fprintf(f, " %d,0x%x,\n",
setting->missions[i].baddieDensity,
setting->missions[i].weaponSelection);
OutputCString(f, setting->missions[i].song, 4);
fprintf(f, ",\n");
OutputCString(f, setting->missions[i].map, 4);
fprintf(f, ",\n");
fprintf(f, " %d,%d,%d,%d\n",
setting->missions[i].wallRange,
setting->missions[i].floorRange,
setting->missions[i].roomRange,
setting->missions[i].altRange);
fprintf(f, " }%s\n",
i < setting->missionCount ? "," : "");
}
fprintf(f, "};\n\n");
fprintf(f, "struct CampaignSetting %s_campaign =\n{\n",
name);
OutputCString(f, setting->title, 2);
fprintf(f, ",\n");
OutputCString(f, setting->author, 2);
fprintf(f, ",\n");
OutputCString(f, setting->description, 2);
fprintf(f, ",\n");
fprintf(f, " %d, %s_missions, %d, %s_badguys\n};\n",
setting->missionCount, name,
setting->characterCount, name);
fclose(f);
}
};
void AddFileEntry(struct FileEntry **list, const char *name,
const char *info, int data)
{
struct FileEntry *entry;
if (strcmp(name, "..") == 0) return;
if (strcmp(name, ".") == 0) return;
while (*list && strcmp((*list)->name, name) < 0)
list = &(*list)->next;
if (strcmp(name, "") == 0)
printf(" -> Adding [builtin]\n");
else
printf(" -> Adding [%s]\n", name);
entry = sys_mem_alloc(sizeof(struct FileEntry));
strcpy(entry->name, name);
strcpy(entry->info, info);
entry->data = data;
entry->next = *list;
*list = entry;
}
struct FileEntry *GetFilesFromDirectory(const char *directory)
{
#ifdef SYS_NDS
return 0;
#else
DIR *dir;
struct dirent *d;
struct FileEntry *list = NULL;
dir = opendir(directory);
if (dir != NULL) {
while ((d = readdir(dir)) != NULL)
AddFileEntry(&list, d->d_name, "", 0);
closedir(dir);
}
return list;
#endif
}
void FreeFileEntries(struct FileEntry *entries)
{
struct FileEntry *tmp;
while (entries) {
tmp = entries;
entries = entries->next;
free(tmp);
}
}
void GetCampaignTitles(struct FileEntry **entries)
{
int i;
struct FileEntry *tmp;
char s[10];
while (*entries) {
if (ScanCampaign(join(GetDataFilePath("missions/"),
(*entries)->name),
(*entries)->info,
&i) == CAMPAIGN_OK) {
sprintf(s, " (%d)", i);
strcat((*entries)->info, s);
entries = &((*entries)->next);
} else // Bad campaign
{
tmp = *entries;
*entries = tmp->next;
free(tmp);
}
}
}
/* GetHomeDirectory ()
*
* Uses environment variables to determine the users home directory.
* returns some sort of path (C string)
*
* It's an ugly piece of sh*t... :/
*/
char *cdogs_homepath = NULL;
char * GetHomeDirectory(void)
{
char *home;
char *user;
char *tmp1; /* excessive buffer */
char *tmp2; /* dynamic buffer */
if (cdogs_homepath == NULL) {
char *p;
p = getenv("CDOGS_CONFIG_DIR");
if (p != NULL && strlen(p) != 0) {
cdogs_homepath = strdup(p);
return cdogs_homepath;
}
home = getenv("HOME");
if (home == NULL || strlen(home) == 0) { /* no HOME var, try to get USER */
user = getenv("USER");
if (user == NULL || strlen(user) == 0) { /* someone has a dodgy shell (or windows) */
fprintf(stderr,"%s%s%s%s",
"##############################################################\n",
"# You don't have the environment variables HOME or USER set. #\n",
"# It is suggested you get a better shell. :D #\n",
"##############################################################\n");
tmp1 = calloc(MAX_STRING_LEN, sizeof(char)); /* lots of chars... */
strcpy(tmp1, "/tmp/unknown.user/");
tmp2 = calloc(strlen(tmp1)+1, 1);
strcpy(tmp2, tmp1);
free(tmp1);
cdogs_homepath = tmp2;
return cdogs_homepath;
} else { /* hopefully they have USER set... */
tmp1 = calloc(MAX_STRING_LEN, 1); /* lots of chars... */
strcpy(tmp1, "/home/");
strcat(tmp1, user);
strcat(tmp1, "/");
tmp2 = calloc(strlen(tmp1)+1, 1);
strcpy(tmp2, tmp1);
free(tmp1);
//setenv("HOME", tmp1, 0); /* set the HOME var anyway :) */
cdogs_homepath = tmp2;
return cdogs_homepath;
}
}
tmp1 = calloc(MAX_STRING_LEN, sizeof(char)); /* lots of chars... */
strcpy(tmp1, home);
strcat(tmp1, "/");
tmp2 = calloc(strlen(tmp1)+1, 1);
strcpy(tmp2, tmp1);
free(tmp1);
cdogs_homepath = tmp2;
}
return cdogs_homepath;
}
#if 0
int IsWritable(char *name)
{
struct stat s;
int tmp;
int ret;
tmp=stat(name, &s);
if (tmp == -1 && errno == ENOENT) {
printf("Error: '%s' does not exist\n", name);
ret = FS_OBJ_NEXIST;
} else if (ret == 1) {
uid_t u;
u = getuid();
/* Check if it's a file and rw */
if (((s.s_mode & S_IFMT) == S_IFREG) &&
)
}
return ret;
}
#endif
/* GetDataFilePath()
*
* returns a full path to a data file...
*
* XXX: Use default dir as fallback?
*/
static char *data_path = NULL;
static char data_pbuf[255];
char * GetDataFilePath(const char *path)
{
if (!data_path) {
char *tmp;
if ((tmp = getenv("CDOGS_DATA_DIR")) != NULL && strlen(tmp) != 0) {
data_path = strdup(tmp);
} else {
tmp = calloc(strlen(CDOGS_DATA_DIR)+strlen(path)+1,sizeof(char));
strcpy(tmp, CDOGS_DATA_DIR);
strcat(tmp, path);
data_path = strdup(tmp);
free(tmp);
}
}
strcpy(data_pbuf, data_path);
strcat(data_pbuf, "/");
strcat(data_pbuf, path);
return data_pbuf;
}
char * join(const char *s1, const char *s2)
{
char *tmp;
tmp = calloc(strlen(s1)+strlen(s2)+1,sizeof(char));
strcpy(tmp, s1);
strcat(tmp, s2);
return tmp;
}
/* GetConfigFilePath()
*
* returns a full path to a data file...
*/
char cfpath[255];
char * GetConfigFilePath(const char *name)
{
char *tmp;
char *homedir;
#if !defined (SYS_WIN) && SYS_PSP && SYS_NDS
homedir = GetHomeDirectory();
#else
homedir = "";
#endif
//tmp = calloc(strlen(homedir) + strlen(name) + strlen(CDOGS_CFG_DIR) + 1, sizeof(char));
strcpy(cfpath, homedir);
strcat(cfpath, CDOGS_CFG_DIR);
strcat(cfpath, name);
return cfpath;
}
#ifdef SYS_WIN
#define mkdir(p, a) mkdir(p)
#endif
#ifdef _MSC_VER
typedef int mode_t;
#define mkdir(p, a) _mkdir(p)
#endif
int mkdir_deep(const char *path, mode_t m)
{
int i;
char part[255];
debug("path: %s\n", path);
for (i = 0; i < strlen(path); i++) {
if (path[i] == '\0') break;
if (path[i] == '/') {
strncpy(part, path, i + 1);
part[i+1] = '\0';
if (mkdir(part, m) == -1) {
if (errno == EEXIST) continue;
else return 1;
}
}
}
return 0;
}
void SetupConfigDir(void)
{
char *cfg_p = GetConfigFilePath("");
printf("Creating Config dir... ");
#ifdef _MSC_VER
#define S_IRUSR 0
#define S_IXUSR 0
#define S_IWUSR 0
#endif
if (mkdir_deep(cfg_p, S_IRUSR | S_IXUSR | S_IWUSR) == 0) {
if (errno != EEXIST)
printf("Config dir created.\n");
else
printf("No need. Already exists!\n");
} else {
switch (errno) {
case EACCES:
printf("Permission denied!\n");
break;
default:
printf("Error! errno is %d\n", errno);
}
}
return;
}
#ifdef _MSC_VER
#endif
char dir_buf[250];
char * GetPWD(void)
{
getcwd(dir_buf, 250);
return dir_buf;
}