-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstr.c
More file actions
172 lines (140 loc) · 4.08 KB
/
Copy pathstr.c
File metadata and controls
172 lines (140 loc) · 4.08 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
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
/* STR.C -- COLD-WAVE Operating System
Copyright (C) 2011-12 Munshi Riaz
riaz.2012@gmail.com
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 3 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, see <http://www.gnu.org/licenses/>.*/
#include <str.h>
char * strcat(char * destination,char * source)
{
register char * dp=destination; //destination pointer, in a register to make it quicker
register char * sp=source;
while (*dp) dp++;
while (*sp) //no test on the necessary space for
//destination:i.e overflow possible
{ //sp may not contain an END_CHAR
*dp = *sp;
dp++;
sp++;
}
*dp = END_CHAR;
return destination;
}
/*!
The function searches for the first element of the string s that equals (char)c. It considers the terminating
null character as part of the string. If successful, the function returns the address of the matching element;
otherwise, it returns a null pointer.
*/
char * strchr(char * s,int c)
{
register char * tempS=s;
while ( (*tempS != c) && (*tempS != END_CHAR) )//search c until the end
{
tempS++;
}
if (*tempS ==c) return tempS; //c was found
else return (char*) NULL;
}
/*!
The function compares successive elements from two strings, s1 and s2, until it finds elements that are not equal.
* If all elements are equal, the function returns zero.
* If the differing element from s1 is greater than the element from s2, the function returns a positive value.
* Otherwise, the function returns a negative value.
*/
int strcmp(char * s1,char * s2)
{
register char * tempS1=s1, * tempS2=s2;
while ( (*tempS1 != END_CHAR) && (*tempS2 != END_CHAR) && (*tempS1 == *tempS2) )
{
tempS1++;
tempS2++;
}
return (*tempS1)-(*tempS2);
}
/* an other algorithme possible for the strcmp function, By the OsKit group kernel:
int strcmp(const char *s1, const char *s2)
{
unsigned int a, b;
while ( (a = *s1++), (b = *s2++), a && b) {
if (a != b)
return (a-b);
}
return a-b;
}*/
/*!
The function copies the string s2, including its terminating null character, to successive elements of the array
of char whose first element has the address s1. It returns s1.
*/
inline char * strcopy(char s1[], const char s2[])
{
register int i=-1;
do
{
i++;
s1[i]=s2[i];
} while (s2[i] != END_CHAR);
return s1;
}
inline char * strcpy(char s1[], const char s2[])
{
return strcopy(s1,s2);
}
/*!
The function returns the number of characters in the string s, not including its terminating null character.
*/
/*size_t strlen(const char * s)
{
register int i=0;
while (s[i] != END_CHAR)
{
i++;
}
return i;
}*/
/*!
* read a line of text that ends with <ENTER>; the final '\n' is not added to
* the result text, but NULL ('\0') caracter is added
* this function is secured, it doesn't accept more than LINE_LENGTH (for exemple :255) characters
*/
char * Sreadline()
{
char car;
int i=0; //to walk throught "line"
char line[LINE_LENGTH];
car=getkey();
while ((car != '\n') && (i <= MAX_LINE_CHAR))
{
line[i]=car;
i++;
car=getkey();
}
line[i]='\0';
return line;
}
/*!
* read a line of text that ends with <ENTER>; the final '\n' is not added to
* the result text, but NULL ('\0') caracter is added
* this function is a non secured, it can cause an overflow
*/
char * readline()
{
char car;
int i=0; //to walk throught "line"
char line[LINE_LENGTH];
car=getkey();
while (car != '\n')
{
line[i]=car;
i++;
car=getkey();
}
line[i]='\0';
return line;
}