Reworking usermode/libc to closer C compliance
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / lib.c
1 /*
2  AcessOS 1
3  Dynamic Loader
4  By thePowersGang
5 */
6 #include "common.h"
7 #include <stdint.h>
8
9 // === CODE ===
10 char *strcpy(char *dest, const char *src)
11 {
12         char    *ret = dest;
13         while(*src) {
14                 *dest = *src;
15                 src ++; dest ++;
16         }
17         *dest = '\0';
18         return ret;
19 }
20
21 char *strcat(char *dest, const char *src)
22 {
23         char    *ret = dest;
24         while(*dest)    dest++;
25         while(*src)             *dest++ = *src++;
26         *dest = '\0';
27         return ret;
28 }
29
30 /**
31  * \fn int strcmp(const char *s1, const char *s2)
32  * \brief Compare two strings
33  */
34 int strcmp(const char *s1, const char *s2)
35 {
36         while(*s1 && *s1 == *s2) s1++,s2++;
37         return *s1-*s2;
38 }
39
40 /**
41  * \fn int strlen(const char *str)
42  * \brief 
43  */
44 int strlen(const char *str)
45 {
46          int    len = 0;
47         while(*str)     len++,str++;
48         return len;
49 }
50
51 int memcmp(const void *p1, const void *p2, int len)
52 {
53         const char      *b1 = p1, *b2 = p2;
54         while(len --)
55         {
56                 if(b1 != b2)    return b1 - b2;
57         }
58         return 0;
59 }
60
61 /**
62  * \fn int file_exists(char *filename)
63  * \brief Checks if a file exists
64  */
65 int file_exists(const char *filename)
66 {
67          int    fd;
68         //fd = open(filename, OPENFLAG_READ);
69         fd = open(filename, 0);
70         if(fd == -1)    return 0;
71         close(fd);
72         return 1;
73 }
74
75 uint64_t __udivdi3(uint64_t Num, uint64_t Den)
76 {
77         uint64_t        ret = 0, add = 1;
78
79         // Find what power of two times Den is > Num
80         while( Num >= Den )
81         {
82                 Den <<= 1;
83                 add <<= 1;
84         }
85
86         // Search backwards
87         while( add > 1 )
88         {
89                 add >>= 1;
90                 Den >>= 1;
91                 // If the numerator is > Den, subtract and add to return value
92                 if( Num > Den )
93                 {
94                         ret += add;
95                         Num -= Den;
96                 }
97         }
98 //      if(Rem) *Rem = Num;
99         return ret;
100 }
101

UCC git Repository :: git.ucc.asn.au