strncpy.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2006-2007 Axis Communications AB
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <string.h>
  7. /* Experimentally off - libc_hidden_proto(memset) */
  8. /* Experimentally off - libc_hidden_proto(strncpy) */
  9. char *strncpy(char *dest, const char *src, size_t count)
  10. {
  11. char *ret = dest;
  12. unsigned long himagic = 0x80808080L;
  13. unsigned long lomagic = 0x01010101L;
  14. while (count && (unsigned long)src & (sizeof src - 1))
  15. {
  16. count--;
  17. if (!(*dest++ = *src++))
  18. {
  19. goto finalize;
  20. }
  21. }
  22. while (count >= sizeof (unsigned long))
  23. {
  24. unsigned long value = *(unsigned long*)src;
  25. unsigned long magic;
  26. if ((magic = (value - lomagic) & himagic))
  27. {
  28. if (magic & ~value)
  29. {
  30. break;
  31. }
  32. }
  33. *(unsigned long*)dest = value;
  34. dest += sizeof (unsigned long);
  35. src += sizeof (unsigned long);
  36. count -= sizeof (unsigned long);
  37. }
  38. while (count)
  39. {
  40. count--;
  41. if (!(*dest++ = *src++))
  42. break;
  43. }
  44. finalize:
  45. if (count)
  46. {
  47. memset(dest, '\0', count);
  48. }
  49. return ret;
  50. }
  51. libc_hidden_def(strncpy)