-
Notifications
You must be signed in to change notification settings - Fork 0
/
prop.c
1558 lines (1161 loc) · 36.6 KB
/
prop.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
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
*
* 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, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <raf@raf.org>
*/
/*
=head1 NAME
I<libslack(prop)> - program properties file module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/prop.h>
const char *prop_get(const char *name);
const char *prop_get_or(const char *name, const char *default_value);
const char *prop_set(const char *name, const char *value);
int prop_get_int(const char *name);
int prop_get_int_or(const char *name, int default_value);
int prop_set_int(const char *name, int value);
double prop_get_double(const char *name);
double prop_get_double_or(const char *name, double default_value);
double prop_set_double(const char *name, double value);
int prop_get_bool(const char *name);
int prop_get_bool_or(const char *name, int default_value);
int prop_set_bool(const char *name, int value);
int prop_unset(const char *name);
int prop_save(void);
int prop_clear(void);
int prop_locker(Locker *locker);
=head1 DESCRIPTION
This module provides support for system-wide and user-specific (generic and
program-specific) properties in "well-known" locations:
/etc/properties/app - system-wide, generic properties
~/.properties/app - user-defined, generic properties
/etc/properties/app.progname - system-wide, program-specific properties
~/.properties/app.progname - user-defined, program-specific properties
Note that, depending on how I<libslack> was installed, the system-wide
C</etc/properties> directory might not be under C</etc>. It might be under
another directory, such as C</usr/local/etc> or C</opt/local/etc>. If you
aren't sure where it is expected, run C<libslack-config --cflags> and look
for the definition of C<ETC_DIR>.
When the client first requests, sets, or unsets a property, all properties
relevant to the current program are loaded from these files in the order
given above. This order ensures that program-specific properties override
generic properties and that user-defined properties override system-wide
properties. The client can change properties at runtime and save the current
properties back to disk (to the user-defined, program-specific properties
file).
Program names (as returned by I<prog_name(3)>) are converted into file name
suffixes by replacing every occurrence of the file path separator (C<'/'>)
with a C<'-'>. Properties files consist of one property per line.
Each property is specified by its name, followed by C<'='> followed by its
value. The name must not have a C<'='> in it unless it is quoted with a
preceding C<'\'>. Special characters are expressed in I<C> string literal
notation (e.g. C<\a\b\f\n\r\t\v\x1b>). Spaces immediately before and after
the C<'='> are stripped unless they are quoted with a preceding C<'\'>. The
properties files may also contain blank lines and comments (C<'#'> until the
end of the line).
Boolean property values can be expressed as C<0> or C<1>, C<true> or
C<false>, C<yes> or C<no>, C<on> or C<off> (case insensitive).
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#include "config.h"
#include "std.h"
#include <pwd.h>
#include <sys/stat.h>
#include "prog.h"
#include "daemon.h"
#include "map.h"
#include "err.h"
#include "lim.h"
#include "mem.h"
#include "prop.h"
#include "str.h"
#include "locker.h"
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
#ifndef isodigit
#define isodigit(c) (isdigit((int)(unsigned char)c) && (unsigned char)(c) < '8')
#endif
#ifndef is_space
#define is_space(c) isspace((int)(unsigned char)(c))
#endif
typedef struct Prop Prop;
struct Prop
{
Map *map;
Prop *defaults;
};
#ifndef TEST
static struct
{
int init;
Prop *prop;
char *home;
int dirty;
Locker *locker;
}
g = { 0, NULL, NULL, 0, NULL };
/*
C<Prop *prop_create(Map *map, Prop *defaults)>
Creates and returns a I<Prop> containing C<map> and C<defaults>. On error,
returns C<null> with C<errno> set appropriately. The new I<Prop> will
destroy C<map> and C<defaults> when it is destroyed.
*/
static Prop *prop_create(Map *map, Prop *defaults)
{
Prop *prop;
if (!(prop = mem_new(Prop)))
return NULL;
prop->map = map;
prop->defaults = defaults;
return prop;
}
/*
C<void prop_release(Prop *prop)>
Releases (deallocates) C<prop>.
*/
static void prop_release(Prop *prop)
{
if (!prop)
return;
map_release(prop->map);
prop_release(prop->defaults);
mem_release(prop);
}
/*
C<int key_cmp(const char **a, const char **b)>
Compares the two strings pointed to by C<a> and C<b> using I<strcmp(3)>.
*/
static int key_cmp(const char **a, const char **b)
{
return strcmp(*a, *b);
}
static const char *special_code = "abfnrtv";
static const char *special_char = "\a\b\f\n\r\t\v";
static const char *eq = "=";
/*
C<String *quote_special(const char *src)>
Replaces every occurrence in C<src> of special characters (represented in
I<C> by C<"\a">, C<"\b">, C<"\f">, C<"\n">, C<"\r">, C<"\t">, C<"\v">) with
their corresponding I<C> representation. Other non-printable characters are
replaced with their I<ASCII> codes in hexadecimal (i.e. "\xhh"). It is the
caller's responsibility to deallocate the returned I<String> with
I<str_release(3)> or I<str_destroy(3)>. It is strongly recommended to use
I<str_destroy(3)>, because it also sets the pointer variable to C<null>.
*/
static String *quote_special(const char *src)
{
return encode(src, special_char, special_code, '\\', 1);
}
/*
C<String *unquote_special(const char *src)>
Replaces every occurrence in C<src> of C<"\a">, C<"\b">, C<"\f">, C<"\n">,
C<"\r">, C<"\t"> or C<"\v"> with the corresponding special characters (as
interpreted by I<C>). Ascii codes in octal or hexadecimal (i.e. "\ooo" or
"\xhh") are replaced with their corresponding I<ASCII> characters. It is the
caller's responsibility to deallocate the returned I<String> with
I<str_release(3)> or I<str_destroy(3)>. It is strongly recommended to use
I<str_destroy(3)>, because it also sets the pointer variable to C<null>.
*/
static String *unquote_special(const char *src)
{
return decode(src, special_char, special_code, '\\', 1);
}
/*
C<String *quote_equals(const char *src)>
Replaces every occurrence in C<src> of C<"="> with C<"\=">. It is the
caller's responsibility to deallocate the returned I<String> with
I<str_release(3)> or I<str_destroy(3)>. It is strongly recommended to use
I<str_destroy(3)>, because it also sets the pointer variable to C<null>.
*/
static String *quote_equals(const char *src)
{
return encode(src, eq, eq, '\\', 0);
}
/*
C<String *quote_equals(const char *src)>
Replaces every occurrence in C<src> of C<"\="> with C<"=">. It is the
caller's responsibility to deallocate the returned I<String> with
I<str_release(3)> or I<str_destroy(3)>. It is strongly recommended to use
I<str_destroy(3)>, because it also sets the pointer variable to C<null>.
*/
static String *unquote_equals(const char *src)
{
return decode(src, eq, eq, '\\', 0);
}
/*
C<char *user_home(void)>
Returns the user's home directory (obtained from C</etc/passwd>). The return
value is cached so that any subsequent calls will be faster.
*/
static char *user_home(void)
{
struct passwd *pwent;
char *home = NULL;
if (g.home)
return g.home;
if ((pwent = getpwuid(getuid())))
home = pwent->pw_dir;
return g.home = (home && strlen(home)) ? mem_strdup(home) : NULL;
}
/*
C<void prop_parse(Map *map, const char *path, char *line, size_t lineno)>
Parses one line from a properties file. C<path> is the path of the
properties file. C<line> is the text to parse. C<lineno> is the current line
number. The property parsed, if any, is added to C<map>. Emits error
messages when syntax errors occur. That's probably a mistake. To suppress
the error messages, call I<prog_err_none(3)> first and restore error
messages afterwards with something like I<prog_err_stderr(3)>.
*/
static void prop_parse(Map *map, const char *path, char *line, size_t lineno)
{
String *prop, *name;
char *p, *eq, *value, *val, *key;
/* Unquote any special characters in the line */
if (!(prop = unquote_special(line)))
{
error("prop: Out of memory");
return;
}
/* Find first unquoted '=' */
for (p = cstr(prop), eq = strchr(p, '='); eq; eq = strchr(eq + 1, '='))
if (eq == p || eq[-1] != '\\')
break;
if (!eq)
{
error("prop: %s line %d: Expected '='\n%s", path, lineno, line);
str_release(prop);
set_errno(EINVAL);
return;
}
/* Identify and separate the name and value */
value = eq + 1;
while (is_space(value[0]))
++value;
while (eq > p && is_space(eq[-1]) && (eq == p + 1 || eq[-2] != '\\'))
--eq;
*eq = nul;
/* Unquote any quoted trailing space in the key */
if (eq > p + 1 && is_space(eq[-1]) && eq[-2] == '\\')
{
eq[-2] = eq[-1];
eq[-1] = nul;
}
/* Unquote any quoted '=' in the name */
if (!(name = unquote_equals(p)))
{
error("prop: Out of memory");
str_release(prop);
return;
}
key = cstr(name);
/* Unquote any quoted leading space in the value */
if (*value == '\\')
++value;
/* Add this property to the map */
if (!(val = mem_strdup(value)))
{
error("prop: Out of memory");
str_release(prop);
str_release(name);
return;
}
if (map_add(map, key, val) == -1)
{
error("prop: %s line %d: Property %s already defined\n%s", path, lineno, name, line);
mem_release(val);
}
str_release(prop);
str_release(name);
}
/*
C<Prop *prop_load(const char *path, Prop *defaults)>
Creates and returns a new I<Prop> containing C<defaults> and the properties
obtained from the properties file specified by C<path>. On error, returns
C<null> with C<errno> set appropriately.
*/
static Prop *prop_load(const char *path, Prop *defaults)
{
Prop *prop;
Map *map;
if (!(map = map_create((map_release_t *)free)))
return NULL;
if (!daemon_parse_config(path, map, (daemon_config_parser_t *)prop_parse))
{
map_release(map);
return NULL;
}
if (!(prop = prop_create(map, defaults)))
{
map_release(map);
return NULL;
}
return prop;
}
/*
C<int prop_init(void)>
Initialises the I<prop(3)> module. Loads properties from the following locations:
/etc/properties/app - system-wide, generic properties
~/.properties/app - user-defined, generic properties
/etc/properties/app.progname - system-wide, program-specific properties
~/.properties/app.progname - user-defined, program-specific properties
Note that, depending on how I<libslack> was installed, the system-wide
C</etc/properties> directory might not be under C</etc>. It might be under
another directory, such as C</usr/local/etc> or C</opt/local/etc>. If you
aren't sure where it is expected, run C<libslack-config --cflags> and look
for the definition of C<ETC_DIR>.
Properties from the first three files become (nested) defaults. Properties
from the last file becomes the top-level I<Prop>. If the last file doesn't
exist, an empty I<Prop> becomes the top-level I<Prop>.
Called at the start of the first get, set, or unset function called.
*/
static int prop_init(void)
{
char *path;
Prop *prop = NULL;
Prop *prop_next;
char *home;
int writable = 0;
size_t path_len;
path_len = limit_path();
if (!(path = mem_create(path_len, char)))
return -1;
/* System wide generic properties: /etc/properties/app */
snprintf(path, path_len, "%s%cproperties%capp", ETC_DIR, PATH_SEP, PATH_SEP);
if ((prop_next = prop_load(path, prop)))
prop = prop_next;
/* User defined generic properties: ~/.properties/app */
if ((home = user_home()))
{
snprintf(path, path_len, "%s%c.properties%capp", home, PATH_SEP, PATH_SEP);
if ((prop_next = prop_load(path, prop)))
prop = prop_next;
}
if (prog_name())
{
char *progname, *sep;
if (!(progname = mem_strdup(prog_name())))
{
mem_release(path);
prop_release(prop);
return -1;
}
for (sep = strchr(progname, PATH_SEP); sep; sep = strchr(sep, PATH_SEP))
*sep++ = '-';
/* System wide program specific properties: /etc/properties/app.progname */
snprintf(path, path_len, "%s%cproperties%capp.%s", ETC_DIR, PATH_SEP, PATH_SEP, progname);
if ((prop_next = prop_load(path, prop)))
prop = prop_next;
/* User defined program specific properties: ~/.properties/app.progname */
if (home)
{
snprintf(path, path_len, "%s%c.properties%capp.%s", home, PATH_SEP, PATH_SEP, progname);
if ((prop_next = prop_load(path, prop)))
{
prop = prop_next;
writable = 1;
}
}
free(progname);
}
/* Guarantee a user defined program specific property map for prop_set() */
if (!writable)
{
Map *map;
if (!(map = map_create((map_release_t *)free)))
{
mem_release(path);
prop_release(prop);
return -1;
}
if (!(prop_next = prop_create(map, prop)))
{
mem_release(path);
prop_release(prop);
map_release(map);
return -1;
}
prop = prop_next;
}
mem_release(path);
g.prop = prop;
g.init = 1;
return 0;
}
/*
=item C<const char *prop_get(const char *name)>
Returns the value of the property named C<name>. Returns C<null> if there is
no such property. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
const char *prop_get(const char *name)
{
Prop *p;
const char *value = NULL;
int err;
if ((err = locker_wrlock(g.locker)))
return set_errnull(err);
if (!g.init && prop_init() == -1)
{
locker_unlock(g.locker);
return NULL;
}
for (p = g.prop; p; p = p->defaults)
if ((value = map_get(p->map, name)))
break;
if ((err = locker_unlock(g.locker)))
return set_errnull(err);
return value;
}
/*
=item C<const char *prop_get_or(const char *name, const char *default_value)>
Returns the value of the property named C<name>. Returns C<default_value> on
error, or if there is no such property.
=cut
*/
const char *prop_get_or(const char *name, const char *default_value)
{
const char *prop = prop_get(name);
return prop ? prop : default_value;
}
/*
=item C<const char *prop_set(const char *name, const char *value)>
Sets the property named C<name> to a dynamically allocated copy of C<value>.
If I<prop_save(3)> is called after a call to this function, the new property
will be saved to disk, and will be available the next time this program is
executed. On success, returns the copy of C<value>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
const char *prop_set(const char *name, const char *value)
{
char *val;
int err;
if ((err = locker_wrlock(g.locker)))
return set_errnull(err);
if (!g.init && prop_init() == -1)
{
locker_unlock(g.locker);
return NULL;
}
if (!(val = mem_strdup(value)))
{
locker_unlock(g.locker);
return NULL;
}
if (map_put(g.prop->map, name, val) == -1)
{
mem_release(val);
locker_unlock(g.locker);
return NULL;
}
g.dirty = 1;
if ((err = locker_unlock(g.locker)))
return set_errnull(err);
return val;
}
/*
=item C<int prop_get_int(const char *name)>
Returns the value of the property named C<name> as an integer. Returns C<0>
on error, or if there is no such property, or if it is not interpretable as
a decimal integer.
=cut
*/
int prop_get_int(const char *name)
{
return prop_get_int_or(name, 0);
}
/*
=item C<int prop_get_int_or(const char *name, int default_value)>
Returns the value of the property named C<name> as an integer. Returns
C<default_value> on error, or if there is no such property, or if it is not
interpretable as a decimal integer.
=cut
*/
int prop_get_int_or(const char *name, int default_value)
{
const char *prop = prop_get(name);
int val;
return (prop && sscanf(prop, " %d ", &val)) ? val : default_value;
}
/*
=item C<int prop_set_int(const char *name, int value)>
Sets the property named C<name> to C<value>. If I<prop_save(3)> is called
after a call to this function, the new property will be saved to disk and
will be available the next time this program is executed. On success,
returns C<value>. On error, returns C<0>.
=cut
*/
int prop_set_int(const char *name, int value)
{
char buf[128];
snprintf(buf, sizeof(buf), "%d", value);
return prop_set(name, buf) ? value : 0;
}
/*
=item C<double prop_get_double(const char *name)>
Returns the value of the property named C<name> as a double. Returns C<0.0>
on error, or if there is no such property, or if it is not interpretable as
a floating point number.
=cut
*/
double prop_get_double(const char *name)
{
return prop_get_double_or(name, 0.0);
}
/*
=item C<double prop_get_double_or(const char *name, double default_value)>
Returns the value of the property named C<name> as a double. Returns
C<default_value> on error, or if there is no such property, or if it is not
interpretable as a floating point number.
=cut
*/
double prop_get_double_or(const char *name, double default_value)
{
const char *prop = prop_get(name);
double val;
return (prop && sscanf(prop, "%lg", &val)) ? val : default_value;
}
/*
=item C<double prop_set_double(const char *name, double value)>
Sets the property named C<name> to C<value>. If I<prop_save(3)> is called
after a call to this function, the new property will be saved to disk and
will be available the next time this program is executed. On success,
returns C<value>. On error, returns C<0.0>.
=cut
*/
double prop_set_double(const char *name, double value)
{
char buf[128];
snprintf(buf, 128, "%g", value);
return prop_set(name, buf) ? value : -1;
}
/*
=item C<int prop_get_bool(const char *name)>
Returns the value of the property named C<name> as a boolean value. Returns
C<0> on error or if there is no such property. The values: C<"true">,
C<"yes">, C<"on"> and C<"1"> are all interpreted as C<true>. All other
values are interpreted as C<false>.
=cut
*/
int prop_get_bool(const char *name)
{
return prop_get_bool_or(name, 0);
}
/*
=item C<int prop_get_bool_or(const char *name, int default_value)>
Returns the value of the property named C<name> as a boolean value. Returns
C<default_value> on error, or if there is no such property, or if it is not
interpretable as a boolean value. The values: C<"true">, C<"yes">, C<"on">
and C<"1"> are all interpreted as C<true>. The values: C<"false">, C<"no">,
C<"off"> and C<"0"> are all interpreted as C<false>. All other values are
disregarded and will cause C<default_value> to be returned.
=cut
*/
int prop_get_bool_or(const char *name, int default_value)
{
const char *prop = prop_get(name);
char buf[128];
int val;
if (!prop)
return default_value;
if (sscanf(prop, " %d ", &val))
return val;
if (sscanf(prop, " %127s ", buf))
{
if ((buf[0] == 't' || buf[0] == 'T') &&
(buf[1] == 'r' || buf[1] == 'R') &&
(buf[2] == 'u' || buf[2] == 'U') &&
(buf[3] == 'e' || buf[3] == 'E') &&
(buf[4] == nul))
return 1;
if ((buf[0] == 'f' || buf[0] == 'F') &&
(buf[1] == 'a' || buf[1] == 'A') &&
(buf[2] == 'l' || buf[2] == 'L') &&
(buf[3] == 's' || buf[3] == 'S') &&
(buf[4] == 'e' || buf[4] == 'E') &&
(buf[5] == nul))
return 0;
if ((buf[0] == 'y' || buf[0] == 'Y') &&
(buf[1] == 'e' || buf[1] == 'E') &&
(buf[2] == 's' || buf[2] == 'S') &&
(buf[3] == nul))
return 1;
if ((buf[0] == 'n' || buf[0] == 'N') &&
(buf[1] == 'o' || buf[1] == 'O') &&
(buf[2] == nul))
return 0;
if ((buf[0] == 'o' || buf[0] == 'O') &&
(buf[1] == 'n' || buf[1] == 'N') &&
(buf[2] == nul))
return 1;
if ((buf[0] == 'o' || buf[0] == 'O') &&
(buf[1] == 'f' || buf[1] == 'F') &&
(buf[2] == 'f' || buf[2] == 'F') &&
(buf[3] == nul))
return 0;
}
return default_value;
}
/*
=item C<int prop_set_bool(const char *name, int value)>
Sets the property named C<name> to C<value>. If I<prop_save(3)> is called
after a call to this function, the new property will be saved to disk and
will be available the next time this program is executed. On success,
returns C<value>. On error, returns C<0>.
=cut
*/
int prop_set_bool(const char *name, int value)
{
return prop_set_int(name, value);
}
/*
=item C<int prop_unset(const char *name)>
Removes the property named C<name>. Property removal is only saved to disk
when I<prop_save(3)> is called, if the property existed only in the
user-defined, program-specific properties file, or was created by the
program at runtime. On success, returns C<0>. On error, returns C<-1> with
C<errno> set appropriately.
=cut
*/
int prop_unset(const char *name)
{
Prop *p;
int err;
if ((err = locker_wrlock(g.locker)))
return set_errno(err);
if (!g.init && prop_init() == -1)
{
locker_unlock(g.locker);
return -1;
}
for (p = g.prop; p; p = p->defaults)
map_remove(p->map, name);
g.dirty = 1;
if ((err = locker_unlock(g.locker)))
return set_errno(err);
return 0;
}
/*
=item C<int prop_save(void)>
Saves the program's properties to disk. If the property named C<"save"> is
set to C<"false">, C<"no">, C<"off"> or C<"0">, nothing is written to disk.
If no properties were added, removed or changed, nothing is written to disk.
Only the user-defined, program-specific properties are saved. Generic and
system-wide properties files must be edited by hand. Each program can only
save its own properties. They are saved in the following file:
~/.properties/app.progname
Where C<progname> is the name of the program after being translated as
described at the top of the I<DESCRIPTION> section.
The C<~/.properties> directory is created if necessary. On success, returns
C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int prop_save(void)
{
char *path;
size_t path_len;
size_t len;
char *home;
char *progname, *sep;
struct stat status[1];
List *keys;
Lister *k;
FILE *file;
int err;
if (!prop_get_bool_or("save", 1))
return 0;
if ((err = locker_wrlock(g.locker)))
return set_errno(err);
if (!g.dirty)
{
if ((err = locker_unlock(g.locker)))
return set_errno(err);
return 0;
}
if (!prog_name())
{
locker_unlock(g.locker);
return set_errno(EINVAL);
}
if (!(home = user_home()))
{
locker_unlock(g.locker);
return set_errno(EINVAL);
}
path_len = limit_path();
if (!(path = mem_create(path_len, char)))
{
locker_unlock(g.locker);
return -1;
}
snprintf(path, path_len, "%s%c.properties", home, PATH_SEP);
if (stat(path, status) == -1 && mkdir(path, S_IRWXU) == -1)
{
mem_release(path);
locker_unlock(g.locker);
return -1;
}
if (stat(path, status) == -1 || S_ISDIR(status->st_mode) == 0)
{
mem_release(path);
locker_unlock(g.locker);
return set_errno(EINVAL);
}
if (!(progname = mem_strdup(prog_name())))
{