00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "plibc_private.h"
00026
00027 #if !HAVE_STRNDUP
00028
00033 char *strndup (const char *s, size_t n)
00034 {
00035 size_t len = strnlen (s, n);
00036 char *new = (char *) malloc (len + 1);
00037
00038 if (new == NULL)
00039 return NULL;
00040
00041 new[len] = '\0';
00042 return (char *) memcpy (new, s, len);
00043 }
00044 #endif
00045
00046 #if !HAVE_STRNLEN
00047
00051 size_t strnlen (const char *str, size_t maxlen)
00052 {
00053 const char *char_ptr, *end_ptr = str + maxlen;
00054 const unsigned long int *longword_ptr;
00055 unsigned long int longword, magic_bits, himagic, lomagic;
00056
00057 if (maxlen == 0)
00058 return 0;
00059
00060 if (__builtin_expect (end_ptr < str, 0))
00061 end_ptr = (const char *) ~0UL;
00062
00063
00064
00065 for (char_ptr = str; ((unsigned long int) char_ptr
00066 & (sizeof (longword) - 1)) != 0;
00067 ++char_ptr)
00068 if (*char_ptr == '\0')
00069 {
00070 if (char_ptr > end_ptr)
00071 char_ptr = end_ptr;
00072 return char_ptr - str;
00073 }
00074
00075
00076
00077
00078 longword_ptr = (unsigned long int *) char_ptr;
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 magic_bits = 0x7efefeffL;
00090 himagic = 0x80808080L;
00091 lomagic = 0x01010101L;
00092 if (sizeof (longword) > 4)
00093 {
00094
00095
00096 magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
00097 himagic = ((himagic << 16) << 16) | himagic;
00098 lomagic = ((lomagic << 16) << 16) | lomagic;
00099 }
00100 if (sizeof (longword) > 8)
00101 abort ();
00102
00103
00104
00105
00106 while (longword_ptr < (unsigned long int *) end_ptr)
00107 {
00108
00109
00110
00111 longword = *longword_ptr++;
00112
00113 if ((longword - lomagic) & himagic)
00114 {
00115
00116
00117
00118 const char *cp = (const char *) (longword_ptr - 1);
00119
00120 char_ptr = cp;
00121 if (cp[0] == 0)
00122 break;
00123 char_ptr = cp + 1;
00124 if (cp[1] == 0)
00125 break;
00126 char_ptr = cp + 2;
00127 if (cp[2] == 0)
00128 break;
00129 char_ptr = cp + 3;
00130 if (cp[3] == 0)
00131 break;
00132 if (sizeof (longword) > 4)
00133 {
00134 char_ptr = cp + 4;
00135 if (cp[4] == 0)
00136 break;
00137 char_ptr = cp + 5;
00138 if (cp[5] == 0)
00139 break;
00140 char_ptr = cp + 6;
00141 if (cp[6] == 0)
00142 break;
00143 char_ptr = cp + 7;
00144 if (cp[7] == 0)
00145 break;
00146 }
00147 }
00148 char_ptr = end_ptr;
00149 }
00150
00151 if (char_ptr > end_ptr)
00152 char_ptr = end_ptr;
00153 return char_ptr - str;
00154 }
00155 #endif
00156
00157