-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.c
2490 lines (2305 loc) · 75.6 KB
/
default.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
/* default.c
* (c) 2002 Mikulas Patocka, Petr 'Brain' Kulhavy
* This file is a part of the Links program, released under GPL
*
* Does the configuration file.
*/
#include "links.h"
#ifdef HAIKU
#include <FindDirectory.h>
#endif
unsigned char system_name[MAX_STR_LEN];
static void get_system_name(void)
{
#ifdef OS2
if (!os_get_system_name(system_name))
return;
#endif
#if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
{
struct utsname name;
int rs;
memset(&name, 0, sizeof name);
EINTRLOOP(rs, uname(&name));
if (rs >= 0) {
#ifdef OPENVMS
unsigned char * volatile p;
#endif
unsigned char *str = init_str();
int l = 0;
add_to_str(&str, &l, cast_uchar name.sysname);
add_chr_to_str(&str, &l, ' ');
#ifdef OPENVMS
add_to_str(&str, &l, cast_uchar name.version);
#else
add_to_str(&str, &l, cast_uchar name.release);
#endif
add_chr_to_str(&str, &l, ' ');
#ifdef OPENVMS
p = cast_uchar name.nodename + sizeof(name.nodename);
if ((unsigned char *)(&name + 1) - p >= 16 && memchr(cast_const_char p, 0, 16))
add_to_str(&str, &l, cast_uchar p);
else
#endif
add_to_str(&str, &l, cast_uchar name.machine);
safe_strncpy(system_name, str, MAX_STR_LEN);
mem_free(str);
return;
}
}
#endif
#ifdef HAVE_POPEN
/*if (0) {
FILE *f;
unsigned char *p;
memset(system_name, 0, MAX_STR_LEN);
ENULLLOOP(f, popen("uname -srm", "r"));
if (!f) goto fail;
if (!fread(system_name, 1, MAX_STR_LEN - 1, f)) {
pclose(f);
goto fail;
}
pclose(f);
for (p = system_name; *p; p++) if (*p < ' ') {
*p = 0;
break;
}
if (system_name[0]) return;
}
fail:*/
#endif
strcpy(cast_char system_name, SYSTEM_NAME);
}
unsigned char compiler_name[MAX_STR_LEN];
static void get_compiler_name(void)
{
#if defined(__BORLANDC__)
int w = __BORLANDC__+0;
int v1 = w / 0x100;
int v2 = w / 0x10 % 0x10;
int v3 = w % 0x10;
if (v1 == 4 && v2 < 5) v1 = 3;
if (v1 == 4 && v2 == 5) v2 = 0;
if (!v3) sprintf(cast_char compiler_name, "Borland C %d.%d", v1, v2);
else sprintf(cast_char compiler_name, "Borland C %d.%d.%d", v1, v2, v3);
#elif defined(__clang__)
#if !defined(__clang_major__) || !defined(__clang_minor__)
sprintf(cast_char compiler_name, "LLVM/Clang");
#else
int v1 = __clang_major__+0;
int v2 = __clang_minor__+0;
#ifdef __clang_patchlevel__
int v3 = __clang_patchlevel__+0;
#else
int v3 = 0;
#endif
if (v3 > 0) sprintf(cast_char compiler_name, "LLVM/Clang %d.%d.%d", v1, v2, v3);
else sprintf(cast_char compiler_name, "LLVM/Clang %d.%d", v1, v2);
#endif
#elif defined(__COMO_VERSION__)
int w = __COMO_VERSION__+0;
int v1 = w / 100;
int v2 = w % 100;
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Comeau C %d.%d", v1, v2 / 10);
else sprintf(cast_char compiler_name, "Comeau C %d.%02d", v1, v2);
#elif defined(__convexc__)
sprintf(cast_char compiler_name, "Convex C");
#elif defined(_CRAYC)
#if !defined(_RELEASE) || !defined(_RELEASE_MINOR)
sprintf(cast_char compiler_name, "Cray C");
#else
int v1 = _RELEASE+0;
int v2 = _RELEASE_MINOR+0;
sprintf(cast_char compiler_name, "Cray C %d.%d", v1, v2);
#endif
#elif defined(__DCC__)
#ifndef __VERSION_NUMBER__
sprintf(cast_char compiler_name, "Diab C");
#else
int w = __VERSION_NUMBER__+0;
int v1 = w / 1000;
int v2 = w / 100 % 10;
int v3 = w % 100;
sprintf(cast_char compiler_name, "Diab C %d.%d.%02d", v1, v2, v3);
#endif
#elif defined(__DMC__)
int w = __DMC__+0;
int v1 = w / 0x100;
int v2 = w / 0x10 % 0x10;
int v3 = w % 0x10;
if (!v3) sprintf(cast_char compiler_name, "Digital Mars C %d.%d", v1, v2);
else sprintf(cast_char compiler_name, "Digital Mars C %d.%d.%d", v1, v2, v3);
#elif defined(__DECC_VER)
int w = __DECC_VER+0;
int v1 = w / 10000000;
int v2 = w / 100000 % 100;
int v3 = w % 10000;
sprintf(cast_char compiler_name, "DEC C %d.%d-%03d", v1, v2, v3);
#elif defined(__ghs__)
#ifndef __GHS_VERSION_NUMBER__
sprintf(cast_char compiler_name, "Green Hill C");
#else
int w = __GHS_VERSION_NUMBER__+0;
int v1 = w / 100;
int v2 = w / 10 % 10;
int v3 = w % 10;
sprintf(cast_char compiler_name, "Green Hill C %d.%d.%d", v1, v2, v3);
#endif
#elif defined(__HIGHC__)
sprintf(cast_char compiler_name, "MetaWare High C");
#elif defined(__HP_cc)
int w = __HP_cc+0;
int v1 = w / 10000;
int v2 = w / 100 % 100;
int v3 = w % 100;
if (w <= 1) sprintf(cast_char compiler_name, "HP CC");
else sprintf(cast_char compiler_name, "HP CC %d.%02d.%02d", v1, v2, v3);
#elif defined(__xlc__)
int w = __xlc__+0;
int v1 = w / 0x100;
int v2 = w % 0x100;
sprintf(cast_char compiler_name, "IBM XL C %X.%X", v1, v2);
#elif defined(__IBMC__) && defined(__COMPILER_VER__)
unsigned w = __COMPILER_VER__+0;
int v0 = w / 0x10000000;
int v1 = w / 0x1000000 % 0x10;
int v2 = w / 0x10000 % 0x100;
int v3 = w % 0x10000;
unsigned char *os = !v0 ? "S/370" : v0 == 1 ? "OS/390" : v0 == 4 ? "z/OS" : "";
sprintf(cast_char compiler_name, "IBM%s%s XL C %X.%0X.%X", *os ? " " : "", os, v1, v2, v3);
#elif defined(__ICC)
int w = __ICC+0;
int v1 = w / 100;
int v2 = w % 100;
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Intel C %d.%d", v1, v2 / 10);
else sprintf(cast_char compiler_name, "Intel C %d.%02d", v1, v2);
#elif defined(__LCC__)
sprintf(cast_char compiler_name, "LCC");
#elif defined(__NDPC__)
sprintf(cast_char compiler_name, "Microway NDP C");
#elif defined(_MSC_VER)
int w = _MSC_VER+0;
int v1 = w / 100;
int v2 = w % 100;
unsigned char *visual = cast_uchar "";
if (v1 >= 8) {
v1 -= 6;
if (v1 == 2) v1 = 1;
visual = cast_uchar "Visual ";
}
if (!(v2 % 10)) sprintf(cast_char compiler_name, "Microsoft %sC %d.%d", visual, v1, v2 / 10);
else sprintf(cast_char compiler_name, "Microsoft %sC %d.%02d", visual, v1, v2);
#elif defined(__MWERKS__)
int w = __MWERKS__+0;
int v1 = w / 0x1000;
int v2 = w / 0x100 % 0x10;
int v3 = w % 0x100;
if (w <= 1) sprintf(cast_char compiler_name, "Metrowerks CodeWarrior");
sprintf(cast_char compiler_name, "Metrowerks CodeWarrior %x.%x.%x", v1, v2, v3);
#elif defined(__NWCC__)
sprintf(cast_char compiler_name, "NWCC");
#elif defined(__OPEN64__)
unsigned char *n = cast_uchar "Open64 " __OPEN64__;
if (strlen(cast_const_char n) >= sizeof(cast_char compiler_name)) n = cast_uchar "Open64";
strcpy(cast_char compiler_name, cast_const_char n);
#elif defined(__PATHSCALE__)
unsigned char *n = cast_uchar "PathScale " __PATHSCALE__;
if (strlen(cast_const_char n) >= sizeof(cast_char compiler_name)) n = cast_uchar "PathScale";
strcpy(cast_char compiler_name, cast_const_char n);
#elif defined(__PCC__)
int v1 = __PCC__+0;
#ifdef __PCC_MINOR__
int v2 = __PCC_MINOR__+0;
#else
int v2 = 0;
#endif
#ifdef __PCC_MINORMINOR__
int v3 = __PCC_MINORMINOR__+0;
#else
int v3 = 0;
#endif
sprintf(cast_char compiler_name, "PCC %d.%d.%d", v1, v2, v3);
#elif defined(__PGI) || defined(__PGIC__)
#if !defined(__PGIC__) || !defined(__PGIC_MINOR__)
sprintf(cast_char compiler_name, "The Portland Group C");
#else
int v1 = __PGIC__+0;
int v2 = __PGIC_MINOR__+0;
#ifdef __PGIC_PATCHLEVEL__
int v3 = __PGIC_PATCHLEVEL__+0;
#else
int v3 = 0;
#endif
if (v3 > 0) sprintf(cast_char compiler_name, "The Portland Group C %d.%d.%d", v1, v2, v3);
else sprintf(cast_char compiler_name, "The Portland Group C %d.%d", v1, v2);
#endif
#elif defined(__SASC__)
int w = __SASC__+0;
int v1 = w / 100;
int v2 = w % 100;
sprintf(cast_char compiler_name, "SAS C %d.%02d", v1, v2);
#elif (defined(__sgi) && defined(_COMPILER_VERSION)) || defined(_SGI_COMPILER_VERSION)
#ifdef _SGI_COMPILER_VERSION
int w = _SGI_COMPILER_VERSION;
#else
int w = _COMPILER_VERSION;
#endif
int v1 = w / 100;
int v2 = w / 10 % 10;
int v3 = w % 10;
sprintf(cast_char compiler_name, "MIPSpro %d.%d.%d", v1, v2, v3);
#elif defined(__SUNPRO_C)
int w = __SUNPRO_C+0;
int div = w >= 0x1000 ? 0x1000 : 0x100;
int v2_digits = w >= 0x1000 ? 2 : 1;
int v1 = w / div;
int v2 = w % div / 0x10;
int v3 = w % 0x10;
if (!v3) sprintf(cast_char compiler_name, "Sun C %X.%0*X", v1, v2_digits, v2);
else sprintf(cast_char compiler_name, "Sun C %X.%0*X.%X", v1, v2_digits, v2, v3);
#elif defined(__SYSC__) && defined(__SYSC_VER__)
int w = __SYSC_VER__+0;
int v1 = w / 10000;
int v2 = w / 100 % 100;
int v3 = w % 100;
sprintf(cast_char compiler_name, "Dignus Systems C %d.%02d.%02d", v1, v2, v3);
#elif defined(__TenDRA__)
sprintf(cast_char compiler_name, "TenDRA C");
#elif defined(__TINYC__)
sprintf(cast_char compiler_name, "Tiny C");
#elif defined(_UCC)
#if !defined(_MAJOR_REV) || !defined(_MINOR_REV)
sprintf(cast_char compiler_name, "Ultimate C");
#else
int v1 = _MAJOR_REV+0;
int v2 = _MAJOR_REV+0;
sprintf(cast_char compiler_name, "Ultimate C %d.%d", v1, v2);
#endif
#elif defined(__USLC__)
sprintf(cast_char compiler_name, "USL C");
#elif defined(__VAXC)
sprintf(cast_char compiler_name, "VAX C");
#elif defined(__VOSC__)
sprintf(cast_char compiler_name, "Stratus VOS C");
#elif defined(__WATCOMC__)
int w = __WATCOMC__+0;
int v1 = w / 100;
int v2 = w % 100;
unsigned char *op = cast_uchar "";
if (v1 >= 12) {
v1 -= 11;
op = cast_uchar "Open";
}
if (!(v2 % 10)) sprintf(cast_char compiler_name, "%sWatcom C %d.%d", op, v1, v2 / 10);
else sprintf(cast_char compiler_name, "%sWatcom C %d.%02d", op, v1, v2);
#elif defined(__GNUC__)
int v1 = __GNUC__+0;
#ifdef __GNUC_MINOR__
int v2 = __GNUC_MINOR__+0;
#else
int v2 = -1;
#endif
#ifdef __GNUC_PATCHLEVEL__
int v3 = __GNUC_PATCHLEVEL__+0;
#else
int v3 = 0;
#endif
#if defined(__llvm__)
unsigned char *prefix = cast_uchar "LLVM/";
#else
unsigned char *prefix = cast_uchar "";
#endif
if (v1 == 2 && (v2 >= 90 && v2 <= 91)) sprintf(cast_char compiler_name, "%sEGCS 1.%d", prefix, v2 - 90);
else if (v3 > 0 && v2 >= 0) sprintf(cast_char compiler_name, "%sGNU C %d.%d.%d", prefix, v1, v2, v3);
else if (v2 >= 0) sprintf(cast_char compiler_name, "%sGNU C %d.%d", prefix, v1, v2);
else sprintf(cast_char compiler_name, "%sGNU C %d", prefix, v1);
#else
strcpy(cast_char compiler_name, "unknown compiler");
#endif
}
static void do_exit(int x)
{
/* avoid compiler warnings about unreachable code */
if (value_1()) exit(x);
}
struct option {
int p;
unsigned char *(*rd_cmd)(struct option *, unsigned char ***, int *);
unsigned char *(*rd_cfg)(struct option *, unsigned char *);
void (*wr_cfg)(struct option *, unsigned char **, int *);
int min, max; /* for double min and max are in 1/100's (e.g. 0.1 is min==10) */
void *ptr;
char *cfg_name;
char *cmd_name;
};
static unsigned char *p_arse_options(int argc, unsigned char *argv[], struct option **opt)
{
unsigned char *e, *u = NULL;
int i;
for (i = 0; i < argc; i++) {
if (strlen(cast_const_char argv[i]) >= MAXINT) {
fprintf(stderr, "Too long parameter\n");
return NULL;
}
}
while (argc) {
argv++, argc--;
if (argv[-1][0] == '-') {
struct option *options;
struct option **op;
for (op = opt; (options = *op); op++) for (i = 0; options[i].p; i++)
if (options[i].rd_cmd && options[i].cmd_name &&
!casestrcmp(cast_uchar options[i].cmd_name, &argv[-1][1])) {
if ((e = options[i].rd_cmd(&options[i], &argv, &argc))) {
if (e[0]) fprintf(stderr, "Error parsing option %s: %s\n", argv[-1], e);
return NULL;
}
goto found;
}
uu:
#ifdef GRDRV_DIRECTFB
if (!strncmp(cast_const_char argv[-1], "--dfb:", 6))
goto found;
#endif
fprintf(stderr, "Unknown option %s\n", argv[-1]);
return NULL;
} else if (!u) u = argv[-1];
else goto uu;
found:;
}
if (u) return u;
return cast_uchar "";
}
static unsigned char *get_token(unsigned char **line)
{
unsigned char *s = NULL;
int l = 0;
int escape = 0;
int quote = 0;
while (**line == ' ' || **line == 9) (*line)++;
if (**line) {
for (s = init_str(); **line; (*line)++) {
if (escape)
escape = 0;
else if (**line == '\\') {
escape = 1;
continue;
}
else if (**line == '"') {
quote = !quote;
continue;
}
else if ((**line == ' ' || **line == 9) && !quote)
break;
add_chr_to_str(&s, &l, **line);
}
}
return s;
}
static int get_token_num(unsigned char **line, int min, int max, int *result)
{
long l;
unsigned char *end;
unsigned char *t = get_token(line);
if (!t) return -1;
l = strtolx(t, &end);
if (*end || end == t || l < min || l > max) {
mem_free(t);
return -2;
}
mem_free(t);
*result = (int)l;
return 0;
}
static void parse_config_file(unsigned char *name, unsigned char *file, struct option **opt)
{
struct option *options;
struct option **op;
int err = 0;
int line = 0;
unsigned char *e;
int i;
unsigned char *n, *p;
unsigned char *tok;
int nl, pl;
while (file[0]) {
line++;
while (file[0] && (file[0] == ' ' || file[0] == 9)) file++;
n = file;
while (file[0] && file[0] > ' ') file++;
if (file == n) {
if (file[0]) file++;
continue;
}
while (file[0] == 9 || file[0] == ' ') file++;
p = file;
while (file[0] && file[0] != 10 && file[0] != 13) file++;
pl = (int)(file - p);
if (file[0]) {
if ((file[1] == 10 || file[1] == 13) && file[0] != file[1]) file++;
file++;
}
tok = NULL;
if (n[0] == '#') goto f;
if (!(tok = get_token(&n))) goto f;
nl = (int)strlen(cast_const_char tok);
for (op = opt; (options = *op); op++)
for (i = 0; options[i].p; i++) if (options[i].cfg_name && (size_t)nl == strlen(cast_const_char options[i].cfg_name) && !casecmp(tok, cast_uchar options[i].cfg_name, nl)) {
unsigned char *o = memacpy(p, pl);
if (options[i].rd_cfg && (e = options[i].rd_cfg(&options[i], o))) {
if (e[0]) fprintf(stderr, "Error parsing config file %s, line %d: %s\n", name, line, e), err = 1;
}
mem_free(o);
goto f;
}
fprintf(stderr, "Unknown option in config file %s, line %d\n", name, line);
err = 1;
f:
if (tok) mem_free(tok);
}
if (err) fprintf(stderr, "\007"), portable_sleep(1000);
}
static unsigned char *create_config_string(struct option *options)
{
unsigned char *s = init_str();
int l = 0;
int i;
add_to_str(&s, &l, cast_uchar "# This file is automatically generated by Links -- please do not edit.");
#if defined(__DECC_VER) && !defined(OPENVMS)
do_not_optimize_here(&options);
#endif
for (i = 0; options[i].p; i++) {
if (options[i].wr_cfg)
options[i].wr_cfg(&options[i], &s, &l);
}
add_to_str(&s, &l, cast_uchar NEWLINE);
return s;
}
unsigned char *read_config_file(unsigned char *name)
{
int h, r;
int l = 0;
unsigned char *cfg_buffer, *s;
int rs;
h = c_open(name, O_RDONLY | O_NOCTTY);
if (h == -1) return NULL;
s = init_str();
cfg_buffer = mem_alloc(page_size);
while ((r = hard_read(h, cfg_buffer, page_size)) > 0) {
int i;
for (i = 0; i < r; i++) if (!cfg_buffer[i]) cfg_buffer[i] = ' ';
add_bytes_to_str(&s, &l, cfg_buffer, r);
}
mem_free(cfg_buffer);
if (r == -1) mem_free(s), s = NULL;
EINTRLOOP(rs, close(h));
return s;
}
int write_to_config_file(unsigned char *name, unsigned char *c, int do_sync)
{
int rr;
int h, w;
int count = 0;
int tmp_namel;
unsigned char *tmp_name;
int rs, err;
try_new_count:
tmp_namel = 0;
tmp_name = init_str();
add_to_str(&tmp_name, &tmp_namel, name);
for (w = tmp_namel - 1; w >= 0; w--) {
if (dir_sep(tmp_name[w]))
break;
if (tmp_name[w] == '.') {
if (w <= tmp_namel - 2) {
tmp_name[w + 2] = 0;
tmp_namel = w + 2;
}
break;
}
}
add_num_to_str(&tmp_name, &tmp_namel, count);
h = c_open3(tmp_name, O_WRONLY | O_NOCTTY | O_CREAT | O_TRUNC | O_EXCL, 0600);
if (h == -1) {
err = errno;
if (err == EEXIST && count < MAXINT) {
count++;
mem_free(tmp_name);
goto try_new_count;
}
goto free_err;
}
rr = (int)strlen(cast_const_char c);
if (hard_write(h, c, rr) != rr) {
err = errno;
goto close_unlink_err;
}
if (do_sync) {
EINTRLOOP(rs, fsync(h));
if (rs) {
err = errno;
goto close_unlink_err;
}
#ifdef DOS
_flush_disk_cache();
#endif
}
EINTRLOOP(rs, close(h));
if (rs) {
err = errno;
goto unlink_err;
}
#if defined(OPENVMS)
/* delete all versions of the file */
count = 0;
do {
EINTRLOOP(rs, unlink(cast_const_char name));
} while (!rs && ++count < 65536);
#elif !defined(RENAME_OVER_EXISTING_FILES)
EINTRLOOP(rs, unlink(cast_const_char name));
#endif
EINTRLOOP(rs, rename(cast_const_char tmp_name, cast_const_char name));
if (rs) {
err = errno;
goto unlink_err;
}
mem_free(tmp_name);
if (do_sync) {
#if defined(DOS)
_flush_disk_cache();
#elif !defined(OS2) && !defined(OPENVMS)
unsigned char *e, *le;
tmp_name = stracpy(name);
le = tmp_name;
for (e = tmp_name; *e; e++) if (dir_sep(*e)) le = e;
while (le > tmp_name && dir_sep(le[-1])) le--;
if (le == tmp_name && dir_sep(*le)) le++;
#ifdef DOS_FS
if (le - tmp_name <= 2 && upcase(tmp_name[0]) >= 'A' && upcase(tmp_name[0]) <= 'Z' && tmp_name[1] == ':') {
if (dir_sep(tmp_name[2]))
le = tmp_name + 3;
else
le = tmp_name + 2;
}
#endif
*le = 0;
h = c_open(*tmp_name ? tmp_name : cast_uchar ".", O_RDONLY | O_NOCTTY);
if (h != -1) {
EINTRLOOP(rs, fsync(h));
EINTRLOOP(rs, close(h));
}
mem_free(tmp_name);
#endif
}
return 0;
close_unlink_err:
EINTRLOOP(rs, close(h));
unlink_err:
EINTRLOOP(rs, unlink(cast_const_char tmp_name));
free_err:
mem_free(tmp_name);
return get_error_from_errno(err);
}
#ifdef OPENVMS
static void translate_vms_to_unix(unsigned char **str)
{
unsigned char *n;
if (!*str || strchr(cast_const_char *str, '/')) return;
n = cast_uchar decc$translate_vms(cast_const_char *str);
if (!n || (my_intptr_t)n == -1) return;
mem_free(*str);
*str = stracpy(n);
}
#endif
static unsigned char *get_home(int *n)
{
struct stat st;
int rs;
unsigned char *home;
unsigned char *home_links;
unsigned char *config_dir;
EINTRLOOP(rs, stat(".", &st));
if (rs && (home = cast_uchar getenv("HOME")))
EINTRLOOP(rs, chdir(cast_const_char home));
home = NULL;
config_dir = stracpy(cast_uchar getenv("CONFIG_DIR"));
if (n) *n = 1;
#ifdef WIN
if (!home) {
home = stracpy(cast_uchar getenv("APPDATA"));
#ifdef HAVE_CYGWIN_CONV_PATH
/*
* Newer Cygwin complains about windows-style path, so
* we have to convert it.
*/
if (home) {
unsigned char *new_path;
ssize_t sz = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, home, NULL, 0);
if (sz < 0 || sz >= MAXINT)
goto skip_path_conv;
new_path = mem_alloc(sz);
sz = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, home, new_path, sz);
if (sz < 0) {
mem_free(new_path);
goto skip_path_conv;
}
mem_free(home);
home = new_path;
skip_path_conv:;
}
#endif
if (home) {
EINTRLOOP(rs, stat(cast_const_char home, &st));
if (rs || !S_ISDIR(st.st_mode)) {
mem_free(home);
home = NULL;
}
}
}
#endif
#ifdef HAIKU
home = mem_alloc(B_FILE_NAME_LENGTH);
if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, 1, cast_char home, B_FILE_NAME_LENGTH) != B_OK) {
mem_free(home);
home = NULL;
}
#endif
if (!home) home = stracpy(cast_uchar getenv("HOME"));
#ifdef WIN
/* When we run in Cygwin without Cygwin environment, it reports home "/".
Unfortunatelly, it can't write anything to that directory */
if (home && !strcmp(cast_const_char home, "/")) {
mem_free(home);
home = NULL;
}
#endif
#ifdef OPENVMS
if (!home) home = stracpy(cast_uchar "/SYS$LOGIN");
translate_vms_to_unix(&home);
translate_vms_to_unix(&config_dir);
#endif
if (!home) {
int i;
home = stracpy(path_to_exe);
if (!home) {
if (config_dir) mem_free(config_dir);
return NULL;
}
for (i = (int)strlen(cast_const_char home) - 1; i >= 0; i--) if (dir_sep(home[i])) {
home[i + 1] = 0;
goto br;
}
home[0] = 0;
br:;
}
while (home[0] && home[1] && dir_sep(home[strlen(cast_const_char home) - 1])) home[strlen(cast_const_char home) - 1] = 0;
if (home[0]) add_to_strn(&home, cast_uchar "/");
home_links = stracpy(home);
if (config_dir) {
add_to_strn(&home_links, config_dir);
while (home_links[0] && dir_sep(home_links[strlen(cast_const_char home_links) - 1])) home_links[strlen(cast_const_char home_links) - 1] = 0;
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (!rs && S_ISDIR(st.st_mode)) {
add_to_strn(&home_links, cast_uchar "/links");
} else {
fprintf(stderr, "CONFIG_DIR set to %s. But directory %s doesn't exist.\n\007", config_dir, home_links);
portable_sleep(3000);
mem_free(home_links);
home_links = stracpy(home);
goto add_dot_links;
}
} else {
add_dot_links:
#if defined(DOS)
add_to_strn(&home_links, cast_uchar "links.cfg");
#elif defined(OPENVMS) || defined(HAIKU)
add_to_strn(&home_links, cast_uchar "links");
#else
add_to_strn(&home_links, cast_uchar ".links");
#endif
}
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (rs) {
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#ifdef OPENVMS
if (errno == EEXIST) {
memset(&st, 0, sizeof st);
goto home_ok;
}
#endif
if (config_dir) goto failed;
goto first_failed;
}
if (S_ISDIR(st.st_mode)) goto home_ok;
/* This is a Cygwin hack! Cygwin reports stat for "links" if no
"links" exists and only "links.exe" does. So try to create directory
anyway. */
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
first_failed:
mem_free(home_links);
home_links = stracpy(home);
#ifdef DOS
add_to_strn(&home_links, cast_uchar "links.cfg");
#else
add_to_strn(&home_links, cast_uchar "links");
#endif
EINTRLOOP(rs, stat(cast_const_char home_links, &st));
if (rs) {
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
#ifdef OPENVMS
if (errno == EEXIST) {
memset(&st, 0, sizeof st);
goto home_ok;
}
#endif
goto failed;
}
if (S_ISDIR(st.st_mode)) goto home_ok;
EINTRLOOP(rs, mkdir(cast_const_char home_links, 0700));
if (!rs) goto home_creat;
failed:
mem_free(home_links);
mem_free(home);
if (config_dir) mem_free(config_dir);
return NULL;
home_ok:
if (n) *n = 0;
if ((st.st_mode & 07777) != 0700) {
home_creat:
#ifdef HAVE_CHMOD
EINTRLOOP(rs, chmod(cast_const_char home_links, 0700));
#endif
}
add_to_strn(&home_links, cast_uchar "/");
mem_free(home);
if (config_dir) mem_free(config_dir);
return home_links;
}
void init_home(void)
{
get_system_name();
get_compiler_name();
links_home = get_home(&first_use);
if (!links_home) {
fprintf(stderr, "Unable to find or create links config directory. Please check, that you have $HOME variable set correctly and that you have write permission to your home directory.\n\007");
portable_sleep(3000);
return;
}
}
/* prefix: directory
* name: name of the configuration file (typ. links.cfg)
*/
static int write_config_data(unsigned char *prefix, unsigned char *name, struct option *o, struct terminal *term)
{
int err;
unsigned char *c, *config_file;
if (!(c = create_config_string(o))) return -1;
config_file = stracpy(prefix);
if (!config_file) {
mem_free(c);
if (term) msg_box(term, NULL, TEXT_(T_CONFIG_ERROR), AL_CENTER, TEXT_(T_UNABLE_TO_WRITE_TO_CONFIG_FILE), cast_uchar ": ", TEXT_(T_HOME_DIRECTORY_INACCESSIBLE), MSG_BOX_END, NULL, 1, TEXT_(T_CANCEL), msg_box_null, B_ENTER | B_ESC);
return -1;
}
add_to_strn(&config_file, name);
if ((err = write_to_config_file(config_file, c, 1))) {
if (term) msg_box(term, NULL, TEXT_(T_CONFIG_ERROR), AL_CENTER, TEXT_(T_UNABLE_TO_WRITE_TO_CONFIG_FILE), cast_uchar ": ", get_err_msg(err, term), MSG_BOX_END, NULL, 1, TEXT_(T_CANCEL), msg_box_null, B_ENTER | B_ESC);
mem_free(c);
mem_free(config_file);
return -1;
}
mem_free(c);
mem_free(config_file);
return 0;
}
static void add_nm(struct option *o, unsigned char **s, int *l)
{
if (*l) add_to_str(s, l, cast_uchar NEWLINE);
add_to_str(s, l, cast_uchar o->cfg_name);
add_chr_to_str(s, l, ' ');
}
static void add_quoted_to_str(unsigned char **s, int *l, unsigned char *q)
{
add_chr_to_str(s, l, '"');
while (*q) {
if (*q == '"' || *q == '\\') add_chr_to_str(s, l, '\\');
add_chr_to_str(s, l, *q);
q++;
}
add_chr_to_str(s, l, '"');
}
static unsigned char *num_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
unsigned char *end;
long l;
if (!tok) return cast_uchar "Missing argument";
l = strtolx(tok, &end);
if (*end) {
mem_free(tok);
return cast_uchar "Number expected";
}
if (l < o->min || l > o->max || l != (long)(int)l) {
mem_free(tok);
return cast_uchar "Out of range";
}
*(int *)o->ptr = (int)l;
mem_free(tok);
return NULL;
}
static void num_wr(struct option *o, unsigned char **s, int *l)
{
add_nm(o, s, l);
add_knum_to_str(s, l, *(int *)o->ptr);
}
static unsigned char *dbl_rd(struct option *o, unsigned char *c)
{
unsigned char *tok = get_token(&c);
char *end;
double d;
if (!tok) return cast_uchar "Missing argument";
if (strlen(cast_const_char tok) >= 1000) {
mem_free(tok);
return cast_uchar "Number is too long";
}
d = strtod(cast_const_char tok, &end);
if (*end) {
mem_free(tok);
return cast_uchar "Number expected";
}
if (d < 0 || d > o->max || 100*d < o->min || 100*d > o->max) {
mem_free(tok);
return cast_uchar "Out of range";
}
*(double *)o->ptr = d;
mem_free(tok);
return NULL;
}
static void dbl_wr(struct option *o, unsigned char **s, int *l)
{
unsigned char number[80];
snprintf(cast_char number, sizeof number, "%.6f", *(double*)o->ptr);
add_nm(o, s, l);
add_to_str(s, l, number);
}
static unsigned char *str_rd(struct option *o, unsigned char *c)
{