summaryrefslogtreecommitdiff
path: root/sos-code-article5/sos/klibc.c
blob: 9d719c15d09aa1c79dfaf4b24017c1656dc4d7ef (plain) (blame)
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
/* Copyright (C) 2004  David Decotigny (with INSA Rennes for vsnprintf)
   Copyright (C) 2003  The KOS Team
   Copyright (C) 1999  Free Software Foundation

   This program 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
   of the License, or (at your option) any later version.
   
   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA. 
*/
#include "klibc.h"

/* For an optimized version, see BSD sources ;) */
void *memcpy(void *dst0, const void *src0, register unsigned int size)
{
  char *dst;
  const char *src;
  for (dst = (char*)dst0, src = (const char*)src0 ;
       size > 0 ;
       dst++, src++, size--)
    *dst = *src;
  return dst0;
}

/* ditto */
void *memset(void *dst0, register int c, register unsigned int length)
{
  char *dst;
  for (dst = (char*) dst0 ;
       length > 0 ;
       dst++, length --)
    *dst = (char)c;
  return dst0;
}

int memcmp(const void *s1, const void *s2, sos_size_t len)
{
  const unsigned char *c1, *c2;
  unsigned int i;
 
  for (i = 0, c1 = s1, c2 = s2; i < len; i++, c1++, c2++)
    {
      if(*c1 != *c2)
        return *c1 - *c2;
    }
 
  return 0;
}


unsigned int strlen(register const char *str)
{
  unsigned int retval = 0;
  
  while (*str++)
    retval++;
  
  return retval;
}


unsigned int strnlen(const char * s, sos_size_t count)
{
  const char *sc;
  
  for (sc = s; count-- && *sc != '\0'; ++sc)
    /* nothing */continue;

  return sc - s;
}


char *strzcpy(register char *dst, register const char *src, register int len)
{
  int i;

  if (len <= 0)
    return dst;
  
  for (i = 0; i < len; i++)
    {
      dst[i] = src[i];
      if(src[i] == '\0')
        return dst;
    }
  
  dst[len-1] = '\0'; 
  return dst;
}


char *strzcat (char *dest, const char *src, sos_size_t n)
{
  char *res = dest;
  
  for ( ; *dest ; dest++);
  
  for ( ; *src ; src++, dest++) {
    *dest = *src;
    n--;
    if (n <= 0)
      break;
  }

  *dest = '\0';
  return res;
}

int strcmp(register const char *s1, register const char *s2)
{
  while (*s1 == *s2++)
    if (*s1++ == 0)
      return (0);
  
  return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}


int strncmp(register const char *s1, register const char *s2, register int len)
{
  char c1 = '\0', c2 = '\0';
  
  while (len > 0)
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0' || c1 != c2)
        return c1 - c2;
      len--;
    }
  
  return c1 - c2;
}


/* I (d2) borrowed and rewrote this for Nachos/INSA Rennes. Thanks to
   them for having kindly allowed me to do so. */
int vsnprintf(char *buff, sos_size_t len, const char * format, va_list ap)
{
  sos_size_t i, result;
  
  if (!buff || !format || (len < 0))
    return -1;
  
#define PUTCHAR(thechar) \
  do { \
    if (result < len-1) \
      *buff++ = (thechar);  \
    result++; \
  } while (0)
  
  result = 0;
  for(i=0 ; format[i] != '\0' ; i++){
    switch (format[i])
      {
      case '%':
	i++;
	switch(format[i])
	  {
	  case '%':
	    {
	      PUTCHAR('%');
	      break;
	    }
	  case 'i':;
	  case 'd':
	    {
	      int integer = va_arg(ap,int);
	      int cpt2 = 0;
	      char buff_int[16];
	      
	      if (integer<0)
		PUTCHAR('-');
	      /* Ne fait pas integer = -integer ici parce que INT_MIN
		 n'a pas d'equivalent positif (int = [-2^31, 2^31-1]) */
	      
	      do {
		int m10 = integer%10;
		m10 = (m10 < 0)? -m10:m10;
		buff_int[cpt2++]=(char)('0'+ m10);
		integer=integer/10;
	      } while(integer!=0);
	      
	      for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--)
		PUTCHAR(buff_int[cpt2]);
	      
	      break;
	    }
	    
	  case 'c':
	    {
	      int value = va_arg(ap,int);
	      PUTCHAR((char)value);
	      break;
	    }
	    
	  case 's':
	    {
	      char *string = va_arg(ap,char *);
	      if (! string)
		string = "(null)";
	      for( ; *string != '\0' ; string++)
		PUTCHAR(*string);
	      break;
	    }

	  case 'p':
	    PUTCHAR('0');
	    PUTCHAR('x');
	  case 'x':
	    {
	      unsigned int hexa = va_arg(ap,int);
	      unsigned int nb;
	      int i, had_nonzero = 0;
	      for(i=0 ; i < 8 ; i++)
		{
		  nb = (unsigned int)(hexa << (i*4));
		  nb = (nb >> 28) & 0xf;
		  // Skip the leading zeros
		  if (nb == 0)
		    {
		      if (had_nonzero)
			PUTCHAR('0');
		    }
		  else
		    {
		      had_nonzero = 1;
		      if (nb < 10)
			PUTCHAR('0'+nb);
		      else
			PUTCHAR('a'+(nb-10));
		    }
		}
	      if (! had_nonzero)
		PUTCHAR('0');
	      break;
	    }
	    break;
	
	  default:
            PUTCHAR('%');
	    PUTCHAR(format[i]);
	  }
	break;
	
      default:
        PUTCHAR(format[i]);
      }
  }
  
  *buff = '\0';
  return result;
}


int snprintf(char * buff, sos_size_t len, const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  len = vsnprintf(buff, len, format, ap);
  va_end(ap);
 
  return len;
}