📄 libc.c
字号:
if ((long)_ulong < 0) {
_ulong = -_ulong;
sign = '-';
}
base = 10;
goto number;
case 'n':
if (flags & LONGINT)
*va_arg(argp, long *) = cnt;
else if (flags & SHORTINT)
*va_arg(argp, short *) = cnt;
else
*va_arg(argp, int *) = cnt;
break;
case 'O':
flags |= LONGINT;
/*FALLTHROUGH*/
case 'o':
ARG();
base = 8;
goto nosign;
case 'p':
/*
* ``The argument shall be a pointer to void. The
* value of the pointer is converted to a sequence
* of printable characters, in an implementation-
* defined manner.''
* -- ANSI X3J11
*/
/* NOSTRICT */
_ulong = (U32)va_arg(argp, void *);
base = 16;
goto nosign;
case 's':
if (!(t = va_arg(argp, char *)))
t = "(null)";
if (prec >= 0) {
/*
* can't use strlen; can only look for the
* NUL in the first `prec' characters, and
* strlen() will go further.
*/
char *p;
if ((p = memchr(t, 0, prec))) {
size = p - t;
if (size > prec)
size = prec;
} else
size = prec;
} else
size = strlen(t);
sign = '\0';
goto pforw;
case 'U':
flags |= LONGINT;
/*FALLTHROUGH*/
case 'u':
ARG();
base = 10;
goto nosign;
case 'X':
digs = "0123456789ABCDEF";
/* FALLTHROUGH */
case 'x':
ARG();
base = 16;
/* leading 0x/X only if non-zero */
if (flags & ALT && _ulong != 0)
flags |= HEXPREFIX;
/* unsigned conversions */
nosign: sign = '\0';
/*
* ``... diouXx conversions ... if a precision is
* specified, the 0 flag will be ignored.''
* -- ANSI X3J11
*/
number: if ((dprec = prec) >= 0)
flags &= ~ZEROPAD;
/*
* ``The result of converting a zero value with an
* explicit precision of zero is no characters.''
* -- ANSI X3J11
*/
t = buf + BUF;
if (_ulong != 0 || prec != 0) {
do {
*--t = digs[_ulong % base];
_ulong /= base;
} while (_ulong);
digs = "0123456789abcdef";
if (flags & ALT && base == 8 && *t != '0')
*--t = '0'; /* octal leading 0 */
}
size = buf + BUF - t;
pforw:
/*
* All reasonable formats wind up here. At this point,
* `t' points to a string which (if not flags&LADJUST)
* should be padded out to `width' places. If
* flags&ZEROPAD, it should first be prefixed by any
* sign or other prefix; otherwise, it should be blank
* padded before the prefix is emitted. After any
* left-hand padding and prefixing, emit zeroes
* required by a decimal [diouxX] precision, then print
* the string proper, then emit zeroes required by any
* leftover floating precision; finally, if LADJUST,
* pad with blanks.
*/
/*
* compute actual size, so we know how much to pad
* fieldsz excludes decimal prec; realsz includes it
*/
fieldsz = size + fpprec;
if (sign)
fieldsz++;
if (flags & HEXPREFIX)
fieldsz += 2;
realsz = dprec > fieldsz ? dprec : fieldsz;
/* right-adjusting blank padding */
if ((flags & (LADJUST|ZEROPAD)) == 0 && width)
for (n = realsz; n < width; n++)
putc(' ', pca);
/* prefix */
if (sign)
putc(sign, pca);
if (flags & HEXPREFIX) {
putc('0', pca);
putc((char)*fmt, pca);
}
/* right-adjusting zero padding */
if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
for (n = realsz; n < width; n++)
putc('0', pca);
/* leading zeroes from decimal precision */
for (n = fieldsz; n < dprec; n++)
putc('0', pca);
/* the string or number proper */
n=size;
while (--n >= 0)
putc(*t++, pca);
/* trailing f.p. zeroes */
while (--fpprec >= 0)
putc('0', pca);
/* left-adjusting padding (always blank) */
if (flags & LADJUST)
for (n = realsz; n < width; n++)
putc(' ', pca);
/* finally, adjust cnt */
cnt += width > realsz ? width : realsz;
break;
case '\0': /* "%?" prints ?, unless ? is NULL */
return (cnt);
default:
putc((char)*fmt, pca);
cnt++;
}
}
/* NOTREACHED */
}
/* ********************************************************************* */
/* Global functions */
/*
*********************************************************************************************
* atoi
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int atoi(char *s)
{
return (int) strtol (s, (char **) 0, 10);
}
/*
*********************************************************************************************
* memchr
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
void *memchr(
const void *s,
int c,
size_t n)
{
char *mys = (char *)s;
while ((int)--n >= 0)
if (*mys++ == c)
return (void *) --mys;
return NULL;
}
/*
*********************************************************************************************
* putch
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
static void putch(char ch, char **pca)
{
*(*pca)++ = ch;
}
/*
*********************************************************************************************
* vsprintf
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int vsprintf(char *buf, char *fmt, va_list ap)
{
int len = _doprntx(fmt, ap, (void (*)())putch, &buf);
putch(0, &buf);
return len;
}
/*
*********************************************************************************************
* sprintf
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int sprintf(char* buf, ...)
{
va_list args;
char *fmt;
int len;
va_start(args, buf);
fmt = va_arg(args, char *);
len = _doprntx(fmt, args, (void (*)())putch, &buf);
va_end(args);
putch(0, &buf);
return len;
}
/*
*********************************************************************************************
* strcpy
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
char *strcpy(char *s1, const char *s2)
{
char *s = s1;
while ((*s1++ = *s2++));
return s;
}
/*
*********************************************************************************************
* strlen
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int strlen(const char *s)
{
int l = 0;
while (*s++) l++;
return l;
}
/*
*********************************************************************************************
* strtol
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
long strtol(
const char *str,
char **endptr,
int base)
{
long i = 0;
int s = 1;
int c;
/* skip white space */
while(isspace(*str)) {
str++;
}
/* sign flag check */
if (*str == '+') str++;
else if (*str == '-') {
s = -1;
str++;
}
if (*str == '0') {
if (toupper(*++str) == 'X') base = 16,str++;
else if (base == 0) base = 8;
}
if (base == 0) base = 10;
if (base <= 10)
/* digit str to number */
for (; isdigit(*str); str++) {
if (i < max_allowable(base))
i = i * base + (*str - '0');
else {
i = MAXINT(sizeof(S32));
errno = ERANGE;
}
}
else if (base > 10) {
for (; (c = *str); str++) {
if (isdigit(c))
c = c - '0';
else {
c = toupper(c);
if (c >= 'A' && c < ('A' - 10 + base))
c = c - 'A' + 10;
else
break;
}
if (i < max_allowable(base))
i = i * base + c;
else {
i = MAXINT(sizeof(S32));
errno = ERANGE;
}
}
}
else
return 0; /* negative base is not allowed */
if (endptr) *endptr = (char *) str;
if (s == -1)
i = -i;
return i;
}
/*
*********************************************************************************************
* tolower
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int tolower(int c)
{
if (isupper(c))
return c + ('a' - 'A'); else
return c;
}
/*
*********************************************************************************************
* toupper
*
* Description: Look in the standard headers.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
int toupper(int c)
{
if (islower(c))
return c - ('a' - 'A'); else
return c;
}
/* ********************************************************************* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -