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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
/* XDG Base Directory Specifications support library. */
/* Implements equivalent cross platform functionality too. */
/*************************************************************************
Copyright 2011, 2013 Graeme W. Gill
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*************************************************************************/
/*
This function provides support for the XDG Base Directory Specifications
in a cross platform compatible way.
[ Note that for MSWin each path in a set is separated by a ';' character.
and that DATA and CONF will be in the same directory. ]
The following paths are used for each of the 5 XDG concepts, listed in order
of priority:
Per user application related data.
Per user application configuration settings.
Per user application cache storage area.
Local system wide application related data.
Local system wide application configuration settings.
Unix:
$XDG_DATA_HOME
$HOME/.local/share
$XDG_CONF_HOME
$HOME/.config
$XDG_CACHE_HOME
$HOME/.cache
$XDG_DATA_DIRS
/usr/local/share:/usr/share
$XDG_CONF_DIRS
/etc/xdg
OS X:
$XDG_DATA_HOME
$HOME/Library/Application Support
$XDG_CONF_HOME
$HOME/Library/Preferences
$XDG_CACHE_HOME
$HOME/Library/Caches
$XDG_DATA_DIRS
/Library/Application Support
$XDG_CONF_DIRS
/Library/Preferences
MSWin:
$XDG_DATA_HOME
$APPDATA
$HOME/.local/share
$XDG_CONF_HOME
$APPDATA
$HOME/.config
$XDG_CACHE_HOME
$APPDATA/Cache
$HOME/.cache
$XDG_DATA_DIRS
$ALLUSERSPROFILE
$XDG_CONF_DIRS
$ALLUSERSPROFILE
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#ifndef NT
# include <unistd.h>
# include <glob.h>
#else
# include <io.h>
# include <direct.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "numsup.h"
#include "conv.h"
#include "aglob.h"
#include "xdg_bds.h"
#undef DEBUG
#ifdef DEBUG
#define DBGA g_log, 0
#define DBG(xxx) a1logd xxx ;
#else
#define DBG(xxx)
#endif /* DEBUG */
#ifdef NT
# define stat _stat
# define mode_t int
# define mkdir(A,B) _mkdir(A)
# define mputenv _putenv
# define unlink _unlink
# define rmdir _rmdir
#else
/* UNIX putenv is a pain.. */
static void mputenv(char *ss) {
int ll = strlen(ss);
ss = strdup(ss);
if (ll > 0 && ss[ll-1]== '=') {
ss[ll-1] = '\000';
unsetenv(ss);
} else {
putenv(ss);
}
}
#endif
/* Allocate a copy of the string, and normalize the */
/* path separator to '/' */
/* Append a string. Free in. Return NULL on error. */
static char *append(char *in, char *app) {
char *rv;
if ((rv = malloc(strlen(in) + strlen(app) + 1)) == NULL) {
a1loge(g_log, 1, "xdg_bds: append malloc failed\n");
free(in);
return NULL;
}
strcpy(rv, in);
strcat(rv, app);
free(in);
return rv;
}
/* Append a ':' or ';' then a string. Free in. Return NULL on error. */
static char *cappend(char *in, char *app) {
int inlen;
char *rv;
inlen = strlen(in);
if ((rv = malloc(inlen + 1 + strlen(app) + 1)) == NULL) {
a1loge(g_log, 1, "xdg_bds: cappend malloc failed\n");
free(in);
return NULL;
}
strcpy(rv, in);
if (inlen > 1)
strcat(rv, SSEPS);
strcat(rv, app);
free(in);
return rv;
}
/* Append a '/' then a string. Free in. Return NULL on error. */
static char *dappend(char *in, char *app) {
int inlen;
char *rv;
inlen = strlen(in);
if ((rv = malloc(inlen + 1 + strlen(app) + 1)) == NULL) {
a1loge(g_log, 1, "xdg_bds: dappend malloc failed\n");
free(in);
return NULL;
}
strcpy(rv, in);
if (inlen > 1 && in[inlen-1] != '/')
strcat(rv, "/");
strcat(rv, app);
free(in);
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Do internal cleanup */
static void xdg_ifree(char ***paths, char **fnames, int nopaths) {
int i;
if (paths != NULL) {
if (*paths != NULL) {
for (i = 0; i < nopaths; i++) {
if ((*paths)[i] != NULL)
free ((*paths)[i]);
}
}
free(*paths);
*paths = NULL;
}
if (fnames != NULL) {
for (i = 0; i < nopaths; i++) {
if (fnames[i] != NULL)
free (fnames[i]);
}
free(fnames);
}
}
/* Free a return value */
void xdg_free(char **paths, int nopaths) {
int i;
if (paths != NULL) {
for (i = 0; i < nopaths; i++) {
if (paths[i] != NULL)
free (paths[i]);
}
free(paths);
}
}
/* Return the number of matching full paths to the given subpath for the */
/* type of storage and access required. Return 0 if there is an error. */
/* The files are always unique (ie. the first match to a given filename */
/* in the possible XDG list of directories is returned, and files with */
/* the same name in other XDG directories are ignored) */
/* Wildcards should not be used for xdg_write. */
/* The list should be free'd using xdg_free() after use. */
/* XDG environment variables and the subpath are assumed to be using */
/* the '/' path separator. Multiple read paths are separated by SSEP */
/* When "xdg_write", the necessary path to the file will be created. */
/* If we're running as sudo and are creating a user dir/file, */
/* we drop to using the underlying SUDO_UID/GID. If we are creating a */
/* local system dir/file as sudo and have dropped to the SUDO_UID/GID, */
/* then revert back to root uid/gid. */
int xdg_bds(
xdg_error *er, /* Return an error code */
char ***paths, /* Retun array pointers to paths */
xdg_storage_type st, /* Specify the storage type */
xdg_op_type op, /* Operation type */
xdg_scope sc, /* Scope if write */
char *pfname /* Sub-path and file name(s) */
) {
char *path = NULL; /* Directory paths to search, separated by ':' or ';' */
char **fnames = NULL; /* Filename component of each path being returned */
int npaths = 0; /* Number of paths being returned */
int napaths = 0; /* Number of paths allocated */
DBG((DBGA,"xdg_bds called with st %s, op %s, sc %s, pfnames '%s'\n",
st == xdg_data ? "data" : st == xdg_conf ? "config" : st == xdg_cache ? "cache" : "unknown",
op == xdg_write ? "write" : op == xdg_read ? "read" : "unknown",
sc == xdg_user ? "user" : sc == xdg_local ? "local" : "unknown",
pfname))
*paths = NULL;
/* Initial, empty path */
if ((path = strdup("")) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
xdg_ifree(paths, fnames, npaths);
return 0;
}
/* Create a set of ':'/';' separated search paths */
/* User scope */
if (op == xdg_read || sc == xdg_user) {
if (st == xdg_data) {
char *xdg, *home;
if ((xdg = getenv("XDG_DATA_HOME")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
} else if (getenv("HOME") == NULL && (xdg = getenv("APPDATA")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#endif
} else {
if ((home = getenv("HOME")) == NULL
#ifdef NT
&& (home = getenv("APPDATA")) == NULL
#endif
) {
if (er != NULL) *er = xdg_nohome;
free(path);
DBG((DBGA,"no $HOME\n"))
return 0;
}
if ((path = cappend(path, home)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
if (getenv("HOME") != NULL)
path = dappend(path, ".local/share");
#else
#ifdef __APPLE__
path = dappend(path, "Library/Application Support");
#else /* Unix, Default */
path = dappend(path, ".local/share");
#endif
#endif
if (path == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
}
} else if (st == xdg_conf) {
char *xdg, *home;
if ((xdg = getenv("XDG_CONF_HOME")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
} else if (getenv("HOME") == NULL && (xdg = getenv("APPDATA")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#endif
} else {
if ((home = getenv("HOME")) == NULL
#ifdef NT
&& (home = getenv("APPDATA")) == NULL
#endif
) {
if (er != NULL) *er = xdg_nohome;
free(path);
DBG((DBGA,"no $HOME\n"))
return 0;
}
if ((path = cappend(path, home)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
if (getenv("HOME") != NULL)
path = dappend(path, ".config");
#else
#ifdef __APPLE__
path = dappend(path, "Library/Preferences");
#else /* Unix, Default */
path = dappend(path, ".config");
#endif
#endif
if (path == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
}
} else if (st == xdg_cache) {
char *xdg, *home;
if ((xdg = getenv("XDG_CACHE_HOME")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
} else if (getenv("HOME") == NULL && (xdg = getenv("APPDATA")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
if ((path = dappend(path, "Cache")) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#endif
} else {
if ((home = getenv("HOME")) == NULL
#ifdef NT
&& (home = getenv("APPDATA")) == NULL
#endif
) {
if (er != NULL) *er = xdg_nohome;
free(path);
DBG((DBGA,"no $HOME\n"))
return 0;
}
if ((path = cappend(path, home)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
#ifdef NT
if (getenv("HOME") != NULL)
path = dappend(path, ".cache");
else
path = dappend(path, "Cache");
#else
#ifdef __APPLE__
path = dappend(path, "Library/Caches");
#else /* Unix, Default */
path = dappend(path, ".cache");
#endif
#endif
if (path == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
}
}
}
/* Local system scope */
if (op == xdg_read || sc == xdg_local) {
char *xdg;
if (st == xdg_data) {
if ((xdg = getenv("XDG_DATA_DIRS")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
} else {
#ifdef NT
/*
QT uses $COMMON_APPDATA expected to be
C:\Documents and Settings\All Users\Application Data\
while others use $CommonAppData.
Both seem poorly supported,
*/
char *home;
if ((home = getenv("ALLUSERSPROFILE")) == NULL
) {
if (er != NULL) *er = xdg_noalluserprofile;
free(path);
DBG((DBGA,"no $ALLUSERSPROFILE\n"))
return 0;
}
path = cappend(path, home);
#else
#ifdef __APPLE__
path = cappend(path, "/Library");
#else
path = cappend(path, "/usr/local/share:/usr/share");
#endif
#endif
if (path == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
}
} else if (st == xdg_conf) {
if ((xdg = getenv("XDG_CONF_DIRS")) != NULL) {
if ((path = cappend(path, xdg)) == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
} else {
#ifdef NT
char *home;
if ((home = getenv("ALLUSERSPROFILE")) == NULL
) {
if (er != NULL) *er = xdg_noalluserprofile;
free(path);
DBG((DBGA,"no $ALLUSERSPROFILE\n"))
return 0;
}
path = cappend(path, home);
#else
#ifdef __APPLE__
path = cappend(path, "/Library/Preferences");
#else
path = cappend(path, "/etc/xdg");
#endif
#endif
if (path == NULL) {
if (er != NULL) *er = xdg_alloc;
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
return 0;
}
}
}
}
#ifdef NT
/* Replace all backslashes with forward slashes */
{
char *cp;
for (cp = path; *cp != '\000'; cp++) {
if (*cp == '\\')
*cp = '/';
}
}
#endif
DBG((DBGA,"Paths to search '%s'\n",path));
/* Hmm. */
if (strlen(path) == 0) {
free(path);
if (er != NULL) *er = xdg_nopath;
*paths = NULL;
return 0;
}
{
char *spath = NULL; /* sub path out of paths */
char *cp, *ep;
/* For each search path */
for (cp = path; *cp != '\000';) {
char *sname = NULL; /* sub name out of pfnames */
char *ncp, *nep;
/* Copy search path */
if ((ep = strchr(cp, SSEP)) == NULL)
ep = cp + strlen(cp);
if ((ep - cp) == 0) {
free(path);
if (er != NULL) *er = xdg_mallformed;
xdg_ifree(paths, fnames, npaths);
return 0;
}
if ((spath = (char *)malloc(ep - cp + 1)) == NULL) {
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
free(path);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
memmove(spath, cp, ep - cp);
spath[ep - cp] = '\000';
/* For each filename part */
for (ncp = pfname; *ncp != '\000';) {
int rlen = 0; /* Number of chars of search path up to subpath & filename */
char *pp;
char *schpath; /* Path to search */
/* Copy filename path */
if ((nep = strchr(ncp, SSEP)) == NULL)
nep = ncp + strlen(ncp);
if ((nep - ncp) == 0) {
free(spath);
free(path);
if (er != NULL) *er = xdg_mallformed;
xdg_ifree(paths, fnames, npaths);
return 0;
}
if ((sname = (char *)malloc(nep - ncp + 1)) == NULL) {
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
free(spath);
free(path);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
memmove(sname, ncp, nep - ncp);
sname[nep - ncp] = '\000';
/* append subpath & subname */
if ((schpath = strdup(spath)) == NULL
|| (schpath = dappend(schpath, sname)) == NULL) {
free(sname);
free(spath);
free(path);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
DBG((DBGA,"Full path to check '%s'\n",schpath));
/* Figure out where the filename starts */
if ((pp = strrchr(schpath, '/')) == NULL)
rlen = 0;
else
rlen = pp - schpath + 1;
if (op == xdg_read) {
char *fpath; /* Full path of matched */
aglob gg; /* Glob structure */
/* Setup the file glob */
if (aglob_create(&gg, schpath)) {
free(schpath);
free(sname);
free(spath);
free(path);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
/* While we have matching filenames */
DBG((DBGA,"Getting glob results for '%s'\n",schpath))
free(schpath);
for (;;) {
int i;
if ((fpath = aglob_next(&gg)) == NULL) {
if (gg.merr) { /* Malloc error */
free(sname);
free(spath);
free(path);
aglob_cleanup(&gg);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
break; /* No more matches */
}
DBG((DBGA,"Found match with '%s'\n",fpath))
/* Check that this one hasn't already been found */
/* in a different search directory */
for (i = 0; i < npaths; i++) {
if (strcmp(fpath + rlen, fnames[i]) == 0) {
/* Already been found earlier - ignore it */
break;
}
}
if (i < npaths) {
free(fpath);
DBG((DBGA,"Ignoring it because it's already in list\n"))
continue; /* Ignore it */
}
/* Found a file, so append it to the list */
if (npaths >= napaths) { /* Need more space in arrays */
napaths = napaths * 2 + 1;
if ((*paths = realloc(*paths, sizeof(char *) * napaths)) == NULL
|| (fnames = realloc(fnames, sizeof(char *) * napaths)) == NULL) {
a1loge(g_log, 1, "xdg_bds: realloc failed\n");
free(fpath);
free(sname);
free(spath);
free(path);
aglob_cleanup(&gg);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
}
if (((*paths)[npaths] = strdup(fpath)) == NULL) {
a1loge(g_log, 1, "xdg_bds: strdup failed\n");
free(fpath);
free(sname);
free(spath);
free(path);
aglob_cleanup(&gg);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
/* The non-searchpath part of the name found */
if ((fnames[npaths] = strdup(fpath + rlen)) == NULL) {
a1loge(g_log, 1, "xdg_bds: strdup failed\n");
free((*paths)[npaths]);
free(fpath);
free(sname);
free(spath);
free(path);
aglob_cleanup(&gg);
if (er != NULL) *er = xdg_alloc;
xdg_ifree(paths, fnames, npaths);
return 0;
}
free(fpath);
fpath = NULL;
npaths++;
}
aglob_cleanup(&gg);
/* Fall through to next search path */
} else { /* op == xdg_write */
char *pp = schpath;
struct stat sbuf;
mode_t mode = 0700; /* Default directory mode */
if (sc == xdg_user)
mode = 0700; /* Default directory mode for user */
else
mode = 0755; /* Default directory mode local system shared */
#ifndef NT
/* If we're creating a user dir/file and running as root sudo */
if (sc == xdg_user && geteuid() == 0) {
char *uids, *gids;
int uid, gid;
DBG((DBGA,"We're setting a user dir/file running as root\n"))
if ((uids = getenv("SUDO_UID")) != NULL
&& (gids = getenv("SUDO_GID")) != NULL) {
uid = atoi(uids);
gid = atoi(gids);
if (setegid(gid) || seteuid(uid)) {
DBG((DBGA,"seteuid or setegid failed\n"))
} else {
DBG((DBGA,"Set euid %d and egid %d\n",uid,gid))
}
}
/* If setting local system dir/file and not effective root, but sudo */
} else if (sc == xdg_local && getuid() == 0 && geteuid() != 0) {
if (getenv("SUDO_UID") != NULL
&& getenv("SUDO_GID") != NULL) {
DBG((DBGA,"We're setting a local system dir/file with uid = 0 && euid != 0\n"))
setegid(getgid());
seteuid(getuid());
DBG((DBGA,"Set euid %d, egid %d\n",geteuid(),getegid()))
}
}
#endif /* !NT */
#ifdef NT
if (*pp != '\000' /* Skip drive number */
&& ((*pp >= 'a' && *pp <= 'z') || (*pp >= 'A' && *pp <= 'Z'))
&& pp[1] == ':')
pp += 2;
#endif
if (*pp == '/')
pp++; /* Skip root directory */
/* Check each directory in hierarchy, and */
/* create it if it doesn't exist. */
DBG((DBGA,"About to check & create whole path '%s'\n",schpath))
for (;pp != NULL && *pp != '\000';) {
if ((pp = strchr(pp, '/')) != NULL) {
*pp = '\000';
DBG((DBGA,"Checking path '%s'\n",schpath))
if (stat(schpath,&sbuf) != 0) {
/* Doesn't exist */
DBG((DBGA,"Path '%s' doesn't exist - creating it\n",schpath))
if (mkdir(schpath, mode) != 0) {
DBG((DBGA,"mkdir failed - giving up on this one\n"))
break;
}
} else {
mode = sbuf.st_mode;
}
*pp = '/';
pp++;
}
}
/* If we got to the end of the hierarchy, */
/* then the path looks good to write to, */
/* so create a list of one and we're done */
if (pp == NULL || *pp == '\000') {
if ((*paths = malloc(sizeof(char *))) == NULL) {
a1loge(g_log, 1, "xdg_bds: malloc failed\n");
free(schpath);
free(sname);
free(spath);
free(path);
if (er != NULL) *er = xdg_alloc;
return 0;
}
if (((*paths)[npaths] = schpath) == NULL) {
free(sname);
free(spath);
free(path);
if (er != NULL) *er = xdg_alloc;
free(*paths);
return 0;
}
npaths++;
DBG((DBGA,"Returning 0: '%s'\n",(*paths)[0]))
free(sname);
free(spath);
free(path);
return npaths;
}
}
/* Move on to the next name part */
free(sname); sname = NULL;
if (*nep == SSEP)
ncp = nep+1;
else
ncp = nep;
}
/* Move on to the next search path */
free(spath); spath = NULL;
if (*ep == SSEP)
cp = ep+1;
else
cp = ep;
}
}
/* We're done looking through search paths */
free(path);
if (npaths == 0) { /* Didn't find anything */
if (er != NULL) *er = xdg_nopath;
xdg_ifree(paths, fnames, npaths);
} else {
xdg_ifree(NULL, fnames, npaths);
#ifdef DEBUG
{
int i;
a1logd(DBGA,"Returning list\n");
for (i = 0; i < npaths; i++)
a1logd(DBGA," %d: '%s'\n",i,(*paths)[i]);
}
#endif
}
return npaths;
}
/* Return a string corresponding to the error value */
char *xdg_errstr(xdg_error er) {
switch (er) {
case xdg_ok:
return "OK";
case xdg_alloc:
return "memory allocation failed";
case xdg_nohome:
return "There is no $HOME";
case xdg_noalluserprofile:
return "Theres no $ALLUSERSPROFILE is no $ALLUSERSPROFILE";
case xdg_nopath:
return "There is no resulting path";
case xdg_mallformed:
return "Malfomed path fount";
default:
return "unknown";
}
}
/* ---------------------------------------------------------------- */
#ifdef STANDALONE_TEST
/* test code */
/* Return nz on error */
static int touch(char *name) {
FILE *fp;
if ((fp = fopen(name,"w")) == NULL)
return 1;
if (fclose(fp))
return 1;
return 0;
}
/* Check a file can be opened */
/* Return nz on error */
static int check(char *name) {
FILE *fp;
if ((fp = fopen(name,"r")) == NULL)
return 1;
if (fclose(fp))
return 1;
return 0;
}
/* Delete a path and a file */
/* Return nz on error */
static int delpath(char *path, int depth) {
int i;
char *pp;
for (i = 0; i < depth;) {
// a1logd(DBGA,"deleting '%s'\n",path);
if (i == 0) {
if (unlink(path)) {
// a1logd(DBGA,"unlink '%s' failed\n",path);
return 1;
}
} else {
if (rmdir(path)) {
// a1logd(DBGA,"rmdir '%s' failed\n",path);
return 1;
}
}
i++;
if (i == depth)
return 0;
if ((pp = strrchr(path, '/')) == NULL)
return 0;
*pp = '\000';
}
return 0;
}
/* Run a test */
static int runtest(
xdg_storage_type st, /* Specify the storage type */
xdg_scope sc, /* Scope if write */
char *pfname, /* Sub-path and file name */
char *env, /* Environment variable being set */
char *envv, /* Value to set it to */
char *defv, /* default variable needed for read */
int depth /* Cleanup depth */
) {
xdg_error er;
char *xval;
char **paths;
int nopaths;
char buf[200];
if ((xval = getenv(env)) != NULL) /* Save value before mods */
xval = strdup(xval);
if (*env != '\000') { /* If it is to be set */
sprintf(buf, "%s=%s",env,envv);
mputenv(buf);
}
printf("\nTesting Variable %s\n",env);
if ((nopaths = xdg_bds(&er, &paths, st, xdg_write, sc, pfname)) == 0) {
printf("Write test failed with %s\n",xdg_errstr(er));
return 1;
}
printf("Create %s %s returned '%s'\n",
st == xdg_data ? "Data" : st == xdg_data ? "Conf" : "Cache",
sc == xdg_data ? "User" : "Local", paths[0]);
if (touch(paths[0])) {
printf("Creating file %s failed\n",paths[0]);
return 1;
}
if (check(paths[0])) {
printf("Checking file %s failed\n",paths[0]);
return 1;
}
if (sc == xdg_local && *env != '\000') { /* Add another path */
sprintf(buf, "%s=xdgtestXXX%c%s",env,SSEP,envv);
mputenv(buf);
}
if (defv != NULL) {
sprintf(buf, "%s=xdg_NOT_%s",defv,defv);
mputenv(buf);
}
xdg_free(paths, nopaths);
if ((nopaths = xdg_bds(&er, &paths, st, xdg_read, sc, pfname)) < 1) {
printf("Read test failed with %s\n",xdg_errstr(er));
return 1;
}
printf(" Read %s %s returned '%s'\n",
st == xdg_data ? "Data" : st == xdg_data ? "Conf" : "Cache",
sc == xdg_data ? "User" : "Local",paths[0]);
if (check(paths[0])) {
printf("Checking file %s failed\n",paths[0]);
return 1;
}
if (delpath(paths[0], depth)) {
printf("Warning: Deleting file %s failed\n",paths[0]);
}
xdg_free(paths, nopaths);
/* Restore variables value */
if (xval == NULL)
sprintf(buf, "%s=",env);
else
sprintf(buf, "%s=%s",env,xval);
mputenv(buf);
if (defv != NULL) {
sprintf(buf, "%s=",defv);
mputenv(buf);
}
return 0;
}
typedef struct {
xdg_storage_type st; /* Storage type */
xdg_scope sc; /* Scope if write */
char *defv[2]; /* Default variables needed for user & local tests on read */
char *envn[10]; /* Environment variable name to set */
} testcase;
int
main() {
char buf1[200], buf2[200];
int i, j;
#ifdef NT
testcase cases[5] = {
{ xdg_data, xdg_user, {"ALLUSERSPROFILE", NULL},
{ "XDG_DATA_HOME", "APPDATA", "HOME", "APPDATA", NULL } },
{ xdg_conf, xdg_user, {"ALLUSERSPROFILE", NULL},
{ "XDG_CONF_HOME", "APPDATA", "HOME", "APPDATA", NULL } },
{ xdg_cache, xdg_user, {NULL, NULL},
{ "XDG_CACHE_HOME", "APPDATA", "HOME", "APPDATA", NULL } },
{ xdg_data, xdg_local, {NULL, "HOME"},
{ "XDG_DATA_DIRS", "ALLUSERSPROFILE", NULL } },
{ xdg_conf, xdg_local, {NULL, "HOME"},
{ "XDG_CONF_DIRS", "ALLUSERSPROFILE", NULL } }
};
#else /* Apple, Unix, Default */
testcase cases[5] = {
{ xdg_data, xdg_user, {NULL, NULL},
{ "XDG_DATA_HOME", "HOME", NULL } },
{ xdg_conf, xdg_user, {NULL, NULL},
{ "XDG_CONF_HOME", "HOME", NULL } },
{ xdg_cache, xdg_user, {NULL, NULL},
{ "XDG_CACHE_HOME", "HOME", NULL } },
{ xdg_data, xdg_local, {NULL, "HOME"},
{ "XDG_DATA_DIRS", "", NULL } },
{ xdg_conf, xdg_local, {NULL, "HOME"},
{ "XDG_CONF_DIRS", "", NULL } }
};
#endif
/* First clear all the environment variables */
for (i = 0; i < 5; i++) {
for (j = 0; ;j++) {
if (cases[i].envn[j] == NULL)
break;
sprintf(buf1, "%s=",cases[i].envn[j]);
mputenv(buf1);
}
}
/* Then run all the tests */
for (i = 0; i < 5; i++) {
for (j = 0; ;j++) {
if (cases[i].envn[j] == NULL)
break;
sprintf(buf1, "xdgtest%d",i);
sprintf(buf2, "application/%s",cases[i].st == xdg_data ? "data" :
cases[i].st == xdg_conf ? "config" : "cache");
if (runtest(cases[i].st, cases[i].sc, buf2, cases[i].envn[j],buf1,
cases[i].defv[cases[i].sc == xdg_user ? 0 : 1],9))
exit(1);
}
}
printf("Test completed OK\n");
return 0;
}
#endif /* STANDALONE_TEST */
|