-
Notifications
You must be signed in to change notification settings - Fork 87
/
hscutl.c
4737 lines (4121 loc) · 148 KB
/
hscutl.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
/* HSCUTL.C (C) Copyright Ivan Warren & Others, 2003-2012 */
/* (C) Copyright TurboHercules, SAS 2010-2011 */
/* (C) and others 2011-2023 */
/* Hercules Platform Port & Misc Functions */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/*-------------------------------------------------------------------*/
/* HSCUTL.C -- Implementation of functions used in hercules that */
/* may be missing on some platform ports, or other convenient mis- */
/* laneous global utility functions. */
/*-------------------------------------------------------------------*/
/* (c) 2003-2006 Ivan Warren & Others -- Released under the Q Public */
/* License -- This file is portion of the HERCULES S/370, S/390 and */
/* z/Architecture emulator */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _HSCUTL_C_
#define _HUTIL_DLL_
#include "hercules.h"
#if defined(NEED_GETOPT_WRAPPER)
/*
GETOPT DYNAMIC LINKING KLUDGE...
For some odd reason, on some platforms (namely windows & possibly
Darwin) dynamically linking to the libc imports STATIC versions
of 'getopt' and 'getopt_long' into the link-edited shared library.
If any program then link edits against BOTH the shared library AND
libc, then the linker complains about 'getopt' and/or 'getopt_long'
being defined multiple times. In an effort to overcome this, I am
defining a stub version of 'getopt' and 'getop_long' that can be
called by loadable modules instead.
-- Ivan
** ZZ FIXME: the above needs to be re-verified to see whether it is
** still true or not. I suspect it may no longer be true due to the
** subtle bug fixed in the "_HC_CHECK_NEED_GETOPT_WRAPPER" m4 macro
** in the "./autoconf/hercules.m4" member. -- Fish, Feb 2005
*/
int herc_opterr=0;
char *herc_optarg=NULL;
int herc_optopt=0;
int herc_optind=1;
#if defined(NEED_GETOPT_OPTRESET)
int herc_optreset=0;
#endif
DLL_EXPORT int herc_getopt(int ac,char * const av[],const char *opt)
{
int rc;
#if defined(NEED_GETOPT_OPTRESET)
optreset=herc_optreset;
#endif
rc=getopt(ac,av,opt);
#if defined(NEED_GETOPT_OPTRESET)
herc_optreset=optreset;
#endif
herc_optarg=optarg;
herc_optind=optind;
herc_optopt=optind;
herc_opterr=optind;
return(rc);
}
#if defined(HAVE_GETOPT_LONG)
struct option; // (fwd ref)
int getopt_long (int, char *const *, const char *, const struct option *, int *);
struct option; // (fwd ref)
DLL_EXPORT int herc_getopt_long(int ac,
char * const av[],
const char *opt,
const struct option *lo,
int *li)
{
int rc;
#if defined(NEED_GETOPT_OPTRESET)
optreset=herc_optreset;
#endif
optind=herc_optind;
rc=getopt_long(ac,av,opt,lo,li);
#if defined(NEED_GETOPT_OPTRESET)
herc_optreset=optreset;
#endif
herc_optarg=optarg;
herc_optind=optind;
herc_optopt=optind;
herc_opterr=optind;
return(rc);
}
#endif /* HAVE_GETOPT_LONG */
#endif /* NEED_GETOPT_WRAPPER */
#if !defined(WIN32) && !defined(HAVE_STRERROR_R)
static LOCK strerror_lock;
DLL_EXPORT void strerror_r_init(void)
{
initialize_lock(&strerror_lock);
}
DLL_EXPORT int strerror_r(int err,char *bfr,size_t sz)
{
char *wbfr;
obtain_lock(&strerror_lock);
wbfr=strerror(err);
if(wbfr==NULL || (int)wbfr==-1)
{
release_lock(&strerror_lock);
return(-1);
}
if(sz<=strlen(wbfr))
{
errno=ERANGE;
release_lock(&strerror_lock);
return(-1);
}
strncpy(bfr,wbfr,sz);
release_lock(&strerror_lock);
return(0);
}
#endif // !defined(HAVE_STRERROR_R)
#if !defined(HAVE_STRLCPY)
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
/* ** NOTE ** returns 'size_t' and NOT 'char*' like strncpy! */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $";
#endif /* LIBC_SCCS and not lint */
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
/* ** NOTE ** returns 'size_t' and NOT 'char*' like strncpy! */
DLL_EXPORT size_t
strlcpy(char *dst, const char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif // !defined(HAVE_STRLCPY)
#if !defined(HAVE_STRLCAT)
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
/* ** NOTE ** returns 'size_t' and NOT 'char*' like strncpy! */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $";
#endif /* LIBC_SCCS and not lint */
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
/* ** NOTE ** returns 'size_t' and NOT 'char*' like strncat! */
DLL_EXPORT size_t
strlcat(char *dst, const char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif // !defined(HAVE_STRLCAT)
/* The following structures are defined herein because they are private structures */
/* that MUST be opaque to the outside world */
typedef struct _SYMBOL_TOKEN
{
char *var;
char *val;
} SYMBOL_TOKEN;
#define SYMBOL_TABLE_INCREMENT 256
#define SYMBOL_BUFFER_GROWTH 256
#define MAX_SYMBOL_SIZE 31
#define SYMBOL_QUAL_1 '$'
#define SYMBOL_QUAL_2 '('
#define SYMBOL_QUAL_3 ')'
static SYMBOL_TOKEN **symbols=NULL;
static int symbol_count=0;
static int symbol_max=0;
/* This function retrieves or allocates a new SYMBOL_TOKEN */
static SYMBOL_TOKEN *get_symbol_token(const char *sym, int alloc)
{
SYMBOL_TOKEN *tok;
int i;
for(i=0;i<symbol_count;i++)
{
tok=symbols[i];
if(tok==NULL)
{
continue;
}
#if defined( CASELESS_SYMBOLS )
if(strcasecmp(symbols[i]->var,sym)==0)
#else
if(strcmp(symbols[i]->var,sym)==0)
#endif
{
return(symbols[i]);
}
}
if(!alloc)
{
return(NULL);
}
if(symbol_count>=symbol_max)
{
symbol_max+=SYMBOL_TABLE_INCREMENT;
if(symbols==NULL)
{
symbols=malloc(sizeof(SYMBOL_TOKEN *)*symbol_max);
if(symbols==NULL)
{
symbol_max=0;
symbol_count=0;
return(NULL);
}
}
else
{
symbols=realloc(symbols,sizeof(SYMBOL_TOKEN *)*symbol_max);
if(symbols==NULL)
{
symbol_max=0;
symbol_count=0;
return(NULL);
}
}
}
tok=malloc(sizeof(SYMBOL_TOKEN));
if(tok==NULL)
{
return(NULL);
}
tok->var=malloc(MAX_SYMBOL_SIZE+1);
if(tok->var==NULL)
{
free(tok);
return(NULL);
}
strncpy(tok->var,sym,MAX_SYMBOL_SIZE);
tok->var[MAX_SYMBOL_SIZE]=0; /* Ensure null termination */
tok->val=NULL;
symbols[symbol_count]=tok;
symbol_count++;
return(tok);
}
DLL_EXPORT void del_symbol(const char *sym)
{
SYMBOL_TOKEN *tok;
int i;
for(i=0;i<symbol_count;i++)
{
tok=symbols[i];
if(tok==NULL)
{
continue;
}
#if defined( CASELESS_SYMBOLS )
if(strcasecmp(symbols[i]->var,sym)==0)
#else
if(strcmp(symbols[i]->var,sym)==0)
#endif
{
if ( tok->val != NULL ) free(tok->val);
if ( tok->var != NULL ) free(tok->var);
free(tok);
symbols[i] = NULL;
return;
}
}
return;
}
DLL_EXPORT void set_symbol(const char *sym, const char *value)
{
SYMBOL_TOKEN *tok;
size_t size;
if ( sym == NULL || value == NULL || strlen(sym) == 0 )
return;
tok=get_symbol_token(sym,1);
if(tok==NULL)
{
return;
}
if(tok->val!=NULL)
{
free(tok->val);
}
size = strlen(value)+1;
tok->val=malloc(size);
if(tok->val==NULL)
{
return;
}
strlcpy(tok->val,value,size);
return;
}
DLL_EXPORT const char* get_symbol( const char* sym )
{
SYMBOL_TOKEN* tok;
char buf[ MAX_ENVVAR_LEN ];
if (CMD( sym, DATE, 4 ))
{
// Rebuild new value each time date/time symbol is retrieved
time_t raw_tt;
time( &raw_tt ); // YYYYMMDD
strftime( buf, sizeof( buf ) - 1, "%Y%m%d", localtime( &raw_tt ));
set_symbol( sym = "DATE", buf );
}
else if (CMD( sym, TIME, 4 ))
{
// Rebuild new value each time date/time symbol is retrieved
time_t raw_tt;
time( &raw_tt ); // HHMMSS
strftime( buf, sizeof( buf ) - 1, "%H%M%S", localtime( &raw_tt ));
set_symbol( sym = "TIME", buf );
}
if (!(tok = get_symbol_token( sym, 0 )))
{
// Add this environment variable to our DEFSYM pool
const char* val = getenv( sym );
MSGBUF( buf, "%s", val ? val : "" );
set_symbol( sym, buf );
// (now try again; should succeed this time)
tok = get_symbol_token( sym, 0 );
}
return tok->val;
}
DLL_EXPORT char *resolve_symbol_string(const char *text)
{
char buf[MAX_PATH*4]; /* Statement buffer */
char dflt[MAX_PATH*4]; /* temp location for default */
int c; /* Character work area */
int i = 0; /* Position in the input */
int stmtlen = 0; /* Statement length */
int inc_dollar = -1; /* >=0 Ndx of dollar */
int inc_lbrace = -1; /* >=0 Ndx of lbrace + 1 */
int inc_colon = -1; /* >=0 Ndx of colon */
int inc_equals = -1; /* >=0 Ndx of equals */
int lstarted; /* Indicate if non-whitespace*/
char *inc_envvar; /* ->Environment variable */
char symt = 0; /* Character work area */
if( strstr( text, "$(" ) == NULL && strstr( text, "${" ) == NULL )
{
/* Malloc anyway - the caller will free() */
return( strdup( text ) );
}
memset(buf, 0, sizeof(buf));
while(1)
{
for (i = 0, stmtlen = 0, lstarted = 0; ;i++)
{
c = text[i];
/* Ignore nulls and carriage returns */
if (c == '\0' ) break;
/* Check if it is a white space and no other character yet */
if(!lstarted && isspace(c)) continue;
lstarted=1;
/* Check that statement does not overflow buffer */
if (stmtlen >= (int)(sizeof(buf) - 1))
{
WRMSG( HHC01418, "E" );
return( strdup(text) );
}
/* inc_dollar already processed? */
if (inc_dollar >= 0)
{
/* Left brace already processed? */
if (inc_lbrace >= 0)
{
/* End of variable spec? */
if ( c == symt )
{
/* Terminate it */
buf[stmtlen] = '\0';
/* Terminate var name if we have a inc_colon specifier */
if (inc_colon >= 0)
{
buf[inc_colon] = '\0';
}
/* Terminate var name if we have a default value */
if (inc_equals >= 0)
{
buf[inc_equals] = '\0';
}
/* Reset statement index to start of variable */
stmtlen = inc_dollar;
/* Get variable value */
inc_envvar = (char *)get_symbol (&buf[inc_lbrace]);
/* Variable unset? */
if (inc_envvar == NULL || strlen(inc_envvar) == 0 )
{
/* Substitute default if specified */
if (inc_equals >= 0)
{
memset(dflt, 0, sizeof(dflt));
STRLCPY( dflt, &buf[inc_equals+1] );
inc_envvar = dflt;
}
}
/* Have a value? (environment or default) */
if (inc_envvar != NULL)
{
/* Check that statement does not overflow buffer */
if (stmtlen+strlen(inc_envvar) >= sizeof(buf) - 1)
{
WRMSG( HHC01418, "E" );
return( strdup( text ) );
}
/* Copy to buffer and update index */
stmtlen += snprintf( &buf[stmtlen],
(sizeof(buf) - stmtlen) - 1,
"%s", inc_envvar );
}
memset(&buf[stmtlen], 0, (sizeof(buf) - stmtlen));
/* Reset indexes */
inc_equals = -1;
inc_colon = -1;
inc_lbrace = -1;
inc_dollar = -1;
continue;
}
else if (c == ':' && inc_colon < 0 && inc_equals < 0)
{
/* Remember possible start of default specifier */
inc_colon = stmtlen;
}
else if (c == '=' && inc_equals < 0)
{
/* Remember possible start of default specifier */
inc_equals = stmtlen;
}
}
else // (inc_lbrace < 0)
{
/* Remember start of variable name */
if ( c == '(' )
{
symt = ')' ;
inc_lbrace = stmtlen + 1;
}
else if ( c == '{' )
{
symt = '}' ;
inc_lbrace = stmtlen + 1;
}
else
{
/* Reset inc_dollar specifier if immediately following
character is not a left brace */
inc_dollar = -1;
}
}
}
else // (inc_dollar < 0)
{
/* Enter variable substitution state */
if (c == '$')
{
inc_dollar = stmtlen;
}
}
/* Append character to buffer */
buf[stmtlen++] = c;
} /* end for(stmtlen) */
break;
}
return (strdup(buf));
}
/* (called by defsym panel command) */
DLL_EXPORT void list_all_symbols()
{
SYMBOL_TOKEN* tok; int i;
for (i=0; i < symbol_count; i++)
if ((tok = symbols[i]) != NULL)
// "Symbol %-12s %s"
WRMSG( HHC02199, "I", tok->var, tok->val ? tok->val : "" );
}
/* Subtract 'beg_timeval' from 'end_timeval' yielding 'dif_timeval' */
/* Return code: success == 0, error == -1 (difference was negative) */
DLL_EXPORT int timeval_subtract
(
struct timeval *beg_timeval,
struct timeval *end_timeval,
struct timeval *dif_timeval
)
{
struct timeval begtime;
struct timeval endtime;
ASSERT ( beg_timeval -> tv_sec >= 0 && beg_timeval -> tv_usec >= 0 );
ASSERT ( end_timeval -> tv_sec >= 0 && end_timeval -> tv_usec >= 0 );
memcpy(&begtime,beg_timeval,sizeof(struct timeval));
memcpy(&endtime,end_timeval,sizeof(struct timeval));
dif_timeval->tv_sec = endtime.tv_sec - begtime.tv_sec;
if (endtime.tv_usec >= begtime.tv_usec)
{
dif_timeval->tv_usec = endtime.tv_usec - begtime.tv_usec;
}
else
{
dif_timeval->tv_sec--;
dif_timeval->tv_usec = (endtime.tv_usec + 1000000) - begtime.tv_usec;
}
return ((dif_timeval->tv_sec < 0 || dif_timeval->tv_usec < 0) ? -1 : 0);
}
/* Add 'dif_timeval' to 'accum_timeval' (use to accumulate elapsed times) */
/* Return code: success == 0, error == -1 (accum_timeval result negative) */
DLL_EXPORT int timeval_add
(
struct timeval *dif_timeval,
struct timeval *accum_timeval
)
{
ASSERT ( dif_timeval -> tv_sec >= 0 && dif_timeval -> tv_usec >= 0 );
ASSERT ( accum_timeval -> tv_sec >= 0 && accum_timeval -> tv_usec >= 0 );
accum_timeval->tv_sec += dif_timeval->tv_sec;
accum_timeval->tv_usec += dif_timeval->tv_usec;
if (accum_timeval->tv_usec >= 1000000)
{
int usec = accum_timeval->tv_usec / 1000000;
accum_timeval->tv_sec += usec;
accum_timeval->tv_usec -= usec * 1000000;
}
return ((accum_timeval->tv_sec < 0 || accum_timeval->tv_usec < 0) ? -1 : 0);
}
/*
Easier to use timed_wait_condition that waits for the specified
relative amount of time without you having to build an absolute
timeout time yourself. Use the "timed_wait_condition_relative_usecs"
macro to call it.
*/
DLL_EXPORT int timed_wait_condition_relative_usecs_impl
(
COND* pCOND, // ptr to condition to wait on
LOCK* pLOCK, // ptr to controlling lock (must be held!)
U32 usecs, // max #of microseconds to wait
struct timeval* pTV, // [OPTIONAL] ptr to tod value (may be NULL)
const char* loc // (location)
)
{
struct timespec timeout_timespec;
struct timeval now;
if (!pTV)
{
pTV = &now;
gettimeofday( pTV, NULL );
}
timeout_timespec.tv_sec = pTV->tv_sec + ( usecs / 1000000 );
timeout_timespec.tv_nsec = pTV->tv_usec + ( usecs % 1000000 );
if ( timeout_timespec.tv_nsec >= 1000000 )
{
timeout_timespec.tv_sec += timeout_timespec.tv_nsec / 1000000;
timeout_timespec.tv_nsec %= 1000000;
}
timeout_timespec.tv_nsec *= 1000;
// return timed_wait_condition( pCOND, pLOCK, &timeout_timespec );
return hthread_timed_wait_condition( pCOND, pLOCK, &timeout_timespec, loc );
}
/*********************************************************************
The following couple of Hercules 'utility' functions may be defined
elsewhere depending on which host platform we're being built for...
For Windows builds (e.g. MingW32), the functionality for the below
functions is defined in 'w32util.c'. For other host build platforms
(e.g. Linux, Apple, etc), the functionality for the below functions
is defined right here in 'hscutil.c'...
*********************************************************************/
#if !defined(_MSVC_)
/* THIS module (hscutil.c) is to provide the below functionality.. */
/*
Returns outpath as a host filesystem compatible filename path.
This is a Cygwin-to-MSVC transitional period helper function.
On non-Windows platforms it simply copies inpath to outpath.
On Windows it converts inpath of the form "/cygdrive/x/foo.bar"
to outpath in the form "x:/foo.bar" for Windows compatibility.
*/
DLL_EXPORT char *hostpath( char *outpath, const char *inpath, size_t buffsize )
{
if (inpath && outpath && buffsize > 1)
strlcpy( outpath, inpath, buffsize );
else if (outpath && buffsize)
*outpath = 0;
return outpath;
}
/* Poor man's "fcntl( fd, F_GETFL )"... */
/* (only returns access-mode flags and not any others) */
DLL_EXPORT int get_file_accmode_flags( int fd )
{
int flags = fcntl( fd, F_GETFL );
return ( flags & O_ACCMODE );
}
/* Set socket to blocking or non-blocking mode... */
DLL_EXPORT int socket_set_blocking_mode( int sfd, int blocking_mode )
{
int flags = fcntl( sfd, F_GETFL );
if ( blocking_mode )
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
return fcntl( sfd, F_SETFL, flags );
}
/* Initialize/Deinitialize sockets package... */
int socket_init ( void ) { return 0; }
DLL_EXPORT int socket_deinit ( void ) { return 0; }
/* Determine whether a file descriptor is a socket or not... */
/* (returns 1==true if it's a socket, 0==false otherwise) */
DLL_EXPORT int socket_is_socket( int sfd )
{
struct stat st;
return ( fstat( sfd, &st ) == 0 && S_ISSOCK( st.st_mode ) );
}
/* Set the SO_KEEPALIVE option and timeout values for a
socket connection to detect when client disconnects.
Returns 0==success, +1==warning, -1==failure
(*) Warning failure means function only partially
succeeded (not all requested values were set)
*/
int set_socket_keepalive( int sfd,
int idle_time,
int probe_interval,
int probe_count )
{
#if !defined( HAVE_BASIC_KEEPALIVE )
UNREFERENCED( sfd );
UNREFERENCED( idle_time );
UNREFERENCED( probe_interval );
UNREFERENCED( probe_count );
errno = EOPNOTSUPP;
return -1;
#else // basic, partial or full: must attempt setting keepalive
int idle, intv, cnt;
int rc, l_tcp, optval, succeeded = 0, tried = 0;
struct protoent * tcpproto;
/* Retrieve TCP protocol value (mostly for FreeBSD portability) */
tcpproto = getprotobyname("TCP");
if (!tcpproto)
{
WRMSG( HHC02219, "E", "getprotobyname(\"TCP\")", strerror( HSO_errno ));
return -1;
}
l_tcp = tcpproto->p_proto;
optval = 1;
#if defined( HAVE_DECL_SO_KEEPALIVE ) && HAVE_DECL_SO_KEEPALIVE
tried++;
rc = setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
if (rc)
{
WRMSG( HHC02219, "E", "setsockopt(SO_KEEPALIVE)", strerror( HSO_errno ));
return -1;
}
else
succeeded++;
#endif
optval = idle_time;
#if defined( HAVE_DECL_TCP_KEEPALIVE ) && HAVE_DECL_TCP_KEEPALIVE
tried++;
rc = setsockopt(sfd, l_tcp, TCP_KEEPALIVE, &optval, sizeof(optval));
if (rc)
WRMSG( HHC02219, "E", "setsockopt(TCP_KEEPALIVE)", strerror( HSO_errno ));
else
{
succeeded++;
idle = idle_time;
}
#endif
/* Only try setting individual keepalive values if full or partial
keepalive is supported. Otherwise if we have only basic keepalive
then don't bother even trying
*/
#if defined( HAVE_FULL_KEEPALIVE ) || defined( HAVE_PARTIAL_KEEPALIVE )
optval = idle_time;
#if defined( HAVE_DECL_TCP_KEEPIDLE ) && HAVE_DECL_TCP_KEEPIDLE
tried++;
rc = setsockopt(sfd, l_tcp, TCP_KEEPIDLE, &optval, sizeof(optval));
if (rc)
WRMSG( HHC02219, "E", "setsockopt(TCP_KEEPIDLE)", strerror( HSO_errno ));
else
{
succeeded++;
idle = idle_time;
}
#endif
optval = probe_interval;
#if defined( HAVE_DECL_TCP_KEEPINTVL ) && HAVE_DECL_TCP_KEEPINTVL
tried++;
rc = setsockopt(sfd, l_tcp, TCP_KEEPINTVL, &optval, sizeof(optval));
if (rc)
WRMSG( HHC02219, "E", "setsockopt(TCP_KEEPINTVL)", strerror( HSO_errno ));
else
{
succeeded++;
intv = probe_interval;
}
#endif
optval = probe_count;
#if defined( HAVE_DECL_TCP_KEEPCNT ) && HAVE_DECL_TCP_KEEPCNT
tried++;
rc = setsockopt(sfd, l_tcp, TCP_KEEPCNT, &optval, sizeof(optval));
if (rc)
WRMSG( HHC02219, "E", "setsockopt(TCP_KEEPCNT)", strerror( HSO_errno ));
else
{
succeeded++;
cnt = probe_count;
}
#endif
#endif // defined( HAVE_FULL_KEEPALIVE ) || defined( HAVE_PARTIAL_KEEPALIVE )
/* Retrieve values in use, ignoring any error */
get_socket_keepalive( sfd, &idle, &intv, & cnt );
/* Determine return code: did everything succeed and,
more importantly, were all of our values accepted?
Did the values we tried setting (if any) "stick"?
*/
if (succeeded >= tried)
rc = (idle != idle_time || intv != probe_interval || cnt != probe_count) ? +1 : 0;
else
rc = (succeeded <= 0 ? -1 : +1);
return rc;
#endif // (KEEPALIVE)
}
/* Function to retrieve keepalive values. 0==success, -1=failure */
int get_socket_keepalive( int sfd, int* idle_time, int* probe_interval,
int* probe_count )
{
#if defined( HAVE_FULL_KEEPALIVE ) || defined( HAVE_PARTIAL_KEEPALIVE )
struct protoent* tcpproto;
int optval, l_tcp;
socklen_t optlen = sizeof( optval );
/* Retrieve TCP protocol value (mostly for FreeBSD portability) */
tcpproto = getprotobyname("TCP");
if (!tcpproto)
{
WRMSG( HHC02219, "E", "getprotobyname(\"TCP\")", strerror( HSO_errno ));
return -1;
}
l_tcp = tcpproto->p_proto;
#else
UNREFERENCED( sfd );
#endif // HAVE_FULL_KEEPALIVE || HAVE_PARTIAL_KEEPALIVE
/* Default values */
*idle_time = sysblk.kaidle;
*probe_interval = sysblk.kaintv;
*probe_count = sysblk.kacnt;
#if defined( HAVE_FULL_KEEPALIVE ) || defined( HAVE_PARTIAL_KEEPALIVE )
/* Retrieve the actual values from the system */
#if defined( HAVE_DECL_TCP_KEEPIDLE ) && HAVE_DECL_TCP_KEEPIDLE
if (getsockopt( sfd, l_tcp, TCP_KEEPIDLE, &optval, &optlen ) >= 0) *idle_time = optval;
#endif
#if defined( HAVE_DECL_TCP_KEEPINTVL ) && HAVE_DECL_TCP_KEEPINTVL
if (getsockopt( sfd, l_tcp, TCP_KEEPINTVL, &optval, &optlen ) >= 0) *probe_interval = optval;
#endif
#if defined( HAVE_DECL_TCP_KEEPCNT ) && HAVE_DECL_TCP_KEEPCNT
if (getsockopt( sfd, l_tcp, TCP_KEEPCNT, &optval, &optlen ) >= 0) *probe_count = optval;
#endif
#endif // HAVE_FULL_KEEPALIVE || HAVE_PARTIAL_KEEPALIVE
return 0;
}
// Hercules low-level file open...
DLL_EXPORT int hopen( const char* path, int oflag, ... )
{
int pmode = 0;
if (oflag & O_CREAT)
{
va_list vargs;
va_start( vargs, oflag );
pmode = va_arg( vargs, int );
}
return open( path, oflag, pmode );
}
/* Determine whether process is running "elevated" or not */
/* (returns 1==true running elevated, 0==false otherwise) */
bool are_elevated()
{
return (sysblk.euid == 0) ? true : false;
}
#endif // !defined(_MSVC_)
//////////////////////////////////////////////////////////////////////////////////////////
/*******************************************/
/* Read/Write to socket functions */
/*******************************************/
DLL_EXPORT int hgetc(int s)
{
char c;
int rc;
rc=recv(s,&c,1,0);
if(rc<1)
{
return EOF;
}
return c;
}
DLL_EXPORT char * hgets(char *b,size_t c,int s)
{
size_t ix=0;
while(ix<c)
{
b[ix]=hgetc(s);
if ((signed char)b[ix] == EOF)
{
return NULL;
}
b[ix+1]=0;
if(b[ix]=='\n')
{
return(b);
}
ix++;
}
return NULL;
}
DLL_EXPORT int hwrite(int s,const char *bfr,size_t sz)
{
return send(s,bfr,(int)sz,0); /* (int) cast is for _WIN64 */
}
DLL_EXPORT int hprintf(int s,char *fmt,...)
{
char *bfr;
size_t bsize=1024;
int rc;