代碼
stdarg.h
#ifndef _STDARG_H
#define _STDARG_H
typedef char *va_list;
/* Amount of space required in an argument list for an arg of type TYPE.
TYPE may alternatively be an expression whose type is used. */
#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
#ifndef __sparc__
#define va_start(AP, LASTARG) \
(AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#else
#define va_start(AP, LASTARG) \
(__builtin_saveregs (), \
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
#endif
void va_end (va_list); /* Defined in gnulib */
#define va_end(AP)
#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))
#endif /* _STDARG_H */
string.h
#ifndef _STRING_H_
#define _STRING_H_
#ifndef NULL
#define NULL ((void *) 0)
#endif
#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif
extern char * strerror(int errno);
/*
* This string-include defines all string functions as inline
* functions. Use gcc. It also assumes ds=es=data space, this should be
* normal. Most of the string-functions are rather heavily hand-optimized,
* see especially strtok,strstr,str[c]spn. They should work, but are not
* very easy to understand. Everything is done entirely within the register
* set, making the functions fast and clean. String instructions have been
* used through-out, making for "slightly" unclear code :-)
*
* (C) 1991 Linus Torvalds
*/
extern inline char * strcpy(char * dest,const char *src)
{
__asm__("cld\n"
"1:\tlodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b"
::"S" (src),"D" (dest));
return dest;
}
static inline char * strncpy(char * dest,const char *src,int count)
{
__asm__("cld\n"
"1:\tdecl %2\n\t"
"js 2f\n\t"
"lodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n\t"
"rep\n\t"
"stosb\n"
"2:"
::"S" (src),"D" (dest),"c" (count));
return dest;
}
extern inline char * strcat(char * dest,const char * src)
{
__asm__("cld\n\t"
"repne\n\t"
"scasb\n\t"
"decl %1\n"
"1:\tlodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b"
::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff));
return dest;
}
static inline char * strncat(char * dest,const char * src,int count)
{
__asm__("cld\n\t"
"repne\n\t"
"scasb\n\t"
"decl %1\n\t"
"movl %4,%3\n"
"1:\tdecl %3\n\t"
"js 2f\n\t"
"lodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n"
"2:\txorl %2,%2\n\t"
"stosb"
::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff),"g" (count)
);
return dest;
}
extern inline int strcmp(const char * cs,const char * ct)
{
register int __res ;
__asm__("cld\n"
"1:\tlodsb\n\t"
"scasb\n\t"
"jne 2f\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n\t"
"xorl %%eax,%%eax\n\t"
"jmp 3f\n"
"2:\tmovl $1,%%eax\n\t"
"jl 3f\n\t"
"negl %%eax\n"
"3:"
:"=a" (__res):"D" (cs),"S" (ct));
return __res;
}
static inline int strncmp(const char * cs,const char * ct,int count)
{
register int __res ;
__asm__("cld\n"
"1:\tdecl %3\n\t"
"js 2f\n\t"
"lodsb\n\t"
"scasb\n\t"
"jne 3f\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n"
"2:\txorl %%eax,%%eax\n\t"
"jmp 4f\n"
"3:\tmovl $1,%%eax\n\t"
"jl 4f\n\t"
"negl %%eax\n"
"4:"
:"=a" (__res):"D" (cs),"S" (ct),"c" (count));
return __res;
}
static inline char * strchr(const char * s,char c)
{
register char * __res ;
__asm__("cld\n\t"
"movb %%al,%%ah\n"
"1:\tlodsb\n\t"
"cmpb %%ah,%%al\n\t"
"je 2f\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n\t"
"movl $1,%1\n"
"2:\tmovl %1,%0\n\t"
"decl %0"
:"=a" (__res):"S" (s),"0" (c));
return __res;
}
static inline char * strrchr(const char * s,char c)
{
register char * __res;
__asm__("cld\n\t"
"movb %%al,%%ah\n"
"1:\tlodsb\n\t"
"cmpb %%ah,%%al\n\t"
"jne 2f\n\t"
"movl %%esi,%0\n\t"
"decl %0\n"
"2:\ttestb %%al,%%al\n\t"
"jne 1b"
:"=d" (__res):"0" (0),"S" (s),"a" (c));
return __res;
}
extern inline int strspn(const char * cs, const char * ct)
{
register char * __res;
__asm__("cld\n\t"
"movl %4,%%edi\n\t"
"repne\n\t"
"scasb\n\t"
"notl %%ecx\n\t"
"decl %%ecx\n\t"
"movl %%ecx,%%edx\n"
"1:\tlodsb\n\t"
"testb %%al,%%al\n\t"
"je 2f\n\t"
"movl %4,%%edi\n\t"
"movl %%edx,%%ecx\n\t"
"repne\n\t"
"scasb\n\t"
"je 1b\n"
"2:\tdecl %0"
:"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
);
return __res-cs;
}
extern inline int strcspn(const char * cs, const char * ct)
{
register char * __res;
__asm__("cld\n\t"
"movl %4,%%edi\n\t"
"repne\n\t"
"scasb\n\t"
"notl %%ecx\n\t"
"decl %%ecx\n\t"
"movl %%ecx,%%edx\n"
"1:\tlodsb\n\t"
"testb %%al,%%al\n\t"
"je 2f\n\t"
"movl %4,%%edi\n\t"
"movl %%edx,%%ecx\n\t"
"repne\n\t"
"scasb\n\t"
"jne 1b\n"
"2:\tdecl %0"
:"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
);
return __res-cs;
}
extern inline char * strpbrk(const char * cs,const char * ct)
{
register char * __res ;
__asm__("cld\n\t"
"movl %4,%%edi\n\t"
"repne\n\t"
"scasb\n\t"
"notl %%ecx\n\t"
"decl %%ecx\n\t"
"movl %%ecx,%%edx\n"
"1:\tlodsb\n\t"
"testb %%al,%%al\n\t"
"je 2f\n\t"
"movl %4,%%edi\n\t"
"movl %%edx,%%ecx\n\t"
"repne\n\t"
"scasb\n\t"
"jne 1b\n\t"
"decl %0\n\t"
"jmp 3f\n"
"2:\txorl %0,%0\n"
"3:"
:"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
);
return __res;
}
extern inline char * strstr(const char * cs,const char * ct)
{
register char * __res ;
__asm__("cld\n\t" \
"movl %4,%%edi\n\t"
"repne\n\t"
"scasb\n\t"
"notl %%ecx\n\t"
"decl %%ecx\n\t" /* NOTE! This also sets Z if searchstring='' */
"movl %%ecx,%%edx\n"
"1:\tmovl %4,%%edi\n\t"
"movl %%esi,%%eax\n\t"
"movl %%edx,%%ecx\n\t"
"repe\n\t"
"cmpsb\n\t"
"je 2f\n\t" /* also works for empty string, see above */
"xchgl %%eax,%%esi\n\t"
"incl %%esi\n\t"
"cmpb $0,-1(%%eax)\n\t"
"jne 1b\n\t"
"xorl %%eax,%%eax\n\t"
"2:"
:"=a" (__res):"0" (0),"c" (0xffffffff),"S" (cs),"g" (ct)
);
return __res;
}
extern inline int strlen(const char * s)
{
register int __res ;
__asm__("cld\n\t"
"repne\n\t"
"scasb\n\t"
"notl %0\n\t"
"decl %0"
:"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff));
return __res;
}
extern char * ___strtok;
extern inline char * strtok(char * s,const char * ct)
{
register char * __res ;
__asm__("testl %1,%1\n\t"
"jne 1f\n\t"
"testl %0,%0\n\t"
"je 8f\n\t"
"movl %0,%1\n"
"1:\txorl %0,%0\n\t"
"movl $-1,%%ecx\n\t"
"xorl %%eax,%%eax\n\t"
"cld\n\t"
"movl %4,%%edi\n\t"
"repne\n\t"
"scasb\n\t"
"notl %%ecx\n\t"
"decl %%ecx\n\t"
"je 7f\n\t" /* empty delimeter-string */
"movl %%ecx,%%edx\n"
"2:\tlodsb\n\t"
"testb %%al,%%al\n\t"
"je 7f\n\t"
"movl %4,%%edi\n\t"
"movl %%edx,%%ecx\n\t"
"repne\n\t"
"scasb\n\t"
"je 2b\n\t"
"decl %1\n\t"
"cmpb $0,(%1)\n\t"
"je 7f\n\t"
"movl %1,%0\n"
"3:\tlodsb\n\t"
"testb %%al,%%al\n\t"
"je 5f\n\t"
"movl %4,%%edi\n\t"
"movl %%edx,%%ecx\n\t"
"repne\n\t"
"scasb\n\t"
"jne 3b\n\t"
"decl %1\n\t"
"cmpb $0,(%1)\n\t"
"je 5f\n\t"
"movb $0,(%1)\n\t"
"incl %1\n\t"
"jmp 6f\n"
"5:\txorl %1,%1\n"
"6:\tcmpb $0,(%0)\n\t"
"jne 7f\n\t"
"xorl %0,%0\n"
"7:\ttestl %0,%0\n\t"
"jne 8f\n\t"
"movl %0,%1\n"
"8:"
:"=b" (__res),"=S" (___strtok)
:"0" (___strtok),"1" (s),"g" (ct)
);
return __res;
}
static inline void * memcpy(void * dest,const void * src, int n)
{
__asm__("cld\n\t"
"rep\n\t"
"movsb"
::"c" (n),"S" (src),"D" (dest)
);
return dest;
}
extern inline void * memmove(void * dest,const void * src, int n)
{
if (dest<src)
__asm__("cld\n\t"
"rep\n\t"
"movsb"
::"c" (n),"S" (src),"D" (dest)
);
else
__asm__("std\n\t"
"rep\n\t"
"movsb"
::"c" (n),"S" (src+n-1),"D" (dest+n-1)
);
return dest;
}
static inline int memcmp(const void * cs,const void * ct,int count)
{
register int __res ;
__asm__("cld\n\t"
"repe\n\t"
"cmpsb\n\t"
"je 1f\n\t"
"movl $1,%%eax\n\t"
"jl 1f\n\t"
"negl %%eax\n"
"1:"
:"=a" (__res):"0" (0),"D" (cs),"S" (ct),"c" (count)
);
return __res;
}
extern inline void * memchr(const void * cs,char c,int count)
{
register void * __res ;
if (!count)
return NULL;
__asm__("cld\n\t"
"repne\n\t"
"scasb\n\t"
"je 1f\n\t"
"movl $1,%0\n"
"1:\tdecl %0"
:"=D" (__res):"a" (c),"D" (cs),"c" (count)
);
return __res;
}
static inline void * memset(void * s,char c,int count)
{
__asm__("cld\n\t"
"rep\n\t"
"stosb"
::"a" (c),"D" (s),"c" (count)
);
return s;
}
#endif
vsprintf.c
/*
* linux/kernel/vsprintf.c
*
* (C) 1991 Linus Torvalds
*/
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
// Lars Wirzenius 是Linus的好友,在Helsinki大學(xué)的時(shí)候曾同處一間辦公室。在1991年夏季開發(fā)linux時(shí),
// Linus當(dāng)時(shí)對(duì)C語言還不是很熟悉,還不會(huì)使用可變參數(shù)列表函數(shù)功能。因此Lars Wirzenius就為他編寫了
// 這段用于內(nèi)核顯示信息的代碼。他后來(1998年)承認(rèn)這段代碼中有一個(gè)bug,直到1994年才有人發(fā)現(xiàn),
// 并予以糾正。這個(gè)bug是在使用*作為輸出域?qū)挾葧r(shí),忘記遞增指針跳過這個(gè)星號(hào)了。在本代碼中這個(gè)bug
// 還仍然存在(163行)。他的個(gè)人主頁是http://liw.iki.fi/liw/
// 標(biāo)準(zhǔn)參數(shù)頭文件,以宏的形式定義變量參數(shù)列表。主要說明了一個(gè)類型(va_list)和
// 三個(gè)宏(va_start,va_arg和va_end),用于vsprintf,vprintf,vfprintf函數(shù)。
#include <stdarg.h>
#include <string.h>
/* we use this so that we can do without the ctype library */
#define is_digit(c) ((c) >= '0' && (c) <= '9')
// 將字符數(shù)字轉(zhuǎn)換成整數(shù)。輸入是數(shù)字串指針的指針,返回是結(jié)果的數(shù)值,另外指針將前移。
static int skip_atoi(const char **s)
{
int i=0;
while (is_digit(**s))
i = i*10 + *((*s)++) - '0'; // 這里導(dǎo)致指針前移
return i;
}
// 定義常用符號(hào)常數(shù)
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */
// 除法操作,輸入:n為被除數(shù),base為除數(shù);結(jié)果:n為商,函數(shù)返回值為余數(shù)。
#define do_div(n,base) ({ \
int __res; \
__asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
__res; })
// 將整數(shù)轉(zhuǎn)換為指定進(jìn)制的字符串。
// 輸入:num-整數(shù);base-進(jìn)制;size-字符串長度;precision-數(shù)字長度(精度);type-類型選項(xiàng)。
// 輸出:str字符串指針
static char * number(char * str, int num, int base, int size, int precision, int type)
{
char c,sign,tmp[36];
const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
// 根據(jù)類型定義字母集,默認(rèn)是大寫字母
if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
// 如果類型指出要左調(diào)整(靠左邊界),則屏蔽類型中的填零標(biāo)志。
// TODO: 這句沒看懂?? 為什么要把最低位置為0
if (type&LEFT) type &= ~ZEROPAD;
// 本程序只能處理的進(jìn)制范圍:2-36
if (base<2 || base>36)
return 0;
// TODO: 這句跟LEFT相關(guān)??
c = (type & ZEROPAD) ? '0' : ' ' ;
if (type&SIGN && num<0) {
sign='-';
num = -num;
} else
sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
// 如果是帶符號(hào)的,字符串寬度就減一
if (sign) size--;
if (type&SPECIAL) {
if (base==16) size -= 2; // 16進(jìn)制需要減2,用于前面的0x
else if (base==8) size--; // 8進(jìn)制的減1,因?yàn)榍懊娴?
}
i=0;
if (num==0)
tmp[i++]='0';
else while (num!=0)
tmp[i++]=digits[do_div(num,base)];
if (i>precision) precision=i; // 若字符個(gè)數(shù)大于精度值,精度值擴(kuò)展為字符個(gè)數(shù)
size -= precision; // 寬度再減去用于存放數(shù)值字符的個(gè)數(shù)
// 將轉(zhuǎn)換結(jié)果放在str中,如果類型中沒有填零(ZEROPAD)和左靠齊標(biāo)志,
// 則在str中首先填放剩余寬度值指出的空格數(shù)。若需要帶符號(hào)位,則存入符號(hào)
if (!(type&(ZEROPAD+LEFT)))
while(size-->0)
*str++ = ' ';
if (sign)
*str++ = sign;
// 如果是特殊轉(zhuǎn)換的處理,8進(jìn)制和16進(jìn)制分別填入0/0x/0X
if (type&SPECIAL) {
if (base==8)
*str++ = '0';
else if (base==16) {
*str++ = '0';
*str++ = digits[33]; // 'x' or 'X'
}
}
// 如果類型么有左靠齊標(biāo)志,則在剩余的寬度中存放c字符(‘0’或者空格)
if (!(type&LEFT))
while(size-->0)
*str++ = c;
// 若i存有數(shù)值num的數(shù)字個(gè)數(shù),若數(shù)字個(gè)數(shù)小于精度值,則str中放入 精度值-i 個(gè)'0'
while(i<precision--)
*str++ = '0';
// 將數(shù)值轉(zhuǎn)換好的數(shù)字字符填入str中,共i個(gè)
while(i-->0)
*str++ = tmp[i];
// 若寬度值仍大于零,則表達(dá)類型標(biāo)志中有左靠齊標(biāo)志,則在剩余寬度中放入空格
while(size-->0)
*str++ = ' ';
return str;
}
// 格式化輸出
int vsprintf(char *buf, const char *fmt, va_list args)
{
int len;
int i;
char * str; // 用于存放轉(zhuǎn)換過程中的字符串
char *s;
int *ip;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h', 'l', or 'L' for integer fields */
// 掃描格式字符串,對(duì)于不是 % 的就依次存入str
for (str=buf ; *fmt ; ++fmt) {
if (*fmt != '%') {
*str++ = *fmt;
continue;
}
// 取得格式指示字符串中的標(biāo)志域,并將標(biāo)志常量放入flags變量中
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-': flags |= LEFT; goto repeat;
case '+': flags |= PLUS; goto repeat;
case ' ': flags |= SPACE; goto repeat;
case '#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat;
}
// 取當(dāng)前參數(shù)數(shù)字段寬度域值,放入field_width變量中,如果寬度域中是數(shù)值則直接取其為寬度值。
// 如果寬度域中是字符'*',表示下一個(gè)參數(shù)指定寬度。因此調(diào)用va_arg取寬度值。若此時(shí)寬度值
// 小于0,則該負(fù)數(shù)表示其帶有標(biāo)志域'-'標(biāo)志(左靠齊),因此還需要在標(biāo)志變量中填入該標(biāo)志,并
// 將字段寬度值取為其絕對(duì)值。
/* get field width */
field_width = -1;
if (is_digit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
/* it's the next argument */
field_width = va_arg(args, int); // 這里有個(gè)bug,應(yīng)插入++fmt。// TODO: 不懂
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
// 取格式轉(zhuǎn)換串的精度域,并放入precision變量中。精度域開始的標(biāo)志是'.'.
// 其處理過程與上面寬度域的類似。如果精度域中是數(shù)值則直接取其為精度值。如果精度域中是
// 字符'*',表示下一個(gè)參數(shù)指定精度。因此調(diào)用va_arg取精度值。若此時(shí)寬度值小于0,則將
// 字段精度值取為0.
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (is_digit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
/* it's the next argument */
precision = va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
// 分析轉(zhuǎn)換指示符
switch (*fmt) {
// ‘c’ 表示對(duì)應(yīng)參數(shù)應(yīng)是字符。此時(shí)如果標(biāo)志域表明不是左靠齊,則該字段前面
// 放入'寬度域值-1'個(gè)空格字符,然后再放入?yún)?shù)字符。如果寬度域大于0,
// 則表示為左靠齊,則在參數(shù)字符后面添加'寬度值-1'個(gè)空格字符
case 'c':
if (!(flags & LEFT))
while (--field_width > 0)
*str++ = ' ';
*str++ = (unsigned char) va_arg(args, int);
while (--field_width > 0)
*str++ = ' ';
break;
// 's'表示對(duì)應(yīng)參數(shù)是字符串。首先取參數(shù)字符串的長度,若其超過了精度域值,
// 則擴(kuò)展精度域=字符串長度。此時(shí)如果標(biāo)志域表明不是左靠齊,則該字段前放入
// '寬度值-字符串長度'個(gè)空格字符。然后再放入?yún)?shù)字符串。如果寬度域還大于0,
// 則表示為左靠齊,則在參數(shù)字符串后面添加'寬度值-字符串長度'個(gè)空格字符。
case 's':
s = va_arg(args, char *);
len = strlen(s);
if (precision < 0)
precision = len;
else if (len > precision)
len = precision;
if (!(flags & LEFT))
while (len < field_width--)
*str++ = ' ';
for (i = 0; i < len; ++i)
*str++ = *s++;
while (len < field_width--)
*str++ = ' ';
break;
// 'o'表示8進(jìn)制,通過number函數(shù)處理
case 'o':
str = number(str, va_arg(args, unsigned long), 8,
field_width, precision, flags);
break;
// 'p'表示一個(gè)指針類型,此時(shí)若該參數(shù)沒有設(shè)置寬度域,默認(rèn)寬度域?yàn)?
// 并且需要添零,然后用number函數(shù)處理
case 'p':
if (field_width == -1) {
field_width = 8;
flags |= ZEROPAD;
}
str = number(str,
(unsigned long) va_arg(args, void *), 16,
field_width, precision, flags);
break;
// 'x'-轉(zhuǎn)成16進(jìn)制
case 'x':
flags |= SMALL;
case 'X':
str = number(str, va_arg(args, unsigned long), 16,
field_width, precision, flags);
break;
// 'd' & 'i' 表示帶符號(hào)整數(shù);'u' 表示無符號(hào)整數(shù)
case 'd':
case 'i':
flags |= SIGN;
case 'u':
str = number(str, va_arg(args, unsigned long), 10,
field_width, precision, flags);
break;
// 'n'-表示要把到目前為止轉(zhuǎn)換輸出字符數(shù)保存到對(duì)應(yīng)參數(shù)指針指定的位置中。
// 首先利用va_arg()取得該參數(shù)指針,然后將已經(jīng)轉(zhuǎn)換好的字符數(shù)存到指向的位置
case 'n':
ip = va_arg(args, int *);
*ip = (str - buf);
break;
// 若格式轉(zhuǎn)換不是 % ,則表示格式字符串有錯(cuò),直接將一個(gè)“%”寫入輸出串中。
// 如果格式轉(zhuǎn)換符的位置還有字符,則也直接將該字符寫入輸入串中,并返回繼續(xù)處理
// 格式字符串,否則表示已經(jīng)處理到格式字符串的結(jié)尾處,退出循環(huán)。
default:
if (*fmt != '%')
*str++ = '%';
if (*fmt)
*str++ = *fmt;
else
--fmt;
break;
}
}
*str = '\0'; // 字符串結(jié)尾字符:'\0'
return str-buf; // 返回轉(zhuǎn)換好的長度值
}
編譯命令及效果
gcc vsprintf.c
./a.out
策略:只解讀,不修改!