vsprintf.c
stdarg.h
lib/string.h
stdarg.h
ubixos/types.h
sys/_types.h
lib/string.h
src/sys/lib/vsprintf.c
do_div
n
base
({ \ int __res; \ __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \ __res; })
number
is_digit
c
((c) >= '0' && (c) <= '9')
skip_atoi
vsprintf
LEFT
16
number
vsprintf
PLUS
4
number
vsprintf
SIGN
2
number
vsprintf
SMALL
64
number
vsprintf
SPACE
8
number
vsprintf
SPECIAL
32
number
vsprintf
ZEROPAD
1
number
vsprintf
char * static char* number (char *str, int num, int base, int size, int precision, int type) number char * str int num int base int size int precision int type do_div LEFT PLUS SIGN SMALL SPACE SPECIAL ZEROPAD vsprintf
int static int skip_atoi (const char **s) skip_atoi const char ** s is_digit vsprintf
int int vsprintf (char *buf, const char *fmt, vaList args) vsprintf char * buf const char * fmt vaList args is_digit LEFT number PLUS SIGN skip_atoi SMALL SPACE SPECIAL strlen vaArg ZEROPAD kpanic kprintf sprintf

    /*****************************************************************************************
    Copyright(c)2002TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Log: vsprintf_8c.xml,v $
    Revision 1.6  2006/12/19 14:12:54  reddawg
    rtld-elf almost workign

    Revision1.1.1.12006/06/0112:46:16reddawg
    ubix2

    Revision1.22005/10/1200:13:37reddawg
    Removed

    Revision1.1.1.12005/09/2617:24:14reddawg
    nomessage

    Revision1.22004/06/2823:12:58reddawg
    fileformatnowcontainer:/path/to/file

    Revision1.1.1.12004/04/1512:07:11reddawg
    UbixOSv1.0

    Revision1.32004/04/1316:36:33reddawg
    Changedourcopyright,itisallnowunderaBSD-Stylelicense

    $Id: vsprintf_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    /*vsprintf.c--LarsWirzenius&LinusTorvalds.*/
    /*
    *Wirzeniuswrotethisportably,Torvaldsfuckeditup:-)
    */

    #include<stdarg.h>
    #include<lib/string.h>

    /*weusethissothatwecandowithoutthectypelibrary*/
    #defineis_digit(c)((c)>='0'&&(c)<='9')

    staticintskip_atoi(constchar**s)
    {
    inti=0;

    while(is_digit(**s))
    i=i*10+*((*s)++)-'0';
    returni;
    }

    #defineZEROPAD1/*padwithzero*/
    #defineSIGN2/*unsigned/signedlong*/
    #definePLUS4/*showplus*/
    #defineSPACE8/*spaceifplus*/
    #defineLEFT16/*leftjustified*/
    #defineSPECIAL32/*0x*/
    #defineSMALL64/*use'abcdef'insteadof'ABCDEF'*/

    #definedo_div(n,base)({\
    int__res;\
    __asm__("divl%4":"=a"(n),"=d"(__res):"0"(n),"1"(0),"r"(base));\
    __res;})

    staticchar*number(char*str,intnum,intbase,intsize,intprecision
    ,inttype)
    {
    charc,sign,tmp[36];
    constchar*digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    inti;

    if(type&SMALL)digits="0123456789abcdefghijklmnopqrstuvwxyz";
    if(type&LEFT)type&=~ZEROPAD;
    if(base<2||base>36)
    return0;
    c=(type&ZEROPAD)?'0':'';
    if(type&SIGN&&num<0){
    sign='-';
    num=-num;
    }else
    sign=(type&PLUS)?'+':((type&SPACE)?'':0);
    if(sign)size--;
    if(type&SPECIAL){
    if(base==16){size-=2;}
    elseif(base==8){size--;}
    }
    i=0;
    if(num==0)
    tmp[i++]='0';
    elsewhile(num!=0)
    tmp[i++]=digits[do_div(num,base)];
    if(i>precision)precision=i;
    size-=precision;
    if(!(type&(ZEROPAD+LEFT)))
    while(size-->0)
    *str++='';
    if(sign)
    *str++=sign;
    if(type&SPECIAL){
    if(base==8){
    *str++='0';
    }
    elseif(base==16){
    *str++='0';
    *str++=digits[33];
    }
    }
    if(!(type&LEFT))
    while(size-->0)
    *str++=c;
    while(i<precision--)
    *str++='0';
    while(i-->0)
    *str++=tmp[i];
    while(size-->0)
    *str++='';
    returnstr;
    }

    intvsprintf(char*buf,constchar*fmt,vaListargs)
    {
    intlen;
    inti;
    char*str;
    char*s;
    int*ip;

    intflags;/*flagstonumber()*/

    intfield_width;/*widthofoutputfield*/
    intprecision;/*min.#ofdigitsforintegers;max
    numberofcharsforfromstring*/
    intqualifier;/*'h','l',or'L'forintegerfields*/

    for(str=buf;*fmt;++fmt){
    if(*fmt!='%'){
    *str++=*fmt;
    continue;
    }

    /*processflags*/
    flags=0;
    repeat:
    ++fmt;/*thisalsoskipsfirst'%'*/
    switch(*fmt){
    case'-':flags|=LEFT;gotorepeat;
    case'+':flags|=PLUS;gotorepeat;
    case'':flags|=SPACE;gotorepeat;
    case'#':flags|=SPECIAL;gotorepeat;
    case'0':flags|=ZEROPAD;gotorepeat;
    }

    /*getfieldwidth*/
    field_width=-1;
    if(is_digit(*fmt))
    field_width=skip_atoi(&fmt);
    elseif(*fmt=='*'){
    /*it'sthenextargument*/
    field_width=vaArg(args,int);
    if(field_width<0){
    field_width=-field_width;
    flags|=LEFT;
    }
    }

    /*gettheprecision*/
    precision=-1;
    if(*fmt=='.'){
    ++fmt;
    if(is_digit(*fmt))
    precision=skip_atoi(&fmt);
    elseif(*fmt=='*'){
    /*it'sthenextargument*/
    precision=vaArg(args,int);
    }
    if(precision<0)
    precision=0;
    }

    /*gettheconversionqualifier*/
    qualifier=-1;
    if(*fmt=='h'||*fmt=='l'||*fmt=='L'){
    qualifier=*fmt;
    ++fmt;
    }

    switch(*fmt){
    case'c':
    if(!(flags&LEFT))
    while(--field_width>0)
    *str++='';
    *str++=(unsignedchar)vaArg(args,int);
    while(--field_width>0)
    *str++='';
    break;

    case's':
    s=vaArg(args,char*);
    len=strlen(s);
    if(precision<0)
    precision=len;
    elseif(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;

    case'o':
    str=number(str,vaArg(args,unsignedlong),8,
    field_width,precision,flags);
    break;

    case'p':
    if(field_width==-1){
    field_width=8;
    flags|=ZEROPAD;
    }
    str=number(str,
    (unsignedlong)vaArg(args,void*),16,
    field_width,precision,flags);
    break;

    case'x':
    flags|=SMALL;
    case'X':
    str=number(str,vaArg(args,unsignedlong),16,
    field_width,precision,flags);
    break;

    case'd':
    case'i':
    flags|=SIGN;
    case'u':
    str=number(str,vaArg(args,unsignedlong),10,
    field_width,precision,flags);
    break;

    case'n':
    ip=vaArg(args,int*);
    *ip=(str-buf);
    break;

    default:
    if(*fmt!='%')
    *str++='%';
    if(*fmt)
    *str++=*fmt;
    else
    --fmt;
    break;
    }
    }
    *str='\0';
    returnstr-buf;
    }

    /***
    END
    ***/