-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbi_funct.c
1396 lines (1189 loc) · 27.9 KB
/
bi_funct.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
/********************************************
bi_funct.c
copyright 2008-2013,2014, Thomas E. Dickey
copyright 1991-1995,1996, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: bi_funct.c,v 1.102 2014/09/12 00:02:25 tom Exp $
* @Log: bi_funct.c,v @
* Revision 1.9 1996/01/14 17:16:11 mike
* flush_all_output() before system()
*
* Revision 1.8 1995/08/27 18:13:03 mike
* fix random number generator to work with longs larger than 32bits
*
* Revision 1.7 1995/06/09 22:53:30 mike
* change a memcmp() to strncmp() to make purify happy
*
* Revision 1.6 1994/12/13 00:26:32 mike
* rt_nr and rt_fnr for run-time error messages
*
* Revision 1.5 1994/12/11 22:14:11 mike
* remove THINK_C #defines. Not a political statement, just no indication
* that anyone ever used it.
*
* Revision 1.4 1994/12/10 21:44:12 mike
* fflush builtin
*
* Revision 1.3 1993/07/14 11:46:36 mike
* code cleanup
*
* Revision 1.2 1993/07/14 01:22:27 mike
* rm SIZE_T
*
* Revision 1.1.1.1 1993/07/03 18:58:08 mike
* move source to cvs
*
* Revision 5.5 1993/02/13 21:57:18 mike
* merge patch3
*
* Revision 5.4 1993/01/01 21:30:48 mike
* split new_STRING() into new_STRING and new_STRING0
*
* Revision 5.3.1.2 1993/01/27 01:04:06 mike
* minor tuning to str_str()
*
* Revision 5.3.1.1 1993/01/15 03:33:35 mike
* patch3: safer double to int conversion
*
* Revision 5.3 1992/12/17 02:48:01 mike
* 1.1.2d changes for DOS
*
* Revision 5.2 1992/07/08 15:43:41 brennan
* patch2: length returns. I am a wimp
*
* Revision 5.1 1991/12/05 07:55:35 brennan
* 1.1 pre-release
*/
#include <mawk.h>
#include <bi_funct.h>
#include <bi_vars.h>
#include <memory.h>
#include <init.h>
#include <files.h>
#include <fin.h>
#include <field.h>
#include <regexp.h>
#include <repl.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#if defined(mawk_srand) || defined(mawk_rand)
#define USE_SYSTEM_SRAND
#endif
#if defined(HAVE_BSD_STDLIB_H) && defined(USE_SYSTEM_SRAND)
#include <bsd/stdlib.h> /* prototype arc4random */
#endif
#if defined(WINVER) && (WINVER >= 0x501)
#include <windows.h>
#endif
#if OPT_TRACE > 0
#define return_CELL(func, cell) TRACE(("..." func " ->")); \
TRACE_CELL(cell); \
return cell
#else
#define return_CELL(func, cell) return cell
#endif
/* global for the disassembler */
/* *INDENT-OFF* */
BI_REC bi_funct[] =
{ /* info to load builtins */
{ "length", bi_length, 0, 1 }, /* special must come first */
{ "index", bi_index, 2, 2 },
{ "substr", bi_substr, 2, 3 },
{ "sprintf", bi_sprintf, 1, 255 },
{ "sin", bi_sin, 1, 1 },
{ "cos", bi_cos, 1, 1 },
{ "atan2", bi_atan2, 2, 2 },
{ "exp", bi_exp, 1, 1 },
{ "log", bi_log, 1, 1 },
{ "int", bi_int, 1, 1 },
{ "sqrt", bi_sqrt, 1, 1 },
{ "rand", bi_rand, 0, 0 },
{ "srand", bi_srand, 0, 1 },
{ "close", bi_close, 1, 1 },
{ "system", bi_system, 1, 1 },
{ "toupper", bi_toupper, 1, 1 },
{ "tolower", bi_tolower, 1, 1 },
{ "fflush", bi_fflush, 0, 1 },
/* useful gawk extension (time functions) */
{ "systime", bi_systime, 0, 0 },
#ifdef HAVE_MKTIME
{ "mktime", bi_mktime, 1, 1 },
#endif
#ifdef HAVE_STRFTIME
{ "strftime", bi_strftime, 0, 3 },
#endif
{ (char *) 0, (PF_CP) 0, 0, 0 }
};
/* *INDENT-ON* */
/* load built-in functions in symbol table */
void
bi_funct_init(void)
{
register BI_REC *p;
register SYMTAB *stp;
/* length is special (posix bozo) */
stp = insert(bi_funct->name);
stp->type = ST_LENGTH;
stp->stval.bip = bi_funct;
for (p = bi_funct + 1; p->name; p++) {
stp = insert(p->name);
stp->type = ST_BUILTIN;
stp->stval.bip = p;
}
#ifndef NO_INIT_SRAND
/* seed rand() off the clock */
{
CELL c[2];
memset(c, 0, sizeof(c));
c[1].type = C_NOINIT;
bi_srand(c + 1);
}
#endif
}
/**************************************************
string builtins (except split (in split.c) and [g]sub (at end))
**************************************************/
CELL *
bi_length(CELL *sp)
{
size_t len;
TRACE_FUNC("bi_length", sp);
if (sp->type == 0)
cellcpy(sp, field);
else
sp--;
if (sp->type < C_STRING)
cast1_to_s(sp);
len = string(sp)->len;
free_STRING(string(sp));
sp->type = C_DOUBLE;
sp->dval = (double) len;
return_CELL("bi_length", sp);
}
char *
str_str(char *target, size_t target_len, char *key, size_t key_len)
{
register int k = key[0];
int k1;
char *prior;
char *result = 0;
switch (key_len) {
case 0:
break;
case 1:
if (target_len != 0) {
result = memchr(target, k, target_len);
}
break;
case 2:
k1 = key[1];
prior = target;
while (target_len >= key_len && (target = memchr(target, k, target_len))) {
target_len = target_len - (size_t) (target - prior) - 1;
prior = ++target;
if (target[0] == k1) {
result = target - 1;
break;
}
}
break;
default:
key_len--;
prior = target;
while (target_len > key_len && (target = memchr(target, k, target_len))) {
target_len = target_len - (size_t) (target - prior) - 1;
prior = ++target;
if (memcmp(target, key + 1, key_len) == 0) {
result = target - 1;
break;
}
}
break;
}
return result;
}
CELL *
bi_index(CELL *sp)
{
size_t idx;
size_t len;
const char *p;
TRACE_FUNC("bi_index", sp);
sp--;
if (TEST2(sp) != TWO_STRINGS)
cast2_to_s(sp);
if ((len = string(sp + 1)->len)) {
idx = (size_t) ((p = str_str(string(sp)->str,
string(sp)->len,
string(sp + 1)->str,
len))
? p - string(sp)->str + 1
: 0);
} else { /* index of the empty string */
idx = 1;
}
free_STRING(string(sp));
free_STRING(string(sp + 1));
sp->type = C_DOUBLE;
sp->dval = (double) idx;
return_CELL("bi_index", sp);
}
/* substr(s, i, n)
if l = length(s) then get the characters
from max(1,i) to min(l,n-i-1) inclusive */
CELL *
bi_substr(CELL *sp)
{
int n_args, len;
register int i, n;
STRING *sval; /* substr(sval->str, i, n) */
TRACE_FUNC("bi_substr", sp);
n_args = sp->type;
sp -= n_args;
if (sp->type != C_STRING)
cast1_to_s(sp);
/* don't use < C_STRING shortcut */
sval = string(sp);
if ((len = (int) sval->len) == 0) /* substr on null string */
{
if (n_args == 3) {
cell_destroy(sp + 2);
}
cell_destroy(sp + 1);
return_CELL("bi_substr", sp);
}
if (n_args == 2) {
n = len;
if (sp[1].type != C_DOUBLE) {
cast1_to_d(sp + 1);
}
} else {
if (TEST2(sp + 1) != TWO_DOUBLES)
cast2_to_d(sp + 1);
n = d_to_i(sp[2].dval);
}
i = d_to_i(sp[1].dval) - 1; /* i now indexes into string */
/*
* If the starting index is past the end of the string, there is nothing
* to extract other than an empty string.
*/
if (i > len) {
n = 0;
}
/*
* Workaround in case someone's written a script that does substr(0,last-1)
* by transforming it into substr(1,last).
*/
if (i < 0) {
n -= i + 1;
i = 0;
}
/*
* Keep 'n' from extending past the end of the string.
*/
if (n > len - i) {
n = len - i;
}
if (n <= 0) /* the null string */
{
sp->ptr = (PTR) & null_str;
null_str.ref_cnt++;
} else { /* got something */
sp->ptr = (PTR) new_STRING0((size_t) n);
memcpy(string(sp)->str, sval->str + i, (size_t) n);
}
free_STRING(sval);
return_CELL("bi_substr", sp);
}
/*
match(s,r)
sp[0] holds r, sp[-1] holds s
*/
CELL *
bi_match(CELL *sp)
{
char *p;
size_t length;
TRACE_FUNC("bi_match", sp);
if (sp->type != C_RE)
cast_to_RE(sp);
if ((--sp)->type < C_STRING)
cast1_to_s(sp);
cell_destroy(RSTART);
cell_destroy(RLENGTH);
RSTART->type = C_DOUBLE;
RLENGTH->type = C_DOUBLE;
p = REmatch(string(sp)->str,
string(sp)->len,
cast_to_re((sp + 1)->ptr),
&length,
0);
if (p) {
sp->dval = (double) (p - string(sp)->str + 1);
RLENGTH->dval = (double) length;
} else {
sp->dval = 0.0;
RLENGTH->dval = -1.0; /* posix */
}
free_STRING(string(sp));
sp->type = C_DOUBLE;
RSTART->dval = sp->dval;
return_CELL("bi_match", sp);
}
CELL *
bi_toupper(CELL *sp)
{
STRING *old;
register char *p, *q;
TRACE_FUNC("bi_toupper", sp);
if (sp->type != C_STRING)
cast1_to_s(sp);
old = string(sp);
sp->ptr = (PTR) new_STRING0(old->len);
q = string(sp)->str;
p = old->str;
while (*p) {
*q = *p++;
*q = (char) toupper((UChar) * q);
q++;
}
free_STRING(old);
return_CELL("bi_toupper", sp);
}
CELL *
bi_tolower(CELL *sp)
{
STRING *old;
register char *p, *q;
TRACE_FUNC("bi_tolower", sp);
if (sp->type != C_STRING)
cast1_to_s(sp);
old = string(sp);
sp->ptr = (PTR) new_STRING0(old->len);
q = string(sp)->str;
p = old->str;
while (*p) {
*q = *p++;
*q = (char) tolower((UChar) * q);
q++;
}
free_STRING(old);
return_CELL("bi_tolower", sp);
}
/*
* Like gawk...
*/
CELL *
bi_systime(CELL *sp)
{
time_t result;
time(&result);
TRACE_FUNC("bi_systime", sp);
sp++;
sp->type = C_DOUBLE;
sp->dval = (double) result;
return_CELL("bi_systime", sp);
}
#ifdef HAVE_MKTIME
/* mktime(datespec)
Turns datespec into a time stamp of the same form as returned by systime().
The datespec is a string of the form
YYYY MM DD HH MM SS [DST].
*/
CELL *
bi_mktime(CELL *sp)
{
time_t result;
struct tm my_tm;
STRING *sval = string(sp);
int error = 0;
TRACE_FUNC("bi_mktime", sp);
memset(&my_tm, 0, sizeof(my_tm));
switch (sscanf(sval->str, "%d %d %d %d %d %d %d",
&my_tm.tm_year,
&my_tm.tm_mon,
&my_tm.tm_mday,
&my_tm.tm_hour,
&my_tm.tm_min,
&my_tm.tm_sec,
&my_tm.tm_isdst)) {
case 7:
break;
case 6:
my_tm.tm_isdst = -1; /* ask mktime to get timezone */
break;
default:
error = 1; /* not enough data */
break;
}
if (error) {
result = -1;
} else {
my_tm.tm_year -= 1900;
my_tm.tm_mon -= 1;
result = mktime(&my_tm);
}
TRACE(("...bi_mktime(%s) ->%s", sval->str, ctime(&result)));
cell_destroy(sp);
sp->type = C_DOUBLE;
sp->dval = (double) result;
return_CELL("bi_mktime", sp);
}
#endif
/* strftime(format, timestamp, utc)
should be equal to gawk strftime. all parameters are optional:
format: ansi c strftime format descriptor. default is "%c"
timestamp: seconds since unix epoch. default is now
utc: when set and != 0 date is utc otherwise local. default is 0
*/
#ifdef HAVE_STRFTIME
CELL *
bi_strftime(CELL *sp)
{
const char *format = "%c";
time_t rawtime;
struct tm *ptm;
int n_args;
int utc;
STRING *sval = 0; /* strftime(sval->str, timestamp, utc) */
char buff[128];
size_t result;
TRACE_FUNC("bi_strftime", sp);
n_args = sp->type;
sp -= n_args;
if (n_args > 0) {
if (sp->type != C_STRING)
cast1_to_s(sp);
/* don't use < C_STRING shortcut */
sval = string(sp);
if ((int) sval->len != 0) /* strftime on valid format */
format = sval->str;
} else {
sp->type = C_STRING;
}
if (n_args > 1) {
if (sp[1].type != C_DOUBLE)
cast1_to_d(sp + 1);
rawtime = d_to_i(sp[1].dval);
} else {
time(&rawtime);
}
if (n_args > 2) {
if (sp[2].type != C_DOUBLE)
cast1_to_d(sp + 2);
utc = d_to_i(sp[2].dval);
} else {
utc = 0;
}
if (utc != 0)
ptm = gmtime(&rawtime);
else
ptm = localtime(&rawtime);
result = strftime(buff, sizeof(buff) / sizeof(buff[0]), format, ptm);
TRACE(("...bi_strftime (%s, \"%d.%d.%d %d.%d.%d %d\", %d) ->%s\n",
format,
ptm->tm_year,
ptm->tm_mon,
ptm->tm_mday,
ptm->tm_hour,
ptm->tm_min,
ptm->tm_sec,
ptm->tm_isdst,
utc,
buff));
if (sval)
free_STRING(sval);
sp->ptr = (PTR) new_STRING1(buff, result);
while (n_args > 1) {
n_args--;
cell_destroy(sp + n_args);
}
return_CELL("bi_strftime", sp);
}
#endif /* HAVE_STRFTIME */
/************************************************
arithmetic builtins
************************************************/
#if STDC_MATHERR
static void
fplib_err(
char *fname,
double val,
char *error)
{
rt_error("%s(%g) : %s", fname, val, error);
}
#endif
CELL *
bi_sin(CELL *sp)
{
TRACE_FUNC("bi_sin", sp);
#if ! STDC_MATHERR
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = sin(sp->dval);
#else
{
double x;
errno = 0;
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
x = sp->dval;
sp->dval = sin(sp->dval);
if (errno)
fplib_err("sin", x, "loss of precision");
}
#endif
return_CELL("bi_sin", sp);
}
CELL *
bi_cos(CELL *sp)
{
TRACE_FUNC("bi_cos", sp);
#if ! STDC_MATHERR
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = cos(sp->dval);
#else
{
double x;
errno = 0;
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
x = sp->dval;
sp->dval = cos(sp->dval);
if (errno)
fplib_err("cos", x, "loss of precision");
}
#endif
return_CELL("bi_cos", sp);
}
CELL *
bi_atan2(CELL *sp)
{
TRACE_FUNC("bi_atan2", sp);
#if ! STDC_MATHERR
sp--;
if (TEST2(sp) != TWO_DOUBLES)
cast2_to_d(sp);
sp->dval = atan2(sp->dval, (sp + 1)->dval);
#else
{
errno = 0;
sp--;
if (TEST2(sp) != TWO_DOUBLES)
cast2_to_d(sp);
sp->dval = atan2(sp->dval, (sp + 1)->dval);
if (errno)
rt_error("atan2(0,0) : domain error");
}
#endif
return_CELL("bi_atan2", sp);
}
CELL *
bi_log(CELL *sp)
{
TRACE_FUNC("bi_log", sp);
#if ! STDC_MATHERR
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = log(sp->dval);
#else
{
double x;
errno = 0;
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
x = sp->dval;
sp->dval = log(sp->dval);
if (errno)
fplib_err("log", x, "domain error");
}
#endif
return_CELL("bi_log", sp);
}
CELL *
bi_exp(CELL *sp)
{
TRACE_FUNC("bi_exp", sp);
#if ! STDC_MATHERR
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = exp(sp->dval);
#else
{
double x;
errno = 0;
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
x = sp->dval;
sp->dval = exp(sp->dval);
if (errno && sp->dval)
fplib_err("exp", x, "overflow");
/* on underflow sp->dval==0, ignore */
}
#endif
return_CELL("bi_exp", sp);
}
CELL *
bi_int(CELL *sp)
{
TRACE_FUNC("bi_int", sp);
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = sp->dval >= 0.0 ? floor(sp->dval) : ceil(sp->dval);
return_CELL("bi_int", sp);
}
CELL *
bi_sqrt(CELL *sp)
{
TRACE_FUNC("bi_sqrt", sp);
#if ! STDC_MATHERR
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
sp->dval = sqrt(sp->dval);
#else
{
double x;
errno = 0;
if (sp->type != C_DOUBLE)
cast1_to_d(sp);
x = sp->dval;
sp->dval = sqrt(sp->dval);
if (errno)
fplib_err("sqrt", x, "domain error");
}
#endif
return_CELL("bi_sqrt", sp);
}
#if !(defined(mawk_srand) || defined(mawk_rand))
/* For portability, we'll use our own random number generator , taken
from: Park, SK and Miller KW, "Random Number Generators:
Good Ones are Hard to Find", CACM, 31, 1192-1201, 1988.
*/
static long seed; /* must be >=1 and < 2^31-1 */
static CELL cseed; /* argument of last call to srand() */
#define M 0x7fffffff /* 2^31-1 */
#define MX 0xffffffff
#define A 16807
#define Q 127773 /* M/A */
#define R 2836 /* M%A */
#if M == MAX__LONG
#define crank(s) s = A * (s % Q) - R * (s / Q) ;\
if ( s <= 0 ) s += M
#else
/* 64 bit longs */
#define crank(s) { unsigned long t = (unsigned long) s ;\
t = (A * (t % Q) - R * (t / Q)) & MX ;\
if ( t >= M ) t = (t+M)&M ;\
s = (long) t ;\
}
#endif /* M == MAX__LONG */
#endif /* defined(mawk_srand) || defined(mawk_rand) */
static double
initial_seed(void)
{
double result;
#if defined(HAVE_GETTIMEOFDAY)
struct timeval data;
gettimeofday(&data, (struct timezone *) 0);
result = (data.tv_sec * 1000000) + data.tv_usec;
#elif defined(WINVER) && (WINVER >= 0x501)
union {
FILETIME ft;
long long since1601; /* time since 1 Jan 1601 in 100ns units */
} data;
GetSystemTimeAsFileTime(&data.ft);
result = (double) (data.since1601 / 10LL);
#else
time_t now;
(void) time(&now);
result = (double) now;
#endif
return result;
}
CELL *
bi_srand(CELL *sp)
{
#ifdef USE_SYSTEM_SRAND
static long seed = 1;
static CELL cseed =
{
C_DOUBLE, 0, 0, 1.0
};
#endif
CELL c;
TRACE_FUNC("bi_srand", sp);
if (sp->type == C_NOINIT) /* seed off clock */
{
cellcpy(sp, &cseed);
cell_destroy(&cseed);
cseed.type = C_DOUBLE;
cseed.dval = initial_seed();
} else { /* user seed */
sp--;
/* swap cseed and *sp ; don't need to adjust ref_cnts */
c = *sp;
*sp = cseed;
cseed = c;
}
#ifdef USE_SYSTEM_SRAND
seed = d_to_i(cseed.dval);
mawk_srand((unsigned) seed);
#else
/* The old seed is now in *sp ; move the value in cseed to
seed in range [1,M) */
cellcpy(&c, &cseed);
if (c.type == C_NOINIT)
cast1_to_d(&c);
seed = ((c.type == C_DOUBLE)
? (long) (d_to_i(c.dval) & M) % M + 1
: (long) hash(string(&c)->str) % M + 1);
if (seed == M)
seed = M - 1;
cell_destroy(&c);
/* crank it once so close seeds don't give a close
first result */
crank(seed);
#endif
return_CELL("bi_srand", sp);
}
CELL *
bi_rand(CELL *sp)
{
TRACE_FUNC("bi_rand", sp);
#ifdef USE_SYSTEM_SRAND
{
long value = (long) mawk_rand();
sp++;
sp->type = C_DOUBLE;
sp->dval = ((double) value) / RAND_MAX;
}
#else
crank(seed);
sp++;
sp->type = C_DOUBLE;
sp->dval = (double) seed / (double) M;
#endif
return_CELL("bi_rand", sp);
}
#undef A
#undef M
#undef MX
#undef Q
#undef R
#undef crank
/*************************************************
miscellaneous builtins
close, system and getline
fflush
*************************************************/
CELL *
bi_close(CELL *sp)
{
int x;
TRACE_FUNC("bi_close", sp);
if (sp->type < C_STRING)
cast1_to_s(sp);
x = file_close((STRING *) sp->ptr);
free_STRING(string(sp));
sp->type = C_DOUBLE;
sp->dval = (double) x;
return_CELL("bi_close", sp);
}
CELL *
bi_fflush(CELL *sp)
{
int ret = 0;
TRACE_FUNC("bi_fflush", sp);
if (sp->type == 0)
fflush(stdout);
else {
sp--;
if (sp->type < C_STRING)
cast1_to_s(sp);
ret = file_flush(string(sp));
free_STRING(string(sp));
}
sp->type = C_DOUBLE;
sp->dval = (double) ret;
return_CELL("bi_fflush", sp);
}
CELL *
bi_system(CELL *sp GCC_UNUSED)
{
#ifdef HAVE_REAL_PIPES
int pid;
unsigned ret_val;
TRACE_FUNC("bi_system", sp);
if (sp->type < C_STRING)
cast1_to_s(sp);
flush_all_output();
switch (pid = fork()) {
case -1: /* fork failed */
errmsg(errno, "could not create a new process");
ret_val = 127;
break;
case 0: /* the child */
execl(shell, shell, "-c", string(sp)->str, (char *) 0);
/* if get here, execl() failed */
errmsg(errno, "execute of %s failed", shell);
fflush(stderr);
_exit(127);
default: /* wait for the child */
ret_val = (unsigned) wait_for(pid);
break;
}
cell_destroy(sp);
sp->type = C_DOUBLE;
sp->dval = (double) ret_val;
#elif defined(MSDOS)
int retval;
if (sp->type < C_STRING)
cast1_to_s(sp);
retval = DOSexec(string(sp)->str);
free_STRING(string(sp));
sp->type = C_DOUBLE;
sp->dval = (double) retval;
#else
sp = 0;
#endif
return_CELL("bi_system", sp);
}