📄 genlib.c
字号:
}
else {
while(to < end) {
#ifdef WATCHDOG_ENABLED
if (((ulong)to & 0xff) == 0)
WATCHDOG_MACRO;
#endif
*to = val;
if (*to++ != val) {
failed = 1;
break;
}
}
}
done:
if (verbose) {
if (failed)
printf(" failed");
else if (verifyonly)
printf(" OK");
printf("\n");
}
if (failed)
return(-1);
else
return(0);
}
/* strcat():
* Concatenate s2 on the end of s1. S1's space must be large enough.
* Return s1.
*/
char *
strcat(register char *s1, register char *s2)
{
register char *os1;
os1 = s1;
while(*s1++)
;
--s1;
while((*s1++ = *s2++))
;
return(os1);
}
/* strchr():
* Return the ptr in sp at which the character c appears;
* NULL if not found
*/
char *
strchr(register char *sp, register char c)
{
do {
if(*sp == c)
return(sp);
} while(*sp++);
return((char *)0);
}
/* strstr():
* Find the first occurrence of find in s.
*/
char *
strstr(register char *s, register char *find)
{
register char c, sc;
register int len;
if ((c = *find++) != 0) {
len = strlen(find);
do {
do {
if ((sc = *s++) == 0)
return ((char *)0);
} while (sc != c);
} while (strncmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
/* strcmp():
* Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0
*/
int
strcmp(register char *s1,register char * s2)
{
if(s1 == s2)
return(0);
while(*s1 == *s2++)
if(*s1++ == '\0')
return(0);
return(*s1 - *--s2);
}
/* strcpy():
* Copy string s2 to s1. s1 must be large enough.
* return s1
*/
char *
strcpy(register char *s1,register char *s2)
{
register char *os1;
os1 = s1;
while((*s1++ = *s2++))
;
return(os1);
}
/* strlen():
* Returns the number of
* non-NULL bytes in string argument.
*/
int
strlen(register char *s)
{
register char *s0 = s + 1;
while (*s++ != '\0')
;
return (s - s0);
}
/* strncat():
* Concatenate s2 on the end of s1. S1's space must be large enough.
* At most n characters are moved.
* Return s1.
*/
char *
strncat(register char *s1,register char *s2,register int n)
{
register char *os1;
os1 = s1;
while(*s1++)
;
--s1;
while((*s1++ = *s2++))
if(--n < 0) {
*--s1 = '\0';
break;
}
return(os1);
}
/* strncmp():
* Compare strings (at most n bytes)
* returns: s1>s2; >0 s1==s2; 0 s1<s2; <0
*/
int
strncmp(register char *s1,register char *s2,register int n)
{
if(s1 == s2)
return(0);
while(--n >= 0 && *s1 == *s2++)
if(*s1++ == '\0')
return(0);
return((n < 0)? 0: (*s1 - *--s2));
}
/* strncpy():
* Copy s2 to s1, truncating or null-padding to always copy n bytes
* return s1
*/
char *
strncpy(register char *s1,register char *s2,register int n)
{
register char *os1 = s1;
while (--n >= 0)
if ((*s1++ = *s2++) == '\0')
while (--n >= 0)
*s1++ = '\0';
return (os1);
}
/* strpbrk():
* Return ptr to first occurance of any character from `brkset'
* in the character string `string'; NULL if none exists.
*/
char *
strpbrk(register char *string,register char *brkset)
{
register char *p;
do {
for(p=brkset; *p != '\0' && *p != *string; ++p)
;
if(*p != '\0')
return(string);
}
while(*string++);
return((char *)0);
}
/* strrchr():
* Return the ptr in sp at which the character c last
* appears; NULL if not found
*/
char *
strrchr(register char *sp, register char c)
{
register char *r;
r = (char *)0;
do {
if(*sp == c)
r = sp;
} while(*sp++);
return(r);
}
/* strspn():
* Return the number of characters in the maximum leading segment
* of string which consists solely of characters from charset.
*/
int
strspn(char *string,register char *charset)
{
register char *p, *q;
for(q=string; *q != '\0'; ++q) {
for(p=charset; *p != '\0' && *p != *q; ++p)
;
if(*p == '\0')
break;
}
return(q-string);
}
/* strtok():
* uses strpbrk and strspn to break string into tokens on
* sequentially subsequent calls. returns NULL when no
* non-separator characters remain.
* `subsequent' calls are calls with first argument NULL.
*/
char *
strtok(char *string,char *sepset)
{
register char *p, *q, *r;
static char *savept;
/*first or subsequent call*/
p = (string == (char *)0)? savept: string;
if(p == 0) /* return if no tokens remaining */
return((char *)0);
q = p + strspn(p, sepset); /* skip leading separators */
if(*q == '\0') /* return if no tokens remaining */
return((char *)0);
if((r = strpbrk(q, sepset)) == (char *)0) /* move past token */
savept = 0; /* indicate this is last token */
else {
*r = '\0';
savept = ++r;
}
return(q);
}
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
#define MBASE ('z' - 'a' + 1 + 10)
long
strtol(register char *str,char **ptr,register int base)
{
register long val;
register int c;
int xx, neg = 0;
if (ptr != (char **)0)
*ptr = str; /* in case no number is formed */
if (base < 0 || base > MBASE)
return (0); /* base is invalid -- should be a fatal error */
if (!isalnum((c = *str))) {
while (isspace(c))
c = *++str;
switch (c) {
case '-':
neg++;
case '+': /* fall-through */
c = *++str;
}
}
if (base == 0) {
if (c != '0')
base = 10;
else if (str[1] == 'x' || str[1] == 'X')
base = 16;
else
base = 8;
}
/*
* for any base > 10, the digits incrementally following
* 9 are assumed to be "abc...z" or "ABC...Z"
*/
if (!isalnum(c) || (xx = DIGIT(c)) >= base)
return (0); /* no number formed */
if (base == 16 && c == '0' && isxdigit(str[2]) &&
(str[1] == 'x' || str[1] == 'X'))
c = *(str += 2); /* skip over leading "0x" or "0X" */
for (val = -DIGIT(c); isalnum((c = *++str)) && (xx = DIGIT(c)) < base; )
/* accumulate neg avoids surprises near MAXLONG */
val = base * val - xx;
if (ptr != (char **)0)
*ptr = str;
return (neg ? val : -val);
}
unsigned long
strtoul(char *str, char **ptr,int base)
{
long val;
val = strtol(str, ptr, base);
return((unsigned long)val);
}
#if 0
/* tolower():
* If arg is upper-case, return the lower-case, else return the arg.
*/
int
tolower(register int c)
{
if(c >= 'A' && c <= 'Z')
c -= 'A' - 'a';
return(c);
}
/* toupper():
* If arg is lower-case, return upper-case, otherwise return arg.
*/
int
toupper(register int c)
{
if(c >= 'a' && c <= 'z')
c += 'A' - 'a';
return(c);
}
#endif
/* strtolower():
* In-place modification of a string to be all lower case.
*/
char *
strtolower(char *string)
{
char *cp;
cp = string;
while(*cp) {
*cp = tolower(*cp);
cp++;
}
return(string);
}
/* strtoupper():
* In-place modification of a string to be all upper case.
*/
char *
strtoupper(char *string)
{
char *cp;
cp = string;
while(*cp) {
*cp = toupper(*cp);
cp++;
}
return(string);
}
ushort
swap2(ushort sval_in)
{
return(((sval_in & 0x00ff) << 8) | ((sval_in & 0xff00) >> 8));
}
ulong
swap4(ulong sval_in)
{
return(((sval_in & 0x000000ff) << 24) | ((sval_in & 0x0000ff00) << 8) |
((sval_in & 0x00ff0000) >> 8) | ((sval_in & 0xff000000) >> 24));
}
/* Variables used by getopt: */
int optind; /* argv index to first cmd line arg that is not part
* of the option list.
*/
char *optarg; /* pointer to argument associated with an option */
int sp;
int
getopt(int argc,char *argv[],char *opts)
{
register int c;
register char *cp;
if(sp == 1) {
if (optind >= argc)
return(-1);
else if (argv[optind][0] != '-')
return(-1);
else if (argv[optind][1] == '\0')
return(-1);
else if(strcmp(argv[optind], "--") == 0) {
optind++;
return(-1);
}
}
c = argv[optind][sp];
if(c == ':' || (cp=strchr(opts, c)) == 0) {
printf("Illegal option: %c\n",c);
if(argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return('?');
}
if(*++cp == ':') {
if(argv[optind][sp+1] != '\0')
optarg = &argv[optind++][sp+1];
else if(++optind >= argc) {
printf("Option requires argument: %c\n",c);
sp = 1;
return('?');
} else
optarg = argv[optind++];
sp = 1;
} else {
if(argv[optind][++sp] == '\0') {
sp = 1;
optind++;
}
optarg = 0;
}
return(c);
}
/* getoptinit():
* Since getopt() can be used by every command in the monitor
* it's variables must be reinitialized prior to each command
* executed through docommand()...
*/
void
getoptinit(void)
{
sp = 1;
optind = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -