-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi_funct.c
2138 lines (1855 loc) · 49.7 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 1991,2014-2016 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 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
// #define _GNU_SOURCE
// #define __GLIBC__ // https://git.alpinelinux.org/aports/diff/unmaintained/ruby-posix-spawn/0001-Only-use-POSIX_SPAWN_USEVFORK-if-defined-or-if-GNU-l.patch?id=3ce3c4fd596debefbad77328a9b62a39eccf753c
#include "config.h" /* needed to resolve intersystem conflicts on C and C++ declarations of random() and srandom() */
#include "mawk.h"
#include "bi_funct.h"
#include "bi_vars.h"
#include "types_string.h"
#include "init.h"
#include "types_int.h"
#include "files.h"
#include "fin.h"
#include "field.h"
#include "regexp.h"
#include "repl.h"
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/wait.h>
#if defined( __cplusplus )
#define THROW__ throw()
#else
#define THROW__
#endif
#if !HAVE_DECL_RANDOM
extern long int random( void ) THROW__;
#endif
#if !HAVE_DECL_SRANDOM
extern void srandom( unsigned int __seed ) THROW__;
#endif
// D4
#include "xxhash.h"
// D4
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
// #include <ctype.h>
#include <string.h>
#include <err.h>
// D4 spawn
// #include <stdlib.h>
// #include <stdio.h>
// #include <string.h>
#include <unistd.h>
// D4 vfork
// #include <unistd.h>
#include <signal.h>
#include <err.h>
// #include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
// #include <stdio.h>
// https://stackoverflow.com/questions/2731531/faster-forking-of-large-processes-on-linux
// http://manpages.ubuntu.com/manpages/bionic/man3/posix_spawn.3.html
// #define _GNU_SOURCE
#include <spawn.h>
//
// #include <sys/wait.h>
#include <stdarg.h>
// #define _OPEN_SYS_ITOA_EXT
// #include <stdlib.h>
// char * itoa(int n, char * buffer, int radix);
// #define xhash(sval) hash2((sval)->str, (sval)->len)
// https://www.tutorialspotypes_int.com/tpcg.php?p=kpclzA
// https://stackoverflow.com/questions/4014827/best-way-to-switch-on-a-string-in-c
#ifndef SWITCH_CASE_INIT
#define SWITCH_CASE_INIT
#define SWITCH( X ) \
char * __switch_p__ = X; \
for ( int __switch_next__ = 1; __switch_p__; __switch_p__ = 0, __switch_next__ = 1 ) { \
{
#define CASE( X ) \
} \
if ( !__switch_next__ || !( __switch_next__ = strcmp( __switch_p__, X ) ) ) {
#define DEFAULT \
} \
{
#define END \
} \
}
#endif
/*
char *xx = stringf(9000, "hello this it %d !!", 100);
printf("%s",xx);
printf("Length of String is %lu\n", strlen(xx));
printf("Size of String is %lu\n", sizeof(xx));
*/
char *
stringf( const int maxlen, const char * fmt, ... )
{
char * p;
va_list ap;
if ( ( p = malloc( maxlen + 1 ) ) == NULL )
return ( NULL );
va_start( ap, fmt );
(void)vsnprintf( p, maxlen + 1, fmt, ap );
va_end( ap );
return ( p );
}
/*
char** a_v = NULL;
size_t a_c = 0;
// ---
printf("---\n");
a_print(a_v, a_c);
a_push(&a_v, &a_c, "Hello World!");
printf("%d: %s %s %s %s\n",a_c, a_v[0],a_v[1],a_v[2],a_v[3]);
printf("---\n");
a_print(a_v, a_c);
a_push(&a_v, &a_c, "123");
a_push(&a_v, &a_c, "AAAAAAA");
printf("---\n");
a_print(a_v, a_c);
printf("===\n");
printf("%d === ",a_c);
a_print(a_v, a_c);
char* popped = a_pop(&a_v, &a_c);
printf("pop -> %s\n",popped);
printf("%d === ",a_c);
a_print(a_v, a_c);
printf("---\n");
a_push(&a_v, &a_c, "BBBBBB");
printf("%d === ",a_c);
a_print(a_v, a_c);
while (a_c) {
printf("pop -> %s\n",a_pop(&a_v, &a_c));
printf("%d === ",a_c);
a_print(a_v, a_c);
}
printf("null pop -> %s\n",a_pop(&a_v, &a_c));
a_push(&a_v, &a_c, "11");
printf("push %d\n",a_c);
printf("%d: %s \n",a_c, a_v[0]);
a_push(&a_v, &a_c, "22");
printf("push %d\n",a_c);
printf("%d: %s %s \n",a_c, a_v[0], a_v[1]);
printf("===\n");
char** s = NULL;
s = realloc(s, ( 1) * sizeof(char*));
s[0] = NULL;
char* news = "hey";
s = realloc(s, (2) * sizeof(char*));
s[1] = news;
s = realloc(s, (3) * sizeof(char*));
s[2] = NULL;
printf("0: %s\n",s[0]);
printf("1: %s\n",s[1]);
printf("2: %s\n",s[2]);
// 0: (null)
// 1: hey
// 2: (null)
*/
int
a_pushNULL( char *** a_v, size_t * a_c )
{
*a_v = realloc( *a_v, ( *a_c + 1 ) * sizeof( char * ) );
( *a_v )[( *a_c )++] = NULL;
return 0;
}
int
a_push( char *** a_v, size_t * a_c, const char * newStr )
{
if ( newStr == NULL ) {
return a_pushNULL( a_v, a_c );
;
}
char * copy;
char ** p;
if ( ( copy = malloc( strlen( newStr ) + 1 ) ) == NULL ) {
return 1;
}
strcpy( copy, newStr );
// WARN: proper argv can be realloc'd b/c it cannot be freed
if ( ( p = realloc( *a_v, ( *a_c + 1 ) * sizeof( char * ) ) ) == NULL ) {
// If ptr is NULL, realloc() is identical to a call to malloc() for size bytes
free( copy );
return 1;
}
*a_v = p;
( *a_v )[( *a_c )++] = copy;
return 0;
}
char *
a_pop( char *** a_v, size_t * a_c )
{
if ( a_v == NULL || *a_c <= 0 ) {
return NULL;
}
int idx = --( *a_c );
char * newStr = ( *a_v )[idx];
char * copy;
if ( (
copy = malloc( strlen( newStr ) + 1 ) )
== NULL ) {
printf( "\n\n!!!!!!!???\n\n" );
return NULL;
}
strcpy( copy, newStr );
free( newStr );
char ** p;
if ( *a_c == 0 ) {
// *a_v = NULL;
// return copy;
// p = realloc(*a_v, 0);
// p = realloc(*a_v, sizeof(char*));
p = NULL;
free( *a_v );
}
else if ( ( p = realloc( *a_v, ( *a_c ) * sizeof( char * ) ) ) == NULL ) {
printf( "\n\n!!!!!!!\n\n" );
free( copy );
return 0;
}
*a_v = p;
// if (newStr == NULL) { // If ptr is a null pointer, no action occurs.
// }
// https://stackoverflow.com/questions/5666214/how-to-free-c-2d-array
// (*a_v)[idx] = NULL;
// printf(" pop a_c= %u\n",*a_c);
return copy;
}
char *
a_free( char *** a_v, size_t * a_c )
{
if ( a_v != NULL ) {
free( *a_v );
*a_v = NULL;
}
*a_c = 0;
return 0;
}
void
a_print( char ** a_v, size_t a_c )
{
if ( a_v != NULL )
while ( a_c-- )
printf( "%s ", *a_v++ );
printf( "\n" );
}
/* global for the disassembler */
BI_REC bi_funct[] = { /* info to load builtins */
{ "index", bi_index, 2, 2 },
{ "substr", bi_substr, 2, 3 },
{ "hash", bi_hash, 0, 1 }, // *
{ "shm", bi_shm, 1, 4 }, // *
{ "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 },
{ "spawn", bi_spawn, 1, 255 }, // *
{ "popen", bi_popen, 1, 1 }, // *
{ "system", bi_system, 1, 1 },
{ "toupper", bi_toupper, 1, 1 },
{ "tolower", bi_tolower, 1, 1 },
{ "fflush", bi_fflush, 0, 1 },
{ "fhash", bi_fhash, 1, 2 }, // *
{ 0, 0, 0, 0 }
};
double
infty_( void )
{
volatile double x;
x = 0.0;
return ( 1.0 / x );
}
double
nan_( const char * payload )
{
volatile double x, y;
x = y = 0.0;
return ( y / x );
}
/* load built-in functions in symbol table */
void
bi_funct_init( void )
{
register BI_REC * p;
register SYMTAB * stp;
for ( p = bi_funct; p->name; p++ ) {
stp = insert( p->name );
stp->type = ST_BUILTIN;
stp->stval.bip = p;
}
/*#if 0*/ /* zombie lock-step */
#if 1 /* NO zombie lock-step */
/* seed rand() off the clock */
{
CELL c;
c.type = 0;
bi_srand( &c );
}
#endif
}
/**************************************************
string builtins (except split (in split.c) and [g]sub (at end))
**************************************************/
CELL *
bi_length( CELL * sp )
{
size_t len;
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 sp;
}
/* length (size) of an array */
CELL *
bi_alength( CELL * sp )
{
ARRAY ap = (ARRAY)sp->ptr;
sp->type = C_DOUBLE;
sp->dval = (double)ap->size;
return sp;
}
/* Sedwick calls this "brute-force algorithm"
Might be worthwhile trying Boyer-Moore or Knuth...
In 1991 strstr returned 0 for null key.
Logically, it should return target.
So if use for something else modify first return.
However, leaving as is for mawk
*/
char *
str_str( const char * target, size_t target_len, const char * key, size_t key_len )
{
int k;
if ( key_len == 0 )
return 0;
k = key[0];
if ( key_len == 1 ) {
return target_len > 0 ? (char *)memchr( target, k, target_len ) : 0;
}
else {
const char * const end = target + target_len - key_len + 1;
while ( target < end ) {
target = (const char *)memchr( target, k, end - target );
if ( target == 0 )
return 0;
if ( memcmp( target + 1, key + 1, key_len - 1 ) == 0 ) {
return (char *)target;
}
target++;
}
return 0;
}
}
CELL *
bi_index( CELL * sp )
{
register int idx;
size_t len;
char * p;
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_STRINGS )
cast2_to_s( sp );
if ( ( len = string( sp + 1 )->len ) ) {
idx = ( 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 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) */
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 = sval->len ) == 0 ) /* substr on null string */
{
if ( n_args == 3 )
cell_destroy( sp + 2 );
cell_destroy( sp + 1 );
return sp;
}
if ( n_args == 2 ) {
n = MAX__INT;
if ( sp[1].type != C_DOUBLE )
cast1_to_d( sp + 1 );
}
else {
if ( CELL_PAIR_TYPE( sp + 1 ) != TWO_DOUBLES )
cast2_to_d( sp + 1 );
n = d_to_int( sp[2].dval );
}
i = d_to_int( sp[1].dval ) - 1; /* i now indexes into string */
if ( i < 0 ) {
n += i;
i = 0;
}
if ( n > len - i )
n = len - i;
if ( n <= 0 ) /* the null string */
{
sp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
else /* got something */
{
sp->ptr = (void *)new_STRING0( n );
memcpy( string( sp )->str, sval->str + i, n );
}
free_STRING( sval );
return sp;
}
/*
match(s,r)
sp[0] holds r, sp[-1] holds s
*/
CELL *
bi_match( CELL * sp )
{
char * p;
size_t length;
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, ( 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 sp;
}
CELL *
bi_toupper( CELL * sp )
{
STRING * old;
register char *p, *q;
if ( sp->type != C_STRING )
cast1_to_s( sp );
old = string( sp );
sp->ptr = (void *)new_STRING0( old->len );
q = string( sp )->str;
p = old->str;
while ( *p ) {
*q = *p++;
if ( *q >= 'a' && *q <= 'z' )
*q += 'A' - 'a';
q++;
}
free_STRING( old );
return sp;
}
CELL *
bi_tolower( CELL * sp )
{
STRING * old;
register char *p, *q;
if ( sp->type != C_STRING )
cast1_to_s( sp );
old = string( sp );
sp->ptr = (void *)new_STRING0( old->len );
q = string( sp )->str;
p = old->str;
while ( *p ) {
*q = *p++;
if ( *q >= 'A' && *q <= 'Z' )
*q += 'a' - 'A';
q++;
}
free_STRING( old );
return sp;
}
/************************************************
arithemetic builtins
************************************************/
#if STDC_MATHERR
static void
fplib_err( fname, val, error ) char * fname;
double val;
char * error;
{
rt_error( "%s(%g) : %s", fname, val, error );
}
#endif
CELL *
bi_sin( CELL * sp )
{
#if !STDC_MATHERR
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = sin( sp->dval );
return sp;
#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" );
return sp;
#endif
}
CELL *
bi_cos( CELL * sp )
{
#if !STDC_MATHERR
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = cos( sp->dval );
return sp;
#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" );
return sp;
#endif
}
CELL *
bi_atan2( CELL * sp )
{
#if !STDC_MATHERR
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
sp->dval = atan2( sp->dval, ( sp + 1 )->dval );
return sp;
#else
errno = 0;
sp--;
if ( CELL_PAIR_TYPE( sp ) != TWO_DOUBLES )
cast2_to_d( sp );
sp->dval = atan2( sp->dval, ( sp + 1 )->dval );
if ( errno )
rt_error( "atan2(0,0) : domain error" );
return sp;
#endif
}
CELL *
bi_log( CELL * sp )
{
#if !STDC_MATHERR
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
/* Test on many platforms show inconsistent handling of at least
negative arguments, so we do our own argument filtering here */
if ( sp->dval < 0.0 )
sp->dval = nan_( "" );
else if ( sp->dval == 0.0 )
sp->dval = -infty_();
else
sp->dval = log( sp->dval );
return sp;
#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" );
return sp;
#endif
}
CELL *
bi_exp( CELL * sp )
{
#if !STDC_MATHERR
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = exp( sp->dval );
return sp;
#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 */
return sp;
#endif
}
CELL *
bi_int( CELL * sp )
{
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = sp->dval >= 0.0 ? floor( sp->dval ) : ceil( sp->dval );
return sp;
}
CELL *
bi_sqrt( CELL * sp )
{
#if !STDC_MATHERR
if ( sp->type != C_DOUBLE )
cast1_to_d( sp );
sp->dval = sqrt( sp->dval );
return sp;
#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" );
return sp;
#endif
}
#ifdef USE_INTERNAL_RNG
/* This is the RNG from
* Park, SK and Miller KW, "Random Number Generators:
Good Ones are Hard to Find", CACM, 31, 1192-1201, 1988.
Note this implemenation requires sizeof(int) == 4.
Should use int32_t, but we only use this if random() is
missing which means <stdint.h> is missing too.
In 1993 Park and Miller suggested changing
A from 16807 to 48271 which we've done
*/
static int seed = 1;
static double
mawk_random( void )
{
static const int M = 0x7fffffff; /* 2^31-1 */
static const int A = 48271; /* used to be 16807 */
static const int Q = 0x7fffffff / 48271;
static const int R = 0x7fffffff % 48271;
int s = seed;
s = A * ( s % Q ) - R * ( s / Q );
if ( s <= 0 )
s += M;
seed = s;
return (double)s / (double)M;
}
static void
mawk_srand( unsigned int u )
{
u %= 0x7fffffff;
if ( u == 0 ) {
u = 314159265;
}
seed = u;
}
#else /* use random() and srandom() from stdlib */
static double
mawk_random( void )
{
return (double)random() / (double)RAND_MAX;
}
static void
mawk_srand( unsigned int u )
{
srandom( u );
}
#endif
static CELL prev_seed = /* for return value of srand() */
{ C_DOUBLE, 0, 1.0 };
CELL *
bi_srand( CELL * sp )
{
int nargs = sp->type;
CELL c;
if ( nargs == 0 ) {
/* srand() */
/* construct a random seed using gettimeofday,
* fractional bits should be quite random */
struct timeval tv;
char buffer[128];
gettimeofday( &tv, 0 );
sprintf( buffer, "#srand%ld.%ld#", (long)tv.tv_sec, (long)tv.tv_usec );
sp->type = C_STRING;
sp->ptr = new_STRING( buffer );
}
else {
sp--;
}
/* new seed is in *sp, swap it with prev_seed
* no ref_cnt adjust needed since a swap ,
* this puts the return value in *sp */
c = *sp;
*sp = prev_seed;
prev_seed = c;
cellcpy( &c, &prev_seed );
/* now use c to compute an unsigned for call to mawk_srand() */
{
unsigned uval;
int done = 0;
if ( c.type == C_DOUBLE ) {
/* if exact integer, make small minds happy */
double d = c.dval;
int ival = d_to_int( d );
if ( (double)ival == d ) {
uval = ival;
done = 1;
}
}
if ( !done ) {
cast1_to_s( &c );
uval = hash2( string( &c )->str, string( &c )->len );
cell_destroy( &c );
}
mawk_srand( uval );
}
return sp;
}
CELL *
bi_rand( CELL * sp )
{
sp++;
sp->type = C_DOUBLE;
sp->dval = mawk_random();
return sp;
}
/*************************************************
miscellaneous builtins
close, system and getline
fflush
*************************************************/
static double exit_status_d( int );
CELL *
bi_close( CELL * sp )
{
int x;
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 = exit_status_d( x );
return sp;
}
CELL *
bi_fflush( CELL * sp )
{
int ret = 0;
if ( sp->type == 0 ) {
if ( fflush( stdout ) < 0 ) {
write_error( stdout );
mawk_exit( 2 );
}
}
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 sp;
}
/* return from system(), wait(), pclose() is converted
to a normal exit status or 256 + # of killing signal */
static double
exit_status_d( int status )
{
int ret;
if ( status == -1 )
return -1.0;
if ( WIFEXITED( status ) ) {
ret = WEXITSTATUS( status );
}
else {
ret = WTERMSIG( status ) + 256; /* ok Arnold? */
}
return (double)ret;
}
CELL *
bi_system( CELL * sp )
{
int status;
if ( sp->type < C_STRING )
cast1_to_s( sp );
flush_all_output();
status = system( string( sp )->str );
cell_destroy( sp );
sp->type = C_DOUBLE;
sp->dval = exit_status_d( status );
return sp;
}
#if MSDOS
CELL *
bi_system( sp ) register CELL * sp;
{
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;
return sp;
}
#endif
/* getline() */
/* if type == 0 : stack is 0 , target address
if type == F_IN : stack is F_IN, expr(filename), target address
if type == PIPE_IN : stack is PIPE_IN, target address, expr(pipename)
*/
CELL *
bi_getline( CELL * sp )
{
CELL tc;
CELL * cp = 0;
char * p = 0;
size_t len;
FIN * fin_p;
switch ( sp->type ) {
case 0:
sp--;