《操作系统》的实验代码。
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

367 linhas
11 KiB

  1. #include <string.h>
  2. #include <x86.h>
  3. /* *
  4. * strlen - calculate the length of the string @s, not including
  5. * the terminating '\0' character.
  6. * @s: the input string
  7. *
  8. * The strlen() function returns the length of string @s.
  9. * */
  10. size_t
  11. strlen(const char *s) {
  12. size_t cnt = 0;
  13. while (*s ++ != '\0') {
  14. cnt ++;
  15. }
  16. return cnt;
  17. }
  18. /* *
  19. * strnlen - calculate the length of the string @s, not including
  20. * the terminating '\0' char acter, but at most @len.
  21. * @s: the input string
  22. * @len: the max-length that function will scan
  23. *
  24. * Note that, this function looks only at the first @len characters
  25. * at @s, and never beyond @s + @len.
  26. *
  27. * The return value is strlen(s), if that is less than @len, or
  28. * @len if there is no '\0' character among the first @len characters
  29. * pointed by @s.
  30. * */
  31. size_t
  32. strnlen(const char *s, size_t len) {
  33. size_t cnt = 0;
  34. while (cnt < len && *s ++ != '\0') {
  35. cnt ++;
  36. }
  37. return cnt;
  38. }
  39. /* *
  40. * strcpy - copies the string pointed by @src into the array pointed by @dst,
  41. * including the terminating null character.
  42. * @dst: pointer to the destination array where the content is to be copied
  43. * @src: string to be copied
  44. *
  45. * The return value is @dst.
  46. *
  47. * To avoid overflows, the size of array pointed by @dst should be long enough to
  48. * contain the same string as @src (including the terminating null character), and
  49. * should not overlap in memory with @src.
  50. * */
  51. char *
  52. strcpy(char *dst, const char *src) {
  53. #ifdef __HAVE_ARCH_STRCPY
  54. return __strcpy(dst, src);
  55. #else
  56. char *p = dst;
  57. while ((*p ++ = *src ++) != '\0')
  58. /* nothing */;
  59. return dst;
  60. #endif /* __HAVE_ARCH_STRCPY */
  61. }
  62. /* *
  63. * strncpy - copies the first @len characters of @src to @dst. If the end of string @src
  64. * if found before @len characters have been copied, @dst is padded with '\0' until a
  65. * total of @len characters have been written to it.
  66. * @dst: pointer to the destination array where the content is to be copied
  67. * @src: string to be copied
  68. * @len: maximum number of characters to be copied from @src
  69. *
  70. * The return value is @dst
  71. * */
  72. char *
  73. strncpy(char *dst, const char *src, size_t len) {
  74. char *p = dst;
  75. while (len > 0) {
  76. if ((*p = *src) != '\0') {
  77. src ++;
  78. }
  79. p ++, len --;
  80. }
  81. return dst;
  82. }
  83. /* *
  84. * strcmp - compares the string @s1 and @s2
  85. * @s1: string to be compared
  86. * @s2: string to be compared
  87. *
  88. * This function starts comparing the first character of each string. If
  89. * they are equal to each other, it continues with the following pairs until
  90. * the characters differ or until a terminanting null-character is reached.
  91. *
  92. * Returns an integral value indicating the relationship between the strings:
  93. * - A zero value indicates that both strings are equal;
  94. * - A value greater than zero indicates that the first character that does
  95. * not match has a greater value in @s1 than in @s2;
  96. * - And a value less than zero indicates the opposite.
  97. * */
  98. int
  99. strcmp(const char *s1, const char *s2) {
  100. #ifdef __HAVE_ARCH_STRCMP
  101. return __strcmp(s1, s2);
  102. #else
  103. while (*s1 != '\0' && *s1 == *s2) {
  104. s1 ++, s2 ++;
  105. }
  106. return (int)((unsigned char)*s1 - (unsigned char)*s2);
  107. #endif /* __HAVE_ARCH_STRCMP */
  108. }
  109. /* *
  110. * strncmp - compares up to @n characters of the string @s1 to those of the string @s2
  111. * @s1: string to be compared
  112. * @s2: string to be compared
  113. * @n: maximum number of characters to compare
  114. *
  115. * This function starts comparing the first character of each string. If
  116. * they are equal to each other, it continues with the following pairs until
  117. * the characters differ, until a terminating null-character is reached, or
  118. * until @n characters match in both strings, whichever happens first.
  119. * */
  120. int
  121. strncmp(const char *s1, const char *s2, size_t n) {
  122. while (n > 0 && *s1 != '\0' && *s1 == *s2) {
  123. n --, s1 ++, s2 ++;
  124. }
  125. return (n == 0) ? 0 : (int)((unsigned char)*s1 - (unsigned char)*s2);
  126. }
  127. /* *
  128. * strchr - locates first occurrence of character in string
  129. * @s: the input string
  130. * @c: character to be located
  131. *
  132. * The strchr() function returns a pointer to the first occurrence of
  133. * character in @s. If the value is not found, the function returns 'NULL'.
  134. * */
  135. char *
  136. strchr(const char *s, char c) {
  137. while (*s != '\0') {
  138. if (*s == c) {
  139. return (char *)s;
  140. }
  141. s ++;
  142. }
  143. return NULL;
  144. }
  145. /* *
  146. * strfind - locates first occurrence of character in string
  147. * @s: the input string
  148. * @c: character to be located
  149. *
  150. * The strfind() function is like strchr() except that if @c is
  151. * not found in @s, then it returns a pointer to the null byte at the
  152. * end of @s, rather than 'NULL'.
  153. * */
  154. char *
  155. strfind(const char *s, char c) {
  156. while (*s != '\0') {
  157. if (*s == c) {
  158. break;
  159. }
  160. s ++;
  161. }
  162. return (char *)s;
  163. }
  164. /* *
  165. * strtol - converts string to long integer
  166. * @s: the input string that contains the representation of an integer number
  167. * @endptr: reference to an object of type char *, whose value is set by the
  168. * function to the next character in @s after the numerical value. This
  169. * parameter can also be a null pointer, in which case it is not used.
  170. * @base: x
  171. *
  172. * The function first discards as many whitespace characters as necessary until
  173. * the first non-whitespace character is found. Then, starting from this character,
  174. * takes as many characters as possible that are valid following a syntax that
  175. * depends on the base parameter, and interprets them as a numerical value. Finally,
  176. * a pointer to the first character following the integer representation in @s
  177. * is stored in the object pointed by @endptr.
  178. *
  179. * If the value of base is zero, the syntax expected is similar to that of
  180. * integer constants, which is formed by a succession of:
  181. * - An optional plus or minus sign;
  182. * - An optional prefix indicating octal or hexadecimal base ("0" or "0x" respectively)
  183. * - A sequence of decimal digits (if no base prefix was specified) or either octal
  184. * or hexadecimal digits if a specific prefix is present
  185. *
  186. * If the base value is between 2 and 36, the format expected for the integral number
  187. * is a succession of the valid digits and/or letters needed to represent integers of
  188. * the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The
  189. * sequence may optionally be preceded by a plus or minus sign and, if base is 16,
  190. * an optional "0x" or "0X" prefix.
  191. *
  192. * The strtol() function returns the converted integral number as a long int value.
  193. * */
  194. long
  195. strtol(const char *s, char **endptr, int base) {
  196. int neg = 0;
  197. long val = 0;
  198. // gobble initial whitespace
  199. while (*s == ' ' || *s == '\t') {
  200. s ++;
  201. }
  202. // plus/minus sign
  203. if (*s == '+') {
  204. s ++;
  205. }
  206. else if (*s == '-') {
  207. s ++, neg = 1;
  208. }
  209. // hex or octal base prefix
  210. if ((base == 0 || base == 16) && (s[0] == '0' && s[1] == 'x')) {
  211. s += 2, base = 16;
  212. }
  213. else if (base == 0 && s[0] == '0') {
  214. s ++, base = 8;
  215. }
  216. else if (base == 0) {
  217. base = 10;
  218. }
  219. // digits
  220. while (1) {
  221. int dig;
  222. if (*s >= '0' && *s <= '9') {
  223. dig = *s - '0';
  224. }
  225. else if (*s >= 'a' && *s <= 'z') {
  226. dig = *s - 'a' + 10;
  227. }
  228. else if (*s >= 'A' && *s <= 'Z') {
  229. dig = *s - 'A' + 10;
  230. }
  231. else {
  232. break;
  233. }
  234. if (dig >= base) {
  235. break;
  236. }
  237. s ++, val = (val * base) + dig;
  238. // we don't properly detect overflow!
  239. }
  240. if (endptr) {
  241. *endptr = (char *) s;
  242. }
  243. return (neg ? -val : val);
  244. }
  245. /* *
  246. * memset - sets the first @n bytes of the memory area pointed by @s
  247. * to the specified value @c.
  248. * @s: pointer the the memory area to fill
  249. * @c: value to set
  250. * @n: number of bytes to be set to the value
  251. *
  252. * The memset() function returns @s.
  253. * */
  254. void *
  255. memset(void *s, char c, size_t n) {
  256. #ifdef __HAVE_ARCH_MEMSET
  257. return __memset(s, c, n);
  258. #else
  259. char *p = s;
  260. while (n -- > 0) {
  261. *p ++ = c;
  262. }
  263. return s;
  264. #endif /* __HAVE_ARCH_MEMSET */
  265. }
  266. /* *
  267. * memmove - copies the values of @n bytes from the location pointed by @src to
  268. * the memory area pointed by @dst. @src and @dst are allowed to overlap.
  269. * @dst pointer to the destination array where the content is to be copied
  270. * @src pointer to the source of data to by copied
  271. * @n: number of bytes to copy
  272. *
  273. * The memmove() function returns @dst.
  274. * */
  275. void *
  276. memmove(void *dst, const void *src, size_t n) {
  277. #ifdef __HAVE_ARCH_MEMMOVE
  278. return __memmove(dst, src, n);
  279. #else
  280. const char *s = src;
  281. char *d = dst;
  282. if (s < d && s + n > d) {
  283. s += n, d += n;
  284. while (n -- > 0) {
  285. *-- d = *-- s;
  286. }
  287. } else {
  288. while (n -- > 0) {
  289. *d ++ = *s ++;
  290. }
  291. }
  292. return dst;
  293. #endif /* __HAVE_ARCH_MEMMOVE */
  294. }
  295. /* *
  296. * memcpy - copies the value of @n bytes from the location pointed by @src to
  297. * the memory area pointed by @dst.
  298. * @dst pointer to the destination array where the content is to be copied
  299. * @src pointer to the source of data to by copied
  300. * @n: number of bytes to copy
  301. *
  302. * The memcpy() returns @dst.
  303. *
  304. * Note that, the function does not check any terminating null character in @src,
  305. * it always copies exactly @n bytes. To avoid overflows, the size of arrays pointed
  306. * by both @src and @dst, should be at least @n bytes, and should not overlap
  307. * (for overlapping memory area, memmove is a safer approach).
  308. * */
  309. void *
  310. memcpy(void *dst, const void *src, size_t n) {
  311. #ifdef __HAVE_ARCH_MEMCPY
  312. return __memcpy(dst, src, n);
  313. #else
  314. const char *s = src;
  315. char *d = dst;
  316. while (n -- > 0) {
  317. *d ++ = *s ++;
  318. }
  319. return dst;
  320. #endif /* __HAVE_ARCH_MEMCPY */
  321. }
  322. /* *
  323. * memcmp - compares two blocks of memory
  324. * @v1: pointer to block of memory
  325. * @v2: pointer to block of memory
  326. * @n: number of bytes to compare
  327. *
  328. * The memcmp() functions returns an integral value indicating the
  329. * relationship between the content of the memory blocks:
  330. * - A zero value indicates that the contents of both memory blocks are equal;
  331. * - A value greater than zero indicates that the first byte that does not
  332. * match in both memory blocks has a greater value in @v1 than in @v2
  333. * as if evaluated as unsigned char values;
  334. * - And a value less than zero indicates the opposite.
  335. * */
  336. int
  337. memcmp(const void *v1, const void *v2, size_t n) {
  338. const char *s1 = (const char *)v1;
  339. const char *s2 = (const char *)v2;
  340. while (n -- > 0) {
  341. if (*s1 != *s2) {
  342. return (int)((unsigned char)*s1 - (unsigned char)*s2);
  343. }
  344. s1 ++, s2 ++;
  345. }
  346. return 0;
  347. }