-
Notifications
You must be signed in to change notification settings - Fork 0
/
ydbposix.c
998 lines (878 loc) · 27.6 KB
/
ydbposix.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
/****************************************************************
* *
* Copyright (c) 2012-2022 Fidelity National Information *
* Services, Inc. and/or its subsidiaries. All rights reserved. *
* *
* Copyright (c) 2018-2024 YottaDB LLC and/or its subsidiaries. *
* All rights reserved. *
* *
* This source code contains the intellectual property *
* of its copyright holder(s), and is made available *
* under a license. If you do not know the terms of *
* the license, please stop and do not read further. *
* *
****************************************************************/
/* Caution - these functions are not thread-safe */
#include <errno.h>
#include <regex.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include "libyottadb.h"
#define MAXREGEXMEM 65536 /* Maximum memory to allocate for a compiled regular expression */
#define MINMALLOC 64 /* Minimum space to request from ydb_malloc - MAXREGEXMEM/MINMALLOC should be a power of 4 */
#define CP_BUF_SIZE 4096 /* Size of the buffer used for copying a file. */
/* On certain platforms the st_Xtime field of the stat structure got replaced by a timespec st_Xtim field, which in turn has tv_sec
* and tv_nsec fields. For compatibility reasons, those platforms define an st_Xtime macro which points to st_Xtim.tv_sec. Whenever
* we detect such a situation, we define a nanosecond flavor of that macro to point to st_Xtim.tv_nsec.
*
* Need to confirm whether we even need these #defines – Bhaskar 20190226
*
*/
#if defined st_atime
# define st_natime st_atim.tv_nsec
#endif
#if defined st_ctime
# define st_nctime st_ctim.tv_nsec
#endif
#if defined st_mtime
# define st_nmtime st_mtim.tv_nsec
#endif
/* Translation tables for various include file #define names to the platform values for those names */
/* Names *must* be in alphabetic order of strings; otherwise search will return incorrect results */
#define EINTR_OPER(ACTION, RC) \
{ \
do \
{ \
RC = ACTION; \
} while ((-1 == RC) && (EINTR == errno)); \
}
static const char *clocks[] =
{
"CLOCK_BOOTTIME",
"CLOCK_MONOTONIC",
"CLOCK_MONOTONIC_COARSE",
"CLOCK_MONOTONIC_RAW",
"CLOCK_PROCESS_CPUTIME_ID",
"CLOCK_REALTIME",
"CLOCK_REALTIME_COARSE",
"CLOCK_THREAD_CPUTIME_ID"
};
static const int clock_values[] =
{
CLOCK_BOOTTIME,
CLOCK_MONOTONIC,
CLOCK_MONOTONIC_COARSE,
CLOCK_MONOTONIC_RAW,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_REALTIME,
CLOCK_REALTIME_COARSE,
CLOCK_THREAD_CPUTIME_ID
};
static const char *fmodes[] =
{
"S_IFBLK", "S_IFCHR", "S_IFDIR", "S_IFIFO", "S_IFLNK", "S_IFMT", "S_IFREG",
"S_IFSOCK", "S_IRGRP", "S_IROTH", "S_IRUSR", "S_IRWXG", "S_IRWXO", "S_IRWXU",
"S_ISGID", "S_ISUID", "S_ISVTX", "S_IWGRP", "S_IWOTH", "S_IWUSR", "S_IXGRP",
"S_IXOTH", "S_IXUSR"
};
static const int fmode_values[] =
{
S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG,
S_IFSOCK, S_IRGRP, S_IROTH, S_IRUSR, S_IRWXG, S_IRWXO, S_IRWXU,
S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP,
S_IXOTH, S_IXUSR
};
static const char *rlimit[] =
{ /* skip NICE because it has evil implications in GT.M */
# ifndef _AIX
"RLIMIT_MEMLOCK", "RLIMIT_MSGQUEUE", "RLIMIT_LOCKS", "RLIMIT_NPROC", "RLIMIT_RTPRIO", "RLIMIT_SIGPENDING",
# endif
"RLIMIT_AS", "RLIMIT_CORE", "RLIMIT_CPU", "RLIMIT_DATA", "RLIMIT_FSIZE",
"RLIMIT_NOFILE", "RLIMIT_STACK", "RLIMIT_RSS"
};
static const int rlimit_values[]=
{
# ifndef _AIX
RLIMIT_MEMLOCK, RLIMIT_MSGQUEUE, RLIMIT_LOCKS, RLIMIT_NPROC, RLIMIT_RTPRIO, RLIMIT_SIGPENDING,
# endif
RLIMIT_AS, RLIMIT_CORE, RLIMIT_CPU, RLIMIT_DATA, RLIMIT_FSIZE,
RLIMIT_NOFILE, RLIMIT_STACK, RLIMIT_RSS
};
static const char *priority[] =
{
"LOG_ALERT", "LOG_CRIT", "LOG_DEBUG", "LOG_EMERG", "LOG_ERR",
"LOG_INFO", "LOG_LOCAL0", "LOG_LOCAL1", "LOG_LOCAL2",
"LOG_LOCAL3", "LOG_LOCAL4", "LOG_LOCAL5", "LOG_LOCAL6",
"LOG_LOCAL7", "LOG_NOTICE", "LOG_USER", "LOG_WARNING"
};
static const int priority_values[] =
{
LOG_ALERT, LOG_CRIT, LOG_DEBUG, LOG_EMERG, LOG_ERR,
LOG_INFO, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2,
LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6,
LOG_LOCAL7, LOG_NOTICE, LOG_USER, LOG_WARNING
};
static const char *regxflags[] =
{
"REG_BADBR", "REG_BADPAT", "REG_BADRPT", "REG_EBRACE", "REG_EBRACK", "REG_ECOLLATE",
"REG_ECTYPE", "REG_EESCAPE", "REG_EPAREN", "REG_ERANGE", "REG_ESPACE", "REG_ESUBREG",
"REG_EXTENDED", "REG_ICASE", "REG_NEWLINE", "REG_NOMATCH", "REG_NOSUB",
"REG_NOTBOL", "REG_NOTEOL",
"sizeof(regex_t)", "sizeof(regmatch_t)", "sizeof(regoff_t)"
};
static const int regxflag_values[] =
{
REG_BADBR, REG_BADPAT, REG_BADRPT, REG_EBRACE, REG_EBRACK, REG_ECOLLATE,
REG_ECTYPE, REG_EESCAPE, REG_EPAREN, REG_ERANGE, REG_ESPACE, REG_ESUBREG,
REG_EXTENDED, REG_ICASE, REG_NEWLINE, REG_NOMATCH, REG_NOSUB,
REG_NOTBOL, REG_NOTEOL,
sizeof(regex_t), sizeof(regmatch_t), sizeof(regoff_t)
};
static const char *signals[] =
{
"SIGABRT", "SIGALRM", "SIGBUS", "SIGCHLD", "SIGCONT", "SIGFPE", "SIGHUP", "SIGILL",
"SIGINT", "SIGKILL", "SIGPIPE", "SIGQUIT", "SIGSEGV", "SIGSTOP", "SIGTERM",
"SIGTRAP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGUSR1", "SIGUSR2",
"SIGXCPU", "SIGXFSZ"
};
static const int signal_values[] =
{
SIGABRT, SIGALRM, SIGBUS, SIGCHLD, SIGCONT, SIGFPE, SIGHUP, SIGILL,
SIGINT, SIGKILL, SIGPIPE, SIGQUIT, SIGSEGV, SIGSTOP, SIGTERM,
SIGTRAP, SIGTSTP, SIGTTIN, SIGTTOU, SIGURG, SIGUSR1, SIGUSR2,
SIGXCPU, SIGXFSZ
};
static const char *sysconfs[] =
{
"ARG_MAX", "BC_BASE_MAX", "BC_DIM_MAX", "BC_SCALE_MAX", "BC_STRING_MAX", "CHILD_MAX",
"COLL_WEIGHTS_MAX", "EXPR_NEST_MAX", "HOST_NAME_MAX", "LINE_MAX", "LOGIN_NAME_MAX", "OPEN_MAX",
"PAGESIZE", "POSIX2_C_DEV", "POSIX2_FORT_DEV", "POSIX2_FORT_RUN", "POSIX2_SW_DEV", "POSIX2_VERSION",
"RE_DUP_MAX", "STREAM_MAX", "SYMLOOP_MAX", "TTY_NAME_MAX", "TZNAME_MAX", "_POSIX2_LOCALEDEF",
"_POSIX_VERSION"
};
static const int sysconf_values[] =
{
_SC_ARG_MAX, _SC_BC_BASE_MAX, _SC_BC_DIM_MAX, _SC_BC_SCALE_MAX, _SC_BC_STRING_MAX, _SC_CHILD_MAX,
_SC_COLL_WEIGHTS_MAX, _SC_EXPR_NEST_MAX, _SC_HOST_NAME_MAX, _SC_LINE_MAX, _SC_LOGIN_NAME_MAX, _SC_OPEN_MAX,
_SC_PAGESIZE, _SC_2_C_DEV, _SC_2_FORT_DEV, _SC_2_FORT_RUN, _SC_2_SW_DEV, _SC_2_VERSION,
_SC_RE_DUP_MAX, _SC_STREAM_MAX, _SC_SYMLOOP_MAX, _SC_TTY_NAME_MAX, _SC_TZNAME_MAX, _SC_2_LOCALEDEF,
_SC_VERSION
};
/* Prototypes */
int posix_chmod(int argc, char *file, int mode, int *err_num);
int posix_clock_gettime(int argc, int clk_id, long *tv_sec, long *tv_nsec, int *err_num);
int posix_chmod(int argc, char *file, int mode, int *err_num);
int posix_clock_gettime(int argc, int clk_id, long *tv_sec, long *tv_nsec, int *err_num);
int posix_cp(int argc, char *source, char *dest, int *err_num);
int posix_getrlimit(int argc, int resource, unsigned long *cv, int *err_num);
int posix_gettimeofday(int argc, long *tv_sec, long *tv_usec, int *err_num);
int posix_getuid(int argc, unsigned long *id);
int posix_localtime(int argc, long timep, int *sec, int *min, int *hour,
int *mday, int *mon, int *year, int *wday,
int *yday, int *isdst, int *err_num);
int posix_mkdir(int argc, char *dirname, int mode, int *err_num);
int posix_mkdtemp(int argc, char *template, int *err_num);
int posix_mktime(int argc, int year, int mon, int mday, int hour,
int min, int sec, int *wday, int *yday, int *isdst,
long *unixtime, int *err_num);
int posix_realpath(int argc, char *file, ydb_string_t *result, int *err_num);
int posix_regcomp(int argc, ydb_string_t *pregstr, char *regex, int cflags, int *err_num);
int posix_regexec(int argc, ydb_string_t *pregstr, char *string, int nmatch, ydb_string_t *pmatch,
int eflags, int *matchsuccess);
int posix_regfree(int argc, ydb_string_t *pregstr);
int posix_rmdir(int argc, char *pathname, int *err_num);
int posix_setenv(int argc, char *name, char *value, int overwrite, int *err_num);
int posix_stat(int argc, char *fname, unsigned long *dev, unsigned long *ino, unsigned long *mode,
unsigned long *nlink, unsigned long *uid, unsigned long *gid, unsigned long *rdev, long *size,
long *blksize, long *blocks, long *atime, long *atimen, long *mtime,
long *mtimen, long *ctime, long *ctimen, int *err_num);
int posix_symlink(int argc, char *target, char *name, int *err_num);
int posix_sysconf(int argc, int name, long *value, int *err_num);
int posix_syslog(int argc, int priority, char *message);
int posix_umask(int argc, int mode, int *prev_mode, int *err_num);
int posix_unsetenv(int argc, char *name, int *err_num);
int posix_utimes(int argc, char *file, int *err_num);
int posixutil_searchstrtab(const char *tblstr[], const int tblval[], int tblsize, char *str, int *strval);
int posixhelper_clockval(int argc, char *symconst, int *symval);
int posixhelper_filemodeconst(int argc, char *symconst, int *symval);
int posixhelper_rlimitconst(int argc, char *symconst, int *symval);
int posixhelper_regconst(int argc, char *symconst, int *symval);
int posixhelper_regofft2offsets(int argc, ydb_string_t *regofftbytes, int *rmso, int *rmeo);
int posixhelper_signalval(int argc, char *symconst, int *symval);
int posixhelper_sysconfval(int argc, char *symconst, int *symval);
int posixhelper_syslogconst(int argc, char *symconst, int *symval);
/* POSIX routines */
int posix_chmod(int argc, char *file, int mode, int *err_num)
{
if (3 != argc)
return (int)-argc;
*err_num = (-1 == chmod(file, (mode_t)mode)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_clock_gettime(int argc, int clk_id, long *tv_sec, long *tv_nsec, int *err_num)
{
struct timespec tp;
if (4 != argc)
return (int)-argc;
if (0 == clock_gettime((clockid_t)clk_id, &tp))
{
*tv_sec = (long)tp.tv_sec;
*tv_nsec = (long)tp.tv_nsec;
*err_num = 0;
} else
*err_num = (int)errno;
return (int)*err_num;
}
int posix_cp(int argc, char *source, char *dest, int *err_num)
{
int fd1, fd2, rc;
char *buf_ptr;
char buffer[CP_BUF_SIZE];
struct stat statbuf;
ssize_t read_count, written_count;
if (3 != argc)
return (int)-argc;
if (-1 == stat((char *)source, &statbuf))
{
*err_num = (int)errno;
return (int)*err_num;
}
EINTR_OPER(open(source, O_RDONLY), fd1);
if (-1 == fd1)
{
*err_num = (int)errno;
return (int)*err_num;
}
EINTR_OPER(open(dest, O_WRONLY | O_CREAT, statbuf.st_mode), fd2);
if (-1 == fd2)
{
*err_num = (int)errno;
EINTR_OPER(close(fd1), rc);
return (int)*err_num;
}
EINTR_OPER(ftruncate(fd2, 0), rc);
if (-1 == rc)
{
*err_num = (int)errno;
EINTR_OPER(close(fd1), rc);
EINTR_OPER(close(fd2), rc);
return (int)*err_num;
}
*err_num = 0;
do
{
EINTR_OPER(read(fd1, buffer, CP_BUF_SIZE), read_count);
if (0 < read_count)
{
buf_ptr = buffer;
while (0 < read_count)
{
EINTR_OPER(write(fd2, buf_ptr, read_count), written_count);
if (0 < written_count)
{
read_count -= written_count;
buf_ptr += written_count;
} else
{
if (-1 == written_count)
*err_num = (int)errno;
break;
}
}
} else
{
if (-1 == read_count)
*err_num = (int)errno;
break;
}
} while (0 == *err_num);
EINTR_OPER(close(fd1), rc);
EINTR_OPER(close(fd2), rc);
if (0 == *err_num)
*err_num = (-1 == chmod(dest, statbuf.st_mode)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_getrlimit(int argc, int resource, unsigned long *cv, int *err_num)
{
struct rlimit rl;
if (3 != argc)
return (int)-argc;
else if (-1 == getrlimit(resource, &rl))
*err_num = errno;
else
{
*cv = (unsigned long)rl.rlim_cur;
*err_num = 0;
}
return (int)*err_num;
}
int posix_gettimeofday(int argc, long *tv_sec, long *tv_usec, int *err_num)
{
struct timeval currtimeval;
if (3 != argc)
return (int)-argc;
if (-1 == gettimeofday(&currtimeval, NULL))
*err_num = (int)errno;
else
{
*tv_sec = (long)currtimeval.tv_sec;
*tv_usec = (long)currtimeval.tv_usec;
*err_num = 0;
}
return (int)*err_num;
}
int posix_getuid(int argc, unsigned long *id)
{
if (1 != argc)
return (int)-argc;
*id = (unsigned long)getuid();
return 0;
}
int posix_localtime(int argc, long timep, int *sec, int *min, int *hour,
int *mday, int *mon, int *year, int *wday,
int *yday, int *isdst, int *err_num)
{
struct tm *currtimetm;
if (11 != argc)
return (int)-argc;
currtimetm = localtime((time_t *)&timep);
if (currtimetm)
{
*sec = (int)currtimetm->tm_sec;
*min = (int)currtimetm->tm_min;
*hour = (int)currtimetm->tm_hour;
*mday = (int)currtimetm->tm_mday;
*mon = (int)currtimetm->tm_mon;
*year = (int)currtimetm->tm_year;
*wday = (int)currtimetm->tm_wday;
*yday = (int)currtimetm->tm_yday;
*isdst = (int)currtimetm->tm_isdst;
*err_num = 0;
} else
{
/* Linux does not set errno as required by POSIX std as of "IEEE Std 1003.1-2001" */
*err_num = (int)-1;
}
return (int)*err_num;
}
int posix_mkdir(int argc, char *dirname, int mode, int *err_num)
{
if (3 != argc)
return (int)-argc;
/* Possible return codes on error are EACCESS, EDQUOT, EEXIST, EFAULT, ELOOP, EMLINK, ENAMETOOLONG,
* ENOENT, ENOMEM, ENOSPC, ENOTDIR, EPERM, and EROFS.
*/
*err_num = (-1 == mkdir((char *)dirname, (mode_t)mode)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_mkdtemp(int argc, char *template, int *err_num)
{
if (2 != argc)
return (int)-argc;
/* Possible return codes on error are EACCESS, EDQUOT, EEXIST, EFAULT, ELOOP, EMLINK, ENAMETOOLONG,
* ENOENT, ENOMEM, ENOSPC, ENOTDIR, EPERM, EROFS.
*/
*err_num = (mkdtemp((char *)template)) ? 0 : (int)errno;
return (int)*err_num;
}
int posix_mktime(int argc, int year, int mon, int mday, int hour,
int min, int sec, int *wday, int *yday, int *isdst,
long *unixtime, int *err_num)
{
struct tm time_str;
if (11 != argc)
return (int)-argc;
time_str.tm_year = (int)year;
time_str.tm_mon = (int)mon;
time_str.tm_mday = (int)mday;
time_str.tm_hour = (int)hour;
time_str.tm_min = (int)min;
time_str.tm_sec = (int)sec;
time_str.tm_isdst = (int)(*isdst);
if (-1 == (*unixtime = (long)mktime(&time_str))) /* Warning - assignment */
{
*err_num = (int)-1;
} else
{
*wday = (int)time_str.tm_wday;
*yday = (int)time_str.tm_yday;
/* Only set DST if passed -1 */
if (-1 == *isdst)
*isdst = (int)time_str.tm_isdst;
*err_num = 0;
}
return (int)*err_num;
}
int posix_realpath(int argc, char *file, ydb_string_t *result, int *err_num)
{
if (3 != argc)
return (int)-argc;
if (realpath((const char *)file, result->address))
{
result->length = strlen(result->address);
*err_num = 0;
} else
{
result->length = 0;
*err_num = (int)errno;
}
return (int)*err_num;
}
int posix_regcomp(int argc, ydb_string_t *pregstr, char *regex, int cflags, int *err_num)
{
regex_t *preg;
if (4 != argc)
return (int)-argc;
preg = (regex_t *)ydb_malloc(sizeof(regex_t));
*err_num = (int)regcomp(preg, regex, (int)cflags);
if (0 == *err_num)
{
(pregstr->length) = sizeof(char *);
memcpy(pregstr->address, &preg, pregstr->length);
}
return (int)*err_num;
}
/* posix_regexec() does not entirely follow the implementation of the POSIX regexec(). The latter returns 0 for a
* successful match, REG_NOMATCH otherwise. But returning non-zero to YottaDB from a C function will invoke the YottaDB
* error trap, which is not desirable for the non-match of a pattern. Therefore, posix_regexec() always returns
* zero and the result of the match is in the parameter *matchsuccess with 1 meaning a successful match and 0
* otherwise.
*/
int posix_regexec(int argc, ydb_string_t *pregstr, char *string, int nmatch, ydb_string_t *pmatch,
int eflags, int *matchsuccess)
{
regex_t *preg;
regmatch_t *result;
size_t resultsize;
if (6 != argc)
return (int)-argc;
memcpy(&preg, pregstr->address, pregstr->length);
resultsize = nmatch * sizeof(regmatch_t);
result = (regmatch_t *)ydb_malloc(resultsize);
*matchsuccess = (0 == regexec(preg, (char *)string, (size_t)nmatch, result, (int)eflags));
if (*matchsuccess)
memcpy(pmatch->address, result, resultsize);
ydb_free((void*)result);
return 0;
}
int posix_regfree(int argc, ydb_string_t *pregstr)
{
regex_t *preg;
if (1 != argc)
return (int)-argc;
memcpy(&preg, pregstr->address, pregstr->length);
/* regfree is a void function */
regfree(preg);
ydb_free((void *)preg);
return 0;
}
int posix_rmdir(int argc, char *pathname, int *err_num)
{
if (2 != argc)
return (int)-argc;
*err_num = (-1 == rmdir((const char *)pathname)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_setenv(int argc, char *name, char *value, int overwrite, int *err_num)
{
if (4 != argc)
return (int)-argc;
*err_num = (-1 == setenv((char *)name, (char *)value, (int)overwrite)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_stat(int argc, char *fname, unsigned long *dev, unsigned long *ino, unsigned long *mode,
unsigned long *nlink, unsigned long *uid, unsigned long *gid, unsigned long *rdev, long *size,
long *blksize, long *blocks, long *atime, long *atimen, long *mtime,
long *mtimen, long *ctime, long *ctimen, int *err_num)
{
struct stat thisfile;
int retval;
if (18 != argc)
return (int)-argc;
*err_num = (-1 == stat((char *)fname, &thisfile)) ? (int)errno : 0;
if (0 == *err_num)
{
*dev = (unsigned long)thisfile.st_dev; /* ID of device containing file */
*ino = (unsigned long)thisfile.st_ino; /* inode number */
*mode = (unsigned long)thisfile.st_mode; /* protection */
*nlink = (unsigned long)thisfile.st_nlink; /* number of hard links */
*uid = (unsigned long)thisfile.st_uid; /* user ID of owner */
*gid = (unsigned long)thisfile.st_gid; /* group ID of owner */
*rdev = (unsigned long)thisfile.st_rdev; /* device ID (if special file) */
*size = (long)thisfile.st_size; /* total size, in bytes */
*blksize = (long)thisfile.st_blksize; /* blocksize for file system I/O */
*blocks = (long)thisfile.st_blocks; /* number of 512B blocks allocated */
*atime = (long)thisfile.st_atime; /* time (secs) of last access */
*atimen = (long)thisfile.st_natime; /* time (nsecs) of last access */
*mtime = (long)thisfile.st_mtime; /* time (secs) of last modification */
*mtimen = (long)thisfile.st_nmtime; /* time (nsecs) of last modification */
*ctime = (long)thisfile.st_ctime; /* time (secs) of last status change */
*ctimen = (long)thisfile.st_nctime; /* time (nsecs) of last status change */
}
return (int)*err_num;
}
int posix_symlink(int argc, char *target, char *name, int *err_num)
{
if (3 != argc)
return (int)-argc;
*err_num = (-1 == symlink(target, name)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_sysconf(int argc, int name, long *value, int *err_num)
{
if (3 != argc)
return (int)-argc;
errno = 0;
if (0 <= (*value = (long)sysconf(name))) /* Warning - assignment */
*err_num = 0;
else
*err_num = (0 == errno) ? 0 : (int)errno;
return (int)*err_num;
}
/* posix_syslog() does not entirely follow the format of POSIX syslog(). For one thing, syslog() provides for a
* variable number of arguments, whereas posix_syslog() can only accommodate a fixed number. Additionally, per
* http://lab.gsi.dit.upm.es/semanticwiki/index.php/Category:String_Format_Overflow_in_syslog(), the safe way to
* use syslog() is to force the format to "%s". Note that while POSIX syslog() returns no value, posix_syslog()
* returns 0; otherwise, YottaDB will raise a runtime error.
*/
int posix_syslog(int argc, int priority, char *message)
{
if (2 != argc)
return (int)-argc;
/* syslog() is a void function */
syslog((int)priority, "%s", (char *)message);
return (int)0;
}
int posix_umask(int argc, int mode, int *prev_mode, int *err_num)
{
if (3 != argc)
return (int)-argc;
*prev_mode = (int)umask(mode);
return 0;
}
int posix_unsetenv(int argc, char *name, int *err_num)
{
if (2 != argc)
return (int)-argc;
*err_num = (-1 == unsetenv(name)) ? (int)errno : 0;
return (int)*err_num;
}
int posix_utimes(int argc, char *file, int *err_num)
{
if (2 != argc)
return (int)-argc;
*err_num = (-1 == utimes(file, NULL)) ? (int)errno : 0;
return (int)*err_num;
}
/* Utility routines used by Helper routines below */
int posixutil_searchstrtab(const char *tblstr[], const int tblval[], int tblsize, char *str, int *strval)
{
int compflag, current, first, last;
first = 0;
last = tblsize - 1;
for (; ;)
{
current = (first + last) / 2;
compflag = strcmp(tblstr[current], str);
if (0 == compflag)
{
*strval = tblval[current];
return (int)0;
}
if (first == last)
return (int)1;
if (0 > compflag)
first = (first == current) ? (current + 1) : current;
else
last = current;
}
}
/* Helper routines */
/* Given a clock name, provide the numeric value */
int posixhelper_clockval(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(clocks, clock_values, sizeof(clocks) / sizeof(clocks[0]), symconst, symval);
}
/* Given a symbolic constant for file mode, provide the numeric value */
int posixhelper_filemodeconst(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(fmodes, fmode_values, sizeof(fmodes) / sizeof(fmodes[0]), symconst, symval);
}
/* Given a symbolic constant for limit, provide the numeric value */
int posixhelper_rlimitconst(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(rlimit, rlimit_values, sizeof(rlimit) / sizeof(rlimit[0]), symconst, symval);
}
/* Given a symbolic constant for regex facility or level, provide the numeric value */
int posixhelper_regconst(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(regxflags, regxflag_values, sizeof(regxflags) / sizeof(regxflags[0]), symconst, symval);
}
/* Endian independent conversion from regmatch_t bytestring to offsets */
int posixhelper_regofft2offsets(int argc, ydb_string_t *regofftbytes, int *rmso, int *rmeo)
{
regmatch_t buf;
if (3 != argc)
return (int)-argc;
memcpy(&buf, regofftbytes->address, sizeof(regmatch_t));
*rmso = (int)((regoff_t)(buf.rm_so));
*rmeo = (int)((regoff_t)(buf.rm_eo));
return 0;
}
/* Given a signal name, provide the numeric value */
int posixhelper_signalval(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(signals, signal_values, sizeof(signals) / sizeof(signals[0]), symconst, symval);
}
/* Given a configuration name, provide the numeric value */
int posixhelper_sysconfval(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(sysconfs, sysconf_values, sizeof(sysconfs) / sizeof(sysconfs[0]), symconst, symval);
}
/* Given a symbolic constant for syslog facility or level, provide the numeric value */
int posixhelper_syslogconst(int argc, char *symconst, int *symval)
{
if (2 != argc)
return (int)-argc;
return posixutil_searchstrtab(priority, priority_values, sizeof(priority) / sizeof(priority[0]), symconst, symval);
}
/* libm functions contributed to YottaDB by a contributor who wished to remain anonymous */
#include <math.h>
int posix_m_exp (int c, double *x, double *rc);
int posix_m_log (int c, double *x, double *rc);
int posix_m_log10 (int c, double *x, double *rc);
int posix_m_cos (int c, double *x, double *rc);
int posix_m_sin (int c, double *x, double *rc);
int posix_m_arcsin(int c, double *x, double *rc);
int posix_m_arccos(int c, double *x, double *rc);
int posix_m_tan (int c, double *x, double *rc);
int posix_m_arctan(int c, double *x, double *rc);
int posix_m_sqrt (int c, double *x, double *rc);
int posix_m_cosh (int c, double *x, double *rc);
int posix_m_sinh (int c, double *x, double *rc);
int posix_m_tanh (int c, double *x, double *rc);
int posix_m_acosh (int c, double *x, double *rc);
int posix_m_asinh (int c, double *x, double *rc);
int posix_m_atanh (int c, double *x, double *rc);
int posix_m_ceil (int c, double *x, double *rc);
int posix_m_fabs (int c, double *x, double *rc);
int posix_m_floor (int c, double *x, double *rc);
int posix_m_pow (int c, double *x, double *y, double *rc);
int posix_m_fmod (int c, double *x, double *y, double *rc);
int posix_m_fdim (int c, double *x, double *y, double *rc);
int posix_m_fmax (int c, double *x, double *y, double *rc);
int posix_m_fmin (int c, double *x, double *y, double *rc);
int posix_m_fma (int c, double *x, double *y, double *z, double *rc);
int posix_m_exp(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =exp(*x);
return 0;
}
int posix_m_log(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if (*x<0)
return -1;
*rc =log(*x);
return 0;
}
int posix_m_log10(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if (*x <0)
return -1;
*rc =log10(*x);
return 0;
}
int posix_m_cos(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =cos(*x);
return 0;
}
int posix_m_sin(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =sin(*x);
return 0;
}
int posix_m_arcsin(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if ((*x <-1) || (*x >1))
return -1;
*rc =asin(*x);
return 0;
}
int posix_m_arccos(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if ((*x <-1) || (*x >1))
return -1;
*rc=acos(*x);
return 0;
}
int posix_m_tan(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =tan(*x);
return 0;
}
int posix_m_arctan(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =atan(*x);
return 0;
}
int posix_m_sqrt(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if (*x <0)
return -1;
*rc =sqrt(*x);
return 0;
}
int posix_m_cosh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =cosh(*x);
return 0;
}
int posix_m_sinh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =sinh(*x);
return 0;
}
int posix_m_tanh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =tanh(*x);
return 0;
}
int posix_m_acosh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if (*x <1)
return -1;
*rc =acosh(*x);
return 0;
}
int posix_m_asinh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =asinh(*x);
return 0;
}
int posix_m_atanh(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
if ((*x <-1) || (*x >1))
return -1;
*rc =atanh(*x);
return 0;
}
int posix_m_ceil(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =ceil(*x);
return 0;
}
int posix_m_fabs(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =fabs(*x);
return 0;
}
int posix_m_floor(int argc, double *x, double *rc)
{
if (2 !=argc)
return -argc;
*rc =floor(*x);
return 0;
}
int posix_m_pow(int argc, double *x, double *y, double *rc)
{
if (3 !=argc)
return -argc;
*rc =pow(*x,*y);
return 0;
}
int posix_m_fmod(int argc, double *x, double *y, double *rc)
{
if (3 !=argc)
return -argc;
*rc =fmod(*x,*y);
return 0;
}
int posix_m_fdim(int argc, double *x, double *y, double *rc)
{
if (3 !=argc)
return -argc;
*rc =fdim(*x,*y);
return 0;
}
int posix_m_fmax(int argc, double *x, double *y, double *rc)
{
if (3 !=argc)
return -argc;
*rc =fmax(*x,*y);
return 0;
}
int posix_m_fmin(int argc, double *x, double *y, double *rc)
{
if (3 !=argc)
return -argc;
*rc =fmin(*x,*y);
return 0;
}
int posix_m_fma(int argc, double *x, double *y, double *z, double *rc)
{
if (4 !=argc)
return -argc;
*rc =fma(*x,*y,*z);
return 0;
}