home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / strcpy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  1.4 KB  |  67 lines

  1. /* In SunOS 4.1.1 the strcpy function references memory past the last byte of 
  2.    the string!  This will core dump if the memory following the last byte is 
  3.    not mapped.
  4.  
  5.    Here are correct versions by hbs@lucid.com.
  6. */
  7.  
  8. #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
  9.  
  10. #define MAGIC    0x7efefeff
  11. #define HIGH_BIT_P(c) ((c) & hi_bit)
  12. #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
  13.  
  14. char *
  15. strcpy (char *to, const char *from)
  16. {
  17.   char *return_value = to;
  18.   if (to == from)
  19.     return to;
  20.   else if (ALIGNED (to) && ALIGNED (from))
  21.     {
  22.       register unsigned long *to1 = (unsigned long *) to;
  23.       register unsigned long *from1 = (unsigned long *) from;
  24.       register unsigned long c;
  25.       register unsigned long magic = MAGIC;
  26.       register unsigned long not_magic = ~magic;
  27. /*      register unsigned long hi_bit = 0x80000000; */
  28.  
  29.       while (c = *from1)
  30.         {
  31.           if (HAS_ZERO(c)) 
  32.             {
  33.               to = (char *) to1;
  34.               from = (char *) from1;
  35.               goto slow_loop;
  36.             }
  37.           else
  38.             {
  39.               *to1 = c;
  40.               to1++; 
  41.               from1++;
  42.             }
  43.         }
  44.  
  45.       to = (char *) to1;
  46.       *to = (char) 0;
  47.       return return_value;
  48.     }
  49.   else
  50.     {
  51.       char c;
  52.  
  53.     slow_loop:
  54.  
  55.       while (c = *from)
  56.         {
  57.           *to = c;
  58.           to++;
  59.           from++;
  60.         }
  61.       *to = (char) 0;
  62.     }
  63.   return return_value;
  64. }
  65.  
  66.  
  67.