string.c
1.63 KB
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
#include "romloader.h"
/*
* Concatenate s2 on the end of s1. S1's space must be large enough.
* Return s1.
*/
char *
strcat(char *s1, const char *s2)
{
register char *os1;
os1 = s1;
while (*s1++)
;
--s1;
while (*s1++ = *s2++)
;
return(os1);
}
/* @(#)strcmp.c 4.1 (Berkeley) 12/21/80 */
/*
* Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0
*/
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == '\0')
return(0);
return(*s1 - *--s2);
}
/* @(#)strcpy.c 4.1 (Berkeley) 10/5/82 */
/*
* Copy string s2 to s1. s1 must be large enough.
* return s1
*/
char *
strcpy(char *s1, const char *s2)
{
register char *os1;
os1 = s1;
while (*s1++ = *s2++)
;
return(os1);
}
/* @(#)strlen.c 4.1 (Berkeley) 12/21/80 */
/*
* Returns the number of
* non-NULL bytes in string argument.
*/
unsigned int
strlen(const char *s)
{
register unsigned int n;
n = 0;
while (*s++)
n++;
return(n);
}
char *
strstr(const char *instr1, const char *instr2)
{
register const char *s1, *s2, *tptr;
register char c;
if (instr2 == (char *)0 || *instr2 == '\0')
return((char *)instr1);
s1 = instr1;
s2 = instr2;
c = *s2;
while (*s1)
if (*s1++ == c) {
tptr = s1;
while ((c = *++s2) == *s1++ && c)
;
if (c == 0)
return((char *)tptr - 1);
s1 = tptr;
s2 = instr2; /* reset search string */
c = *s2;
}
return((char *)0);
}
/*
* Return the ptr in sp at which the character c appears;
* NULL if not found
*/
char *
strchr(const char *sp, int c)
{
register char ch = (char)c;
do {
if(*sp == ch)
return((char *)sp);
} while(*sp++);
return(0);
}