📄 genlib.c
字号:
* non-NULL bytes in string argument. */intstrlen(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 */intstrncmp(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. */intstrspn(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)longstrtol(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 longstrtoul(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. */inttolower(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. */inttoupper(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);}ushortswap2(ushort sval_in){ uchar *cp_in, *cp_out; ushort sval_out; cp_in = (uchar *)&sval_in; cp_out = (uchar *)&sval_out; cp_out[0] = cp_in[1]; cp_out[1] = cp_in[0]; return(sval_out);}ulongswap4(ulong sval_in){ uchar *cp_in, *cp_out; ulong sval_out; cp_in = (uchar *)&sval_in; cp_out = (uchar *)&sval_out; cp_out[0] = cp_in[3]; cp_out[1] = cp_in[2]; cp_out[2] = cp_in[1]; cp_out[3] = cp_in[0]; return(sval_out);}/* 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;intgetopt(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()... */voidgetoptinit(void){ sp = 1; optind = 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -