aboutsummaryrefslogtreecommitdiff
path: root/libstd/src/__host.c
blob: d0428b0fbca3a134854778a5e73e2a917e172e63 (plain)
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
/*

   Copyright (C) 2007-2008 Free Software Foundation, Inc.
   Contributed by STMicroelectronics

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.  */

#if !defined OPT_MSCORLIB_WRAPPER

   /* If we are compiling this, we are sure this is to build C-runtime*/
#  define __LIBSTD_CRT_BUILD

#  include "include/__cdefs.h"

#  if defined(__GNU_CIL__)
#    error "__host.c : Sorry : Platform not supported"
#  endif

#endif


/* include __host before any target include to avoid errors
   generated by defines in them */
#include "include/__host.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <float.h>
#include <math.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>



static int my_errno = 0;

#define UPDATE_ERRNO() do { if (errno!=0) update_errno(); } while (0)

static void update_errno(void)
{
    switch (errno) {
    case 0:
        break;
#if defined ENOENT
    case ENOENT: my_errno = __LIBSTD_ENOENT; break;
#endif
#if defined EIO
    case EIO: my_errno = __LIBSTD_EIO; break;
#endif
#if defined EBADF
    case EBADF: my_errno = __LIBSTD_EBADF; break;
#endif
#if defined EAGAIN
    case EAGAIN:  my_errno = __LIBSTD_EAGAIN; break;
#endif
#if defined ENOMEM
    case ENOMEM:  my_errno = __LIBSTD_ENOMEM; break;
#endif
#if defined EACCES
    case EACCES: my_errno = __LIBSTD_EACCES; break;
#endif
#if defined EFAULT
    case EFAULT: my_errno = __LIBSTD_EFAULT; break;
#endif
#if defined EEXIST
    case EEXIST: my_errno = __LIBSTD_EEXIST; break;
#endif
#if defined ENOTDIR
    case ENOTDIR: my_errno = __LIBSTD_ENOTDIR; break;
#endif
#if defined EINVAL
    case EINVAL: my_errno = __LIBSTD_EINVAL; break;
#endif
#if defined EMFILE
    case EMFILE: my_errno = __LIBSTD_EMFILE; break;
#endif
#if defined ESPIPE
    case ESPIPE: my_errno = __LIBSTD_ESPIPE; break;
#endif
#if defined EDOM
    case EDOM: my_errno = __LIBSTD_EDOM; break;
#endif
#if defined ERANGE
    case ERANGE: my_errno = __LIBSTD_ERANGE; break;
#endif
#if defined ENAMETOOLONG
    case ENAMETOOLONG: my_errno = __LIBSTD_ENAMETOOLONG; break;
#endif
#if defined EILSEQ
    case EILSEQ: my_errno = __LIBSTD_EILSEQ; break;
#endif
#if defined ENOSYS
    case ENOSYS:
#endif
    default:
        my_errno = __LIBSTD_ENOSYS;
        break;
    }
}

LIBSTD_HPROTO_IMPL(void *, malloc, unsigned int size)
{
    void *result = malloc((size_t)size);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(void *, realloc, void *ptr, unsigned int size)
{
    void *result = realloc(ptr, (size_t)size);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(void, free, void *ptr)
{
    free(ptr);
}

LIBSTD_HPROTO_IMPL(char *, getenv, char *name)
{
    return getenv(name);
}

/* Close a file descriptor. */
LIBSTD_HPROTO_IMPL(int, close, int fildes)
{
    int result;
    result = close(fildes);
    UPDATE_ERRNO();
    return result;
}

/* Return file size. */
LIBSTD_HPROTO_IMPL(int, filesize, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
         result = s.st_size;
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(long long, file_lastaccess_sutc, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
#ifdef __USE_MISC
        result = s.st_atim.tv_sec;
#else        
        result = s.st_atime;
#endif                
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(long long, file_lastwrite_sutc, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
#ifdef __USE_MISC
        result = s.st_mtim.tv_sec;
#else        
        result = s.st_mtime;
#endif                
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(long long, file_filecreation_sutc, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
#ifdef __USE_MISC
        result = s.st_ctim.tv_sec;
#else        
        result = s.st_ctime;
#endif                
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(int, file_mode_flags, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
        result = s.st_mode;
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(int, file_uid, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
        result = s.st_uid;
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

LIBSTD_HPROTO_IMPL(int, file_gid, char* path)
{
    int result;
    struct stat s;
    if (stat(path, &s) == 0)
        result = s.st_gid;
    else {
        UPDATE_ERRNO();
        result = -1;
    }
    return result;
}

/* Called when process exit, to signal IOs _cleanup to host. */
LIBSTD_HPROTO_IMPL(void, exit, int status)
{
    exit(status);
}

/* Control over an open file descriptor.*/
LIBSTD_HPROTO_IMPL(int, fcntl, int fildes, int cmd, int arg)
{
    long result;
    result = fcntl(fildes, cmd, arg);
    UPDATE_ERRNO();
    return result;
}

/* Manages file pointers.  */
LIBSTD_HPROTO_IMPL(long, lseek, int fildes, long offset, int whence)
{
    long result;
    result = lseek(fildes, offset, whence);
    UPDATE_ERRNO();
    return result;
}

/* Create a directory entry.  */
LIBSTD_HPROTO_IMPL(int, link, char *existing, char *anew)
{
    int result;
    result = link(existing, anew);
    UPDATE_ERRNO();
    return result;
}


/* Opens a file descriptor on a file.
   oflags is oneof : O_RDONLY (0), O_WRONLY (1), O_RDWR (2)*/
LIBSTD_HPROTO_IMPL(int, open, char *path, int oflag)
{
    int result;
    int host_flags = 0;

    if ((oflag & __LIBSTD_O_WRONLY) != 0) {
        host_flags |= O_WRONLY;
    }
    if ((oflag & __LIBSTD_O_RDWR) != 0) {
        host_flags |= O_RDWR;
    }
    if ((oflag & __LIBSTD_O_RDONLY) != 0) {
        host_flags |= O_RDONLY;
    }
    if ((oflag & __LIBSTD_O_CREAT) != 0) {
        host_flags |= O_CREAT;
/*         host_flag = host_flag | 0x0040; */
    }
    if ((oflag & __LIBSTD_O_TRUNC) != 0) {
        host_flags |= O_TRUNC;
    }
    if ((oflag & __LIBSTD_O_APPEND) != 0) {
        host_flags |= O_APPEND;
/*         host_flag = host_flag | 0x0400; */
    }
    if ((oflag & __LIBSTD_O_BINARY) != 0) {
#if defined O_BINARY
        host_flags |= O_BINARY;
#endif
    }

    result = open(path, host_flags, 0666);
    UPDATE_ERRNO();
    return result;
}

/* Attempts to read bytes from stream.  */
LIBSTD_HPROTO_IMPL(int, read, int fildes, void *buf, int nbytes)
{
    int result;
    result = read(fildes, buf, nbytes);
    UPDATE_ERRNO();
    return result;
}

/* Renames a file. */
LIBSTD_HPROTO_IMPL(int, rename, char *oldname, char *newname)
{
    int result;
    result = rename(oldname, newname);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(int, remove, char *filename)
{
    int result;
    result = remove(filename);
    UPDATE_ERRNO();
    return result;
}

/* Removes a directory entry.  */
LIBSTD_HPROTO_IMPL(int, unlink, const char *path)
{
    int result;
    result = unlink(path);
    UPDATE_ERRNO();
    return result;
}

/* Attempts to write bytes into stream.    */
LIBSTD_HPROTO_IMPL(int, write, int fildes, void *buf, int nbytes)
{
    int result;
    result = write(fildes, buf, nbytes);
    UPDATE_ERRNO();
    return result;
}

/* Getting host montime */
LIBSTD_HPROTO_IMPL(int, clock, void)
{
    int __host__clock_ = -1 ;
#if defined(__ST40)
    struct tms temp;
    times(&temp);
    __host__clock_ = temp.tms_utime + temp.tms_stime;
#else
    __host__clock_ = clock();
#endif
    return __host__clock_ ;
}

LIBSTD_HPROTO_IMPL(unsigned long, time, void *tptr)
{
    unsigned long *l_tptr = (unsigned long *)tptr;
    return time(l_tptr);
}

LIBSTD_HPROTO_IMPL(void, gmtime, void *timer,
                         int *tm_sec,  int *tm_min,  int *tm_hour,
                         int *tm_mday, int *tm_mon,  int *tm_year,
                         int *tm_wday, int *tm_yday, int *tm_isdst)
{
    const unsigned long *l_timer = (const unsigned long *)timer;
    struct tm * _tm = gmtime(l_timer);
    *tm_sec   = _tm->tm_sec;
    *tm_min   = _tm->tm_min;
    *tm_hour  = _tm->tm_hour;
    *tm_mday  = _tm->tm_mday;
    *tm_mon   = _tm->tm_mon;
    *tm_year  = _tm->tm_year;
    *tm_wday  = _tm->tm_wday;
    *tm_yday  = _tm->tm_yday;
    *tm_isdst = _tm->tm_isdst;
}

LIBSTD_HPROTO_IMPL(void, gettimeofday, void *_tv_sec, void *_tv_usec)
{
    long *tv_sec  = (long *)_tv_sec;
    long *tv_usec = (long *)_tv_usec;

    struct timeval tv;

    gettimeofday (&tv, NULL);

    *tv_sec  = (long)(tv.tv_sec);
    *tv_usec = (long)(tv.tv_usec);
    
}

/* Getting process time ifnormations */
LIBSTD_HPROTO_IMPL(int, gettimes, unsigned long * clks, unsigned long * tms_utime, unsigned long *tms_stime, unsigned long *tms_cutime, unsigned long *tms_cstime)
{
  struct tms t;

  *clks = times (&t);
  *tms_utime = t.tms_utime;
  *tms_stime = t.tms_stime;
  *tms_cutime = t.tms_cutime;
  *tms_cstime = t.tms_cstime; 
  
  return 0;
}

/* get configuration information at runtime */
LIBSTD_HPROTO_IMPL(long, getsysconf, int name)
{
  return sysconf(name);
}

LIBSTD_HPROTO_IMPL(int, fpclassify, double p0)
{
    return fpclassify(p0);
}

LIBSTD_HPROTO_IMPL(double, dbl_epsilon, void)
{
    return DBL_EPSILON;
}

LIBSTD_HPROTO_IMPL(double, dbl_minval, void)
{
    return DBL_MIN;
}

LIBSTD_HPROTO_IMPL(double, dbl_maxval, void)
{
    return DBL_MAX;
}

LIBSTD_HPROTO_IMPL(double, dbl_hugeval, void)
{
    return HUGE_VAL;
}

LIBSTD_HPROTO_IMPL(float, flt_epsilon, void)
{
    return FLT_EPSILON;
}

LIBSTD_HPROTO_IMPL(float, flt_minval, void)
{
    return FLT_MIN;
}

LIBSTD_HPROTO_IMPL(float, flt_maxval, void)
{
    return FLT_MAX;
}

LIBSTD_HPROTO_IMPL(float, flt_hugeval, void)
{
    return HUGE_VALF;
}

LIBSTD_HPROTO_IMPL(float, flt_nan, void)
{
    return NAN;
}

LIBSTD_HPROTO_IMPL(float, flt_infinity, void)
{
    return INFINITY;
}

/*************************************************************************************************/
/*************** MATH FUNCTIONS ******************************************************************/
/*************************************************************************************************/
LIBSTD_HPROTO_IMPL(double, acos, double p0)
{
    return acos(p0);
}

LIBSTD_HPROTO_IMPL(float, acosf, float p0)
{
    return acosf(p0);
}

LIBSTD_HPROTO_IMPL(double, asin, double p0)
{
    return asin(p0);
}

LIBSTD_HPROTO_IMPL(float, asinf, float p0)
{
    return asinf(p0);
}

LIBSTD_HPROTO_IMPL(double, atan, double p0)
{
    return atan(p0);
}

LIBSTD_HPROTO_IMPL(float, atanf, float p0)
{
    return atanf(p0);
}

LIBSTD_HPROTO_IMPL(double, atan2, double p0, double p1)
{
    return atan2(p0, p1);
}

LIBSTD_HPROTO_IMPL(float, atan2f, float p0, float p1)
{
    return atan2f(p0, p1);
}

LIBSTD_HPROTO_IMPL(double, cos, double p0)
{
    return cos(p0);
}

LIBSTD_HPROTO_IMPL(float, cosf, float p0)
{
    return cosf(p0);
}

LIBSTD_HPROTO_IMPL(double, sin, double p0)
{
    return sin(p0);
}

LIBSTD_HPROTO_IMPL(float, sinf, float p0)
{
    return sinf(p0);
}

LIBSTD_HPROTO_IMPL(double, tan, double p0)
{
    return tan(p0);
}

LIBSTD_HPROTO_IMPL(float, tanf, float p0)
{
    return tanf(p0);
}

LIBSTD_HPROTO_IMPL(double, cosh, double p0)
{
    return cosh(p0);
}

LIBSTD_HPROTO_IMPL(float, coshf, float p0)
{
    return coshf(p0);
}

LIBSTD_HPROTO_IMPL(double, sinh, double p0)
{
    return sinh(p0);
}

LIBSTD_HPROTO_IMPL(float, sinhf, float p0)
{
    return sinhf(p0);
}

LIBSTD_HPROTO_IMPL(double, tanh, double p0)
{
    return tanh(p0);
}

LIBSTD_HPROTO_IMPL(float, tanhf, float p0)
{
    return tanhf(p0);
}

LIBSTD_HPROTO_IMPL(double, exp, double p0)
{
    return exp(p0);
}

LIBSTD_HPROTO_IMPL(float, expf, float p0)
{
    return expf(p0);
}

LIBSTD_HPROTO_IMPL(double, exp2, double p0)
{
    return exp2(p0);
}

LIBSTD_HPROTO_IMPL(float, exp2f, float p0)
{
    return exp2f(p0);
}

LIBSTD_HPROTO_IMPL(double, expm1, double p0)
{
    return expm1(p0);
}

LIBSTD_HPROTO_IMPL(float, expm1f, float p0)
{
    return expm1f(p0);
}

LIBSTD_HPROTO_IMPL(double, log, double p0)
{
    return log(p0);
}

LIBSTD_HPROTO_IMPL(float, logf, float p0)
{
    return logf(p0);
}

LIBSTD_HPROTO_IMPL(double, log10, double p0)
{
    return log10(p0);
}

LIBSTD_HPROTO_IMPL(float, log10f, float p0)
{
    return log10f(p0);
}

LIBSTD_HPROTO_IMPL(double, log1p, double p0)
{
    return log1p(p0);
}

LIBSTD_HPROTO_IMPL(float, log1pf, float p0)
{
    return log1pf(p0);
}

LIBSTD_HPROTO_IMPL(double, log2, double p0)
{
    return log2(p0);
}

LIBSTD_HPROTO_IMPL(float, log2f, float p0)
{
    return log2f(p0);
}


LIBSTD_HPROTO_IMPL(double, cbrt, double p0)
{
    return cbrt(p0);
}

LIBSTD_HPROTO_IMPL(float, cbrtf, float p0)
{
    return cbrtf(p0);
}

LIBSTD_HPROTO_IMPL(double, fabs, double p0)
{
    return fabs(p0);
}

LIBSTD_HPROTO_IMPL(float, fabsf, float p0)
{
    return fabsf(p0);
}

LIBSTD_HPROTO_IMPL(double, pow, double p0, double p1)
{
    return pow(p0,p1);
}

LIBSTD_HPROTO_IMPL(float, powf, float p0, float p1)
{
    return powf(p0,p1);
}

LIBSTD_HPROTO_IMPL(double, sqrt, double p0)
{
    return sqrt(p0);
}

LIBSTD_HPROTO_IMPL(float, sqrtf, float p0)
{
    return sqrtf(p0);
}

LIBSTD_HPROTO_IMPL(double, ceil, double p0)
{
    return ceil(p0);
}

LIBSTD_HPROTO_IMPL(float, ceilf, float p0)
{
    return ceilf(p0);
}

LIBSTD_HPROTO_IMPL(double, floor, double p0)
{
    return floor(p0);
}

LIBSTD_HPROTO_IMPL(float, floorf, float p0)
{
    return floorf(p0);
}

LIBSTD_HPROTO_IMPL(double, round, double p0)
{
    return round(p0);
}

LIBSTD_HPROTO_IMPL(float, roundf, float p0)
{
    return roundf(p0);
}

LIBSTD_HPROTO_IMPL(long, lround, double p0)
{
    return lround(p0);
}

LIBSTD_HPROTO_IMPL(long, lroundf, float p0)
{
    return lroundf(p0);
}

LIBSTD_HPROTO_IMPL(double, ldexp, double p0, int p1)
{
    double result = ldexp(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(float, ldexpf, float p0, int p1)
{
    float result = ldexpf(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(double, hypot, double p0, double p1)
{
    double result = hypot(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(float, hypotf, float p0, float p1)
{
    float result = hypotf(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(double, scalbn, double p0, int p1)
{
    double result = scalbn(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(float, scalbnf, float p0, int p1)
{
    float result = scalbnf(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(double, scalbln, double p0, long p1)
{
    double result = scalbln(p0,p1);
    UPDATE_ERRNO();
    return result;
}

LIBSTD_HPROTO_IMPL(float, scalblnf, float p0, long p1)
{
    float result = scalblnf(p0,p1);
    UPDATE_ERRNO();
    return result;
}

/* ensure the prototype is defined correctly */
#if defined(errno)
#undef errno
#endif
LIBSTD_HPROTO_IMPL(int *, errno, void)
{
    return &my_errno;
}