File: stringops.c

package info (click to toggle)
delo 0.8-2.5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 476 kB
  • ctags: 1,272
  • sloc: ansic: 4,027; makefile: 200; sh: 141; asm: 55
file content (231 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download | duplicates (4)
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
/*
 * This file is licensed under the terms of the GNU General Public License,
 * version 2. See the file COPYING in the main directory for details.
 * 
 * Slooow, but small string operations, so that we don't have to link lib in.
 * Originally from linux/lib/string.c, which is
 *	Copyright (C) 1991, 1992  Linus Torvalds
 *
 * Added I/O functions for libcom_err.
 *
 *  Copyright (C) 2003  Thiemo Seufer <seufer@csv.ica.uni-stuttgart.de>
 */

#include "delo.h"
#include "stringops.h"

void *memset(void *s, int c, size_t count)
{
	char *xs = (char *) s;
	while (count--)
		*xs++ = c;
	return s;
}

void *memcpy(void *dest, const void *src, size_t count)
{
	char *tmp = (char *) dest, *s = (char *) src;
	while (count--)
		*tmp++ = *s++;
	return dest;
}

int memcmp(const void *cs, const void *ct, size_t count)
{
	const unsigned char *su1, *su2;
	signed char res = 0;

	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
		if ((res = *su1 - *su2) != 0)
			break;
	return res;
}

char *strcpy(char *dest, const char *src)
{
	char *tmp = dest;
	while ((*dest++ = *src++) != '\0');
	return tmp;
}

char *strncpy(char *dest, const char *src, int count)
{
	char *tmp = dest;
	while (count-- && (*dest++ = *src++) != '\0');
	return tmp;
}

char *strcat(char *dest, const char *src)
{
	char *tmp = dest;
	while (*dest) dest++;
	while ((*dest++ = *src++) != '\0');
	return tmp;
}

char *strncat(char *dest, const char *src, int n)
{
	char *tmp = dest;
	while (*dest) dest++;
	while (n && (*dest++ = *src++) != '\0') n--;
	if (!n) *dest = 0;
	return tmp;
}

int strcmp(const char *cs, const char *ct)
{
	int res;
	while (1)
		if ((res = *cs - *ct++) != 0 || !*cs++)
			break;
	return res;
}

int strncmp(const char *cs, const char *ct, int count)
{
	int res = 0;
	while (count) {
		if ((res = *cs - *ct++) != 0 || !*cs++)
			break;
		count--;
	}
	return res;
}

char *strchr(const char *s, int c)
{
	for(; *s != (char) c; ++s)
		if (*s == '\0')
			return 0;
	return (char *) s;
}

char *strrchr(const char *s, int c)
{
	const char *p = s + strlen(s);
	do {
		if (*p == (char)c)
			return (char *)p;
	} while (--p >= s);
	return 0;
}

unsigned int strlen(const char *s)
{
	const char *sc;
	for (sc = s; *sc != '\0'; ++sc);
	return sc - s;
}

char *strdup(const char *str)
{
	char *ret;
	ret = malloc(strlen(str) + 1);
	strcpy(ret, str);
	return ret;
}

int tolower(int c)
{
	if (c >= 'A' && c <= 'Z') return c - 'A' + 'a';
	return c;
}

int strcasecmp(const char *cs, const char *ct)
{
	register signed char __res;
	while (1)
		if ((__res = tolower(*cs) - tolower(*ct++)) != 0 || !*cs++)
			break;
	return __res;
}

int strncasecmp(const char *cs, const char *ct, size_t n)
{
	register signed char __res = 0;
	while (n--)
		if ((__res = tolower(*cs) - tolower(*ct++)) != 0 || !*cs++)
			break;
	return __res;
}

char *strstr(const char *s1, const char *s2)
{
	int l1, l2;

	l2 = strlen(s2);
	if (!l2)
		return (char *) s1;
	l1 = strlen(s1);
	while (l1 >= l2) {
		l1--;
		if (!memcmp(s1,s2,l2))
			return (char *) s1;
		s1++;
	}
	return 0;
}

int isdigit(char c)
{
	return (c >= '0' && c <= '9') ? 1 : 0;
}

typedef unsigned long time_t;

/* Not a string op, and we don't support it. */
time_t time(time_t *result)
{
	if (result)
		*result = -1;
	return -1;
}

/* libcom_err wants to have some stdio functions. */
typedef int FILE;
FILE *stderr;

int fputc(int c, UNUSED FILE *stream)
{
	printf("%c", c);
	return c;
}

int fputs(const char *s, UNUSED FILE *stream)
{
	printf("%s", s);
	return 1;
}

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
	size_t i;

	for (i = 0; i < (size * nmemb); i++) {
		char c = *((char *)ptr)++;

		fputc(c, stream);
	}
	return nmemb;
}

typedef int va_list;

int vfprintf(UNUSED FILE *stream, UNUSED const char *format,
	     UNUSED va_list ap)
{
	/* Link dummy, unused as long as com_err() gets provided a NULL
	   pointer as format arg. */
	return 0;
}

int fflush(UNUSED FILE *stream)
{
	return 0;
}

/* We don't support this */
char *strerror(UNUSED int errno)
{
	return NULL;
}