The file string.h is also included in romcc code, which has no malloc().
[coreboot.git] / src / include / string.h
1 #ifndef STRING_H
2 #define STRING_H
3
4 #include <stddef.h>
5 #include <stdlib.h>
6
7 void *memcpy(void *dest, const void *src, size_t n);
8 void *memmove(void *dest, const void *src, size_t n);
9 void *memset(void *s, int c, size_t n);
10 int memcmp(const void *s1, const void *s2, size_t n);
11 int sprintf(char * buf, const char *fmt, ...);
12
13 // yes, linux has fancy ones. We don't care. This stuff gets used 
14 // hardly at all. And the pain of including those files is just too high.
15
16 //extern inline void strcpy(char *dst, char *src) {while (*src) *dst++ = *src++;}
17
18 //extern inline int strlen(char *src) { int i = 0; while (*src++) i++; return i;}
19
20 static inline size_t strnlen(const char *src, size_t max) 
21
22         size_t i = 0;
23         while((*src++) && (i < max)) {
24                 i++;
25         }
26         return i;
27 }
28
29 static inline size_t strlen(const char *src)
30 {
31         size_t i = 0;
32         while(*src++) {
33                 i++;
34         }
35         return i;
36 }
37
38 static inline char *strchr(const char *s, int c)
39 {
40         for (; *s; s++) {
41                 if (*s == c)
42                         return (char *) s;
43         }       
44         return 0;
45 }
46
47 #ifndef __ROMCC__
48 static inline char *strdup(const char *s)
49 {   
50         size_t sz = strlen(s) + 1;
51         char *d = malloc(sz);
52         memcpy(d, s, sz);
53         return d;
54 }
55 #endif
56
57 static inline char *strncpy(char *to, const char *from, int count)
58 {
59         register char *ret = to;
60
61         while (count > 0) {
62                 count--;
63                 if ((*to++ = *from++) == '\0')
64                         break;
65         }
66
67         while (count > 0) {
68                 count--;
69                 *to++ = '\0';
70         }
71         return ret;
72 }
73
74 static inline int strcmp(const char *s1, const char *s2)
75 {   
76         int r;
77
78         while ((r = (*s1 - *s2)) == 0 && *s1) {
79                 s1++;
80                 s2++;
81         }
82         return r;
83 }  
84
85 static inline int strncmp(const char *s1, const char *s2, int maxlen)
86 {
87         int i;
88
89         for (i = 0; i < maxlen; i++) {
90                 if (s1[i] != s2[i])
91                         return s1[i] - s2[i];
92         }
93
94         return 0;
95 }
96
97 static inline int isspace(int c)
98 {
99         switch (c) {
100         case ' ': case '\f': case '\n':
101         case '\r': case '\t': case '\v':
102                 return 1;
103         default:
104                 return 0;
105         }
106 }
107
108 static inline int isdigit(int c)
109 {
110         return (c >= '0' && c <= '9');
111 }
112
113 static inline int isxdigit(int c)
114 {
115         return ((c >= '0' && c <= '9') ||
116                 (c >= 'a' && c <= 'f') ||
117                 (c >= 'A' && c <= 'F'));
118 }
119
120 static inline int isupper(int c)
121 {
122         return (c >= 'A' && c <= 'Z');
123 }
124
125 static inline int islower(int c)
126 {
127         return (c >= 'a' && c <= 'z');
128 }
129
130 static inline int toupper(int c)
131 {
132         if (islower(c))
133                 c -= 'a'-'A';
134         return c;
135 }
136
137 static inline int tolower(int c)
138 {
139         if (isupper(c))
140                 c -= 'A'-'a';
141         return c;
142 }
143 #endif /* STRING_H */