91static info fmtinfo[] = {
92 {
'd', 10,
"0123456789", 1, 0, RADIX, },
93 {
's', 0, 0, 0, 0, STRING, },
94 {
'S', 0, 0, 0, 0, SEEIT, },
95 {
'z', 0, 0, 0, 0, MEM_STRING, },
96 {
'c', 0, 0, 0, 0, CHAR, },
97 {
'o', 8,
"01234567", 0,
"0", RADIX, },
98 {
'u', 10,
"0123456789", 0, 0, RADIX, },
99 {
'x', 16,
"0123456789abcdef", 0,
"x0", RADIX, },
100 {
'X', 16,
"0123456789ABCDEF", 0,
"X0", RADIX, },
101 {
'r', 10,
"0123456789", 0, 0, ORDINAL, },
102 {
'f', 0, 0, 1, 0, FLOAT, },
103 {
'e', 0,
"e", 1, 0, EXP, },
104 {
'E', 0,
"E", 1, 0, EXP, },
105 {
'g', 0,
"e", 1, 0, GENERIC, },
106 {
'G', 0,
"E", 1, 0, GENERIC, },
107 {
'i', 10,
"0123456789", 1, 0, RADIX, },
108 {
'n', 0, 0, 0, 0, SIZE, },
109 {
'S', 0, 0, 0, 0, SEEIT, },
110 {
'%', 0, 0, 0, 0, PERCENT, },
111 {
'b', 2,
"01", 0,
"b0", RADIX, },
112 {
'p', 16,
"0123456789ABCDEF", 0,
"x0", RADIX, },
113 {
'\'', 0, 0, 0, 0, CHARLIT, },
115#define NINFO (sizeof(fmtinfo)/sizeof(info))
121#ifndef NOFLOATINGPOINT
135static int getdigit(
long double *val,
int *cnt){
138 if( (*cnt)++ >= MAXDIG )
return '0';
142 *val = (*val - d)*10.0;
185int vxprintf(func,arg,format,ap)
186 void (*func)(
char*,int,
void*);
191 register const char *fmt;
193 register char *bufpt;
194 register int precision;
199 int flag_leftjustify;
202 int flag_alternateform;
206 unsigned long longvalue;
208#ifndef NOFLOATINGPOINT
209 long double realvalue;
217 static char spaces[] =
219#define SPACESIZE (sizeof(spaces)-1)
220#ifndef NOFLOATINGPOINT
232 for(; (c=(*fmt))!=0; ++fmt){
237 while( (c=(*++fmt))!=
'%' && c!=0 ) amt++;
238 (*func)(bufpt,amt,arg);
242 if( (c=(*++fmt))==0 ){
249 flag_leftjustify = flag_plussign = flag_blanksign =
250 flag_alternateform = flag_zeropad = flag_center = 0;
253 case '-': flag_leftjustify = 1; c = 0;
break;
254 case '+': flag_plussign = 1; c = 0;
break;
255 case ' ': flag_blanksign = 1; c = 0;
break;
256 case '#': flag_alternateform = 1; c = 0;
break;
257 case '0': flag_zeropad = 1; c = 0;
break;
258 case '=': flag_center = 1; c = 0;
break;
261 }
while( c==0 && (c=(*++fmt))!=0 );
262 if( flag_center ) flag_leftjustify = 0;
266 width = va_arg(ap,
int);
268 flag_leftjustify = 1;
274 width = width*10 + c -
'0';
278 if( width > BUFSIZE-10 ){
286 precision = va_arg(ap,
int);
289 if( precision<0 ) precision = -precision;
294 precision = precision*10 + c -
'0';
299 if( precision>BUFSIZE-40 ) precision = BUFSIZE-40;
312 for(idx=0; (
unsigned int)idx<NINFO; idx++){
313 if( c==fmtinfo[idx].fmttype ){
314 infop = &fmtinfo[idx];
346 if(( flag_long )&&( infop->flag_signed )){
347 signed long t = va_arg(ap,
signed long);
349 }
else if(( flag_long )&&( !infop->flag_signed )){
350 unsigned long t = va_arg(ap,
unsigned long);
352 }
else if(( !flag_long )&&( infop->flag_signed )){
353 signed int t = va_arg(ap,
signed int) & ((
unsigned long) 0xffffffff);
356 unsigned int t = va_arg(ap,
unsigned int) & ((
unsigned long) 0xffffffff);
362 if( longvalue==0 ) flag_alternateform = 0;
366 if( longvalue==0 && infop->base==8 ) flag_alternateform = 0;
368 if( infop->flag_signed ){
369 if( *(
long*)&longvalue<0 ){
370 longvalue = -*(
long*)&longvalue;
372 }
else if( flag_plussign ) prefix =
'+';
373 else if( flag_blanksign ) prefix =
' ';
376 if( flag_zeropad && precision<width-(prefix!=0) ){
377 precision = width-(prefix!=0);
379 bufpt = &buf[BUFSIZE];
380 if( xtype==ORDINAL ){
385 if( a==0 || a>3 || (b>10 && b<14) ){
402 cset = infop->charset;
405 *(--bufpt) = cset[longvalue%base];
406 longvalue = longvalue/base;
407 }
while( longvalue>0 );
409 length = (int)(&buf[BUFSIZE]-bufpt);
410 if(infop->fmttype ==
'p')
413 flag_alternateform = 1;
416 for(idx=precision-length; idx>0; idx--){
419 if( prefix ) *(--bufpt) = prefix;
420 if( flag_alternateform && infop->prefix ){
423 if( *bufpt!=pre[0] ){
424 for(pre=infop->prefix; (x=(*pre))!=0; pre++) *(--bufpt) = x;
428 length = (int)(&buf[BUFSIZE]-bufpt);
433#ifdef NOFLOATINGPOINT
436 realvalue = va_arg(ap,
double);
437 if( precision<0 ) precision = 6;
438 if( precision>BUFSIZE-10 ) precision = BUFSIZE-10;
440 realvalue = -realvalue;
443 if( flag_plussign ) prefix =
'+';
444 else if( flag_blanksign ) prefix =
' ';
447 if( infop->type==GENERIC && precision>0 ) precision--;
451 for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
454 if( precision>MAXDIG-1 ) idx = MAXDIG-1;
455 else idx = precision;
456 for(rounder=0.5; idx>0; idx--, rounder*=0.1);
458 if( infop->type==FLOAT ) realvalue += rounder;
463 while( realvalue>=1e8 && k++<100 ){ realvalue *= 1e-8; exp+=8; }
464 while( realvalue>=10.0 && k++<100 ){ realvalue *= 0.1; exp++; }
465 while( realvalue<1e-8 && k++<100 ){ realvalue *= 1e8; exp-=8; }
466 while( realvalue<1.0 && k++<100 ){ realvalue *= 10.0; exp--; }
478 flag_exp = xtype==EXP;
480 realvalue += rounder;
481 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
483 if( xtype==GENERIC ){
484 flag_rtz = !flag_alternateform;
485 if( exp<-4 || exp>precision ){
488 precision = precision - exp;
499 if( xtype==FLOAT && exp+precision<BUFSIZE-30 ){
500 flag_dp = (precision>0 || flag_alternateform);
501 if( prefix ) *(bufpt++) = prefix;
502 if( exp<0 ) *(bufpt++) =
'0';
503 else for(; exp>=0; exp--) *(bufpt++) = getdigit(&realvalue,&nsd);
504 if( flag_dp ) *(bufpt++) =
'.';
505 for(exp++; exp<0 && precision>0; precision--, exp++){
508 while( (precision--)>0 ) *(bufpt++) = getdigit(&realvalue,&nsd);
510 if( flag_rtz && flag_dp ){
511 while( bufpt>=buf && *bufpt==
'0' ) *(bufpt--) = 0;
512 if( bufpt>=buf && *bufpt==
'.' ) *(bufpt--) = 0;
516 flag_dp = (precision>0 || flag_alternateform);
517 if( prefix ) *(bufpt++) = prefix;
518 *(bufpt++) = getdigit(&realvalue,&nsd);
519 if( flag_dp ) *(bufpt++) =
'.';
520 while( (precision--)>0 ) *(bufpt++) = getdigit(&realvalue,&nsd);
522 if( flag_rtz && flag_dp ){
523 while( bufpt>=buf && *bufpt==
'0' ) *(bufpt--) = 0;
524 if( bufpt>=buf && *bufpt==
'.' ) *(bufpt--) = 0;
527 if( exp || flag_exp ){
528 *(bufpt++) = infop->charset[0];
529 if( exp<0 ){ *(bufpt++) =
'-'; exp = -exp; }
530 else { *(bufpt++) =
'+'; }
532 *(bufpt++) = (exp/100)+
'0';
535 *(bufpt++) = exp/10+
'0';
536 *(bufpt++) = exp%10+
'0';
542 length = (int)(bufpt-buf);
547 if( flag_zeropad && !flag_leftjustify && length < width){
549 int nPad = width - length;
550 for(i=width; i>=nPad; i--){
551 bufpt[i] = bufpt[i-nPad];
554 while( nPad-- ) bufpt[i++] =
'0';
560 *(va_arg(ap,
int*)) =
count;
570 c = buf[0] = (xtype==CHAR ? va_arg(ap,
int) : *++fmt);
572 for(idx=1; idx<precision; idx++) buf[idx] = c;
581 zMem = bufpt = va_arg(ap,
char*);
582 if( bufpt==0 ) bufpt =
"(null)";
583 length = strlen(bufpt);
584 if( precision>=0 && precision<length ) length = precision;
590 char *arg = va_arg(ap,
char*);
591 for(i=0; i<BUFSIZE-1 && (c = *arg++)!=0; i++){
592 if( c<0x20 || c>=0x7f ){
594 buf[i] = (c&0x1f)+0x40;
601 if( precision>=0 && precision<length ) length = precision;
609 (*func)(
"%",idx,arg);
619 if( !flag_leftjustify ){
621 nspace = width-length;
626 flag_leftjustify = 1;
629 while( (
unsigned int)nspace>=SPACESIZE ){
630 (*func)(spaces,SPACESIZE,arg);
633 if( nspace>0 ) (*func)(spaces,nspace,arg);
637 (*func)(bufpt,length,arg);
640 if( xtype==MEM_STRING && zMem ){
644 if( flag_leftjustify ){
646 nspace = width-length;
649 while( (
unsigned int)nspace>=SPACESIZE ){
650 (*func)(spaces,SPACESIZE,arg);
653 if( nspace>0 ) (*func)(spaces,nspace,arg);
657 return errorflag ? -1 :
count;
666 void (*func)(
char*,
int,
void*),
674 ret = vxprintf(func,arg,format,ap);
693void __sout(
char *,
int,
void *);
695void __sout(txt,amt,arg)
701 register const char *t;
709 while( a-- >0 && head<tail ) *(head++) = *(t++);
711 while( a-- >0 ) *(head++) = *(t++);
719int vsnprintf(
char *buf,
size_t n,
const char *fmt, va_list ap){
722 arg.last = &buf[n-1];
724 return vxprintf(__sout,&arg,fmt,ap);
729int snprintf(
char *str,
size_t sz,
const char *format, ...)
736 arg.last = &str[sz-1];
738 va_start(args, format);
739 ret = vxprintf(__sout, &arg, format, args);
747int vsprintf(
char *buf,
const char *fmt, va_list ap){
752 return vxprintf(__sout,&arg,fmt,ap);
758int sprintf (
char *str,
const char *format, ...)
767 va_start(args, format);
768 ret = vxprintf(__sout, &arg, format, args);
791void __mout(
char *,
int,
void*);
795void __mout(zNewText,nNewChar,arg)
800 struct sgMprintf *pM = (
struct sgMprintf*)arg;
801 if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
802 pM->nAlloc = pM->nChar + nNewChar*2 + 1;
803 if( pM->zText==pM->zBase ){
804 pM->zText = malloc(pM->nAlloc);
805 if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar);
807 pM->zText = realloc(pM->zText, pM->nAlloc);
811 memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
812 pM->nChar += nNewChar;
813 pM->zText[pM->nChar] = 0;
827char *mprintf(
const char *zFormat, ...){
829 struct sgMprintf sMprintf;
833 va_start(ap,zFormat);
835 sMprintf.nAlloc =
sizeof(zBuf);
836 sMprintf.zText = zBuf;
837 sMprintf.zBase = zBuf;
838 vxprintf(__mout,&sMprintf,zFormat,ap);
840 if( sMprintf.zText==sMprintf.zBase ){
841 zNew = malloc( sMprintf.nChar+1 );
842 if( zNew ) strcpy(zNew,zBuf);
844 zNew = realloc(sMprintf.zText,sMprintf.nChar+1);
857char *vmprintf(
const char *zFormat,va_list ap){
858 struct sgMprintf sMprintf;
861 sMprintf.zText = zBuf;
862 sMprintf.nAlloc =
sizeof(zBuf);
863 sMprintf.zBase = zBuf;
864 vxprintf(__mout,&sMprintf,zFormat,ap);
865 if( sMprintf.zText==sMprintf.zBase ){
866 sMprintf.zText = malloc( strlen(zBuf)+1 );
867 if( sMprintf.zText ) strcpy(sMprintf.zText,zBuf);
869 sMprintf.zText = realloc(sMprintf.zText,sMprintf.nChar+1);
871 return sMprintf.zText;
876int asprintf(
char ** strp,
const char *zFormat, ...){
878 struct sgMprintf sMprintf;
882 va_start(ap,zFormat);
884 sMprintf.nAlloc =
sizeof(zBuf);
885 sMprintf.zText = zBuf;
886 sMprintf.zBase = zBuf;
887 vxprintf(__mout,&sMprintf,zFormat,ap);
889 if( sMprintf.zText==sMprintf.zBase ){
890 zNew = malloc( sMprintf.nChar+1 );
891 if( zNew ) strcpy(zNew,zBuf);
893 zNew = realloc(sMprintf.zText,sMprintf.nChar+1);
898 return sMprintf.nChar+1;
903int vasprintf(
char **strp,
const char *format, va_list ap) {
904 struct sgMprintf sMprintf;
907 sMprintf.zText = zBuf;
908 sMprintf.nAlloc =
sizeof(zBuf);
909 sMprintf.zBase = zBuf;
910 vxprintf(__mout,&sMprintf,format,ap);
911 if( sMprintf.zText==sMprintf.zBase ){
912 sMprintf.zText = malloc( strlen(zBuf)+1 );
913 if( sMprintf.zText ) strcpy(sMprintf.zText,zBuf);
915 sMprintf.zText = realloc(sMprintf.zText,sMprintf.nChar+1);
917 *strp = sMprintf.zText;
918 return sMprintf.nChar;
927void __fout(
char *,
int,
void *);
930void __fout(zNewText,nNewChar,arg)
935 fwrite(zNewText,1,nNewChar,(FILE*)arg);
941int fprintf(FILE *pOut,
const char *zFormat, ...){
945 va_start(ap,zFormat);
946 retc = vxprintf(__fout,pOut,zFormat,ap);
953int vfprintf(FILE *pOut,
const char *zFormat, va_list ap){
954 return vxprintf(__fout,pOut,zFormat,ap);
960int printf(
const char *format, ...)
965 va_start(args, format);
966 ret = vprintf(format, args);
974int vprintf(
const char *format, va_list args)
976 static char buf[PS2LIB_STR_MAX];
979 ret = vsnprintf(buf, PS2LIB_STR_MAX, format, args);
987int putchar(
int chr )
999#define NPM_RPC_SERVER 0x14d704e
1000#define NPM_RPC_PUTS 1
1002extern int _iop_reboot_count;
1004static int npm_puts_sema = -1;
1008static int npm_puts_init()
1012 static int _rb_count = 0;
1013 if(_rb_count != _iop_reboot_count)
1015 _rb_count = _iop_reboot_count;
1022 sema.init_count = 0;
1025 if ((npm_puts_sema = CreateSema(&sema)) < 0)
1030 while (((res = SifBindRpc(&npm_cd, NPM_RPC_SERVER, 0)) >= 0) &&
1031 (npm_cd.server == NULL))
1037 SignalSema(npm_puts_sema);
1043int npmPuts(
const char *buf)
1048 if (npm_puts_init() < 0)
1051 WaitSema(npm_puts_sema);
1054 if (((u32)buf & 15) == 0)
1057 strncpy(p, buf, 511);
1058 ((
char *) p)[511] =
'\0';
1061 if (SifCallRpc(&npm_cd, NPM_RPC_PUTS, 0, p, 512, NULL, 0, NULL, NULL) < 0)
1064 SignalSema(npm_puts_sema);
1071int nprintf(
const char *format, ...)
1073 char buf[PS2LIB_STR_MAX];
1077 va_start(args, format);
1078 ret = vsnprintf(buf, PS2LIB_STR_MAX, format, args);
1087int vnprintf(
const char *format, va_list args)
1089 char buf[PS2LIB_STR_MAX];
1092 ret = vsnprintf(buf, PS2LIB_STR_MAX, format, args);
1100int sio_printf(
const char *format, ...)
1102 static char buf[PS2LIB_STR_MAX];
1106 va_start(args, format);
1107 size = vsnprintf(buf, PS2LIB_STR_MAX, format, args);
1112 if (buf[size - 1] ==
'\n') {
1113 buf[size - 1] =
'\0';
size_t sio_write(void *buf, size_t size)
u32 count
start sector of fragmented bd/file