/boot/memstr.c

https://github.com/XVilka/2ndboot-ng · C · 92 lines · 57 code · 12 blank · 23 comment · 12 complexity · 279aabbd5244bc1844de3808c81d99ce MD5 · raw file

  1. /*
  2. * A module for creating rebootless custom image boot support.
  3. *
  4. * Copyright (C) 2010 XVilka <xvilka at gmail.com>
  5. *
  6. * Inspired by 2ndboot by dimich: http://hg.ezxdev.org/2ndboot/
  7. *
  8. * This file is part of 2ndboot-ng.
  9. *
  10. * 2ndboot-ng is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Foobar is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with 2ndboot-ng. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include "string.h"
  25. void *memcpy(void *dest, const void *src, size_t n) {
  26. char *d = (char*)dest;
  27. const char *s = (const char*)src;
  28. while (n-- > 0) {
  29. *d++ = *s++;
  30. }
  31. return dest;
  32. }
  33. void *memset(void *dest, int fill, size_t n) {
  34. unsigned char *d = (unsigned char*)dest;
  35. while (n-- > 0) {
  36. *d++ = (unsigned char)fill;
  37. }
  38. return dest;
  39. }
  40. int memcmp(const void *s1, const void *s2, size_t n) {
  41. unsigned char *ss1 = (unsigned char*)s1;
  42. unsigned char *ss2 = (unsigned char*)s2;
  43. while (n-- > 0) {
  44. if (*ss1 != *ss2) {
  45. return *ss1 - *ss2;
  46. }
  47. ++ss1;
  48. ++ss2;
  49. }
  50. return 0;
  51. }
  52. size_t strlen(const char *s) {
  53. size_t n = 0;
  54. while (*s++) n++;
  55. return n;
  56. }
  57. char *strcpy(char *d, const char *s) {
  58. char *dest = d;
  59. while (*d++ = *s++);
  60. return dest;
  61. }
  62. int strcmp(const char *s1, const char *s2) {
  63. size_t l1, l2;
  64. l1 = strlen(s1);
  65. l2 = strlen(s2);
  66. if (l1 < l2) {
  67. return -1;
  68. }
  69. if (l1 > l2) {
  70. return 1;
  71. }
  72. while (*s1) {
  73. if (*s1 != *s2) {
  74. return *s1 - *s2;
  75. }
  76. ++s1;
  77. ++s2;
  78. }
  79. return 0;
  80. }