-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkdb.c
1340 lines (1153 loc) · 27.4 KB
/
kdb.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
/***************************************************************************
*
* Copyright (c) 2005,2007 Vladislav Moskovets (webface-dev(at)vlad.org.ua)
*
* 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
*
*************************************************************************** */
/* TODO:
* * multiline import (export)
* * raw block partition support for storage
* * types
*
*
* */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <sys/file.h>
#include <unistd.h>
#include <error.h>
#include <stdarg.h>
#include <syslog.h> // openlog(), syslog()
#include "md5.h"
// #define DEBUG
#define MAX_LINES 2048
#define MAX_LINE_SIZE 1024
#define READ_BUFFER_LEN (1024*32)
#define WRITE_BUFFER_LEN READ_BUFFER_LEN
#define FILE_BUF 1024
#define HEADER_LINE "KDB\n"
#define FOOTER_LINE "KDB END\n"
#define true 1
#define false 0
#define COUNTS_NAME "kdb_lines_count"
#define NEXT_NAME "kdb_next_name"
#define ENV_STRING "%ENV"
#define KDB_RES "/etc/kdb.res"
#define KDB_RES_MD5 "/etc/kdb.res.md5"
#define MD5_SUFFIX ".md5"
#define WARN
#define FLOCK(f) {if(flock(fileno(f), LOCK_EX)) { perror("flock"); syslog(LOG_ERR, "flock() fail, error code %d", errno); }; };
#define FUNLOCK(f) flock(fileno(f), LOCK_UN);
#define WILDCARD_LOOP(i, iname) for( i=0; i < db_lines_count; i++ ) \
if ( match_wildcard(iname, db_lines[i].name) )
char *str_escape(const char *source);
char *str_unescape(const char *source);
int match_wildcard(const char *pattern, const char *string);
int is_wildcarded(const char* str);
int copyfile(const char *, const char *);
typedef struct {
char *name;
char *value;
} _db_record;
char db_filename[255];
FILE *db_file=NULL;
_db_record db_lines[MAX_LINES];
char db_header[MAX_LINE_SIZE];
int db_lines_count;
int quotation;
int make_local;
char local_str[]="local ";
int make_export;
int need_print_count;
int need_write;
int db_already_readed;
#ifdef DEBUG
void debug(char *format, ...){
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
#else
inline void _debug(char* format, ...){}
#define debug if(0)_debug
#endif
#ifdef WARN
void warn(char *format, ...){
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
vsyslog(LOG_WARNING, format, ap);
va_end(ap);
}
#else
inline void warn(char* format, ...){}
#endif
void show_usage(char *name)
{
printf("Usage: %s [OPTIONS] ARG [: ARG] \n", name);
printf("where OPTIONS:= l|q|qq|e|c\n");
printf(" ARG := { set pattern=value |\n");
printf(" rm pattern | del pattern |\n");
printf(" isset key |\n");
printf(" list [pattern] | ls [pattern] |\n");
printf(" slist key | sls key |\n");
printf(" klist pattern | kls pattern |\n");
printf(" listrm key | lrm key |\n");
printf(" listadd key_=value | ladd key_=value |\n");
printf(" rename oldkey newkey | rn oldkey newkey |\n");
printf(" create [filename] |\n");
printf(" import [filename] |\n");
printf(" edit }\n");
return;
};
char *get_dbfilename()
{
if(strlen(db_filename))
return db_filename;
if (getenv("KDB"))
strcpy(db_filename, getenv("KDB"));
else if(!getuid())
strcpy(db_filename, "/etc/kdb");
else
sprintf(db_filename, "%s/.kdb", getenv("HOME"));
return db_filename;
}
char *set_dbfilename(const char* filename)
{
strcpy(db_filename, filename);
return db_filename;
}
int db_open()
{
debug("DEBUG: Using %s as database\n", get_dbfilename());
if ( db_file )
return true;
db_file = fopen(get_dbfilename(), "r+");
if (!db_file) {
fprintf(stderr, "fopen '%s' %s\n", get_dbfilename(), strerror(errno));
syslog(LOG_ERR, "fopen '%s' %s\n", get_dbfilename(), strerror(errno));
return false;
};
// lock the file
FLOCK(db_file);
return true;
}
int db_close()
{
debug("db_close()\n");
if (db_file) {
FUNLOCK(db_file);
fclose(db_file);
}
db_file = NULL;
return true;
};
int kdbinit()
{
db_already_readed=0;
db_lines_count=0;
quotation=0;
make_export=0;
make_local=0;
need_print_count=0;
need_write=false;
int i;
db_filename[0]='\0';
for(i=0; i<MAX_LINES; i++)
db_lines[i].name=NULL, db_lines[i].value=NULL;
return 0;
}
inline int db_add(const char* name, const char* value, int dup)
{
if ( db_lines_count >= MAX_LINES ) {
warn("Warning: db_add(): MAX_LINES %d reached\n", MAX_LINES);
return false;
}
debug("DEBUG: db_add(name='%s', value='%s', dup=%d): db_lines[%d]\n", name, value, dup, db_lines_count);
db_lines[db_lines_count].name=dup?strdup(name):(char*)name;
db_lines[db_lines_count].value=dup?strdup(value):(char*)value;
db_lines_count++;
return true;
}
// Parse str "name=value" and put 'name' to *name and 'value' to *value
int parse_pair(const char* str, char* name, char* value)
{
int len = strlen(str);
if (!len)
return false;
char s[len+10];
strcpy(s, str);
if (s[len-1]=='\n')
s[len-1]='\0';
char *eq=strchr(s, '=');
if (!eq || eq==s) {
syslog(LOG_ERR, "Error: can't parse: %s", s);
return false;
}
eq[0]='\0';
strcpy(name, s );
strcpy(value, eq+1);
return true;
}
int parse_header(char *header)
{
if (strncmp(HEADER_LINE, header, 3)){
syslog(LOG_ERR, "bad file format\n");
return false;
}
return true;
}
int db_unserialize(const char *buf)
{
int len=strlen(buf);
char name[len+10], value[len+10];
debug("DEBUG: db_unserialize ('%s')\n", buf);
if ( parse_pair(buf, name, value) )
return db_add(str_unescape(name), str_unescape(value), false);
else
return false;
}
int db_serialize(int index, char *buf)
{
char *ename=str_escape(db_lines[index].name);
char *evalue=str_escape(db_lines[index].value);
int len = sprintf(buf, "%s=%s\n", ename, evalue);
debug("DEBUG: db_serialize (%d, '%s=%s')\n", index, ename, evalue);
free(ename);
free(evalue);
return len;
}
int db_read()
{
int result=true;
char *buf=NULL;
char *pointer=NULL;
char head_buf[sizeof(HEADER_LINE)+10];
// check if file already readed
if (db_already_readed)
return true;
if (! db_open() )
return false;
// read header
if (! fgets(head_buf, sizeof(head_buf), db_file) ) {
if ( ferror(db_file) ) {
perror("fgets");
return false;
}
}
// check header
if (! parse_header(head_buf) )
return false;
if (! (buf=(char*) calloc(READ_BUFFER_LEN, 1)) ) {
perror("calloc");
return false;
}
while ( result=fgets(buf, READ_BUFFER_LEN, db_file) && result != EOF ) {
if ( ! strncmp(buf, FOOTER_LINE, sizeof(FOOTER_LINE)) )
break;
if ( ! db_unserialize(buf) )
return false;
}
db_already_readed = true;
if (buf)
free(buf);
return true;
}
int sort_func(const void *pa, const void *pb){
_db_record *a=(_db_record*)pa;
_db_record *b=(_db_record*)pb;
debug("DEBUG: sort() compare '%s' and '%s'\n", a->name, b->name);
return strcmp(a->name, b->name);
}
int db_sort(){
qsort(db_lines, db_lines_count, sizeof(_db_record), sort_func);
}
inline int print_pair(const char* name, const char* value)
{
char *prefix="";
if (make_local)
prefix="local ";
else if (make_export)
prefix="export ";
//debug("DEBUG: print_pair(): %s%s=%s\n", prefix, name, value);
switch(quotation) {
case 0:
if (name)
printf("%s%s=%s\n", prefix, name, value);
else
printf("%s\n", value);
break;
case 1:
if (name)
printf("%s%s=\"%s\"\n", prefix, name, value);
else
printf("\"%s\"\n", value);
break;
default:
if (name)
printf("%s%s='%s'\n", prefix, name, value);
else
printf("'%s'\n", value);
break;
}
return true;
}
int db_set_index(int index, const char *name, const char *value, int dup)
{
if ( index < 0 || index >=db_lines_count ) {
warn("Error: db_set_index(): index %d is out of bound\n", index);
return false;
}
debug("DEBUG: db_set_index(): sets db_lines[%d]: name=%s, value=%s\n", index, name, value);
if (name) {
// release memory
if ( db_lines[index].name )
free(db_lines[index].name);
// replace values
db_lines[index].name = dup?strdup(name):(char*)name;
}
if (value) {
// release memory
if ( db_lines[index].value )
free(db_lines[index].value);
// replace values
db_lines[index].value = dup?strdup(value):(char*)value;
};
need_write++;
return true;
}
int find_key(const char *key)
{
int i;
if ( !key )
return false;
for(i=0; i<db_lines_count; i++)
if (!strcmp(key, db_lines[i].name))
return i;
return -1;
}
int db_set(const char *name, const char* value)
{
int result=false;
int index;
if ( ! db_read() )
return false;
index = find_key(name);
if (index != -1) {
result=db_set_index(index, name, value, true);
} else {
result=db_add(name, value, true);
}
if (result)
need_write++;
return result;
}
int db_del_index(int index)
{
int i=index;
if (index < 0 || index >= db_lines_count)
return false;
// release memory
if ( db_lines[index].name )
free(db_lines[index].name);
if ( db_lines[index].value )
free(db_lines[index].value);
while( i < db_lines_count-1 ) {
db_lines[i].name=db_lines[i+1].name;
db_lines[i].value=db_lines[i+1].value;
i++;
}
debug(" DEBUG: deleted db_lines[%d]\n", index);
db_lines_count--;
need_write=true;
return true;
}
/*
md5 = 1 — generate MD5 file
md5 = 0 — do not generate MD5 file
*/
int db_write(int md5)
{
int i, len, written;
int result=true;
char *buf, *p, filebuf[FILE_BUF], kdb_md5[FILE_BUF];
size_t count;
struct cvs_MD5Context context;
unsigned char checksum[16];
FILE *fd;
/* backup kdb and MD5 of kdb */
if (md5)
{
/* generate MD5 file name */
snprintf(kdb_md5, FILE_BUF, "%s%s", get_dbfilename(), MD5_SUFFIX);
/* copy db file to reserve file */
if (copyfile(get_dbfilename(), KDB_RES) == false)
{
fprintf(stderr, "Can't copy %s to %s", get_dbfilename(), KDB_RES);
syslog(LOG_ERR, "Can't copy %s to %s", get_dbfilename(), KDB_RES);
return false;
}
/* copy db MD5 file to reserve file */
copyfile(kdb_md5, KDB_RES_MD5);
}
// sorts the data
debug("DEBUG: sorts data\n");
db_sort();
rewind(db_file);
ftruncate(fileno(db_file), 0);
// alloc
if (! (buf=(char*) calloc(READ_BUFFER_LEN, 1)) ) {
perror("calloc");
return false;
}
p=buf;
p+=sprintf(buf, HEADER_LINE);
// write all db_lines
for(i=0; i<db_lines_count; i++)
p+=db_serialize(i, p);
// append footer
strcat(p, FOOTER_LINE);
p+=sizeof(FOOTER_LINE)-1;
debug("DEBUG: writing data to database\n");
fwrite(buf, 1, p-buf, db_file);
free(buf);
if (md5)
{
/* calculate MD5 */
rewind(db_file);
cvs_MD5Init(&context);
while ((count = fread(filebuf, 1, FILE_BUF, db_file)) > 0)
cvs_MD5Update(&context, filebuf, count);
if (ferror(db_file))
{
perror("fread()");
return false;
}
cvs_MD5Final(checksum, &context);
/* write MD5 */
fd = fopen(kdb_md5, "w");
if (!fd)
{
fprintf(stderr, "fopen '%s' %s\n", kdb_md5, strerror(errno));
syslog(LOG_ERR, "fopen '%s' %s\n", kdb_md5, strerror(errno));
return false;
}
for (i = 0; i < 16; i++)
{
if (fprintf(fd, "%02x", (unsigned int) checksum[i]) < 0)
{
perror("fprintf()");
return false;
}
}
if (fprintf(fd, "\n") < 0)
{
perror("fprintf()");
return false;
}
fclose(fd);
}
return result;
}
void print_count(int count)
{
char ls_count[sizeof(COUNTS_NAME)+20];
if (need_print_count) {
sprintf(ls_count, "%d", count);
print_pair(COUNTS_NAME, ls_count);
};
}
/****************
*
* kdb commands
*
****************/
int import(const char *filename)
{
FILE *file;
int result=true;
char buf[MAX_LINE_SIZE];
char name[MAX_LINE_SIZE], value[MAX_LINE_SIZE];
if (!filename || (!strlen(filename)) || (!strcmp("-", filename)) )
file=stdin;
else {
file = fopen(filename, "r+");
if (!file) {
perror("fopen");
result=false;
};
};
if (! result )
return false;
db_file = file;
db_already_readed = false;
if ( ! db_read() )
return false;
fclose(file);
db_file = NULL;
db_open();
/* write kdb and generate md5 */
db_write(1);
need_write++;
return result;
}
int export(const char *filename)
{
FILE *file, *tmp;
int result=true;
char buf[MAX_LINE_SIZE];
char name[MAX_LINE_SIZE], value[MAX_LINE_SIZE];
if (!filename || (!strlen(filename)) || (!strcmp("-", filename)) )
file=stdout;
else {
file = fopen(filename, "w+");
if (!file) {
perror("fopen");
result=false;
};
};
if (! result )
return false;
db_read();
tmp = db_file;
db_file = file;
/* write KDB without generating MD5 */
db_write(0);
fclose(file);
db_file = tmp;
return true;
}
int edit(const char* me)
{
int result=true;
char *tmpname=tmpnam(NULL);
char editor[255];
char buf[255];
if (!tmpname)
return false;
if (getenv("EDITOR"))
sprintf(editor, "%s %s", getenv("EDITOR"), tmpname) ;
else
sprintf(editor, "vi %s", tmpname);
sprintf(buf, "%s export > %s", me, tmpname);
if (system(buf))
return false;
if (! system(editor)) {
result=import(tmpname);
remove(tmpname);
}
return result;
}
int rename(const char *oldkey, const char* newkey)
{
db_read();
// first - remove newkey
int index = find_key(newkey);
if ( index != -1 )
db_del_index(index);
// replace oldkey name to new name
index = find_key(oldkey);
if ( index == -1 )
return false;
db_set_index(index, newkey, NULL, true);
return ++need_write;
}
int get(const char *key)
{
int i;
db_read();
WILDCARD_LOOP(i, key)
print_pair(NULL, db_lines[i].value);
return true;
}
int kdb_appget(const char *key,char **ptr)
{
int i,cnt = 0;
static char buf[512];
db_read();
printf("KDB: buf=%p\n",buf);
WILDCARD_LOOP(i, key){
if( !cnt ){
snprintf(buf,512,"%s",db_lines[i].value);
}
cnt++;
}
*ptr = buf;
return cnt;
}
int isset(const char *key)
{
db_read();
return (find_key(key) !=-1);
}
// set with wildcard matching
int set(const char *str)
{
int len=strlen(str);
char iname[len+10], ivalue[len+10];
int i;
db_read();
if (!parse_pair(str, iname, ivalue))
return false;
if (! strcmp (ivalue, ENV_STRING) ) {
char *value;
const char *name=iname;
if ( !strncmp(name, "FORM_", 5) ) // 5 - sizeof("FORM_")
name+=5;
value=getenv(name);
return db_set( name, value?value:"" );
} else if ( is_wildcarded(iname) ) {
WILDCARD_LOOP(i, iname) {
db_set_index(i, NULL, ivalue, true);
need_write++;
}
} else {
return db_set( iname, ivalue );
}
return true;
}
// delete with wildcard matching
int del(const char *str)
{
int i=0;
db_read();
while ( i != db_lines_count ) {
WILDCARD_LOOP(i, str) {
db_del_index(i);
need_write++;
break;
}
}
return true;
}
int sublist(const char *key)
{
int result=true;
int i,count=0;
int len=strlen(key);
char name[len+10], value[len+10];
db_read();
for( i=0; i<db_lines_count; i++ ) {
if( key && len ){
if ( !strncmp(key, db_lines[i].name, len) ) {
char *s=db_lines[i].name+len;
print_pair(s, db_lines[i].value);
count++;
}
}
}
print_count(count);
return result;
}
// keylist with wildcard matching
int keylist(const char *key)
{
int i=0, count=0;
db_read();
WILDCARD_LOOP(i, key) {
print_pair(NULL, db_lines[i].name);
count++;
}
print_count(count);
return true;
}
// sub sub keylist with wildcard matching
// finds patter in 'name=value'
// and cutoff cut_prefix and cut_suffix
// Example:
// # kdb ls
// sys_iface_dsl0_valid=1
// sys_iface_dsl1_valid=0
// sys_iface_eth0_valid=1
// sys_iface_eth1_valid=1
// # kdb sskls sys*valid=1 sys_iface_ _valid
// dsl0
// eth0
// eth1
//
int sskeylist(const char* pattern, const char *cut_prefix, const char* cut_suffix)
{
int i=0, count=0;
int result=true;
int len=strlen(cut_prefix);
char *tail;
char strbuf[MAX_LINE_SIZE];
db_read();
for( i=0; i<db_lines_count; i++ ) {
if( len ){
snprintf(strbuf, sizeof(strbuf), "%s=%s", db_lines[i].name, db_lines[i].value);
if ( (match_wildcard(pattern, strbuf)) && ( !strncmp(cut_prefix, db_lines[i].name, len) ) ) {
char *s=db_lines[i].name+len;
strncpy(strbuf, s, sizeof(strbuf));
tail = strstr(strbuf, cut_suffix);
if (tail)
tail[0]='\0';
print_pair(NULL, strbuf);
}
}
}
return true;
}
int listrm(const char *key)
{
int result=true;
int i, count=0, index;
char name[MAX_LINE_SIZE], value[MAX_LINE_SIZE], prefix_name[MAX_LINE_SIZE], tmps[MAX_LINE_SIZE], new_name[MAX_LINE_SIZE];
char *s;
_db_record local_lines[MAX_LINES];
int local_lines_count=0;
db_read();
s=strrchr(key, '_');
if(!s) {
return false;
};
strncpy(prefix_name, key, s-key+1);
debug("DEBUG: prefix_name=%s\n", prefix_name);
// finds all key_[0-9]+ keys, and move it to local_lines array
i=0;
while(true) {
snprintf(tmps, sizeof(tmps), "%s%d", prefix_name, i);
index=find_key(tmps);
if ( index >= 0 ) {
debug( "DEBUG: db_lines[%d]='%s' \n", index, db_lines[index]);
// if index != rm_index - add pair to temp local_lines array
if ( strcmp(db_lines[index].name, key) ) {
strcpy(name, db_lines[index].name);
strcpy(value, db_lines[index].value);
// finds last '_'
s=strrchr(name, '_');
// truncate name after last '_'
s[1]='\0';
snprintf(new_name, sizeof(new_name), "%s%d", name, local_lines_count);
debug( " DEBUG: new_name=%s value=%s \n", new_name, value);
local_lines[local_lines_count].name=strdup(new_name);
local_lines[local_lines_count++].value=strdup(value);
debug( " DEBUG: added to local_lines '%s=%s' \n", local_lines[local_lines_count-1].name, local_lines[local_lines_count-1].value);
};
db_del_index(index);
} else {
break;
};
i++;
}
// yes, we have some keys=values
if (i) {
i=0;
for(i=0; i<local_lines_count; i++) {
db_set(local_lines[i].name, local_lines[i].value);
if (local_lines[i].name)
free(local_lines[i].name), free(local_lines[i].value);
}
}
return result;
}
int list_getnextindex(const char *name)
{
int i, index;
char s[MAX_LINE_SIZE];
for ( i=0; i < MAX_LINES; i++) {
snprintf(s, sizeof(s), "%s%d", name, i);
index=find_key(s);
if ( index == -1 )
return i;
};
return -1;
}
// UNFINISHED!!!
int list_getnext(const char *str)
{
int result=true;
int i, index;
const int max_index=MAX_LINES/2;
char name[MAX_LINE_SIZE], value[MAX_LINE_SIZE], s[MAX_LINE_SIZE];
db_read();
parse_pair(str, name, value);
for ( i=0; i < strlen(name); i++ )
if ( name[i] = '%' ) {
name[i]='\0';
break;
}
/*
for ( index=0; index < max_index; index++ ) {
sprintf(s, "%s%d_", name, index);
for ( i=0; i < db_lines_count; i++ ) {
if ( ! strncmp (name, db_lines[i].name ) )
break;
}
}*/
return true;
}
int listadd(const char* str)
{
int result=true;
int i,index;
char name[MAX_LINE_SIZE], value[MAX_LINE_SIZE], s[MAX_LINE_SIZE];
db_read();
parse_pair(str, name, value);
// check for '_' at the end of string
if ( name[strlen(name)-1] != '_' ) {
syslog(LOG_ERR, "Error: key should ends with '_'\n");
return false;
};
snprintf(s, sizeof(s), "%s%d", name, list_getnextindex(name));
if (! strcmp (value, ENV_STRING) ) {
char *qvalue;
const char *qname=name;
if ( !strncmp(qname, "FORM_", 5) ) // 5 - sizeof("FORM_")
qname+=5;
qvalue=getenv(qname);
need_write++;
return db_add( s, qvalue?qvalue:"", true );
} else {
need_write++;
return db_add(s, value, true);
}
}
// list with wildcard matching
int list(const char *key)
{
int i=0, count=0;
const char *wkey;
// use '*' if key is empty
if (key && key[0])
wkey=key;
else
wkey="*";
db_read();
WILDCARD_LOOP(i, wkey) {
print_pair(db_lines[i].name, db_lines[i].value);
count++;
};
print_count(count);
return true;
}
int createdb(const char *filename)
{
if (strlen(filename))
set_dbfilename(filename);
// create or truncate file
FILE *f=fopen(get_dbfilename(), "w");
if (!f) {
perror("fopen");
return false;
}
fclose(f);
db_open();
need_write=true;
return true;