]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/fosa_misc.c
6c867e0474c64f29d687f3d6b92ac2720f46451e
[frescor/fosa.git] / src_rtlinux / fosa_misc.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //    
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //  This file is part of the FRSH implementation
24 //
25 //  FRSH is free software; you can  redistribute it and/or  modify
26 //  it under the terms of  the GNU General Public License as published by
27 //  the Free Software Foundation;  either  version 2, or (at  your option)
28 //  any later version.
29 //
30 //  FRSH  is distributed  in  the hope  that  it  will  be useful,  but
31 //  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
32 //  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
33 //  General Public License for more details.
34 //
35 //  You should have  received a  copy of  the  GNU  General Public License
36 //  distributed  with  FRSH;  see file COPYING.   If not,  write to the
37 //  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
38 //  02111-1307, USA.
39 //
40 //  As a special exception, if you include this header file into source
41 //  files to be compiled, this header file does not by itself cause
42 //  the resulting executable to be covered by the GNU General Public
43 //  License.  This exception does not however invalidate any other
44 //  reasons why the executable file might be covered by the GNU General
45 //  Public License.
46 // -----------------------------------------------------------------------
47 //fosa_misc.h
48 //==============================================
49 //  ********  ******    ********  **********
50 //  **///// /**    **  **//////  /**     /**
51 //  **      /**    ** /**        /**     /**
52 //  ******* /**    ** /********* /**********
53 //  **////  /**    ** ////////** /**//////**
54 //  **      /**    **        /** /**     /**
55 //  **      /**    **  ********  /**     /**
56 //  //       /******/  ////////   //      // 
57 //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
60
61 //#include <fosa.h>
62 #include <stdarg.h>
63
64 extern int vsprintf(char *str, const char *format, va_list ap);
65 extern void rtl_printf(const char *format, ...);
66 typedef int size_t;
67
68 static char vsprintf_buffer[512];
69
70 int vprintf(const char *fmt, va_list ap) {
71   int i=vsprintf(vsprintf_buffer, fmt, ap);
72   rtl_printf(vsprintf_buffer);
73   return i;
74 }
75
76 int printf(const char *fmt,...) {
77   int i;
78   va_list args;
79
80   va_start(args, fmt);
81   i=vsprintf(vsprintf_buffer, fmt, args);
82   va_end(args);
83   rtl_printf(vsprintf_buffer);
84   return i;
85 }
86
87 typedef unsigned long FILE;
88
89 FILE stdout=2;
90
91 int vfprintf(FILE *stream, const char *fmt, va_list ap) {
92   int i=vsprintf(vsprintf_buffer, fmt, ap);
93   rtl_printf(vsprintf_buffer);
94   return i;
95 }
96
97 static void *memccpy(void *dst, const void *src, int c, int count)
98 {
99   char *a = dst;
100   const char *b = src;
101   while (count--)
102   {
103     *a++ = *b;
104     if (*b==c)
105     {
106       return (void *)a;
107     }
108     b++;
109   }
110   return 0;
111 }
112
113 char *strncpy(char *dest, const char *src, size_t n) {
114   memccpy(dest,src,0,n);
115   return dest;
116 }
117
118 int strncmp(const char *s1, const char *s2, size_t n) {
119   const unsigned char* a=(const unsigned char*)s1;
120   const unsigned char* b=(const unsigned char*)s2;
121   const unsigned char* fini=a+n;
122   while (a<fini) {
123     int res=*a-*b;
124     if (res) return res;
125     if (!*a) return 0;
126     ++a; ++b;
127   }
128   return 0;
129 }
130
131 int puts(const char *s) {
132   rtl_printf(s);
133   return 0;
134 }
135
136 typedef signed long time_t;
137 extern long long gethrtime(void);
138 time_t time(time_t *t) {
139   time_t sec;
140   long long nsec;
141   
142   nsec=gethrtime();
143   sec=(time_t)(nsec/1000000000LL);
144   if (t)
145     *t=sec;
146   return sec;
147 }
148
149 #define NSECS 1000000000LL
150 struct timespec {
151   time_t tv_sec;        /* seconds */
152   long tv_nsec;         /* nanoseconds */
153 };
154
155 extern int nanosleep(const struct timespec *req, struct timespec *rem);
156
157 unsigned int sleep(unsigned int seg) {
158   struct timespec req={
159     .tv_sec=seg%NSECS,
160     .tv_nsec=seg/NSECS,
161   }, rem;
162   nanosleep(&req, &rem);
163   return rem.tv_sec;
164 }
165
166 void exit(int val) {
167   struct timespec req={
168     .tv_sec=1000,
169     .tv_nsec=1000000,
170   };
171   rtl_printf ("Application exited with %d\n", val);
172   while(1) {
173     nanosleep(&req, 0);
174   }
175 }
176
177 void __assert_fail (const char *assertion, const char *file, 
178                     unsigned int line, const char *function) {
179   rtl_printf("Assertion `%s` FILE: %s LINE: %d FUNCTION: %s failed.\n",
180              assertion, file, line, function);
181   exit(-1);
182 }