📄 libc.c
字号:
/* * Copyright (C) 1998, 1999, Jonathan S. Shapiro. * * This file is part of the EROS Operating System. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "system.h"#include "tar.h"typedef long word; /* "word" used for optimal copy speed */#define wsize sizeof(word)#define wmask (wsize - 1)/* * Copy a block of memory, handling overlap. * This is the routine that actually implements * (the portable versions of) bcopy, memcpy, and memmove. */#ifdef MEMCOPYvoid *memcpy(dst0, src0, length)#else#ifdef MEMMOVEvoid *memmove(dst0, src0, length)#elsevoidbcopy(src0, dst0, length)#endif#endif void *dst0; const void *src0; register size_t length;{ register char *dst = dst0; register const char *src = src0; register size_t t; if (length == 0 || dst == src) /* nothing to do */ goto done; /* * Macros: loop-t-times; and loop-t-times, t>0 */#define TLOOP(s) if (t) TLOOP1(s)#define TLOOP1(s) do { s; } while (--t) if ((unsigned long)dst < (unsigned long)src) { /* * Copy forward. */ t = (long)src; /* only need low bits */ if ((t | (long)dst) & wmask) { /* * Try to align operands. This cannot be done * unless the low bits match. */ if ((t ^ (long)dst) & wmask || length < wsize) t = length; else t = wsize - (t & wmask); length -= t; TLOOP1(*dst++ = *src++); } /* * Copy whole words, then mop up any trailing bytes. */ t = length / wsize; TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); t = length & wmask; TLOOP(*dst++ = *src++); } else { /* * Copy backwards. Otherwise essentially the same. * Alignment works as before, except that it takes * (t&wmask) bytes to align, not wsize-(t&wmask). */ src += length; dst += length; t = (long)src; if ((t | (long)dst) & wmask) { if ((t ^ (long)dst) & wmask || length <= wsize) t = length; else t &= wmask; length -= t; TLOOP1(*--dst = *--src); } t = length / wsize; TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); t = length & wmask; TLOOP(*--dst = *--src); }done:#if defined(MEMCOPY) || defined(MEMMOVE) return (dst0);#else return;#endif}intstrncmp(s1, s2, n) register const char *s1, *s2; register size_t n;{ if (n == 0) return (0); do { if (*s1 != *s2++) return (*(unsigned char *)s1 - *(unsigned char *)--s2); if (*s1++ == 0) break; } while (--n != 0); return (0);}char *#ifdef STRCHRstrchr(p, ch)#elseindex(p, ch)#endif register const char *p, ch;{ for (;; ++p) { if (*p == ch) return((char *)p); if (!*p) return((char *)NULL); } /* NOTREACHED */}char *strcpy(to, from) register char *to; register const char *from;{ char *save = to; for (; (*to = *from) != '\0'; ++from, ++to); return(save);}/* * Copy src to dst, truncating or null-padding to always copy n bytes. * Return dst. */char *strncpy(dst, src, n) char *dst; const char *src; register size_t n;{ if (n != 0) { register char *d = dst; register const char *s = src; do { if ((*d++ = *s++) == 0) { /* NUL pad the remaining n-1 bytes */ while (--n != 0) *d++ = 0; break; } } while (--n != 0); } return (dst);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -