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