Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613000
  • 博文数量: 113
  • 博客积分: 2554
  • 博客等级: 少校
  • 技术积分: 1428
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-21 19:53
文章分类

全部博文(113)

文章存档

2014年(1)

2013年(2)

2012年(94)

2011年(16)

分类: LINUX

2012-02-29 16:24:58

  1. long __cdecl _atol_(const char *nptr)
  2. {
  3.     int c;
  4.     long total;
  5.     int sign; /* if '-', then negative, otherwise positive */

  6.     /* skip whitespace */
  7.     while (isspace((int)(unsigned char)*nptr)) ++nptr;

  8.     // 这是由于用到了 int = char 这种赋值,由于int 和char 的占用空间不同,
  9.     // 在char赋值int时,要进行扩充,假设int是4个字节,
  10.     // 那么需要在char前面填充3个字节然后赋值给int,然而这3个字节填充什么呢??
  11.     // 想当然的肯定是填0了,但char是有符号的,一般最高位为1表示负数,
  12.     // 如果简单的扩充,那么变成int后就成了正数了。。。
  13.     // 所以这个填充可能有不同的方式,有的编译器是看最高位吧,
  14.     // 如果为1,就全填充1,如果为0就全填充0,或者有其他的方式。
  15.     // 但对于unsigned char赋值却只用填充0就行。
  16.     c = (int)(unsigned char)*nptr++;
  17.     sign = c;
  18.     if (c == '-' || c == '+') c = (int)(unsigned char)*nptr++; /* skip sign */

  19.     total = 0;
  20.     while (isdigit(c))
  21.     {
  22.         total = 10 * total + (c - '0');
  23.         c = (int)(unsigned char)*nptr++;
  24.     }

  25.     return (sign == '-') ? -total : total;
  26. }

  27. int __cdecl _strcmp_(const char *src, const char *dst)
  28. {
  29.     int ret = 0;

  30.     while (!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
  31.         ++src, ++dst;

  32.     if (ret < 0) ret = -1;
  33.     else if (ret > 0) ret = 1;

  34.     return(ret);
  35. }

  36. char* __cdecl _strcpy_(char* dst, const char* src)
  37. {
  38.     char* tmp = dst;
  39.     while ((*tmp++ = *src++) != '/0');

  40.     return dst;
  41. }

  42. char* __cdecl _strcat_(char *dst, const char *src)
  43. {
  44.     char* tmp = dst;
  45.     while (*tmp++);
  46.     for (--tmp; *tmp++ = *src++;);

  47.     return dst;
  48. }

  49. char* __cdecl _strrev_(char *string)
  50. {
  51.     char *start = string;
  52.     char *left = string;

  53.     while (*string++);
  54.     string -= 2;
  55.     while (left < string)
  56.     {
  57.         char ch = *left;
  58.         *left++ = *string;
  59.         *string-- = ch;
  60.     }

  61.     return(start);
  62. }

  63. char* __cdecl _strchr_(char *string, int ch)
  64. {
  65.     while (string != '/0' && *string == ch) string++;

  66.     return *string == ch ? string : (char*) 0;
  67. }

  68. char * __cdecl _strrchr_(const char * string, int ch)
  69. {
  70.     char *start = (char *)string;

  71.     /* find end of string */
  72.     while (*string++);
  73.     /* search towards front */
  74.     while (--string != start && *string != (char)ch);

  75.     /* char found ? */
  76.     if (*string == (char)ch) return( (char *)string );

  77.     return(NULL);
  78. }


  79. int __cdecl _stricmp_ (const char *dst, const char *src)
  80. {
  81.     int f, l;

  82.     do
  83.     {
  84.         if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z'))
  85.             f -= 'A' - 'a';
  86.         if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z'))
  87.             l -= 'A' - 'a';
  88.     }
  89.     while (f && (f == l));

  90.     return(f - l);
  91. }

  92. size_t __cdecl _strlen_(const char *str)
  93. {
  94.     const char *eos = str;
  95.     while(*eos++);
  96.     return(eos - str - 1);
  97. }

  98. char * __cdecl _strstr_(const char * str1, const char * str2)
  99. {
  100.     char *cp = (char *)str1;
  101.     char *s1 = NULL, *s2 = NULL;

  102.     if (!*str2) return((char *)str1);

  103.     while (*cp)
  104.     {
  105.         s1 = cp;
  106.         s2 = (char *)str2;

  107.         while (*s1 && *s2 && !(*s1-*s2)) s1++, s2++;

  108.         if (!*s2) return(cp);

  109.         cp++;
  110.     }

  111.     return(NULL);
  112. }

  113. char * __cdecl _strset_(char * string, int val)
  114. {
  115.     char *start = string;

  116.     while (*string) *string++ = (char)val;

  117.     return(start);
  118. }

  119. void * __cdecl _memchr_(const void * buf, int chr, size_t cnt)
  120. {
  121.     while (cnt && (*(unsigned char *)buf != (unsigned char)chr))
  122.     {
  123.         buf = (unsigned char *)buf + 1;
  124.         cnt--;
  125.     }

  126.     return(cnt ? (void *)buf : NULL);
  127. }

  128. int __cdecl _memcmp_(const void * buf1, const void * buf2, size_t count)
  129. {
  130.     if (!count) return(0);

  131.     while ( --count && *(char *)buf1 == *(char *)buf2 )
  132.     {
  133.         buf1 = (char *)buf1 + 1;
  134.         buf2 = (char *)buf2 + 1;
  135.     }

  136.     return( *((unsigned char *)buf1) - *((unsigned char *)buf2) );
  137. }

  138. void * __cdecl _memcpy_(void * dst, const void * src, size_t count)
  139. {
  140.     void * ret = dst;

  141.     while (count--)
  142.     {
  143.         *(char *)dst = *(char *)src;
  144.         dst = (char *)dst + 1;
  145.         src = (char *)src + 1;
  146.     }

  147.     return(ret);
  148. }

  149. int __cdecl _memicmp_(const void * first, const void * last, size_t count)
  150. {
  151.     int f = 0;
  152.     int l = 0;

  153.     while ( count-- )
  154.     {
  155.         if ((*(unsigned char *)first == *(unsigned char *)last) ||
  156.             ((f = tolower( *(unsigned char *)first )) ==
  157.             (l = tolower( *(unsigned char *)last ))))
  158.         {
  159.             first = (char *)first + 1;
  160.             last = (char *)last + 1;
  161.         }
  162.         else
  163.             break;
  164.     }

  165.     return (f - l);
  166. }

  167. void * __cdecl _memmove_(void * dst, const void * src, size_t count)
  168. {
  169.     void * ret = dst;

  170.     if (dst <= src || (char *)dst >= ((char *)src + count))
  171.     { // copy from lower addresses to higher addresses
  172.         while (count--)
  173.         {
  174.             *(char *)dst = *(char *)src;
  175.             dst = (char *)dst + 1;
  176.             src = (char *)src + 1;
  177.         }
  178.     }
  179.     else
  180.     { // copy from higher addresses to lower addresses
  181.         dst = (char *)dst + count - 1;
  182.         src = (char *)src + count - 1;

  183.         while (count--)
  184.         {
  185.             *(char *)dst = *(char *)src;
  186.             dst = (char *)dst - 1;
  187.             src = (char *)src - 1;
  188.         }
  189.     }

  190.     return(ret);
  191. }

  192. void * __cdecl _memset_(void *dst, int val, size_t count)
  193. {
  194.     void *start = dst;

  195.     while (count--)
  196.     {
  197.         *(char *)dst = (char)val;
  198.         dst = (char *)dst + 1;
  199.     }

  200.     return(start);
  201. }
阅读(727) | 评论(0) | 转发(0) |
0

上一篇:extern“c”

下一篇:字符串反转

给主人留下些什么吧!~~